lizarb 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +12 -13
  3. data/Rakefile +38 -0
  4. data/app/dev_box.rb +14 -12
  5. data/app/happy_box.rb +2 -2
  6. data/app/net_box.rb +4 -4
  7. data/app/web_box.rb +4 -4
  8. data/app.rb +6 -7
  9. data/{exe/Gemfile → app_global.gemfile.rb} +2 -0
  10. data/app_global.gemfile.rb.lock +19 -0
  11. data/app_global.rb +4 -0
  12. data/{app → app_new}/dev/benches/loops_bench.rb +3 -2
  13. data/{app/dev/generators/bench_generator_test.rb → app_new/dev/benches/loops_bench_test.rb} +2 -2
  14. data/app_new/dev/benches/objects_bench.rb +83 -0
  15. data/{app/dev/generators/record_generator_test.rb → app_new/dev/benches/objects_bench_test.rb} +2 -2
  16. data/app_new/dev/benches/sorted_bench.rb +54 -0
  17. data/app_new/dev/benches/sorted_bench_test.rb +12 -0
  18. data/app_new/dev/benches/variables_bench.rb +55 -0
  19. data/{app/dev/generators/command_generator_test.rb → app_new/dev/benches/variables_bench_test.rb} +2 -2
  20. data/{app → app_new}/dev/commands/calculator_command.rb +1 -1
  21. data/{app → app_new}/dev/commands/circle_command.rb +1 -1
  22. data/{app → app_new}/dev/commands/narrative_method_command.rb +5 -7
  23. data/{app → app_new}/dev/commands/quadratic_command.rb +1 -1
  24. data/app_new/dev_box.rb +14 -0
  25. data/app_new.rb +15 -0
  26. data/exe/lizarb +9 -1
  27. data/lib/app.rb +27 -95
  28. data/lib/dev_system/dev/controllers/bench/not_found_bench.rb +15 -8
  29. data/lib/dev_system/dev/controllers/bench.rb +0 -6
  30. data/lib/dev_system/dev/controllers/command/bench_command.rb +1 -2
  31. data/lib/dev_system/dev/controllers/command/generate_command.rb +3 -27
  32. data/lib/dev_system/dev/controllers/command/new_command.rb +9 -0
  33. data/lib/dev_system/dev/controllers/command/new_command_test.rb +7 -0
  34. data/lib/dev_system/dev/controllers/command/not_found_command.rb +29 -8
  35. data/lib/dev_system/dev/controllers/command/terminal_command.rb +2 -2
  36. data/lib/dev_system/dev/controllers/command/test_command.rb +6 -6
  37. data/lib/dev_system/dev/controllers/command.rb +9 -13
  38. data/lib/dev_system/dev/controllers/generator_dsl_main_part.rb +2 -4
  39. data/{app/dev/generators/bench_generator/bench.rb.erb → lib/dev_system/dev/controllers/generators/bench_generator/controller.rb.erb} +1 -2
  40. data/{app/dev/generators/bench_generator/bench_test.rb.erb → lib/dev_system/dev/controllers/generators/bench_generator/controller_test.rb.erb} +2 -2
  41. data/lib/dev_system/dev/controllers/generators/bench_generator.rb +12 -0
  42. data/lib/dev_system/dev/controllers/generators/bench_generator_test.rb +12 -0
  43. data/lib/dev_system/dev/controllers/generators/command_generator/controller.rb.erb +41 -0
  44. data/{app/dev/generators/command_generator/command_test.rb.erb → lib/dev_system/dev/controllers/generators/command_generator/controller_test.rb.erb} +2 -2
  45. data/lib/dev_system/dev/controllers/generators/command_generator.rb +19 -0
  46. data/lib/dev_system/dev/controllers/generators/command_generator_test.rb +12 -0
  47. data/lib/dev_system/dev/controllers/generators/controller_generator.rb +97 -0
  48. data/lib/dev_system/dev/controllers/generators/controller_generator_test.rb +12 -0
  49. data/lib/dev_system/dev/controllers/generators/generator_generator/template.rb.erb +1 -1
  50. data/lib/dev_system/dev/controllers/generators/new_generator.rb +117 -0
  51. data/lib/dev_system/dev/controllers/generators/new_generator_test.rb +12 -0
  52. data/lib/dev_system/dev/controllers/generators/not_found_generator.rb +20 -8
  53. data/{app/dev → lib/dev_system/dev/controllers}/generators/record_generator.rb +1 -1
  54. data/lib/dev_system/dev/controllers/generators/record_generator_test.rb +12 -0
  55. data/{app/dev → lib/dev_system/dev/controllers}/generators/request_generator.rb +2 -2
  56. data/lib/dev_system/dev/controllers/generators/request_generator_test.rb +12 -0
  57. data/lib/dev_system/dev/controllers/log.rb +3 -0
  58. data/{app/dev/generators/request_generator_test.rb → lib/dev_system/dev/controllers/log_test.rb} +2 -2
  59. data/lib/dev_system/dev/controllers/shell.rb +22 -0
  60. data/lib/dev_system/dev/controllers/shell_test.rb +25 -0
  61. data/lib/dev_system/dev/controllers/terminal/irb_terminal.rb +7 -1
  62. data/lib/dev_system/dev/controllers/terminal/pry_terminal.rb +1 -1
  63. data/lib/dev_system/dev/dev_box.rb +12 -18
  64. data/lib/dev_system/dev/panels/command_panel.rb +54 -11
  65. data/lib/dev_system/dev/panels/command_panel_test.rb +35 -10
  66. data/lib/dev_system/dev/panels/generator_panel.rb +70 -0
  67. data/lib/dev_system/dev/panels/generator_panel_test.rb +34 -0
  68. data/lib/happy_system/happy/happy_box.rb +2 -3
  69. data/lib/lerb.rb +95 -49
  70. data/lib/liza/base/box.rb +6 -1
  71. data/lib/liza/base/panel.rb +16 -18
  72. data/lib/liza/base/panel_test.rb +27 -1
  73. data/lib/liza/base_parts/controller_renderer_part.rb +1 -1
  74. data/lib/liza/ruby_tests/module_test.rb +2 -2
  75. data/lib/liza/test/test_test.rb +5 -1
  76. data/lib/liza/test_parts/test_assertions_part.rb +1 -1
  77. data/lib/liza/test_parts/test_dsl_part.rb +3 -3
  78. data/lib/liza/test_parts/test_tree_part.rb +4 -49
  79. data/lib/liza/unit.rb +73 -79
  80. data/lib/liza/unit_parts/unit_procedure_part_test.rb +9 -9
  81. data/lib/liza/unit_parts/unit_settings_part.rb +76 -0
  82. data/lib/liza/unit_parts/unit_settings_part_test.rb +3 -0
  83. data/lib/liza/unit_test.rb +11 -0
  84. data/lib/liza.rb +10 -1
  85. data/lib/lizarb/ruby/module.rb +1 -1
  86. data/lib/lizarb/version.rb +1 -1
  87. data/lib/lizarb.rb +247 -23
  88. data/lib/net_system/dev/net_command.rb +9 -9
  89. data/lib/net_system/net/controllers/client/redis_client.rb +1 -1
  90. data/lib/net_system/net/controllers/client/sqlite_client.rb +1 -1
  91. data/lib/net_system/net/net_box.rb +4 -6
  92. data/lib/web_system/dev/rack_command.rb +1 -8
  93. data/lib/web_system/dev/request_command.rb +6 -20
  94. data/lib/web_system/web/panels/request_panel.rb +0 -2
  95. data/lib/web_system/web/web_box.rb +4 -6
  96. data/lizarb.gemspec +1 -1
  97. metadata +46 -41
  98. data/.ruby-version +0 -1
  99. data/app/dev/benches/app_bench.rb +0 -5
  100. data/app/dev/benches/app_bench_test.rb +0 -7
  101. data/app/dev/benches/loops_bench_test.rb +0 -11
  102. data/app/dev/commands/app_command.rb +0 -5
  103. data/app/dev/commands/app_command_test.rb +0 -7
  104. data/app/dev/commands/new_command.rb +0 -9
  105. data/app/dev/commands/new_command_test.rb +0 -7
  106. data/app/dev/generators/bench_generator.rb +0 -18
  107. data/app/dev/generators/command_generator/command.rb.erb +0 -26
  108. data/app/dev/generators/command_generator.rb +0 -18
  109. data/exe/Gemfile.lock +0 -13
  110. data/lib/dev_system/dev/controllers/bench_dsl_main_part.rb +0 -58
  111. data/lib/dev_system/dev/controllers/command/echo_command.rb +0 -9
  112. data/lib/dev_system/dev/controllers/command/echo_command_test.rb +0 -12
  113. data/lib/dev_system/dev/controllers/command/version_command.rb +0 -7
  114. data/lib/dev_system/dev/controllers/command/version_command_test.rb +0 -12
  115. data/lib/dev_system/dev/controllers/generators/app_generator.rb +0 -146
  116. data/lib/dev_system/dev/controllers/generators/app_generator_test.rb +0 -12
  117. /data/{app → app_new}/dev/commands/calculator_command_test.rb +0 -0
  118. /data/{app → app_new}/dev/commands/circle_command_test.rb +0 -0
  119. /data/{app → app_new}/dev/commands/narrative_method_command_test.rb +0 -0
  120. /data/{app → app_new}/dev/commands/quadratic_command_test.rb +0 -0
