2012-11-29 03:12:29 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2008-09-26 04:18:49 -04:00
|
|
|
require 'erb'
|
2008-10-17 06:46:23 -04:00
|
|
|
require 'optparse'
|
|
|
|
require 'fileutils'
|
2012-11-29 03:12:29 -05:00
|
|
|
$:.unshift(File.dirname(__FILE__))
|
|
|
|
require 'vpath'
|
2008-09-26 04:18:49 -04:00
|
|
|
|
2012-11-29 03:12:29 -05:00
|
|
|
vpath = VPath.new
|
2008-10-17 06:46:23 -04:00
|
|
|
timestamp = nil
|
|
|
|
output = nil
|
|
|
|
ifchange = nil
|
2013-05-16 00:13:36 -04:00
|
|
|
source = false
|
2015-10-22 02:25:33 -04:00
|
|
|
color = nil
|
2012-08-25 03:20:29 -04:00
|
|
|
|
2008-10-17 06: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 00:13:36 -04:00
|
|
|
o.on('-x', '--source') {source = true}
|
2015-10-22 02:25:33 -04:00
|
|
|
o.on('--color') {color = true}
|
2012-11-29 03:12:29 -05:00
|
|
|
vpath.def_options(o)
|
2008-10-17 06:46:23 -04:00
|
|
|
o.order!(ARGV)
|
2009-02-02 18:15:59 -05:00
|
|
|
end
|
2015-10-22 02:25:33 -04:00
|
|
|
unchanged = "unchanged"
|
|
|
|
updated = "updated"
|
|
|
|
if color or (color == nil && STDOUT.tty?)
|
2015-10-30 20:02:19 -04:00
|
|
|
if (/\A\e\[.*m\z/ =~ IO.popen("tput smso", "r", err: IO::NULL, &:read) rescue nil)
|
2015-10-22 10:02:38 -04:00
|
|
|
beg = "\e["
|
2015-10-22 02:25:33 -04:00
|
|
|
colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:\n]*)/)] : {}
|
|
|
|
reset = "#{beg}m"
|
|
|
|
unchanged = "#{beg}#{colors["pass"] || "32;1"}m#{unchanged}#{reset}"
|
|
|
|
updated = "#{beg}#{colors["fail"] || "31;1"}m#{updated}#{reset}"
|
|
|
|
end
|
|
|
|
end
|
2009-02-02 18:15:59 -05:00
|
|
|
template = ARGV.shift or abort opt.to_s
|
2014-11-06 09:54:39 -05:00
|
|
|
erb = ERB.new(File.read(template), nil, '%-')
|
2008-10-17 06:46:23 -04:00
|
|
|
erb.filename = template
|
2013-05-16 00:13:36 -04:00
|
|
|
result = source ? erb.src : erb.result
|
2008-10-17 06:46:23 -04:00
|
|
|
if output
|
2014-11-26 22:42:54 -05:00
|
|
|
if ifchange and (vpath.open(output, "rb") {|f| f.read} rescue nil) == result
|
2015-10-22 02:25:33 -04:00
|
|
|
puts "#{output} #{unchanged}"
|
2008-10-19 08:12:53 -04:00
|
|
|
else
|
|
|
|
open(output, "wb") {|f| f.print result}
|
2015-10-22 02:25:33 -04:00
|
|
|
puts "#{output} #{updated}"
|
2008-10-17 06: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 08:19:19 -04:00
|
|
|
else
|
|
|
|
print result
|
2008-10-17 06:46:23 -04:00
|
|
|
end
|