耙子上"desc'描述'背后的魔力是什么?

Yan*_*nto 0 ruby rake

例如,在 https://github.com/locomotivecms/wagon/blob/master/Rakefile#L23上

desc 'build the gem and release it to rubygems.org'
task release: :gem do
  sh "gem push pkg/locomotivecms_wagon-#{gemspec.version}.gem"
end
Run Code Online (Sandbox Code Playgroud)

当我跑rake --task,它返回作为描述.

rake clobber_package   # Remove package products
rake gem               # Build the gem file locomotivecms_wagon-2.2.0.beta1.gem
rake package           # Build all the packages
rake release           # build the gem and release it to rubygems.org
rake repackage         # Force a rebuild of the package files
rake spec              # Run RSpec code examples
rake spec:integration  # Run RSpec code examples
rake spec:unit         # Run RSpec code examples
Run Code Online (Sandbox Code Playgroud)

但我不明白他们怎么能接受描述,是不是每次描述时都会被取代?他们如何知道具体描述属于特定任务?

Ale*_*kin 6

想象一下简单的DSL 有一个状态(非常天真的实现):

@tasks = []
@current = nil

def desc text
  @current = Tasl.new(desc: text)
end

def task params, &cb
  @current.update(params)
  yield
   ....
  @tasks << @current
  @current = nil
end
Run Code Online (Sandbox Code Playgroud)

上面的代码需要额外的检查等,但想法是:有状态的DSL用他们的描述收集任务.