Class: DevSystem::ShellCommand

Inherits:
SimpleCommand show all
Defined in:
lib/dev_system/subsystems/shell/commands/shell_command.rb

Instance Attribute Summary

Attributes inherited from Liza::Controller

#menv

Instance Method Summary collapse

Methods inherited from SimpleCommand

#ask?, #default_args, #default_booleans, #default_strings, #given_args, #given_booleans, #given_strings, #input_args, #input_booleans, #input_strings, #set_arg, #set_boolean, #set_default_arg, #set_default_array, #set_default_boolean, #set_default_string, #set_input_arg, #set_input_array, #set_input_boolean, #set_input_string, #set_string, #simple_arg, #simple_arg_ask, #simple_arg_ask_snakecase, #simple_args, #simple_args_from_2, #simple_array, #simple_boolean, #simple_boolean_yes, #simple_booleans, #simple_color, #simple_controller_placement, #simple_string, #simple_strings

Methods inherited from BaseCommand

#action_method_name, #action_name, #around, #call, call, #env, get_command_signatures, #not_found, typo, #typo

Methods inherited from Command

get_command_signatures

Methods inherited from Liza::Controller

#`, `, attr_accessor, attr_reader, attr_writer, #attrs, box, #box, call, color, division, division!, division?, inherited, menv_accessor, menv_reader, menv_writer, on_connected, panel, #panel, plural, require, requirements, sh, #sh, singular, subsystem, subsystem!, subsystem?, subsystem_token, token

Methods inherited from Liza::Unit

_erbs_for, #add, add, cl, #cl, class_methods_defined, const_added, const_missing, constants_defined, define_error, descendants_select, division, erbs_available, erbs_defined, erbs_for, errors, #fetch, fetch, get, #get, instance_methods_defined, log, #log, log?, #log?, #log_array, log_array, log_hash, #log_hash, #log_level, log_level, #log_level?, log_level?, log_levels, #log_levels, #log_render_convert, #log_render_format, #log_render_in, #log_render_out, method_added, methods_defined, namespace, part, raise_error, #raise_error, reload!, #reload!, #render, #render!, #render_stack, renderable_formats_for, renderable_names, section, sections, #set, set, #settings, settings, singleton_method_added, sleep, #sleep, stick, #stick, sticks, #sticks, subclasses_select, subunits, system, #system, system?, test_class, time_diff, #time_diff

Instance Method Details

#afterObject



19
20
21
22
# File 'lib/dev_system/subsystems/shell/commands/shell_command.rb', line 19

def after
  super
  log "#{ time_diff @t }s | done"
end

#beforeObject



11
12
13
14
15
16
17
# File 'lib/dev_system/subsystems/shell/commands/shell_command.rb', line 11

def before
  super
  @t = Time.now
  log "simple_args     #{ simple_args }"
  log "simple_booleans #{ simple_booleans }"
  log "simple_strings  #{ simple_strings }"
end

#call_convertObject

liza shell:convert FORMAT FILENAMES



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
# File 'lib/dev_system/subsystems/shell/commands/shell_command.rb', line 59

def call_convert
  valid_converters = Shell.panel.converters.keys
  color = system.color

  log :normal, (stick color, "valid converters are #{valid_converters.map { stick :black, color, :b, _1.to_s }.map(&:to_s).join ', '}")

  set_input_arg 0 do |default|
    title = "Which converter are we going to use?"
    answer = InputShell.pick_one title, valid_converters
  end

  format = simple_arg(0).to_sym
  log :normal, (stick color, "converter is #{format}")

  set_input_arg 1 do |default|
    title = "Which files are we going to convert?"
    local_files = Dir["*.#{format}"]
    choices = local_files.map { [_1, _1] }.to_h
    selected = [choices.keys.first]
    answers = InputShell.multi_select title, choices, selected: selected
    answers.join(",")
  end

  fnames = simple_arg(1).to_s.split(",")
  log :normal, (stick color, "selected files are #{fnames.join ', '}")
  fnames.each do |fname|
    content = TextShell.read fname
    log :higher, (stick color, "IN:")
    puts content if log? :higher

    format_to = DevBox[:shell].converters[format][:to]
    fname = "#{fname}.#{format_to}"
    convert_env = {format: format, convert_in: content}
    DevBox.convert convert_env

    content = convert_env[:convert_out]
    log :higher, (stick color, "OUT:")
    puts content if log? :higher

    TextShell.write fname, content
  end
end

#call_defaultObject

