问题描述
我有一些要使用 Ruby 编码的输入文件.编码的输出应该根据输入文件的文件名匹配某种模式.为了不手动执行此操作,我想使用 Rake 作为自动化的帮助.此外,我不想为每个输入文件指定一个任务.
I've got some input files which I want to encode using Ruby. The output from the encoding should match some pattern based on the filename of the input file. In order to not do this by hand, I want to use Rake as help for automation. Further I'd like to not specify a single task for every input file.
我尝试了一些 FileList魔法",但没有奏效.代码如下:
I've tried some FileList "magic" but it didn't work out. Here's the code:
desc 'Create all output from specified input'
task :encode do
FileList['input/*.txt'].each {|input| file "output/output_#{input}" => input}
end
有人可以帮忙吗?我没有在网上找到关于多个输出文件作为依赖项的任何信息.
Anyone can help? I didn't find find anything on the web about multiple output files as dependency.
推荐答案
我会考虑使用 Rake 的 rule
任务,它允许基于正则表达式规则动态定义任务.有关详细信息,请参阅 rakefile rdoc 页面,但这里有一个示例:
I would look into using Rake's rule
tasks, which allow tasks to be dynamically defined based on regex rules. See the rakefile rdoc page for details, but here's an example:
# 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/输出_*
.
And then you should be able to run rake encode
in a directory with files matching input/*
, and have the output files be placed in output/output_*
.
这篇关于如何使用输入文件名通过 Rake 生成输出文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!