Class: GameMachine::Protobuf::Generate
- Inherits:
-
Object
- Object
- GameMachine::Protobuf::Generate
- Defined in:
- lib/game_machine/protobuf/generate.rb
Instance Attribute Summary collapse
-
#app_root ⇒ Object
readonly
Returns the value of attribute app_root.
Class Method Summary collapse
Instance Method Summary collapse
- #classname(name) ⇒ Object
- #config_path ⇒ Object
- #erb_template ⇒ Object
- #generate ⇒ Object
- #get_type(field) ⇒ Object
-
#initialize(app_root) ⇒ Generate
constructor
A new instance of Generate.
- #java_root ⇒ Object
- #java_src ⇒ Object
- #model_src ⇒ Object
- #model_template ⇒ Object
- #simple_value?(field) ⇒ Boolean
- #sql_column_name(klass, field) ⇒ Object
- #sql_field(klass, field, dbtype, force_null = false) ⇒ Object
- #varname(name) ⇒ Object
- #write_components(proto, persistent_messages) ⇒ Object
Constructor Details
#initialize(app_root) ⇒ Generate
Returns a new instance of Generate.
17 18 19 |
# File 'lib/game_machine/protobuf/generate.rb', line 17 def initialize(app_root) @app_root = app_root end |
Instance Attribute Details
#app_root ⇒ Object (readonly)
Returns the value of attribute app_root.
16 17 18 |
# File 'lib/game_machine/protobuf/generate.rb', line 16 def app_root @app_root end |
Class Method Details
.compile(path) ⇒ Object
25 26 27 28 29 |
# File 'lib/game_machine/protobuf/generate.rb', line 25 def self.compile(path) loader = CachingProtoLoader.new file = java.io.File.new(path) ProtoUtil.parseProto(file) end |
Instance Method Details
#classname(name) ⇒ Object
164 165 166 |
# File 'lib/game_machine/protobuf/generate.rb', line 164 def classname(name) name.slice(0,1).capitalize + name.slice(1..-1) end |
#config_path ⇒ Object
47 48 49 |
# File 'lib/game_machine/protobuf/generate.rb', line 47 def config_path File.join(app_root,'config') end |
#erb_template ⇒ Object
31 32 33 |
# File 'lib/game_machine/protobuf/generate.rb', line 31 def erb_template File.join(java_root,'component.erb') end |
#generate ⇒ Object
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 |
# File 'lib/game_machine/protobuf/generate.rb', line 168 def generate FileUtils.rm Dir.glob(File.join(java_src,'*.java')) game_protofile = File.join(config_path,'game_messages.proto') protofile = File.join(config_path,'messages.proto') if File.exists?(game_protofile) = File.read(game_protofile) = .gsub('local_persistent_message','message') = .gsub('local_message','message') = .gsub('persistent_message','message') gm = Protobuf::GameMessages.new(game_protofile) = gm. entity_fields = gm.create_entity_fields game_entity_fields = entity_fields.join("\n") else = '' game_entity_fields = '' = [] end = File.read(protofile) = .sub('//GAME_MESSAGES',) = .sub('//GAME_ENTITY_MESSAGES',game_entity_fields) = File.join(config_path,'combined_messages.proto') File.open(,'w') {|f| f.write()} # test message defintion validity #tmp_dir = File.join('/tmp/proto_test') #FileUtils.rm_rf(tmp_dir) #FileUtils.mkdir_p(tmp_dir) #unless system("protoc #{combined_messages_protofile} --java_out=#{tmp_dir}") # return false #end # This just stops generating code when it hits an error, but does not # throw an exception proto = self.class.compile() write_components(proto,) #FileUtils.rm(combined_messages_protofile) end |
#get_type(field) ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/game_machine/protobuf/generate.rb', line 150 def get_type(field) if field.getJavaType.to_s == 'int' return 'Integer' elsif ['boolean','double','float','long'].include?(field.getJavaType.to_s) return field.getJavaType.to_s.clone.capitalize else return field.getJavaType end end |
#java_root ⇒ Object
21 22 23 |
# File 'lib/game_machine/protobuf/generate.rb', line 21 def java_root ENV['JAVA_ROOT'] end |
#java_src ⇒ Object
43 44 45 |
# File 'lib/game_machine/protobuf/generate.rb', line 43 def java_src File.join(java_root,'src','main','java','GameMachine', 'Messages') end |
#model_src ⇒ Object
39 40 41 |
# File 'lib/game_machine/protobuf/generate.rb', line 39 def model_src File.join(java_root,'src','main','java','com', 'game_machine','orm','models') end |
#model_template ⇒ Object
35 36 37 |
# File 'lib/game_machine/protobuf/generate.rb', line 35 def model_template File.join(java_root,'model.erb') end |
#simple_value?(field) ⇒ Boolean
142 143 144 145 146 147 148 |
# File 'lib/game_machine/protobuf/generate.rb', line 142 def simple_value?(field) if ['boolean','double','float','long','int','String'].include?(field.getJavaType.to_s) true else false end end |
#sql_column_name(klass, field) ⇒ Object
92 93 94 |
# File 'lib/game_machine/protobuf/generate.rb', line 92 def sql_column_name(klass,field) "#{klass.underscore}_#{field.name.underscore}" end |
#sql_field(klass, field, dbtype, force_null = false) ⇒ Object
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 |
# File 'lib/game_machine/protobuf/generate.rb', line 96 def sql_field(klass,field,dbtype,force_null=false) if dbtype == 'mysql' txt = case field.getJavaType.to_s when 'boolean' "`#{sql_column_name(klass,field)}` tinyint(4)" when 'double' "`#{sql_column_name(klass,field)}` double" when 'float' "`#{sql_column_name(klass,field)}` float" when 'long' "`#{sql_column_name(klass,field)}` int(11)" when 'int' "`#{sql_column_name(klass,field)}` int(11)" when 'String' "`#{sql_column_name(klass,field)}` varchar(128)" end elsif dbtype == 'postgres' txt = case field.getJavaType.to_s when 'boolean' "#{sql_column_name(klass,field)} integer" when 'double' "#{sql_column_name(klass,field)} double precision" when 'float' "#{sql_column_name(klass,field)} double precision" when 'long' "#{sql_column_name(klass,field)} integer" when 'int' "#{sql_column_name(klass,field)} integer" when 'String' "#{sql_column_name(klass,field)} character varying(128)" end end return nil if txt.nil? if force_null return "#{txt} DEFAULT NULL," end if field.is_required txt = "#{txt} NOT NULL," else txt = "#{txt} DEFAULT NULL," end end |
#varname(name) ⇒ Object
160 161 162 |
# File 'lib/game_machine/protobuf/generate.rb', line 160 def varname(name) name.slice(0,1).downcase + name.slice(1..-1) end |
#write_components(proto, persistent_messages) ⇒ Object
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 |
# File 'lib/game_machine/protobuf/generate.rb', line 51 def write_components(proto,) = proto.getMessages.reject {|| .getName == 'Components'} FileUtils.mkdir_p(java_src) FileUtils.mkdir_p(model_src) = proto.getMessages.map {|m| m.get_name} = proto.getMessages.each_with_object({}) {|v,res| res[v.getName] = v} proto.getMessages.each do || = [] .get_fields.each do |field| if .include?(field.getJavaType.to_s) && !field.is_repeated << field.get_java_type.to_s end end unless .empty? #puts "#{message.getName} #{message_fields.inspect}" end klass = .getName persistent = .include?(klass) #puts "Message: #{message.getName}" out = ERB.new(File.read(erb_template),0,'>').result(binding) out = out.gsub(/^(\s*\r\n){2,}/,"\r\n") src_file = File.join(java_src,"#{.getName}.java") File.open(src_file,'w') {|f| f.write out} if persistent out = ERB.new(File.read(model_template),0,'<>').result(binding) out = out.gsub(/^(\s*\r\n){2,}/,"\r\n") out = out.gsub(/^(\s*\n){2,}/,"\n") src_file = File.join(model_src,"#{.getName}.java") File.open(src_file,'w') {|f| f.write out} end #message.getFields.each do |field| # puts field.default_value_set # puts field.getJavaType # puts field.toString #end end end |