你需要它的規則? 您可以嘗試爲它創建多個文件的任務:
n = 1..6 #Your n-Array
n.each do |i|
targetfile = 'file_c%i.dst' % i #Define the file to be created
desc 'Create %s based n %s' % [ targetfile, 'file_c.src']
file targetfile => 'file_c.src' do |task| #Define task to create targetfile
puts "cmd1 -o #{task.name} -i #{task.source}" #Command to create targetfile
end
end
在我的測試我沒有問題,將其與已定義的規則結合起來。
下面我的完整testcode:
require 'rake'
src_files = Rake::FileList["*.src"]
dst_files = src_files.ext 'dst'
task :default => dst_files
def build_dst(dst, src)
puts "cmd -o #{dst} -i #{src}" #Command to create targetfile
File.open(dst,'w'){|f|} #Dummy to simulate generation
end
rule '.dst' => '.src' do |task|
build_dst task.name, task.source
end
N = 1..6 #array of numbers.
N.each do |i|
targetfile = 'file_c%i.dst' % i #Define the file to be created
desc 'Create %s based n %s' % [ targetfile, 'file_c.src']
file targetfile => 'file_c.src' do |task| #Define task to create targetfile
build_dst task.name, task.source
end
end
#Make dummy-sources.
File.open('file_c.src','w'){|f|}
File.open('x.src','w'){|f|}
Rake.application['x.dst'].invoke
Rake.application['file_c3.dst'].invoke
你會看到,這些命令exectuted預期:
cmd -o x.dst -i x.src
cmd -o file_c3.dst -i file_c.src
一個加法 - 你的問題的範圍: 我有一些問題可以看到你的問題的真實用例。
如果生成file_c<n>.dst
的命令僅依賴於相同的源代碼,那麼生成的dst文件將是相同的 - 您可以複製結果。我會期待一些其他的依賴性差異。
如果您file_c<n>.dst
取決於一個file_c<n>.src
和file_c.src
,那麼你可以使用類似:
require 'rake'
rule '.dst' => '.src' do |task|
puts "cmd -o #{task.name} -i #{task.prerequisites}" #Command to create targetfile
File.open(task.name,'w'){} #Dummy to simulate generation
end
#Add additional prerequsites (cold also defined generic)
file 'file_c3.dst' => 'file_c.src'
#Make dummy-sources.
#~ File.open('file_c.src','w'){|f|}
#~ File.open('file_c3.src','w'){|f|}
Rake.application['file_c3.dst'].invoke
我添加額外的先決條件,並通過task.prerequisites
取代task.source
。
更新:我瞭解到,沒有對具有多個目標的編寫規則的直接支持(參考:http://hyperpolyglot.org/build)。但我覺得應該有辦法做到這一點... – Ninad
觀看吉姆Weirich這個談話,https://www.youtube.com/watch?v=KaEqZtulOus它顯示了一些漂亮的整潔技巧與耙子.. – Doon
@Doon,謝謝。 '動力耙'!? :)吉姆一如既往的精彩演講。現在我明白了文件任務和規則之間的區別 - 而且應該讓規則變得非常簡單,而是將文件任務用於任何比具有不同擴展名更復雜的任務。 – Ninad