2012-11-29 12:12:29 +04:00
|
|
|
# -*- coding: us-ascii -*-
|
2008-09-26 12:18:49 +04:00
|
|
|
require 'erb'
|
2008-10-17 14:46:23 +04:00
|
|
|
require 'optparse'
|
|
|
|
require 'fileutils'
|
2012-11-29 12:12:29 +04:00
|
|
|
$:.unshift(File.dirname(__FILE__))
|
|
|
|
require 'vpath'
|
2008-09-26 12:18:49 +04:00
|
|
|
|
2012-11-29 12:12:29 +04:00
|
|
|
vpath = VPath.new
|
2008-10-17 14:46:23 +04:00
|
|
|
timestamp = nil
|
|
|
|
output = nil
|
|
|
|
ifchange = nil
|
2013-05-16 08:13:36 +04:00
|
|
|
source = false
|
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}
|
2013-05-16 08:13:36 +04:00
|
|
|
o.on('-x', '--source') {source = true}
|
2012-11-29 12:12:29 +04:00
|
|
|
vpath.def_options(o)
|
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
|
2013-05-16 08:13:36 +04:00
|
|
|
result = source ? erb.src : erb.result
|
2008-10-17 14:46:23 +04:00
|
|
|
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
|