liza shell



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
# File 'lib/dev_system/subsystems/shell/commands/shell_command.rb', line 27

def call_default
  puts
  puts "Lizarb                #{ Lizarb.source_location[0] }"
  puts "Liza                  #{ Liza.source_location[0] }"
  puts "App                   #{ App.source_location[0] }"
  puts
  puts "App.type              #{ App.type.inspect }"
  puts "App.name              #{ App.name.inspect }"
  puts "App.root              #{ App.root }/"
  puts "App.file              #{ App.file }"
  puts "App.directory         #{ App.directory }/"
  puts "App.systems_directory #{ App.systems_directory }/"
  puts

  largest_system_name = AppShell.consts[:systems].keys.map(&:to_s).map(&:size).max
  AppShell.consts[:systems].each do |system_name, tree_system|
    system = tree_system["system"][0]
    length = largest_system_name + 15

    puts "#{
      "#{system.token == :dev ? "App.system" : "          "}   #{system.token.inspect}"
        .ljust(length)
        .sub(system.token.inspect, (stick system.token.inspect, system.color).to_s)
    } #{
      system.source_location[0]
    }"
  end

  puts
end

#call_evalObject

liza shell:eval



103
104
105
106
107
108
109
110
# File 'lib/dev_system/subsystems/shell/commands/shell_command.rb', line 103

def call_eval
  s = args.join(' ')
  log :low, (stick system.color, "evaluating:")
  log :lower, s.inspect
  ret = eval s
  log :low, (stick system.color, "returned:")
  log :lower, ret.inspect
end

#call_formatObject

liza shell:format FORMAT FILENAME



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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dev_system/subsystems/shell/commands/shell_command.rb', line 113

def call_format
  valid_formats = Shell.panel.formatters.keys
  color = system.color

  log :normal, (stick color, "valid formatters are #{valid_formats.map { stick :black, color, :b, _1.to_s }.map(&:to_s).join ', '}")

  set_input_arg 0 do |default|
    title = "Which formatter are we going to use?"
    choices = valid_formats
    InputShell.pick_one title, choices
  end
  
  format = simple_arg(0).to_sym
  log :normal, (stick color, "formatter is #{format}")

  set_input_arg 1 do |default|
    title = "Which files are we going to format?"
    local_files = Dir["*.#{format}"]
    choices = local_files.map { [_1, _1] }.to_h
    selected = [choices.keys.first]
    answers = InputShell.multi_select title, choices, selected: selected
    answers.join(",")
  end

  fnames = simple_arg(1).to_s.split(",")
  log :normal, (stick color, "selected files are #{fnames.join ', '}")
  fnames.each do |fname|
    content = TextShell.read fname
    log :higher, (stick color, "IN:")
    puts content if log? :higher

    format_to = DevBox[:shell].formatters[format][:to]
    fname = "#{fname}.#{format_to}"
    format_env = {format: format, format_in: content}
    DevBox.format format_env

    content = format_env[:format_out]
    log :higher, (stick color, "OUT:")
    puts content if log? :higher

    TextShell.write fname, content
  end
end

#call_locObject

liza shell:loc



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/dev_system/subsystems/shell/commands/shell_command.rb', line 187

