forked from agrbin/svgtex
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen-init-scripts.rb
executable file
·71 lines (52 loc) · 1.53 KB
/
gen-init-scripts.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env ruby
require 'erb'
require 'json'
require 'yaml'
rootdir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
indir = File.join(rootdir, 'dist', 'init-scripts')
outdir = indir
class ScriptData
include ERB::Util
@@suffix = {'systemd' => '.service', 'upstart' => '.conf'}
def initialize input_dir
@template = {}
self.init input_dir
end
def set_info root_dir
self.read_info(root_dir).each do |key, value|
self.instance_variable_set "@#{key}".to_sym, value
end
@service_name = @name
@no_file ||= 10000
end
def generate output_dir
@template.each do |name, erb|
File.open(File.join(output_dir, "#{@name}#{@@suffix[name]}"), 'w') do |io|
io.write erb.result(binding())
end
end
end
def init input_dir
Dir.glob(File.join(input_dir, '*.erb')).each do |fname|
@template[File.basename(fname, '.erb')] = ERB.new(File.read(fname))
end
end
def read_info root_dir
data = YAML.load(File.read(File.join(root_dir, 'config.yaml')))['services'][0]['conf']
return data.merge(JSON.load(File.read(File.join(root_dir, 'package.json'))))
end
end
if ARGV.size > 0 and ['-h', '--help'].include? ARGV[0]
puts 'This is a simple script to generate various service init scripts'
puts 'Usage: gen-init-scripts.rb [output_dir]'
exit 1
elsif ARGV.size > 0
outdir = ARGV[0]
end
unless File.directory? outdir
STDERR.puts 'The output directory must exist! Aborting...'
exit 2
end
data = ScriptData.new indir
data.set_info rootdir
data.generate outdir