require 'rake'
task :my_task, [:arg1, :arg2] do |t, args|puts "Args were: #{args} of class #{args.class}"puts "arg1 was: '#{args[:arg1]}' of class #{args[:arg1].class}"puts "arg2 was: '#{args[:arg2]}' of class #{args[:arg2].class}"end
task :invoke_my_task doRake.application.invoke_task("my_task[1, 2]")end
# or if you prefer this syntax...task :invoke_my_task_2 doRake::Task[:my_task].invoke(3, 4)end
# a task with prerequisites passes its# arguments to it prerequisitestask :with_prerequisite, [:arg1, :arg2] => :my_task #<- name of prerequisite task
# to specify default values,# we take advantage of args being a Rake::TaskArguments objecttask :with_defaults, :arg1, :arg2 do |t, args|args.with_defaults(:arg1 => :default_1, :arg2 => :default_2)puts "Args with defaults were: #{args}"end
然后,从命令行:
> rake my_task[1,false]Args were: {:arg1=>"1", :arg2=>"false"} of class Rake::TaskArgumentsarg1 was: '1' of class Stringarg2 was: 'false' of class String
> rake "my_task[1, 2]"Args were: {:arg1=>"1", :arg2=>"2"}
> rake invoke_my_taskArgs were: {:arg1=>"1", :arg2=>"2"}
> rake invoke_my_task_2Args were: {:arg1=>3, :arg2=>4}
> rake with_prerequisite[5,6]Args were: {:arg1=>"5", :arg2=>"6"}
> rake with_defaultsArgs with defaults were: {:arg1=>:default_1, :arg2=>:default_2}
> rake with_defaults['x','y']Args with defaults were: {:arg1=>"x", :arg2=>"y"}
require 'rake'require 'optparse'# Rake task for creating an account
namespace :user do |args|desc 'Creates user account with given credentials: rake user:create'# environment is required to have access to Rails modelstask :create dooptions = {}OptionParser.new(args) do |opts|opts.banner = "Usage: rake user:create [options]"opts.on("-u", "--user {username}","User's email address", String) do |user|options[:user] = userendopts.on("-p", "--pass {password}","User's password", String) do |pass|options[:pass] = passendend.parse!
puts "creating user account..."u = Hash.newu[:email] = options[:user]u[:password] = options[:pass]# with some DB layer like ActiveRecord:# user = User.new(u); user.save!puts "user: " + u.to_sputs "account created."exit 0endend
task :default, [:version] => [:build]
task :build, :version do |t,args|version = args[:version]puts version ? "version is #{version}" : "no version passed"end
namespace :name_sapace_task dodesc "Description task...."task :name_task => :environment dodata = ActiveSupport::JSON.decode(File.read(Rails.root+"public/file.json")) if defined?(data)# and work whit yoour data, example is data["user_id"]
endend
示例json
{"name_task": "I'm a task","user_id": 389,"users_assigned": [389,672,524],"task_id": 3}
namespace :thing dodesc "it does a thing"task :work, [:option, :foo, :bar] do |task, args|puts "work", argsend
task :another, [:option, :foo, :bar] do |task, args|puts "another #{args}"Rake::Task["thing:work"].invoke(args[:option], args[:foo], args[:bar])# or splat the args# Rake::Task["thing:work"].invoke(*args)end
end
namespace :dummy_data dodesc "Tests options hash like arguments"task :test, [:options] => :environment do |t, args|arg_options = args[:options] || '' # nil catch incase no options are providedtwo_d_array = arg_options.scan(/\W*(\w*): (\w*)\W*/)puts two_d_array.to_s + ' # options are regexed into a 2d array'string_key_hash = two_d_array.to_hputs string_key_hash.to_s + ' # options are in a hash with keys as strings'options = two_d_array.map {|p| [p[0].to_sym, p[1]]}.to_hputs options.to_s + ' # options are in a hash with symbols'default_options = {users: '50', friends: '25', colour: 'red', name: 'tom'}options = default_options.merge(options)puts options.to_s + ' # default option values are merged into options'endend
在你得到的命令行上。
$ rake dummy_data:test["users: 100 friends: 50 colour: red"][["users", "100"], ["friends", "50"], ["colour", "red"]] # options are regexed into a 2d array{"users"=>"100", "friends"=>"50", "colour"=>"red"} # options are in a hash with keys as strings{:users=>"100", :friends=>"50", :colour=>"red"} # options are in a hash with symbols{:users=>"100", :friends=>"50", :colour=>"red", :name=>"tom"} # default option values are merged into options