Skip to content

Instantly share code, notes, and snippets.

@Baozi2
Last active January 14, 2019 08:07
Show Gist options
  • Save Baozi2/12db8e49419c2b336c47e7d34d65d9a4 to your computer and use it in GitHub Desktop.
Save Baozi2/12db8e49419c2b336c47e7d34d65d9a4 to your computer and use it in GitHub Desktop.
ruby rake demo 定义 依赖、参数、参数和依赖
desc 'when run `rake` a task name default is run by default '
task :default do
p 'I’m default'
end
desc 't0 task name 可以是 symbol或string'
task 't0' do
p 't0'
end
desc 't1 task description'
task :t1 do
p 'task t1'
end
desc '定义任务依赖'
task :t2 => :t1 do
p 'task t2'
end
desc '定义任务依赖'
task t2_1: :t1 do
p 'task t2_1'
end
desc '定义任务依赖'
task t2_2: [:t1] do
p 'task t2_2'
end
desc '一个任务可以被重复定义 累加每次的action'
task :t1 do
p 'task t1-2'
end
desc 't3 定义依赖任务可以使用 => 或者 hash'
task t3: [:t1, :t0] do
p 't3'
end
desc 't4 参数'
task :t4, :message do |task, args|
p args.message
end
# rake t4[hello]
# 如果有空格需要"" rake "t4[hello world]"
desc 't5 参数是一个list'
task :t5, :message do |task, args|
p args.to_a
p '-' * 10
p args.extras # 次方法获取除第一个参数后的参数
end
# rake t5[1,2,3]
desc 't6 参数和依赖任务'
task :t6, [:name, :time] => :t0 do|task, args|
p args.name
p args[:time]
end
# rake "t6['t6',`date`]"
desc '参数默认值'
taks :t61, [:name] do|task, args|
args.with_defaults(:name => "John")
end
namespace :demo do
task :t1 do
p 'demo:t1'
end
end
desc 'call namespace'
task t7: 'demo:t1' do
p 't7'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment