Improved
This commit is contained in:
parent
56a85b6557
commit
b99af17046
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/ruby
|
#!/usr/bin/ruby
|
||||||
|
require 'getoptlong'
|
||||||
|
|
||||||
def replaceAll(line, subst)
|
def replace_all(line, subst)
|
||||||
subst.each do |sub,rep|
|
subst.each do |sub,rep|
|
||||||
line = line.gsub('{' + sub + '}', rep)
|
line = line.gsub('{' + sub + '}', rep)
|
||||||
end
|
end
|
||||||
|
@ -8,40 +9,108 @@ def replaceAll(line, subst)
|
||||||
return line
|
return line
|
||||||
end
|
end
|
||||||
|
|
||||||
def degenerateCaps(text)
|
def split_parts(text)
|
||||||
text = text.gsub(/[A-Z]+[a-z]+/) do |s|
|
parts = []
|
||||||
s.downcase + '_'
|
text = text.gsub(/[A-Z]+[a-z]*/) do |s|
|
||||||
|
parts << s.downcase
|
||||||
end
|
end
|
||||||
|
|
||||||
return text[0..-2]
|
return parts
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def gen_underscore_lcase(parts)
|
||||||
|
ret = ""
|
||||||
|
|
||||||
|
parts.each do |part|
|
||||||
|
ret += part.downcase + '_'
|
||||||
|
end
|
||||||
|
|
||||||
|
return ret[0..-2]
|
||||||
|
end
|
||||||
|
|
||||||
|
def gen_from_template(template, nameparts, parentparts, subst, out, ext)
|
||||||
|
begin
|
||||||
|
tc = IO.readlines(template)
|
||||||
|
rescue StandardError => boom
|
||||||
|
print "Template file not found #{template}!\n"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if out then
|
||||||
|
f = File.open(out + ext, 'w')
|
||||||
|
else
|
||||||
|
f = $stdout
|
||||||
|
end
|
||||||
|
|
||||||
|
tc.each do |line|
|
||||||
|
f.write(replace_all(line, subst))
|
||||||
|
end
|
||||||
|
|
||||||
|
if f != $stdout then
|
||||||
|
f.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
opts = GetoptLong.new(
|
||||||
|
["--body", "-b", GetoptLong::NO_ARGUMENT],
|
||||||
|
["--header", "-h", GetoptLong::NO_ARGUMENT],
|
||||||
|
["--parent", "-p", GetoptLong::REQUIRED_ARGUMENT],
|
||||||
|
["--body-template", "-c", GetoptLong::REQUIRED_ARGUMENT],
|
||||||
|
["--header-template", "-t", GetoptLong::REQUIRED_ARGUMENT],
|
||||||
|
["--out", "-o", GetoptLong::REQUIRED_ARGUMENT]
|
||||||
|
)
|
||||||
|
|
||||||
|
options = {}
|
||||||
|
opts.each do |arg,value|
|
||||||
|
options[arg] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
parent = (options["--parent"] or "GObject")
|
||||||
|
body_template = (options["--body-template"] or "template.c")
|
||||||
|
header_template = (options["--header-template"] or "template.h")
|
||||||
|
out = (options["--out"] or nil)
|
||||||
|
|
||||||
subst = {}
|
subst = {}
|
||||||
name = ARGV[0]
|
name = ARGV[0]
|
||||||
|
|
||||||
if (name == nil) then
|
if (name == nil) then
|
||||||
print "Class name (capitalized): "
|
print "Class name (camel case, including namespace): "
|
||||||
name = gets[0..-2]
|
name = gets.chomp
|
||||||
end
|
end
|
||||||
|
|
||||||
subst['template_'] = degenerateCaps(name)
|
nameparts = split_parts(name)
|
||||||
subst['template-'] = subst['template_'].sub('_', '-')
|
|
||||||
|
subst['namespace'] = nameparts[0].downcase
|
||||||
|
subst['NAMESPACE'] = nameparts[0].upcase
|
||||||
|
subst['Namespace'] = nameparts[0].capitalize
|
||||||
|
|
||||||
|
subst['template_'] = gen_underscore_lcase(nameparts[1..-1])
|
||||||
|
subst['template-'] = subst['template_'].gsub('_', '-')
|
||||||
subst['TEMPLATE'] = subst['template_'].upcase
|
subst['TEMPLATE'] = subst['template_'].upcase
|
||||||
subst['Template'] = name
|
subst['Template'] = name[nameparts[0].length..-1]
|
||||||
|
|
||||||
tc = IO.readlines("template.c")
|
parentparts = split_parts(parent)
|
||||||
th = IO.readlines("template.h")
|
|
||||||
|
|
||||||
f = File.open('gm-' + subst['template-'] + '.c', 'w')
|
subst['parent_namespace'] = parentparts[0].downcase
|
||||||
tc.each do |line|
|
subst['PARENT_NAMESPACE'] = parentparts[0].upcase
|
||||||
f.write(replaceAll(line, subst))
|
subst['Parent_Namespace'] = parentparts[0].capitalize
|
||||||
|
|
||||||
|
subst['parent_'] = gen_underscore_lcase(parentparts[1..-1])
|
||||||
|
subst['parent-'] = subst['parent_'].gsub('_', '-')
|
||||||
|
subst['PARENT'] = subst['parent_'].upcase
|
||||||
|
subst['Parent'] = parent[parentparts[0].length..-1]
|
||||||
|
|
||||||
|
if !options["--body"] and !options["--header"] then
|
||||||
|
gen_from_template(body_template, nameparts, parentparts, subst, out, ".c")
|
||||||
|
gen_from_template(header_template, nameparts, parentparts, subst, out, '.h')
|
||||||
|
else
|
||||||
|
if options['--body'] then
|
||||||
|
gen_from_template(body_template, nameparts, parentparts, subst, out, '.c')
|
||||||
|
end
|
||||||
|
|
||||||
|
if options['--header'] then
|
||||||
|
gen_from_template(header_template, nameparts, parentparts, subst, out, '.h')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
f.close
|
|
||||||
|
|
||||||
f = File.open('gm-' + subst['template-'] + '.h', 'w')
|
|
||||||
th.each do |line|
|
|
||||||
f.write(replaceAll(line, subst))
|
|
||||||
end
|
|
||||||
f.close
|
|
||||||
|
|
||||||
print "Done ...\n"
|
print "Done ...\n"
|
||||||
|
|
Reference in New Issue