OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / tool / generic_erb.rb
1 require 'erb'
2 require 'optparse'
3 require 'fileutils'
4
5 timestamp = nil
6 output = nil
7 ifchange = nil
8 opt = OptionParser.new do |o|
9   o.on('-t', '--timestamp[=PATH]') {|v| timestamp = v || true}
10   o.on('-o', '--output=PATH') {|v| output = v}
11   o.on('-c', '--[no-]if-change') {|v| ifchange = v}
12   o.order!(ARGV)
13 end or abort opt.opt_s
14 template = ARGV.shift
15 erb = ERB.new(File.read(template), nil, '%')
16 erb.filename = template
17 result = erb.result
18 if output
19   if ifchange and (IO.read(output) rescue nil) == result
20     puts "#{output} unchanged"
21   else
22     open(output, "wb") {|f| f.print result}
23     puts "#{output} updated"
24   end
25   if timestamp
26     if timestamp == true
27       dir, base = File.split(output)
28       timestamp = File.join(dir, ".time." + base)
29     end
30     FileUtils.touch(timestamp)
31   end
32 else
33   print result
34 end