ruby-on-rails - rake 任务使用命名空间耗尽命名空间

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

我为我正在构建的项目设置了一系列自定义 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该目录中的文件。这是我的 helper :

模块 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

关于ruby-on-rails - rake 任务使用命名空间耗尽命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24022217/

相关文章:

ruby - 在 Ruby 中拆分字符串,忽略括号的内容?

ruby-on-rails - 包含相关元素

PHP Namespace,为什么还要用后缀?

python - 将 `exec "pass"` 添加到函数允许对 locals() 的修改反射(reflect)在命名空间中吗?

javascript - ruby on Rails CoffeeScript 输入表单

ruby-on-rails - 类语法,什么时候需要或者不需要符号?

ruby-on-rails - rails 中的私有(private)协会

xml - 如何使用 MSXML2 检测外部 XML 文档中的命名空间?

ruby-on-rails - 使用 curl 将 JSON 数据发布到简单的 Rails 应用程序

ruby-on-rails - 使用 rspec 测试 Rails 引擎生成器