ruby on rails - rake tasks dry up namespace with namespace -


i setup series of custom rake tasks project building name spaced placewise. these tasks can viewed running rake tasks console.

desc 'list available placewise rake tasks application.' task :tasks     result = %x[rake -t | sed -n '/placewise:/{/grep/!p;}']     result.each_line |task|             puts task     end end 

all of these tasks stored in lib/tasks/placewise , built so:

namespace :placewise     namespace :db     desc "drop , create current database, argument [env] = environment."     task :recreate, [:env] |t,args|             env = environments(args.env)             msg("dropping #{env} database")             shell("rails_env=#{env} rake db:drop", step: "1/3")             msg("creating #{env} database")             shell("rails_env=#{env} rake db:create", step: "2/3")             msg("running #{env} database migrations")             shell("rails_env=#{env} rake db:migrate", step: "3/3")     end   end end 

a new task, example may start base setup follows:

namespace :placewise     namespace :example     desc "example"     task :example      end   end end 

as can see namespace :placewise do replicated each time. want keep of our custom rake tasks in same group, however, curious if there way avoid having add namespace each .rake file?

cheers.

unfortunately advised against strategy , in process of discovery found out helper methods not setup correctly. here goes.

i created new modules folder in lib/modules new helper_functions.rb file inside directory. here helpers:

module helperfunctions

    # ------------------------------------------------------------------------------------     # permitted environments     # ------------------------------------------------------------------------------------     def environments(arg)         arg = arg || "development"         environments = ["development", "test", "production"]         if environments.include?(arg)             puts             msg("environment parameter 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 

then in rakefile add:

# shared ruby functions used in rake tasks require file.expand_path('../lib/modules/helper_functions', __file__) include helperfunctions  rails.application.load_tasks  # not add other tasks file, make files in primary lib/tasks dir ending in .rake # placewise tasks should under lib/tasks/placewise folder , end in .rake desc 'list available placewise rake tasks application.' task :tasks     result = %x[rake -t | sed -n '/placewise:/{/grep/!p;}']     result.each_line |task|             puts task     end end 

and .rake tasks like:

namespace :placewise # ------------------------------------------------------------------------------------     namespace :db         # ------------------------------------------------------------------------------------     desc "drop , create current database, argument [env] = environment."     task :recreate, [:env] |t,args|             env = helperfunctions::environments(args.env)             helperfunctions::msg("dropping #{env} database")             helperfunctions::shell("rails_env=#{env} rake db:drop", step: "1/3")             helperfunctions::msg("creating #{env} database")             helperfunctions::shell("rails_env=#{env} rake db:create", step: "2/3")             helperfunctions::msg("running #{env} database migrations")             helperfunctions::shell("rails_env=#{env} rake db:migrate", step: "3/3")     end         # ------------------------------------------------------------------------------------   end # ------------------------------------------------------------------------------------ end 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -