-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.rb
387 lines (327 loc) · 10.2 KB
/
generator.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/ruby
require 'yaml'
require 'fileutils'
require 'pathname'
require 'getoptlong'
build_opts = YAML.load_file("build_opt.yaml")
pwd = FileUtils.pwd()
if build_opts.key?("version")
version = build_opts["version"];
end
run_deps = build_opts["run_deps"] || []
dev_deps = build_opts["dev_deps"] || []
vala_files = []
ui_files = []
ui_vala_files = []
object_files_release = []
object_files_debug = []
def colorize_output(_text, _color)
_color_hash = {
"black" => 0,"red" => 1,"green" =>2 ,"yellow" =>3 ,"blue" =>4 ,"magenta" =>5 ,"cyan" =>6 ,"white" => 7
}
if not _color_hash.key?(_color)
return _text
end
"\033[#{_color_hash[_color] + 30}m #{_text} \033[0m"
end
def path_format(_path, is_dir)
_pwd = Dir.pwd()
_home = Dir.home()
if _path.start_with?("~/")
_path.gsub!("~/", _home + "/")
end
_path = File.absolute_path(_path)
if is_dir and (not _path.end_with?("/"))
_path = _path + "/"
end
_path.gsub!(" ", "\ ")
return _path
end
def get_raw_filename(fname)
fname[fname.rindex("/")+1..fname.length - 1]
end
def rel_path_from_pwd(_path)
_p1 = Pathname.new(_path)
_p2 = Pathname.new(Dir.pwd)
_p1.relative_path_from(_p2).to_s
end
def template_format(_vala_fname, _ui_fname, _modified_path)
_file = File.open(_vala_fname, "r")
_buf = ""
_file.each{|_line|
_t = _line
if _line.include?("GtkTemplate")
_t = "[GtkTemplate (ui = \"" + _ui_fname + "\" )]\n"
end
_buf = _buf + _t
}
_file.close()
_new_path = _modified_path + get_raw_filename(_vala_fname)
_file_write = File.open(_new_path, "w")
_file_write.write(_buf)
_file_write.close()
end
def reload_vala
_build_opts = YAML.load_file("build_opt.yaml")
_tmp = _build_opts["ui_files"]
_mod_src_path = _build_opts["modified_src_path"] || "./modified_src"
mod_src_path = path_format(_mod_src_path, true)
pwd = Dir.pwd
_tmp.each {|_x|
if not _x.is_a?(Hash)
begin
template_format(pwd + "/" + _x + ".vala", pwd + "/" + _x, mod_src_path)
rescue Exception => err
puts colorize_output("[WARN] can't find proper vala for #{pwd + "/" + _x}, may not compile", "yellow")
puts colorize_output("[ERROR] #{err.message}", "red")
end
else if _x.values()[0].key?("path")
_path = _x.values()[0]["path"]
path = path_format(_path, true)
_vala_path = _x.values()[0]["vala_path"]
if not _vala_path == "null"
_vala_path = path_format(_vala_path, false)
begin
template_format(_vala_path, path + _x.keys()[0], mod_src_path)
rescue Exception => err
puts colorize_output("[WARN] can't find proper vala for #{pwd + "/" + _x.keys()[0]}, may not compile", "yellow")
puts colorize_output("[ERROR] #{err.message}", "red")
end
end
end
end
}
end
opts = GetoptLong.new(
['--reload-vala', '-r' , GetoptLong::NO_ARGUMENT]
)
opts.each {|opt, arg|
case opt
when '--reload-vala'
puts colorize_output("[INFO] reloading vala files corresponding to templates", "blue")
reload_vala()
exit
end
}
_mod_src_path = build_opts["modified_src_path"] || "./modified_src"
mod_src_path = path_format(_mod_src_path, true)
if not Dir.exist?(mod_src_path)
puts colorize_output("[INFO] dir #{mod_src_path} not found, creating it", "magenta")
`mkdir -p #{mod_src_path}`
end
if build_opts.key?("vala_files")
_tmp = build_opts["vala_files"]
_tmp.each {|_x|
if not _x.is_a?(Hash)
vala_files.push(pwd + "/" + _x)
else if _x.values()[0].is_a?(String)
_path = _x.values()[0]
path = path_format(_path, true)
vala_files.push(path + _x.keys()[0])
else
puts colorize_output("[FATAL] Invalid path for file #{_x.to_s()}", "red")
end
end
}
end
if build_opts.key?("ui_files")
_tmp = build_opts["ui_files"]
_tmp.each {|_x|
if not _x.is_a?(Hash)
ui_files.push(pwd + "/" + _x)
#assume file name to be pwd/sample.ui.vala
begin
template_format(pwd + "/" + _x + ".vala", pwd + "/" + _x, mod_src_path)
rescue Exception => err
puts colorize_output("[WARN] can't find proper vala for #{pwd + "/" + _x}, may not compile", "yellow")
puts colorize_output("[ERROR] #{err.message}", "red")
end
else if _x.values()[0].key?("path")
_path = _x.values()[0]["path"]
path = path_format(_path, true)
_vala_path = _x.values()[0]["vala_path"]
if not _vala_path == "null"
_vala_path = path_format(_vala_path, false)
begin
template_format(_vala_path, path + _x.keys()[0], mod_src_path)
rescue Exception => err
puts colorize_output("[WARN] can't find proper vala for #{pwd + "/" + _x.keys()[0]}, may not compile", "yellow")
puts colorize_output("[ERROR] #{err.message}", "red")
end
end
# vala_path = path_format(_x, "vala_path")
ui_files.push(path + _x.keys()[0])
else
puts colorize_output("[FATAL] Invalid path for file #{_x.to_s()}", "red")
end
end
}
end
do_debug = build_opts["debug_build"] || false
do_release = build_opts["release_build"] || false
if do_debug
debug_exec = build_opts["debug_executable"] || "debug_run"
end
if do_release
release_exec = build_opts["release_executable"] || "run"
end
_c_path = build_opts["c_path"] || "./builds/"
c_path = path_format(_c_path, true)
if do_debug
_debug_path = build_opts["debug_path"] || "./builds/debug"
debug_path = path_format(_debug_path, true)
end
if do_release
_release_path = build_opts["release_path"] || "./builds/release"
release_path = path_format(_release_path, true)
end
_makefile = build_opts["makefile"] || "./Makefile"
makefile = path_format(_makefile, false)
#write ressource xml
_res_xml = build_opts["res_xml"] || "res.xml"
res_xml = path_format(_res_xml, false)
res_file = File.open(res_xml, "w")
res_file.write(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<gresources>
<gresource prefix=\"#{Dir.pwd}\">\n"
)
ui_files.each {|_ui_file|
_rindex = _ui_file.rindex("/")
_dir = _ui_file[0.._rindex]
_relative_path = rel_path_from_pwd(_dir)
if _relative_path == (".")
_relative_path = _relative_path[1.._relative_path.length - 1]
else
_relative_path = _relative_path + "/"
end
_file_name = _ui_file[_rindex + 1.._ui_file.length - 1]
res_file.write(
" <file compressed=\"true\" preprocess=\"xml-stripblanks\">#{_relative_path}#{_file_name}</file>\n"
)
}
res_file.write(
" </gresource>
</gresources>"
)
res_file.close()
#done
#write makefile
make_file = File.open(makefile, "w")
_pkg = dev_deps.reduce {|a, b| a + " --pkg " + b}
_mod_vala_files = []
vala_files.each {|_x|
_p = _x
if _x.end_with?(".ui.vala")
_p = mod_src_path + get_raw_filename(_x)
end
_mod_vala_files.push(_p)
}
#release block
if do_release
_deps_list = dev_deps.reduce {|a,b| a + " " + b}
_deps_params = "`pkg-config --cflags #{_deps_list} gmodule-export-2.0`"
_deps_params_final = "`pkg-config --libs #{_deps_list} gmodule-export-2.0`"
make_file.write(
"release: compile_vala compile_res
@[ -d \"#{release_path}\" ] || mkdir -p \"#{release_path}\"
@cd \"#{release_path}\" && $(MAKE) -f \"#{makefile}\" #{release_exec}\n\n"
)
_mod_vala_files.each {|_vala_file|
_vala_file_name = _vala_file[_vala_file.rindex("/")+1.._vala_file.rindex(".vala") - 1]
_vala_file_dir = _vala_file[0.._vala_file.rindex("/")]
_rel_path = rel_path_from_pwd(_vala_file_dir)
make_file.write(
"#{release_path}#{_vala_file_name}.o: #{c_path}#{_rel_path}/#{_vala_file_name}.c
gcc -o $@ -c $< -Wall #{_deps_params}\n\n"
)
object_files_release.push("#{release_path}#{_vala_file_name}.o")
}
_res_file_name = res_xml[res_xml.rindex("/")+1..res_xml.length - 1]
make_file.write(
"#{release_path}#{_res_file_name}.o: #{c_path}#{_res_file_name}.c
gcc -o $@ -c $< -Wall #{_deps_params}\n\n"
)
object_files_release.push("#{release_path}#{_res_file_name}.o")
_object_files_all = object_files_release.reduce { |a, b| a + " " + b}
#p _object_files_all
#final exec
make_file.write(
"#{release_exec}: #{_object_files_all}
gcc -o #{release_path}#{release_exec} $^ #{_deps_params_final}\n\n"
)
end
#do debug part, maybe i should invest in a function
if do_debug
_deps_list = dev_deps.reduce {|a,b| a + " " + b}
_deps_params = "`pkg-config --cflags #{_deps_list} gmodule-export-2.0`"
_deps_params_final = "`pkg-config --libs #{_deps_list} gmodule-export-2.0`"
make_file.write(
"debug: compile_vala compile_res
@[ -d \"#{debug_path}\" ] || mkdir -p \"#{debug_path}\"
@cd \"#{debug_path}\" && $(MAKE) -f \"#{makefile}\" #{debug_exec}\n\n"
)
_mod_vala_files.each {|_vala_file|
_vala_file_name = _vala_file[_vala_file.rindex("/")+1.._vala_file.rindex(".vala") - 1]
_vala_file_dir = _vala_file[0.._vala_file.rindex("/")]
_rel_path = rel_path_from_pwd(_vala_file_dir)
make_file.write(
"#{debug_path}#{_vala_file_name}.o: #{c_path}#{_rel_path}/#{_vala_file_name}.c
gcc -o $@ -g -c $< -Wall #{_deps_params}\n\n"
)
object_files_debug.push("#{debug_path}#{_vala_file_name}.o")
}
_res_file_name = res_xml[res_xml.rindex("/")+1..res_xml.length - 1]
make_file.write(
"#{debug_path}#{_res_file_name}.o: #{c_path}#{_res_file_name}.c
gcc -o $@ -g -c $< -Wall #{_deps_params}\n\n"
)
object_files_debug.push("#{debug_path}#{_res_file_name}.o")
_object_files_all = object_files_debug.reduce { |a, b| a + " " + b}
#p _object_files_all
#final exec
make_file.write(
"#{debug_exec}: #{_object_files_all}
gcc -o #{debug_path}#{debug_exec} $^ -g #{_deps_params_final}\n\n"
)
end
#do compile vala
_valas = _mod_vala_files.reduce {|a,b| a + " " + b}
make_file.write(
"compile_vala: reload_vala
@[ -d \"#{c_path}\"] || mkdir -p \"#{c_path}\"
valac --pkg #{_pkg} #{_valas} --target-glib=2.38 --gresources #{res_xml} -C -d #{c_path}\n\n"
)
#do reload vala
make_file.write(
"reload_vala:
@[ -d #{mod_src_path} ] || mkdir -p #{mod_src_path}
ruby #{__FILE__} -r\n\n"
)
#do compile res
_uis = ui_files.reduce {|a,b| a + " " + b}
_res_file_name = res_xml[res_xml.rindex("/")+1..res_xml.length - 1]
make_file.write(
"compile_res: #{res_xml} #{_uis}
@[ -d \"#{c_path}\"] || mkdir -p \"#{c_path}\"
glib-compile-resources #{res_xml} --target=#{c_path}#{_res_file_name}.c --sourcedir=$(srcdir) --c-name _ap --generate-source\n\n"
)
#do cleanup
make_file.write(
"clean:
@rm -rfv #{release_path}
@rm -rfv #{debug_path}
@rm -rfv #{mod_src_path}
@rm -rfv #{c_path}\n\n"
)
#do all
make_file.write(
"all: debug release"
)
make_file.close()
# p vala_files
# p ui_files
# p c_path
# p debug_path
# p release_path
# p makefile