Search code examples
rubyrake

What is the magic behind "desc 'description'" on rake?


For example on 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

When i ran rake --task, it returned as description.

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

But i dont understand how can they take the description, isn't it replaced every time the description called ? How do they know specific description belongs to specific task ?


Solution

  • Imagine simple DSL that has a state (very naïve implementation):

    @tasks = []
    @current = nil
    
    def desc text
      @current = Task.new(desc: text)
    end
    
    def task params, &cb
      @current.update(params)
      yield
       ....
      @tasks << @current
      @current = nil
    end
    

    The code above requires additional checks etc, but the idea is that: stateful DSL collects tasks with their descriptions.