首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rake防护任务

rake防护任务
EN

Stack Overflow用户
提问于 2013-04-10 07:12:19
回答 1查看 1.3K关注 0票数 2

Ruby -我需要让警卫在我的rake任务中运行,但是我找不到在后台运行它的方法。它需要在最后运行第二次,因此让guard >外壳等待命令会阻止最终任务的运行,因此在rake文件中调用sh bundle exec guard不是一个选项。根据这些文件,这应该是可行的:

代码语言:javascript
复制
  ##
  desc "Watch files"
  ##
  task :watcher do
    Guard.setup
    Guard::Dsl.evaluate_guardfile(:guardfile => 'Guardfile', :group => ['Frontend'])
    Guard.guards('copy').run_all
  end
  #end watch files

https://github.com/guard/guard/wiki/Use-Guard-programmatically-cookbook

这是我的守护文件,完整的,(与Rakefile相同的dir )

代码语言:javascript
复制
# 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返回一个错误:

代码语言:javascript
复制
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 directoriesdist目录确实存在。另外,如果我从shell、rake或cmd行调用guard >,那么它运行得很完美,但只剩下外壳。你认为我的问题可能是rake文件中的语法错误吗?(感谢任何帮助;)

EN

回答 1

Stack Overflow用户

发布于 2013-04-10 13:40:09

保护副本在#start方法中进行了一些初始化,因此您需要在运行它之前启动它:

代码语言:javascript
复制
task :watcher do
  Guard.setup
  copy = Guard.guards('copy')
  copy.start
  copy.run_all
end

此外,没有必要再打电话给Guard::Dsl.evaluate_guardfile,维基上的信息已经过时了。

编辑1:继续观察

当你想看dir的时候,你需要开始守卫:

代码语言:javascript
复制
task :watcher do
  Guard.start
  copy = Guard.guards('copy')
  copy.start
  copy.run_all
end

注意:如果您设置了并在之后启动它,那么Hook with name 'load_guard_rc'会失败

编辑2:真的要继续看

保护程序在非阻塞模式下启动,因此为了使呼叫阻塞,您需要等待它:

代码语言:javascript
复制
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选项:

代码语言:javascript
复制
Guard.start({ no_interactions: true })

API绝对不是最优的,当我们移除Ruby1.8.7支持和一些不推荐的东西时,我会改进它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15919277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档