Class: Divine::CodeGenerator
- Inherits:
-
Object
- Object
- Divine::CodeGenerator
- Defined in:
- lib/divine/code_generators/code_generator.rb
Overview
Support basic methods that generate source code file(s) for target language
Instance Method Summary collapse
-
#generate(target, opts) ⇒ Object
generate source code file(s) * Args : -
tagret
-> target language -opts
-> Dictionary that contains generation params [file, parent_class, target_dir, …]. -
#writeFile(path, content) ⇒ Object
Create file * Args : -
path
-> the path in which the file will be written -content
-> the content to be written.
Instance Method Details
#generate(target, opts) ⇒ Object
generate source code file(s)
-
Args :
-
tagret
-> target language -
opts
-> Dictionary that contains generation params [file, parent_class, target_dir, …]
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/divine/code_generators/code_generator.rb', line 185 def generate(target, opts) gen = $language_generators[target.to_sym] raise "Unknown target language: #{target}" unless gen puts "Generating code for #{target}" src = gen.generate_code($all_structs, opts) target_dir = (opts[:target_dir] || ".") + "/" if opts[:package] path = target_dir + opts[:package].gsub(/\./, "/") FileUtils.mkdir_p(path) unless File.exists?(path) for cls in src file_name = path+"/"+cls[:file] writeFile(file_name, cls[:src]) end elsif opts[:file] FileUtils.mkdir_p(target_dir) unless File.exists?(target_dir) path = target_dir + opts[:file] writeFile(path, src[0][:src]) else puts src end end |
#writeFile(path, content) ⇒ Object
Create file
-
Args :
-
path
-> the path in which the file will be written -
content
-> the content to be written
213 214 215 216 217 218 |
# File 'lib/divine/code_generators/code_generator.rb', line 213 def writeFile(path, content) File.open(path, 'w+') do |f| puts "... writing #{path}" f.write(content) end end |