-
Notifications
You must be signed in to change notification settings - Fork 0
/
knifecloudgen.rb
100 lines (78 loc) · 2.47 KB
/
knifecloudgen.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env ruby
# list all erb files in folder
# process erb file and write out in same structure, renaming to remove erb
# list all folders and rename any cloud tags.
require 'json'
require 'fileutils'
require './erb_helpers'
def mylog(string)
unless ENV["enabledebug"].nil?
puts string
end
end
class Codegenerator
def initialize(destination_dir, properties_file)
@destination_dir = destination_dir
@properties_file = properties_file
end
private
def _properties
@properties ||= JSON.parse(File.read(@properties_file))
end
def _src_files
Dir.glob(File.join("templates", "**", "*"))
end
def _erbfiles
Dir.glob(File.join("templates", "**", "*.erb"))
end
# return all folders within source templates
def _folders
Dir.glob(File.join("templates", "**/"))
end
def cloudname
_properties["__MY_CLOUD_NAME__"]
end
def _create_folders_in_destination
_folders.each do |folder|
mylog "Processing folder [#{folder}]..."
destination = folder.sub(/__MY_CLOUD_NAME__/, cloudname).sub(/templates?/, @destination_dir)
mylog "Creating directory (#{destination})..."
begin
Dir.mkdir(destination)
rescue Errno::EEXIST
end
end
end
def _generate_static_files
(_src_files - _erbfiles).each do |file|
unless File.directory?(file)
mylog "Copying static file [#{file}] to [#{file.sub(/templates?/, @destination_dir)}]"
::FileUtils.cp(file, file.sub(/templates?/, @destination_dir))
end
end
end
def _generate_source_files
_erbfiles.each do |erb_file|
mylog "Processing file [#{erb_file}]..."
# Compile the erb
content = ERBHelpers::ERBCompiler.run(File.read(erb_file), _properties)
destination_file_name = File.join(@destination_dir, erb_file.sub(/templates?/, '').sub(/.erb$/, '')).sub(/__MY_CLOUD_NAME__/, cloudname)
mylog "Writing to a file (#{destination_file_name})..."
File.open(destination_file_name, "w:utf-8") do |f|
f.write(content)
f.chmod(0644)
end
end
end
public
def run
_create_folders_in_destination
_generate_static_files
_generate_source_files
end
end
## Main
destination_dir, properties_file = ARGV[0], ARGV[1]
# create destination if required.
Dir.mkdir(destination_dir) unless File.directory?(destination_dir)
Codegenerator.new(destination_dir, properties_file).run