-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenutil.rb
54 lines (45 loc) · 1.09 KB
/
genutil.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'ostruct'
require 'json'
require 'yaml'
require 'pp'
require 'Set'
require 'erb'
class Hash
def to_o
JSON.parse to_json, object_class: OpenStruct
end
end
class Array
def prep_join(prep)
self.map { |e| prep + e }.join;
end
end
def indent(s, times = 1)
s.split("\n").map{|l| (' ' * times) + l}.join("\n")
end
def expand_cargs(args)
(args || []).map{ |arg| "#{ arg.type } #{ arg.name }" }.prep_join(", ")
end
def expand_cparams(args)
(args || []).map{ |arg| arg.name }.prep_join(", ")
end
class ErbalT < OpenStruct
def result(path)
ret = nil
File::open(path, 'r') do |f|
src = f.read.gsub(/\/\*%/, '<%').gsub(/%\*\//, '%>')
ret = ERB.new(src, nil, '-').result(binding)
end
ret.strip + "\n"
end
def render(path, out_path = nil)
header = <<-EOS
// *** WARNING ***
// This file is generated by #{File.basename($0)}
// Keep untouched or ruin the abstraction
EOS
src = header + result(path)
out_path = out_path || path.gsub(/\.template/, '')
File.write(out_path, src)
end
end