2016-06-15 69 views
0

上。例如 https://github.com/locomotivecms/wagon/blob/master/Rakefile#L23耙子上「desc'描述」背後的魔術是什麼?

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

當我跑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 

但我不明白他們怎麼可以採取描述,不是每次描述被調用時都被替換嗎?他們如何知道具體的描述屬於特定的任務?

回答

5

試想一下簡單的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 

上面的代碼需要額外的檢查等,但思路是:狀態DSL收集任務,包括其描述。

+0

感謝@mudasobwa的解釋。 – vigo

相關問題