如何使用输入文件名生成Rake的输出文件名?

我有一些输入文件,我想用Ruby编码。 编码的输出应该基于输入文件的文件名匹配某些模式。 为了不手动执行此操作,我想使用Rake作为自动化的帮助。 此外,我不想为每个输入文件指定单个任务。

我已经尝试了一些FileList“魔术”,但它没有成功。 这是代码:

desc 'Create all output from specified input' task :encode do FileList['input/*.txt'].each {|input| file "output/output_#{input}" => input} end 

有人可以帮忙吗? 我没有在网上找到任何关于多个输出文件作为依赖项的内容。

我将研究使用Rake的rule任务,它允许根据正则表达式规则动态定义任务。 有关详细信息,请参阅rakefile rdoc页面,但这是一个示例:

 # creates the 'output' directory on demand directory "output" # define a task rule covering all the output files rule %r{output/output_} => ['output', proc {|task_name| "input/#{task_name.sub('output/output_', '')}.txt"}] do |t| # replace this with your encoding logic sh "echo 'file #{t.source}' > #{t.name}" end # define a top-level 'encode' task which depends on all the 'output/output_*' tasks task :encode => Dir['input/*.txt'].map {|x| "output/output_#{File.basename(x).sub('.txt', '')}"} 

然后你应该能够在一个文件匹配input/*的目录中运行rake encode ,并将输出文件放在output/output_*