Class: GoonModelGen::Generator
- Inherits:
-
Object
- Object
- GoonModelGen::Generator
- Includes:
- Templates::DSL
- Defined in:
- lib/goon_model_gen/generator.rb
Constant Summary collapse
- DEFAULT_TEMPLATES_DIR =
File.('../templates', __FILE__)
- COLORS =
{ blue: "\e[34m", clear: "\e[0m", }
Constants included from Templates::DSL
Templates::DSL::PARTITION_PATTERNS
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#force ⇒ Object
false/true.
-
#gofmt_disabled ⇒ Object
false/true.
-
#overwrite_custom_file ⇒ Object
false/true.
-
#package_alias_map ⇒ Object
Returns the value of attribute package_alias_map.
-
#packages ⇒ Object
readonly
Returns the value of attribute packages.
-
#skip ⇒ Object
false/true.
-
#templates_dir ⇒ Object
readonly
Returns the value of attribute templates_dir.
-
#thor ⇒ Object
Returns the value of attribute thor.
-
#version_comment ⇒ Object
false/true.
Instance Method Summary collapse
- #execute(variables = {}) ⇒ Object
- #gofmt(content) ⇒ Object
- #header_comments ⇒ Object
-
#initialize(file, packages, templates_dir: DEFAULT_TEMPLATES_DIR, thor: nil) ⇒ Generator
constructor
A new instance of Generator.
- #load_config(cfg) ⇒ Object
- #run(variables = {}) ⇒ Object
Methods included from Templates::DSL
#dependencies, #import, #partition, #partitioned_imports, #user_editable, #user_editable?
Constructor Details
#initialize(file, packages, templates_dir: DEFAULT_TEMPLATES_DIR, thor: nil) ⇒ Generator
Returns a new instance of Generator.
24 25 26 27 28 29 |
# File 'lib/goon_model_gen/generator.rb', line 24 def initialize(file, packages, templates_dir: DEFAULT_TEMPLATES_DIR, thor: nil) @file = file @packages = packages @templates_dir = templates_dir @thor = thor end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
10 11 12 |
# File 'lib/goon_model_gen/generator.rb', line 10 def file @file end |
#force ⇒ Object
false/true
16 17 18 |
# File 'lib/goon_model_gen/generator.rb', line 16 def force @force end |
#gofmt_disabled ⇒ Object
false/true
14 15 16 |
# File 'lib/goon_model_gen/generator.rb', line 14 def gofmt_disabled @gofmt_disabled end |
#overwrite_custom_file ⇒ Object
false/true
17 18 19 |
# File 'lib/goon_model_gen/generator.rb', line 17 def overwrite_custom_file @overwrite_custom_file end |
#package_alias_map ⇒ Object
Returns the value of attribute package_alias_map.
18 19 20 |
# File 'lib/goon_model_gen/generator.rb', line 18 def package_alias_map @package_alias_map end |
#packages ⇒ Object (readonly)
Returns the value of attribute packages.
10 11 12 |
# File 'lib/goon_model_gen/generator.rb', line 10 def packages @packages end |
#skip ⇒ Object
false/true
16 17 18 |
# File 'lib/goon_model_gen/generator.rb', line 16 def skip @skip end |
#templates_dir ⇒ Object (readonly)
Returns the value of attribute templates_dir.
11 12 13 |
# File 'lib/goon_model_gen/generator.rb', line 11 def templates_dir @templates_dir end |
#thor ⇒ Object
Returns the value of attribute thor.
13 14 15 |
# File 'lib/goon_model_gen/generator.rb', line 13 def thor @thor end |
#version_comment ⇒ Object
false/true
15 16 17 |
# File 'lib/goon_model_gen/generator.rb', line 15 def version_comment @version_comment end |
Instance Method Details
#execute(variables = {}) ⇒ 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 |
# File 'lib/goon_model_gen/generator.rb', line 51 def execute(variables = {}) return nil if file.sentences.empty? variables.each do |key, val| define_singleton_method(key){ val } end texts = file.sentences.sort_by(&:template_path).map do |sentence| template_path = File.join(templates_dir, sentence.template_path) # local variables used in tempaltes type = sentence.type package = type.package type.memo.each do |key, val| define_singleton_method(key){ val } end erb = ERB.new(File.read(template_path), nil, "-") erb.filename = template_path erb.result(binding).strip end r = [ header_comments, "package %s" % file.package.name, partitioned_imports(except: [file.package.path]), texts.join("\n\n"), ].join("\n\n").strip << "\n" r = gofmt(r) unless gofmt_disabled r end |
#gofmt(content) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/goon_model_gen/generator.rb', line 100 def gofmt(content) # https://docs.ruby-lang.org/ja/2.5.0/class/IO.html#S_POPEN r = IO.popen(["gofmt"], "r+", err: :out) do |io| io.puts(content) io.close_write io.read end return r unless r.empty? raise "gofmt returned empty output:\n#{content}" end |
#header_comments ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/goon_model_gen/generator.rb', line 84 def header_comments r = [] if user_editable? r << "You can edit this file. goa_model_gen doesn't overwrite this file." else r << "DO NOT EDIT this file." end if version_comment r << "This code generated by goon_model_gen-#{GoonModelGen::VERSION}" end return r.map{|s| "// #{s}" }.join("\n") end |
#load_config(cfg) ⇒ Object
112 113 114 115 116 |
# File 'lib/goon_model_gen/generator.rb', line 112 def load_config(cfg) [:gofmt_disabled, :version_comment, :package_alias_map].each do |key| self.send("#{key}=", cfg.send(key)) end end |
#run(variables = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/goon_model_gen/generator.rb', line 36 def run(variables = {}) output_path = File.join(Golang.gopath, 'src', file.package.path, file.name) if file.custom_suffix && File.exist?(output_path) && !overwrite_custom_file $stderr.puts("%sKEEP%s %s" % [COLORS[:blue], COLORS[:clear], output_path]) return end content = execute(variables) return unless content = {skip: skip, force: force} thor.create_file(output_path, content, ) end |