简体   繁体   English

指南针手表的Ruby Guard

[英]Ruby Guard with Compass Watch

I have the following guardfile in my .net project: 我的.net项目中有以下保护文件:

# Listen for chanes to css|js|cshtml files, copy to server_root, creating new folders as necessary
watch(%r{.+\.(css|js|cshtml?)$}) do |match|
    puts Time.now.asctime + ': '+ match[0] + ' has changed'
    copy_path = server_root + File.dirname(match[0])
    if Dir.exists?(copy_path) == false
        FileUtils.mkdir_p(copy_path)
    end
    FileUtils.cp_r(match[0], copy_path)
end


# Listen for changes to Sass files, compile to local folder
puts `compass compile --time --quiet`
guard :compass do
    watch(%r{(.*)\.s[ac]ss$})
end

What's I am noticing is that over time it get's slower and slower and causes my CPU to run to it's max. 我要注意的是,随着时间的流逝,它变得越来越慢,并且导致我的CPU运行到最大。 This is my first time doing this, but basically I want to watch for those types of files in my project, do a compass compile on sass, and copy any changed file to their counterparts on another project that IIS points to. 这是我第一次这样做,但是基本上我想监视项目中的这些文件类型,在sass上进行指南针编译,并将所有更改的文件复制到IIS指向的另一个项目中的对应文件中。

My main question is how to prevent it from causing my CPU to run hot, and are there any improvements to make this type of action more efficient? 我的主要问题是如何防止它导致我的CPU热运行,是否有任何改进措施可以使这种类型的操作更有效?

From what I see you are copying files from one directory to another - while both directories are watched, and your rules match both the source directories and destination directories, which triggers changes, which triggers copying again, until all that is happening is the constant copying ... 据我所知,您正在将文件从一个目录复制到另一个目录-同时监视两个目录,并且您的规则与源目录和目标目录都匹配,这会触发更改,并再次触发复制,直到发生的一切就是不断复制...

The solution is to watch only one folder and output the files to an unwatched folder. 解决的办法是将这些文件看只有一个文件夹,并输出到一个无人值守的文件夹中。

It's best to put all your source files into eg 'src' and set the server_root to eg 'public' and then tell guard to watch only 'src' by either: 最好将所有源文件放入“ src”中,并将server_root设置为“ public”,然后通过以下任一方法告诉警卫仅监视“ src”:

  • passing the src directory to be watched in the command line: bundle exec guard -w src 在命令行中传递要监视的src目录: bundle exec guard -w src

  • or setting the new directories option in your Guardfile : 或在Guardfile设置新directories选项:

     directories(%w(src)) 

Another option would be to ignore the destination where you've copied files, eg in your Guardfile 另一个选择是忽略文件复制的目的地,例如在Guardfile

    ignore([/^public\//])

so that copying files there won't trigger the copying over and over again. 这样一来,复制文件就不会一次又一次地触发复制。

You might find some useful information here: https://github.com/guard/guard/wiki/Understanding-Guard 您可能会在这里找到一些有用的信息: https : //github.com/guard/guard/wiki/Understanding-Guard

(There are tips that show you what's happening under the hood, so you can what's exactly triggering which action). (有一些技巧可以告诉您幕后发生的事情,因此您可以确切地触发操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM