我有一些输入文件,我想使用 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编码
,并将输出文件放置在 output/输出_*
。
关于ruby - 如何使用 Rake 输入文件名生成输出文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15493501/