2012-11-29 03:12:29 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2016-07-02 17:01:04 -04:00
|
|
|
|
|
|
|
# Used to expand Ruby template files by common.mk, uncommon.mk and
|
|
|
|
# some Ruby extension libraries.
|
|
|
|
|
2008-09-26 04:18:49 -04:00
|
|
|
require 'erb'
|
2008-10-17 06:46:23 -04:00
|
|
|
require 'optparse'
|
2019-07-15 00:15:13 -04:00
|
|
|
require_relative 'lib/vpath'
|
2021-03-12 19:14:20 -05:00
|
|
|
require_relative 'lib/colorize'
|
2008-09-26 04:18:49 -04:00
|
|
|
|
2012-11-29 03:12:29 -05:00
|
|
|
vpath = VPath.new
|
2021-03-12 19:14:20 -05:00
|
|
|
timestamp = nil
|
|
|
|
output = nil
|
|
|
|
ifchange = nil
|
2013-05-16 00:13:36 -04:00
|
|
|
source = false
|
2021-03-12 19:14:20 -05:00
|
|
|
color = nil
|
2017-09-30 22:39:22 -04:00
|
|
|
templates = []
|
2012-08-25 03:20:29 -04:00
|
|
|
|
2017-09-30 22:39:22 -04:00
|
|
|
ARGV.options do |o|
|
2021-03-12 19:14:20 -05:00
|
|
|
o.on('-t', '--timestamp[=PATH]') {|v| timestamp = v || true}
|
2017-09-30 22:39:22 -04:00
|
|
|
o.on('-i', '--input=PATH') {|v| template << v}
|
2021-03-12 19:14:20 -05:00
|
|
|
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}
|
2021-03-12 19:14:20 -05: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)
|
2017-09-30 22:39:22 -04:00
|
|
|
templates << (ARGV.shift or abort o.to_s) if templates.empty?
|
2009-02-02 18:15:59 -05:00
|
|
|
end
|
2021-03-12 19:14:20 -05:00
|
|
|
color = Colorize.new(color)
|
|
|
|
unchanged = color.pass("unchanged")
|
|
|
|
updated = color.fail("updated")
|
2017-04-20 23:01:12 -04:00
|
|
|
|
2017-09-30 22:39:22 -04:00
|
|
|
result = templates.map do |template|
|
2019-05-29 20:58:18 -04:00
|
|
|
if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
|
|
|
|
erb = ERB.new(File.read(template), trim_mode: '%-')
|
2018-02-22 08:28:25 -05:00
|
|
|
else
|
|
|
|
erb = ERB.new(File.read(template), nil, '%-')
|
|
|
|
end
|
2017-09-30 22:39:22 -04:00
|
|
|
erb.filename = template
|
|
|
|
source ? erb.src : proc{erb.result(binding)}.call
|
|
|
|
end
|
|
|
|
result = result.size == 1 ? result[0] : result.join("")
|
2021-03-12 19:14:20 -05:00
|
|
|
if output
|
|
|
|
if ifchange and (vpath.open(output, "rb") {|f| f.read} rescue nil) == result
|
|
|
|
puts "#{output} #{unchanged}"
|
|
|
|
else
|
|
|
|
open(output, "wb") {|f| f.print result}
|
|
|
|
puts "#{output} #{updated}"
|
|
|
|
end
|
|
|
|
if timestamp
|
|
|
|
if timestamp == true
|
|
|
|
dir, base = File.split(output)
|
|
|
|
timestamp = File.join(dir, ".time." + base)
|
|
|
|
end
|
|
|
|
File.open(timestamp, 'a') {}
|
|
|
|
File.utime(nil, nil, timestamp)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
print result
|
|
|
|
end
|