forked from silverweed/lifish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_class.rb
executable file
·101 lines (85 loc) · 2.21 KB
/
create_class.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
#!/usr/bin/env ruby
require 'optparse'
def usage
"Usage: #{$0} <path/to/class>" +
"\n (class must not have an extension)"
end
options = {}
OptionParser.new do |opts|
opts.banner = usage
opts.on('-f', '--force', 'Force overwrite') do |f|
options[:force_overwrite] = f
end
opts.on('-d', '--derive-from=CLASS', 'Set parent class') do |c|
options[:parent] = c
end
opts.on('-h', '--help', 'Generate this help') do
puts opts
exit
end
end.parse!
if ARGV.length < 1
puts usage
exit 1
end
spl = ARGV[0].split '/'
clsname = spl[-1]
subdir = spl[-2]
path = spl[0..-2]
includes = ""
derived = ""
etc = ""
args = ""
constr = " "
func_impl = ""
constr_content = ""
case subdir
when 'entities'
includes = "\n#include \"Entity.hpp\"\n"
derived = ': public lif::Entity '
args = 'const sf::Vector2f& pos'
etc = "\n\n\tvoid update() override;"
constr = "\n\t: lif::Entity(pos)\n"
func_impl = "\n\nvoid #{clsname}::update() {\n\tlif::Entity::update();\n}"
when 'components'
includes = "\n#include \"Component.hpp\"\n"
derived = ': public lif::Component '
args = 'lif::Entity& owner'
constr = "\n\t: lif::Component(owner)\n"
constr_content = "\n\t_declComponent<#{clsname}>();\n"
end
if options[:parent]
derived = ": public lif::#{options[:parent]} "
constr = "\n\t: lif::#{options[:parent]}()\n"
includes = "\n#include \"#{options[:parent]}.hpp\"\n"
func_impl = "\n\nvoid #{clsname}::update() {\n\tlif::#{options[:parent]}::update();\n}"
end
TEMPLATE_HPP = "#pragma once
#{includes}
namespace lif {
class #{clsname} #{derived}{
public:
explicit #{clsname}(#{args});#{etc}
};
}"
TEMPLATE_CPP = "#include \"#{clsname}.hpp\"
using lif::#{clsname};
#{clsname}::#{clsname}(#{args})#{constr}{#{constr_content}}#{func_impl}"
hppfname = "#{ARGV[0]}.hpp"
cppfname = "#{ARGV[0]}.cpp"
if !options[:force_overwrite] and (File.exists? hppfname or File.exists? cppfname)
STDERR.puts "Error: file #{hppfname} or #{cppfname} already exists: use -f to force overwrite."
exit 2
end
begin
File.open(hppfname, 'w') do |f|
f.write TEMPLATE_HPP
end
STDERR.puts "Written file #{hppfname}"
File.open(cppfname, 'w') do |f|
f.write TEMPLATE_CPP
end
STDERR.puts "Written file #{cppfname}"
rescue ex
STDERR.puts "Errors writing files: #{ex}"
end