data/lib/liza/unit.rb CHANGED
@@ -6,86 +6,28 @@ class Liza::Unit
6
6
  App.connect_part self, key, system
7
7
  end
8
8
 
9
- def self.const_missing name
10
- Liza.const name
11
- rescue Liza::ConstNotFound => e
12
- raise NameError, "uninitialized constant #{name}", caller[1..], cause: nil
13
- end
14
-
15
- part :unit_procedure
16
-
17
- # SETTINGS
18
-
19
- def self.settings
20
- @settings ||= {}
21
- end
22
-
23
- def self.get key
24
- return settings[key] if settings.has_key? key
9
+ # CONST MISSING
25
10
 
26
- found = nil
11
+ if Lizarb.ruby_supports_raise_cause?
27
12
 
28
- for klass in ancestors
29
- break unless klass.respond_to? :settings
30
-
31
- if klass.settings.has_key? key
32
- found = klass.settings[key]
33
-
34
- break
35
- end
13
+ def self.const_missing name
14
+ Liza.const name
15
+ rescue Liza::ConstNotFound
16
+ raise NameError, "uninitialized constant #{name}", caller[1..], cause: nil
36
17
  end
37
18
 
38
- found = settings[key] = found.dup if found.is_a? Enumerable
39
-
40
- found
41
- end
42
-
43
- def self.set key, value
44
- settings[key] = value
45
- value
46
- end
19
+ else
47
20
 
