【问题标题】:ruby rake tasks in a namespace shared but not :all命名空间中的 ruby​​ rake 任务共享但不是 :all
【发布时间】:2014-05-31 06:56:54
【问题描述】:

我正在处理以下 rake 文件:

namespace :build do
  desc 'Build development application to the build dist directory'
    task :default do
        Rake::Task[:delete].invoke
    end


    task :development do
        verbose(false) do
            puts "=> Building the frontend test build version of application!"

        end
    end
    desc 'Build production application to the build dist directory minified with no tests'
    task :production do
        verbose(false) do
            puts "=> Building the frontend production version of application!"

        end
    end
end

它有 2 个简单的任务。是的,我在开发过程中把它们拿出来了?我想要实现的是每次调用子 :development 或 :production 任务时都运行 Rake::Task[:delete].invoke

另外,我也对设置 :all 不感兴趣。

感谢您的帮助:)

【问题讨论】:

    标签: ruby-on-rails rake rake-task rakefile


    【解决方案1】:

    您可以直接在依赖任务上表达依赖关系,例如

    namespace :build do
      task :development => :delete do
        puts "development!"
      end
      task :production => :delete do
        puts "production!"
      end
    end
    
    task :delete do
      puts "delete!"
    end
    

    【讨论】:

    • 我在玩这个,但如果你想在其他人被解雇之前或之前运行该任务怎么办?
    • 在我的示例中, :delete 将在 :development 或 :production 之前运行(无论您调用哪个)。(我省略了默认值 - 如果您想要“rake build”,例如,默认为:development [首先使用 :delete!],您可以在命名空间块内添加“task :default => :development”。
    • 好的,我更喜欢这种方法。最后一招,我需要。您将如何添加将在之后运行的任务?因此,正如您所展示的,删除会首先触发,而不是运行它的任务,然后是共享的发布任务?
    • 过早地谈论那个默认的东西。如果您想假装在构建命名空间中有一个默认任务(由“rake build”调用,您实际上需要在 :build 命名空间外部添加一个 :build 任务。
    • 是的。我也在考虑添加后续任务,最好的做法是在另一个调用中折腾还是用一个方法把它干掉?
    【解决方案2】:

    使用rake-hooks 项目,我可以进行如下调整:

    namespace :build do
        desc 'Build development application to the build dist directory'
        task :development do
            verbose(false) do
                puts "=> Building the frontend test build version of application!"
    
            end
        end
        desc 'Build production application to the build dist directory minified with no tests'
        task :production do
            verbose(false) do
                puts "=> Building the frontend production version of application!"
    
            end
        end
    
    end
    before "build:development", "build:production" do
      Rake::Task[:delete].invoke
    end
    

    还要确保 require 'rake/hooks' 在您的 rake 文件的顶部。

    【讨论】:

    • 是的,评论我自己的帖子,如果你使用这种方法,请注意健身房已经过时了。
    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    相关资源
    最近更新 更多