def call_loc
  set_default_array :domains, AppShell.get_writable_domains.keys
  
  set_input_array :domains do |default|
    domains = AppShell.get_writable_domains
    title = "Which domains are we going to inspect?"
    InputShell.pick_domains domains, default, title
  end

  app_shell = AppShell.new

  domains = simple_array(:domains)
  log "domains = #{domains}"
  app_shell.filter_by_domains domains

  app_shell.filter_by_any_name_including simple_args if simple_args.any?

  total = {loc: 0, c: 0, cm: 0, im: 0, views: 0}

  app_shell.get_domains.each do |domain|
    next if domain.empty?
    puts
    puts typo.h1 domain.name.to_s, domain.color
    puts

    domain_total = {loc: 0, c: 0, cm: 0, im: 0, views: 0}

    domain.layers.each do |layer|
      next if layer.objects.empty?

      m = "h#{layer.level}"
      puts typo.send m, layer.name.to_s, layer.color unless layer.level == 1

      puts stick layer.color, layer.path
      puts
    
      layer_total = {loc: 0, c: 0, cm: 0, im: 0, views: 0}
      layer.objects.each do |object|
        object_type = object.class == Module ? "module" : "class"

        loc = CoderayGemShell.loc_for(object)

        sections = object.sections.to_h rescue {}
        # sections_count = sections.count
        consts = sections.map { _2[:constants].count }.sum
        cm = sections.map { _2[:class_methods].count }.sum
        im = sections.map { _2[:instance_methods].count }.sum

        views = object.erbs_defined.count rescue 0

        layer_total[:loc] += loc
        layer_total[:c] += consts
        layer_total[:cm] += cm
        layer_total[:im] += im
        layer_total[:views] += views

        domain_total[:loc] += loc
        domain_total[:c] += consts
        domain_total[:cm] += cm
        domain_total[:im] += im
        domain_total[:views] += views

        total[:loc] += loc
        total[:c] += consts
        total[:cm] += cm
        total[:im] += im
        total[:views] += views

        content = String.new
        content << "{ "
        content << ":loc => #{loc.to_s.rjust_blanks 4}, "


        # content << ":sections => #{sections_count}, "
        content << ":c => #{consts.to_s.rjust_blanks 2}, "
        content << ":cm => #{cm.to_s.rjust_blanks 2}, "
        content << ":im => #{im.to_s.rjust_blanks 2}, "
        content << ":views => #{views.to_s.rjust_blanks 2} "

        content << "}"

        content = stick layer.color, content

        sidebar = "#{object_type} #{(typo.color_class object)}"
        count = object.to_s.length + object_type.length + 2
        size = Log.panel.sidebar_size - count
        sidebar << " " * size if size > 0

        log content, sidebar: sidebar
      end
      puts

      sidebar = "small subtotal"
      size = Log.panel.sidebar_size - sidebar.length - 1
      sidebar << " " * size if size > 0

      content = "{ "
      content << ":loc => #{layer_total[:loc].to_s.rjust_blanks 4}, "
      content << ":c => #{layer_total[:c].to_s.rjust_blanks 2}, "
      content << ":cm => #{layer_total[:cm].to_s.rjust_blanks 2}, "
      content << ":im => #{layer_total[:im].to_s.rjust_blanks 2}, "
      content << ":views => #{layer_total[:views].to_s.rjust_blanks 2} "
      content << "}"
      
      log content, sidebar: sidebar

      puts
    end

    sidebar = "SUBTOTAL"
    size = Log.panel.sidebar_size - sidebar.length - 1
    sidebar << " " * size if size > 0

    content = "{ "
    content << ":loc => #{domain_total[:loc].to_s.rjust_blanks 4}, "
    content << ":c => #{domain_total[:c].to_s.rjust_blanks 2}, "
    content << ":cm => #{domain_total[:cm].to_s.rjust_blanks 2}, "
    content << ":im => #{domain_total[:im].to_s.rjust_blanks 2}, "
    content << ":views => #{domain_total[:views].to_s.rjust_blanks 2} "
    content << "}"
    content = stick :b, :lightest_white, :darkest_black, content

    log content, sidebar: sidebar
  end
  puts


  sidebar = "TOTAL"
  size = Log.panel.sidebar_size - sidebar.length - 1
  sidebar << " " * size if size > 0

  content = "{ "
  content << ":loc => #{total[:loc].to_s.rjust_blanks 4}, "
  content << ":c => #{total[:c].to_s.rjust_blanks 2}, "
  content << ":cm => #{total[:cm].to_s.rjust_blanks 2}, "
  content << ":im => #{total[:im].to_s.rjust_blanks 2}, "
  content << ":views => #{total[:views].to_s.rjust_blanks 2} "
  content << "}"
  content = stick :b, :black, :white, content

  puts
  log content, sidebar: sidebar
  puts
  puts
  puts
  puts stick :b, :black, :white, "loc:   lines of code"
  puts stick :b, :black, :white, "c:     constants"
  puts stick :b, :black, :white, "cm:    class methods"
  puts stick :b, :black, :white, "im:    instance methods"
  puts stick :b, :black, :white, "views: views"
  puts
end

#call_pathsObject

liza shell:paths



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/dev_system/subsystems/shell/commands/shell_command.rb', line 158

def call_paths
  puts

  set_input_array :paths do
    title = "Which paths are we going to show?"
    choices = %w[load_paths loaded_features].map { ["$#{_1.upcase}", _1] }.to_h
    answers = InputShell.multi_select title, choices, selected: :all
  end

  paths = simple_array(:paths)

  if paths.include? "load_paths"
    puts "$LOAD_PATH"
    $LOAD_PATH.each do |path|
      puts path
    end
    puts
  end

  if paths.include? "loaded_features"
    puts "$LOADED_FEATURES"
    $LOADED_FEATURES.each do |path|
      puts path
    end
    puts
  end
end