48
- def self.add list, key = nil, value
49
- if key
50
- fetch(list) { Hash.new }[key] = value
51
- else
52
- fetch(list) { Set.new } << value
21
+ def self.const_missing name
22
+ Liza.const name
23
+ rescue Liza::ConstNotFound
24
+ raise NameError, "uninitialized constant #{name}", caller[1..]
53
25
  end
54
- end
55
-
56
- def self.fetch key, &block
57
- x = get key
58
- x ||= set key, instance_eval(&block)
59
- x
60
- end
61
-
62
- def settings
63
- @settings ||= {}
64
- end
65
26
 
66
- def get key
67
- return settings[key] if settings.has_key? key
68
-
69
- self.class.get key
70
- end
71
-
72
- def set key, value
73
- settings[key] = value
74
27
  end
75
28
 
76
- def add list, key = nil, value
77
- if key
78
- fetch(list) { Hash.new }[key] = value
79
- else
80
- fetch(list) { Set.new } << value
81
- end
82
- end
83
-
84
- def fetch key, &block
85
- x = get key
86
- x ||= set key, eval(&block)
87
- x
88
- end
29
+ part :unit_procedure
30
+ part :unit_settings
89
31
 
90
32
  # LOG
91
33
 
@@ -100,22 +42,75 @@ class Liza::Unit
100
42
  set :log_level, :normal
101
43
  set :log_color, :white
102
44
 
