【问题标题】:rake tasks dry up namespace with namespacerake 任务用命名空间干涸命名空间
【发布时间】:2014-06-03 18:19:16
【问题描述】:

我为我正在构建的项目设置了一系列自定义 rake 任务,这些任务按位置排列。这些任务可以通过从控制台运行rake tasks 来查看。

desc 'List all available placewise rake tasks for this application.'
task :tasks do
    result = %x[rake -T | sed -n '/placewise:/{/grep/!p;}']
    result.each_line do |task|
            puts task
    end
end

所有这些任务都存储在lib/tasks/placewise 中,并像这样构建:

namespace :placewise do
    namespace :db do
    desc "Drop and create the current database, the argument [env] = environment."
    task :recreate, [:env] do |t,args|
            env = environments(args.env)
            msg("Dropping the #{env} database")
            shell("RAILS_ENV=#{env} rake db:drop", step: "1/3")
            msg("Creating the #{env} database")
            shell("RAILS_ENV=#{env} rake db:create", step: "2/3")
            msg("Running the #{env} database migrations")
            shell("RAILS_ENV=#{env} rake db:migrate", step: "3/3")
    end
  end
end

一个新任务,例如可以从基本设置开始,如下所示:

namespace :placewise do
    namespace :example do
    desc "example"
    task :example do

    end
  end
end

如您所见,namespace :placewise do 每次都会被复制。我想将我们所有的自定义 rake 任务保留在同一个组中,但是,我很好奇是否有办法避免将该命名空间添加到每个 .rake 文件中?

干杯。

【问题讨论】:

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


    【解决方案1】:

    不幸的是,有人建议我不要使用这种策略,在发现过程中我发现我的辅助方法设置不正确。就这样吧。

    我在lib/modules 中创建了一个新的模块文件夹,其中包含新的helper_functions.rb 文件。这是我的助手:

    模块 HelperFunctions

        # ------------------------------------------------------------------------------------
        # Permitted environments
        # ------------------------------------------------------------------------------------
        def environments(arg)
            arg = arg || "development"
            environments = ["development", "test", "production"]
            if environments.include?(arg)
                puts
                msg("Environment parameter is valid")
                return arg
            else
                error("Invalid environment parameter")
                exit
            end
        end
        # ------------------------------------------------------------------------------------
        # Console message handler
        # ------------------------------------------------------------------------------------
        def msg(txt, periods: "yes", new_line: "yes")
            txt = txt + "..." if periods == "yes"
            puts "===> " + txt
            puts if new_line == "yes"
        end
        def error(reason)
            puts "**** ERROR! ABORTING: " + reason + "!"
        end
        # ------------------------------------------------------------------------------------
        # Execute Shell Commands
        # ------------------------------------------------------------------------------------
        def shell(cmd, step: nil)
            msg("Starting step #{step}", new_line: "no") if step.present?
            if ENV['TRY']
                puts "-->> " + cmd
            else
                sh %{#{cmd}}
            end
            msg("#{step} completed!", periods: "no")
        end
    end
    

    然后在Rakefile添加:

    # Shared Ruby functions used in rake tasks
    require File.expand_path('../lib/modules/helper_functions', __FILE__)
    include HelperFunctions
    
    Rails.application.load_tasks
    
    # Do not add other tasks to this file, make files in the primary lib/tasks dir ending in .rake
    # All placewise tasks should be under the lib/tasks/placewise folder and end in .rake
    desc 'List all available placewise rake tasks for this application.'
    task :tasks do
        result = %x[rake -T | sed -n '/placewise:/{/grep/!p;}']
        result.each_line do |task|
                puts task
        end
    end
    

    最后我的.rake 任务看起来像:

    namespace :placewise do
    # ------------------------------------------------------------------------------------
        namespace :db do
            # ------------------------------------------------------------------------------------
        desc "Drop and create the current database, the argument [env] = environment."
        task :recreate, [:env] do |t,args|
                env = HelperFunctions::environments(args.env)
                HelperFunctions::msg("Dropping the #{env} database")
                HelperFunctions::shell("RAILS_ENV=#{env} rake db:drop", step: "1/3")
                HelperFunctions::msg("Creating the #{env} database")
                HelperFunctions::shell("RAILS_ENV=#{env} rake db:create", step: "2/3")
                HelperFunctions::msg("Running the #{env} database migrations")
                HelperFunctions::shell("RAILS_ENV=#{env} rake db:migrate", step: "3/3")
        end
            # ------------------------------------------------------------------------------------
      end
    # ------------------------------------------------------------------------------------
    end
    

    【讨论】:

      猜你喜欢
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多