我为我正在构建的项目设置了一系列自定义 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 文件?

干杯。

有帮助吗?

解决方案

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

我在中创建了一个新的模块文件夹 lib/modules 与新的 helper_functions.rb 该目录中的文件。这是我的帮手:

模块辅助函数

    # ------------------------------------------------------------------------------------
    # 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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top