103
- #
45
+ # NOTE: improve logs performance and readability
46
+
47
+ LOG_JUST = 60
48
+
49
+ def self.build_log_sidebar_for source, method_key, method_sep, panel_key: nil
50
+ source = (source.is_a? Class) ? source : source.class
51
+ source_color = source.log_color
52
+ source = source.to_s
53
+
54
+ s = source.bold.colorize(source_color)
55
+ s << "[:#{panel_key}]" if panel_key
56
+ s << "#{method_sep}#{method_key}"
57
+ s.ljust(LOG_JUST)
58
+ end
59
+
60
+ # NOTE: This code needs to be optimized.
61
+ def self._log_extract_method_name kaller
62
+ kaller.each do |s|
63
+ t = s.match(/`(.*)'/)[1]
64
+
65
+ next if t.include? " in <class:"
66
+ return t.split(" ").last if t.include? " in "
67
+
68
+ next if t == "log"
69
+ next if t == "each"
70
+ next if t == "map"
71
+ next if t == "with_index"
72
+ next if t == "instance_exec"
73
+ next if t.start_with? "_"
74
+ return t
75
+ end
76
+
77
+ raise "there's something wrong with kaller"
78
+ end
79
+
80
+ def _log_extract_method_name kaller
81
+ self.class._log_extract_method_name kaller
82
+ end
83
+
84
+ def self.log log_level = :normal, string, kaller: caller
85
+ raise "invalid log_level `#{log_level}`" unless LOG_LEVELS.keys.include? log_level
86
+ return unless log_level? log_level
104
87
 
105
- LOG_JUST = 40
88
+ method_key = _log_extract_method_name kaller
89
+ source = Liza::Unit.build_log_sidebar_for self, method_key, ":"
106
90
 
107
- def self.log log_level = :normal, string
91
+ DevBox[:log].call "#{source} #{string}"
92
+ end
93
+
94
+ def log log_level = :normal, string, kaller: caller
108
95
  raise "invalid log_level `#{log_level}`" unless LOG_LEVELS.keys.include? log_level
109
96
  return unless log_level? log_level
110
97
 
111
- source = (self.is_a? Class) ? self : self.class
112
- source = source.to_s.ljust(LOG_JUST).bold.colorize(source.log_color)
98
+ method_key = _log_extract_method_name kaller
113
99
 
114
- string = "#{source} #{string}"
100
+ case self
101
+ when Liza::Panel
102
+ source = Liza::Unit.build_log_sidebar_for box, method_key, ".", panel_key: @key
103
+ when Liza::UnitTest
104
+ source = Liza::Unit.build_log_sidebar_for self, " ", " "
105
+ else
106
+ source = Liza::Unit.build_log_sidebar_for self, method_key, "#"
107
+ end
115
108
 
116
- DevBox[:log].call string
109
+ DevBox[:log].call "#{source} #{string}"
117
110
  end
118
111
 
112
+ #
113
+
119
114
  def self.log_level
120
115
  get(:log_level) || :normal
121
116
  end
@@ -130,7 +125,6 @@ class Liza::Unit
130
125
  end
131
126
 
132
127
  def self.log?(log_level = :normal)= log_level? log_level
133
- def log(...)= self.class.log(...)
134
128
  def log_level(...)= self.class.log_level(...)
135
129
  def log?(...)= self.class.log?(...)
136
130
  def log_level?(...)= self.class.log_level?(...)
@@ -7,7 +7,6 @@ class Liza::UnitProcedurePartTest < Liza::UnitTest
7
7
  procedure "creates a new scope" do
8
8
  @a = 1
9
9
  b = 2
10
- c = 3
11
10
  assert true
12
11
 
13
12
  proceed if true
@@ -21,7 +20,6 @@ class Liza::UnitProcedurePartTest < Liza::UnitTest
21
20
  procedure "asserts old scope is not accessible" do
22
21
  assert instance_variables.include? :@a
23
22
  assert local_variables.include? :b
24
- refute local_variables.include? :c
25
23
 
26
24
  assert @a == 1
27
25
  assert b == 2
@@ -32,13 +30,15 @@ class Liza::UnitProcedurePartTest < Liza::UnitTest
32
30
 
33
31
  assert x == 100
34
32
 
35
- y =
36
- procedure "asserts proceed calls can be lazy" do
37
- proceed { "slow operation"; 200 } if true
38
- raise "did not get here"
39
- end
40
-
41
- assert y == 200
33
+ y = procedure "asserts proceed calls can be lazy" do
34
+ proceed do
35
+ puts "slow operation"
36
+ 200
37
+ end if true
38
+ raise "did not get here"
39
+ end
40
+
41
+ assert y == 200
42
42
  end
43
43
 
44
44
  test :procedure_rescue_and_ensure do
@@ -0,0 +1,76 @@
1
+ class Liza::UnitSettingsPart < Liza::Part
2
+
3
+ insertion do
4
+ def self.settings
5
+ @settings ||= {}
6
+ end
7
+
8
+ def self.get key
9
+ return settings[key] if settings.has_key? key
10
+
11
+ found = nil
12
+
13
+ for klass in ancestors
14
+ break unless klass.respond_to? :settings
15
+
16
+ if klass.settings.has_key? key
17
+ found = klass.settings[key]
18
+
19
+ break
20
+ end
21
+ end
22
+
23
+ found = settings[key] = found.dup if found.is_a? Enumerable
24
+
25
+ found
26
+ end
27
+
28
+ def self.set key, value
29
+ settings[key] = value
30
+ value
31
+ end
32
+
33
+ def self.add list, key = nil, value
34
+ if key
35
+ fetch(list) { Hash.new }[key] = value
36
+ else
37
+ fetch(list) { Set.new } << value
38
+ end
39
+ end
40
+
41
+ def self.fetch key, &block
42
+ x = get key
43
+ x ||= set key, instance_eval(&block)
44
+ x
45
+ end
46
+
47
+ def settings
48
+ @settings ||= {}
49
+ end
50
+
51
+ def get key
52
+ return settings[key] if settings.has_key? key
53
+
54
+ self.class.get key
55
+ end
56
+
57
+ def set key, value
58
+ settings[key] = value
59
+ end
60
+
61
+ def add list, key = nil, value
62
+ if key
63
+ fetch(list) { Hash.new }[key] = value
64
+ else
65
+ fetch(list) { Set.new } << value
66
+ end
67
+ end
68
+
69
+ def fetch key, &block
70
+ x = get key
71
+ x ||= set key, eval(&block)
72
+ x
73
+ end
74
+ end
75
+
76
+ end
@@ -0,0 +1,3 @@
1
+ class Liza::UnitSettingsPartTest < Liza::UnitTest
2
+
3
+ end
@@ -74,4 +74,15 @@ class Liza::UnitTest < Liza::Test
74
74
  assert class_b.get(:hash) == {a: 10, b: 20, c: 30, d: 40}
75
75
  end
76
76
 
77
+ test :build_log_sidebar_for do
78
+ assert_equality "DevSystem::Command:call ", build_log_sidebar_for(DevSystem::Command, "call", ":", panel_key: nil)
79
+ assert_equality "DevSystem::TestCommand#call ", build_log_sidebar_for(DevSystem::TestCommand, "call", "#", panel_key: nil)
80
+ assert_equality "DevBox[:command].call ", build_log_sidebar_for(DevBox, "call", ".", panel_key: :command)
81
+ end
82
+
83
+ def build_log_sidebar_for source, method_key, method_sep, panel_key: nil
84
+ s = subject_class.build_log_sidebar_for source, method_key, method_sep, panel_key: panel_key
85
+ s.uncolorize
86
+ end
87
+
77
88
  end
data/lib/liza.rb CHANGED
@@ -14,6 +14,10 @@ module Liza
14
14
 
15
15
  #
16
16
 
17
+ def [] name
18
+ const name
19
+ end
20
+
17
21
  # Checks Object, each system, then Liza for Liza::Unit classes
18
22
  def const name
19
23
  name = name.to_s.camelize.to_sym
@@ -29,7 +33,12 @@ module Liza
29
33
 
30
34
  nil
31
35
  rescue NameError
32
- raise ConstNotFound, name
36
+ log "Liza const #{name} not found!"
37
+ if Lizarb.ruby_supports_raise_cause?
38
+ raise ConstNotFound, name, cause: nil
39
+ else
40
+ raise ConstNotFound, name, []
41
+ end
33
42
  end
34
43
 
35
44
  # Checks each system, then Liza for Liza::Unit classes
@@ -4,7 +4,7 @@ class Module
4
4
 
5
5
  # /path/to/liza.rb
6
6
  def source_location
7
- Object.const_source_location name
7
+ Array Object.const_source_location name
8
8
  end
9
9
 
10
10
  # /path/to/liza
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lizarb
4
- VERSION = "1.0.3"
4
+ VERSION = "1.0.4"
5
5
  end
data/lib/lizarb.rb CHANGED
@@ -3,13 +3,16 @@
3
3
  require "colorize"
4
4
  require "json"
5
5
  require "pathname"
6
- require "zeitwerk"
6
+ require "fileutils"
7
7
  require "lerb"
8
8
 
9
9
  require_relative "lizarb/version"
10
10
 
11
+ $APP ||= "app"
12
+
11
13
  module Lizarb
12
14
  class Error < StandardError; end
15
+ class ModeNotFound < Error; end
13
16
 
14
17
  #
15
18
 
@@ -19,9 +22,12 @@ module Lizarb
19
22
 
20
23
  IS_APP_DIR = File.file? "#{CUR_DIR}/app.rb"
21
24
  IS_LIZ_DIR = File.file? "#{CUR_DIR}/lib/lizarb.rb"
25
+ IS_GEM_DIR = File.file? "#{CUR_DIR}/lizarb.gemspec"
22
26
 
23
27
  APP_DIR = IS_APP_DIR ? CUR_DIR : GEM_DIR
24
28
 
29
+ $APP = "app_global" if not IS_APP_DIR
30
+
25
31
  #
26
32
 
27
33
  module_function
@@ -30,48 +36,266 @@ module Lizarb
30
36
  puts s.bold
31
37
  end
32
38
 
39
+ def self.logv s
40
+ log s if $VERBOSE
41
+ end
42
+
43
+ # Returns Lizarb::VERSION as a Gem::Version
44
+ def version
45
+ @version ||= Gem::Version.new VERSION
46
+ end
47
+
48
+ # Returns RUBY_VERSION as a Gem::Version
49
+ def ruby_version
50
+ @ruby_version ||= Gem::Version.new RUBY_VERSION
51
+ end
52
+
53
+ def ruby_supports_raise_cause?
54
+ RUBY_ENGINE != "jruby"
55
+ end
56
+
57
+ def self.load_all
58
+ Zeitwerk::Loader.eager_load_all
59
+ end
60
+
33
61
  # called from exe/lizarb
34
- def call
62
+ def setup
63
+ lookup_and_load_core_ext
64
+ lookup_and_set_gemfile
65
+ end
66
+
67
+ # called from exe/lizarb
68
+ def app &block
35
69
  require "app"
70
+ if block_given?
71
+ App.class_exec(&block)
72
+ else
73
+ lookup_and_require_app
74
+ end
75
+ end
36
76
 
37
- setup_core_ext
38
- setup_gemfile
77
+ # called from exe/lizarb
78
+ def call
79
+ lookup_and_set_mode
80
+ lookup_and_require_dependencies
81
+ lookup_and_load_settings
82
+ require_liza_and_bundle_systems
83
+ end
39
84
 
40
- require "#{APP_DIR}/app"
85
+ # called from exe/lizarb
86
+ def exit verbose: $VERBOSE
87
+ exit_messages if verbose
88
+ super 0
89
+ end
41
90
 
42
- versions = {ruby: RUBY_VERSION, bundler: Bundler::VERSION, zeitwerk: Zeitwerk::VERSION, lizarb: VERSION}
91
+ def exit_messages
92
+ versions = {
93
+ ruby: RUBY_VERSION,
94
+ bundler: Bundler::VERSION,
95
+ zeitwerk: Zeitwerk::VERSION,
96
+ lizarb: VERSION,
97
+ app: $APP,
98
+ mode: $MODE
99
+ }
43
100
  bugs = SPEC.metadata["bug_tracker_uri"]
44
101
  puts versions.to_s.green
45
102
  puts "Report bugs at #{bugs}"
103
+ puts "Fork us on Github at #{bugs}/fork"
46
104
  end
47
105
 
48
- # called from "#{APP_DIR}/app"
49
- def bundle
50
- require "bundler/setup"
51
- Bundler.require :default
106
+ # setup phase
107
+
108
+ def lookup_and_load_core_ext
109
+ files =
110
+ if IS_GEM_DIR
111
+ Dir["#{CUR_DIR}/lib/lizarb/ruby/*.rb"]
112
+ else
113
+ Dir["#{GEM_DIR}/lib/lizarb/ruby/*.rb"] + Dir["#{CUR_DIR}/lib/lizarb/ruby/*.rb"]
114
+ end
115
+
116
+ files.each do |file_name|
117
+ log "#{self} loading #{file_name}" if $VERBOSE
118
+ load file_name
119
+ end
120
+ end
121
+
122
+ def lookup_and_set_gemfile
123
+ gemfile = nil
52
124
 
53
- bundle_liza
125
+ finder = \
126
+ proc do |file_name|
127
+ log "#{self}.#{__method__} #{file_name}" if $VERBOSE
128
+ if File.file? file_name
129
+ file_name
130
+ else
131
+ false
132
+ end
133
+ end
134
+
135
+ gemfile ||= finder.call "#{CUR_DIR}/#{$APP}.gemfile.rb"
136
+ gemfile ||= finder.call "#{GEM_DIR}/#{$APP}.gemfile.rb" unless IS_GEM_DIR
137
+ gemfile ||= finder.call "#{CUR_DIR}/Gemfile"
138
+ gemfile ||= finder.call "#{GEM_DIR}/app_global.gemfile.rb"
54
139
 
55
- check_mode!
140
+ log "#{self} setting BUNDLE_GEMFILE to #{gemfile}" if $VERBOSE
141
+ ENV["BUNDLE_GEMFILE"] = gemfile
56
142
  end
57
143
 
58
- # setup
144
+ # app phase
59
145
 
60
- def setup_core_ext
61
- pattern =
62
- IS_LIZ_DIR ? "lib/lizarb/ruby/*.rb"
63
- : "#{GEM_DIR}/lib/lizarb/ruby/*.rb"
146
+ def lookup_and_require_app
147
+ finder = \
148
+ proc do |lib_name, file_name|
149
+ log "#{self} checking if #{file_name} exists" if $VERBOSE
150
+ if File.file? "#{file_name}"
151
+ require lib_name
152
+ true
153
+ else
154
+ false
155
+ end
156
+ end
64
157
 
65
- Dir[pattern].each &method(:load)
158
+ return if finder.call "#{CUR_DIR}/#{$APP}", "#{CUR_DIR}/#{$APP}.rb"
159
+ return if finder.call "#{GEM_DIR}/#{$APP}", "#{GEM_DIR}/#{$APP}.rb"
160
+
161
+ raise Error, "Could not find #{$APP}.rb in #{CUR_DIR} or #{GEM_DIR}"
66
162
  end
67
163
 
68
- def setup_gemfile
69
- ENV["BUNDLE_GEMFILE"] =
70
- IS_APP_DIR ? "#{CUR_DIR}/Gemfile"
71
- : "#{GEM_DIR}/exe/Gemfile"
164
+ # call phase
165
+
166
+ def lookup_and_set_mode
167
+ raise ModeNotFound, "App #{$APP} has no modes" if App.modes.empty?
168
+
169
+ mode = ENV["MODE"]
170
+ mode ||= App.modes.first
171
+ mode = mode.to_sym
172
+
173
+ raise ModeNotFound, "MODE `#{mode}` is not included in #{App.modes}" unless App.modes.include? mode
174
+
175
+ log "#{self}.#{__method__} #{mode.inspect}" if $VERBOSE
176
+ $MODE = mode
177
+ end
178
+
179
+ def lookup_and_require_dependencies
180
+ require "bundler/setup"
181
+ Bundler.require :default, *App.systems.keys
182
+ end
183
+
184
+ def lookup_and_load_settings
185
+ files = ["#{$APP}.#{$MODE}.env", "#{$APP}.env"]
186
+ require "dotenv"
187
+ Dotenv.load(*files)
188
+ end
189
+
190
+ def require_liza_and_bundle_systems
191
+ require "zeitwerk"
192
+ require "liza"
193
+
194
+ # App.loaders[0] first loads Liza, then each System class
195
+ App.loaders << loader = Zeitwerk::Loader.new
196
+ loader.tag = Liza.to_s
197
+
198
+ # collapse Liza paths
199
+
200
+ # ORDER MATTERS: IGNORE, COLLAPSE, PUSH
201
+ loader.collapse "#{Liza.source_location_radical}/**/*"
202
+ loader.push_dir "#{Liza.source_location_radical}", namespace: Liza
203
+
204
+ # loader setup
205
+
206
+ loader.enable_reloading
207
+ loader.setup
208
+
209
+ # load each System class
210
+
211
+ App.systems.keys.each do |k|
212
+ key = "#{k}_system"
213
+
214
+ App.require_system key
215
+ klass = Object.const_get key.camelize
216
+
217
+ App.systems[k] = klass
218
+ end
219
+
220
+ # App.loaders[1] first loads each System, then the App
221
+ App.loaders << loader = Zeitwerk::Loader.new
222
+
223
+ # collapse each System paths
224
+
225
+ App.systems.each do |k, klass|
226
+ # ORDER MATTERS: IGNORE, COLLAPSE, PUSH
227
+ loader.collapse "#{klass.source_location_radical}/**/*"
228
+ loader.push_dir "#{klass.source_location_radical}", namespace: klass
229
+ end
230
+
231
+ # cherrypick App paths
232
+
233
+ app_dir = "#{APP_DIR}/#{$APP}"
234
+ logv "Lizarb app loader #{app_dir}".on_cyan
235
+ list = Dir["#{app_dir}/*"].to_set
236
+ logv "Lizarb app loader lists #{list.count} entries to review".on_cyan
237
+
238
+ if list.empty?
239
+ logv "no #{app_dir} found".red
240
+ else
241
+ logv "Lizarb app loader found #{app_dir}\t\tCollapsing #{app_dir}/*".on_cyan
242
+
243
+ to_collapse = []
244
+
245
+ App.systems.each do |k, klass|
246
+ box_dir = "#{app_dir}/#{k}"
247
+ box_file = "#{box_dir}_box.rb"
248
+
249
+ if !list.include? box_file
250
+ logv "Lizarb app loader missd #{box_file}".on_light_black
251
+ else
252
+ logv "Lizarb app loader found #{box_file}\t\tto_collapse!".on_cyan
253
+ to_collapse << box_file
254
+
255
+ if !list.include? box_dir
256
+ logv "Lizarb app loader missd #{box_dir}".on_light_black
257
+ else
258
+ logv "Lizarb app loader found #{box_dir}\t\tto_collapse!".on_cyan
259
+ to_collapse << box_dir
260
+ end
261
+ end
262
+ end
263
+
264
+ # ORDER MATTERS: IGNORE, COLLAPSE, PUSH
265
+ to_ignore = list - to_collapse
266
+ to_ignore.each do |file|
267
+ logv "Lizarb app loader missd #{file}\t\tSkipping this one".on_light_black
268
+ loader.ignore file
269
+ end
270
+
271
+ to_collapse.each do |path|
272
+ logv "Lizarb app loader collapsing #{path}".on_cyan
273
+ if path.end_with? ".rb"
274
+ loader.collapse path
275
+ else
276
+ loader.collapse "#{path}/**/*"
277
+ end
278
+ end
279
+ loader.collapse "#{app_dir}/*"
280
+
281
+ loader.push_dir app_dir, namespace: Object
282
+ end
283
+
284
+ # loader setup
285
+
286
+ loader.enable_reloading
287
+ loader.setup
288
+
289
+ # App connects to systems
290
+
291
+ App.systems.each do |k, klass|
292
+ App.connect_system k, klass
293
+ end
294
+
295
+ App.systems.freeze
72
296
  end
73
297
 
74
- # threads
298
+ # thread management
75
299
 
76
300
  def thread_object_id
77
301
  Thread.current.object_id