Class: DevSystem::SimpleGenerator

Inherits:
BaseGenerator show all
Defined in:
lib/dev_system/sub/generator/generators/simple_generator.rb

Defined Under Namespace

Classes: UnitHelper

Instance Attribute Summary

Attributes inherited from BaseGenerator

#env

Instance Method Summary collapse

Methods inherited from BaseGenerator

#args, #call, call, #command, get_generator_signatures

Methods inherited from Generator

get_generator_signatures

Methods inherited from Liza::Controller

color, inherited, on_connected

Methods inherited from Liza::Unit

const_missing, division, part, system, #system, test_class

Instance Method Details

#add_change(change) ⇒ Object



78
79
80
81
82
83
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 78

def add_change change
  log :lower, "#{change.class}"
  @last_change = change
  changes << change
  self
end

#changesObject

changes



70
71
72
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 70

def changes
  @changes ||= []
end

#copy_examples(controller) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 144

def copy_examples controller
  puts
  log controller.to_s
  singular = controller.singular
  plural = controller.plural
  sys = controller.system.token
  
  [
    Lizarb::GEM_DIR,
    Lizarb::APP_DIR,
  ].uniq.each do |dir|
    FileShell.directory? "#{dir}/examples/#{singular}" or next
    copy_files "#{dir}/examples/#{singular}/app/#{sys}/#{plural}", "#{App.folder}/#{sys}/#{plural}"
  end
end

#copy_file(source, target) ⇒ Object



179
180
181
182
183
184
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 179

def copy_file source, target
  path = App.root.join target
  file = TextFileShell.new path
  file.new_lines = TextShell.read_lines source
  add_change file
end

#copy_files(from_folder, to_folder) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 162

def copy_files from_folder, to_folder
  from_pattern = "#{from_folder}/**/*"
  log "from_pattern = #{from_pattern}"

  from_files = Dir[from_pattern]
  log "from_files"
  log_array from_files
  puts

  from_files.each do |source|
    next if File.directory? source

    target = source.sub(from_folder, to_folder)
    copy_file source, target
  end
end

#create_controller(name, controller, place, path, ancestor: controller) {|unit, test| ... } ⇒ Object

create_controller

Yields:

  • (unit, test)


205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 205

def create_controller(name, controller, place, path, ancestor: controller, &block)
  unit, test = UnitHelper.new, UnitHelper.new

  @class_name = "#{name.camelize}#{controller.last_namespace}"
  @class_name = "#{place.split("/").first.camelize}System::#{@class_name}" unless place == "app"

  unit_classes = [@class_name, ancestor.to_s]
  test_classes = unit_classes.map { "#{_1}Test" }

  unit_path = App.root.join(path).join("#{name}_#{controller.division.singular}.rb")
  test_path = App.root.join(path).join("#{name}_#{controller.division.singular}_test.rb")

  # decorate

  yield unit, test

  # create

  create_unit unit, unit_classes, unit_path
  create_unit test, test_classes, test_path

  log "done"
end

#create_file(name, template, format) ⇒ Object

create_file



87
88
89
90
91
92
93
94
95
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 87

def create_file name, template, format
  path = App.root.join name
  file = TextFileShell.new path

  new_lines = render! template, format: format
  file.new_lines = new_lines.strip.split("\n").map { "#{_1}\n" }

  add_change file
end

#create_unit(unit, class_names, path, template = :unit) ⇒ Object

create_unit



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 99

def create_unit unit, class_names, path, template = :unit
  unit.sections.each do |section|
    @current_section = section
    section[:content] = render! section[:name], format: :rb
  end
  unit.views.each do |view|
    @current_view = view
    view[:content] = render! view[:name], format: view[:format]
  end
  @sections = unit.sections
  @views = unit.views
  @class_names = class_names

  file = TextFileShell.new path
  file.new_lines = render! template, format: :rb
  file.new_lines = file.new_lines.split("\n").map { "#{_1}\n" }
  add_change file
end

#informObject



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
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 5

def inform
  log :highest, "informing #{changes.count} changes"

  changes.each do |change|
    puts_line

    action = "updating"
    action = "creating" if change.old_lines.empty?
    # action = "deleting" if change.new_lines.empty? # not implemented

    diff = {
      "+": (change.new_lines - change.old_lines).count,
      "-": (change.old_lines - change.new_lines).count,
    }

    bit = diff.map { "#{_1}#{_2}" }.join(" ")
    relative = Pathname(change.path).relative_path_from(App.root)
    string = "#{action.ljust 8} | #{"#{bit}".rjust 8} lines | #{relative}"
    log :highest, string

    if log_level? :high
      puts relative
      LineDiffShell.log_diff(change.old_lines, change.new_lines) if diff.values.sum.positive?
    end
  end 
end

#last_changeObject



74
75
76
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 74

def last_change
  @last_change
end

#name!Object



188
189
190
191
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 188

def name!
  @name = command.simple_arg_ask_snakecase 0, "Name your new #{@controller_class.last_namespace}:"
  log "@name = #{@name.inspect}"
end

#place!Object



195
196
197
198
199
200
201
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 195

def place!
  places = ControllerShell.places_for(@controller_class)
  @place = places.keys[0] if places.count == 1
  @place ||= command.simple_controller_placement :place, places
  @path = places[@place]
  log "@place, @path = #{@place.inspect}, #{@path.inspect}"
end

#puts_lineObject

helper methods



231
232
233
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 231

def puts_line
  puts "-" * 120
end

#saveObject



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
# File 'lib/dev_system/sub/generator/generators/simple_generator.rb', line 34

def save
  puts_line
  diff = {
    "+": changes.map { _1.new_lines.count }.sum,
    "-": changes.map { _1.old_lines.count }.sum,
  }
  log "saving #{changes.count} files changed: #{diff[:"+"]} insertions(+), #{diff[:"-"]} deletions(-)"

  if env[:args].include? "+confirm"
    answers = changes
  else
    choices = changes.map { |i| [i.relative_path.to_s, i] }.to_h
    answers = box.pick_many "Approve all changes?", choices
  end

  #

  puts_line
  diff = {
    "+": answers.map { _1.new_lines.count }.sum,
    "-": answers.map { _1.old_lines.count }.sum,
  }
  log "saving #{answers.count} files changed: #{diff[:"+"]} insertions(+), #{diff[:"-"]} deletions(-)"

  answers.each do |change|
    if change.old_lines == change.new_lines
      log "skipping #{change.path}"
    else
      log "writing #{change.path}"
      TextShell.write change.path, change.new_lines.join(""), log_level: :lower
    end
  end
end