2008-09-26 12:18:49 +04:00
|
|
|
require 'erb'
|
2008-10-17 14:46:23 +04:00
|
|
|
require 'optparse'
|
|
|
|
require 'fileutils'
|
2008-09-26 12:18:49 +04:00
|
|
|
|
2012-08-25 11:20:29 +04:00
|
|
|
vpath = ["."]
|
|
|
|
def vpath.open(file, *rest)
|
|
|
|
find do |dir|
|
|
|
|
begin
|
|
|
|
path = File.join(dir, file)
|
|
|
|
return File.open(path, *rest) {|f| yield(f)}
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end or raise(Errno::ENOENT, file)
|
|
|
|
end
|
|
|
|
|
2008-10-17 14:46:23 +04:00
|
|
|
timestamp = nil
|
|
|
|
output = nil
|
|
|
|
ifchange = nil
|
2012-08-25 11:20:29 +04:00
|
|
|
|
2008-10-17 14:46:23 +04:00
|
|
|
opt = OptionParser.new do |o|
|
|
|
|
o.on('-t', '--timestamp[=PATH]') {|v| timestamp = v || true}
|
|
|
|
o.on('-o', '--output=PATH') {|v| output = v}
|
|
|
|
o.on('-c', '--[no-]if-change') {|v| ifchange = v}
|
2012-08-25 11:20:29 +04:00
|
|
|
o.on('-v', '--vpath=DIR') {|dirs| vpath.concat dirs.split(File::PATH_SEPARATOR)}
|
2008-10-17 14:46:23 +04:00
|
|
|
o.order!(ARGV)
|
2009-02-03 02:15:59 +03:00
|
|
|
end
|
|
|
|
template = ARGV.shift or abort opt.to_s
|
2008-10-17 14:46:23 +04:00
|
|
|
erb = ERB.new(File.read(template), nil, '%')
|
|
|
|
erb.filename = template
|
|
|
|
result = erb.result
|
|
|
|
if output
|
2012-08-25 11:20:29 +04:00
|
|
|
if ifchange and (vpath.open(output) {|f| f.read} rescue nil) == result
|
2008-10-19 16:12:53 +04:00
|
|
|
puts "#{output} unchanged"
|
|
|
|
else
|
|
|
|
open(output, "wb") {|f| f.print result}
|
|
|
|
puts "#{output} updated"
|
2008-10-17 14:46:23 +04:00
|
|
|
end
|
|
|
|
if timestamp
|
|
|
|
if timestamp == true
|
|
|
|
dir, base = File.split(output)
|
|
|
|
timestamp = File.join(dir, ".time." + base)
|
|
|
|
end
|
|
|
|
FileUtils.touch(timestamp)
|
|
|
|
end
|
2008-10-19 16:19:19 +04:00
|
|
|
else
|
|
|
|
print result
|
2008-10-17 14:46:23 +04:00
|
|
|
end
|