Ruby -我需要让警卫在我的rake任务中运行,但是我找不到在后台运行它的方法。它需要在最后运行第二次,因此让guard >外壳等待命令会阻止最终任务的运行,因此在rake文件中调用sh bundle exec guard不是一个选项。根据这些文件,这应该是可行的:
##
desc "Watch files"
##
task :watcher do
Guard.setup
Guard::Dsl.evaluate_guardfile(:guardfile => 'Guardfile', :group => ['Frontend'])
Guard.guards('copy').run_all
end
#end watch fileshttps://github.com/guard/guard/wiki/Use-Guard-programmatically-cookbook
这是我的守护文件,完整的,(与Rakefile相同的dir )
# Any files created or modified in the 'source' directory
# will be copied to the 'target' directory. Update the
# guard as appropriate for your needs.
guard :copy, :from => 'src', :to => 'dist',
:mkpath => true, :verbose => true但是rake watcher返回一个错误:
07:02:31 - INFO - Using Guardfile at Guardfile.
07:02:31 - ERROR - Guard::Copy - cannot copy, no valid :to directories
rake aborted!
uncaught throw :task_has_failed我尝试过不同的方法,这里提到的太多了,但是都返回了上面的Guard::copy - cannot copy, no valid :to directories。dist目录确实存在。另外,如果我从shell、rake或cmd行调用guard >,那么它运行得很完美,但只剩下外壳。你认为我的问题可能是rake文件中的语法错误吗?(感谢任何帮助;)
发布于 2013-04-10 13:40:09
保护副本在#start方法中进行了一些初始化,因此您需要在运行它之前启动它:
task :watcher do
Guard.setup
copy = Guard.guards('copy')
copy.start
copy.run_all
end此外,没有必要再打电话给Guard::Dsl.evaluate_guardfile,维基上的信息已经过时了。
编辑1:继续观察
当你想看dir的时候,你需要开始守卫:
task :watcher do
Guard.start
copy = Guard.guards('copy')
copy.start
copy.run_all
end注意:如果您设置了并在之后启动它,那么Hook with name 'load_guard_rc'会失败
编辑2:真的要继续看
保护程序在非阻塞模式下启动听,因此为了使呼叫阻塞,您需要等待它:
task :watcher do
Guard.start
copy = Guard.guards('copy')
copy.start
copy.run_all
while ::Guard.running do
sleep 0.5
end
end如果还想禁用交互,可以传递no_interactions选项:
Guard.start({ no_interactions: true })API绝对不是最优的,当我们移除Ruby1.8.7支持和一些不推荐的东西时,我会改进它。
https://stackoverflow.com/questions/15919277
复制相似问题