aro 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Gemfile.lock +3 -2
  4. data/README.md +1 -1
  5. data/aro.gemspec +1 -0
  6. data/bin/aos +2 -2
  7. data/bin/aro +4 -4
  8. data/checksums/aro-0.2.1.gem.sha512 +1 -0
  9. data/checksums/aro-0.2.2.gem.sha512 +1 -0
  10. data/listen.rb +1 -1
  11. data/locale/en.amg.yml +44 -0
  12. data/locale/en.aos.yml +13 -8
  13. data/locale/en.data.yml +30 -0
  14. data/locale/en.dom.yml +10 -8
  15. data/locale/en.usage.yml +22 -22
  16. data/locale/en.yml +16 -11
  17. data/sys/aos/amg.rb +184 -0
  18. data/sys/aos/aos.rb +203 -89
  19. data/sys/aos/data.rb +117 -0
  20. data/{db → sys/aos/db}/migrate/1765148774_create_yous.rb +1 -0
  21. data/sys/aos/db/migrate/1765762724_create_ilibs.rb +22 -0
  22. data/sys/aos/db/migrate/1765907766_create_ilogs.rb +18 -0
  23. data/sys/aos/db.rb +44 -25
  24. data/sys/aos/models/ilib.rb +46 -0
  25. data/sys/aos/models/ilog.rb +27 -0
  26. data/sys/aos/models/you.rb +50 -0
  27. data/sys/aos/vws/base.rb +120 -0
  28. data/sys/aos/{views → vws}/dom.rb +6 -10
  29. data/sys/aos/vws/games/abpps.rb +19 -0
  30. data/sys/aos/{views → vws}/games/game.rb +26 -25
  31. data/sys/aos/vws/games/hbpps.rb +19 -0
  32. data/sys/aos/vws/games/shpps.rb +19 -0
  33. data/sys/aos/vws/games/vipps.rb +19 -0
  34. data/sys/aos/{views → vws}/games.rb +3 -3
  35. data/sys/aos/vws/know/body.rb +19 -0
  36. data/sys/aos/vws/know/mind.rb +19 -0
  37. data/sys/aos/vws/know/spirit.rb +19 -0
  38. data/sys/aos/{views → vws}/know.rb +3 -3
  39. data/sys/aos/vws/root/amg.rb +21 -0
  40. data/sys/aos/vws/root/config.rb +21 -0
  41. data/sys/aos/vws/root/data.rb +21 -0
  42. data/sys/aos/{views/setup.rb → vws/root.rb} +4 -4
  43. data/sys/aos/vws/welcome/waite.rb +31 -0
  44. data/sys/aos/{views → vws}/welcome/winner.rb +3 -3
  45. data/sys/aos/vws/welcome.rb +30 -0
  46. data/{db/migrate/1763374647_create_decks.rb → sys/aro/db/migrate/1763374647_create_tecks.rb} +5 -5
  47. data/sys/aro/db/migrate/1763432541_create_tlogs.rb +20 -0
  48. data/sys/aro/db.rb +16 -15
  49. data/sys/aro/mancy.rb +4 -3
  50. data/sys/{models/deck.rb → aro/models/teck.rb} +56 -62
  51. data/sys/aro/models/tlog.rb +20 -0
  52. data/sys/aro/v.rb +1 -1
  53. data/sys/cli/constants.rb +1 -1
  54. data/sys/cli/dom.rb +1 -1
  55. data/sys/cli/{deck.rb → teck.rb} +20 -15
  56. data/sys/cli.rb +12 -12
  57. data/sys/dom/config.rb +718 -0
  58. data/sys/dom/d.rb +36 -18
  59. data/sys/dom/dom.rb +24 -14
  60. data/sys/reiquire.rb +8 -8
  61. data/sys/shr/ilib/crs/CMakeLists.txt +12 -0
  62. data/sys/shr/ilib/crs/compile.sh +5 -0
  63. data/sys/shr/ilib/crs/crs.c +24 -0
  64. data/sys/shr/ilib/crs/crs.o +0 -0
  65. data/sys/shr/ilib/crs/libcrs.so +0 -0
  66. data/sys/shr/prompt.rb +1 -5
  67. data/sys/shr/t.rb +14 -8
  68. data/sys/shr/version.rb +1 -1
  69. metadata +63 -26
  70. data/db/migrate/1763432541_create_logs.rb +0 -20
  71. data/locale/en.cngelog.yml +0 -5
  72. data/sys/aos/views/base.rb +0 -166
  73. data/sys/aos/views/games/abpps.rb +0 -21
  74. data/sys/aos/views/games/hbpps.rb +0 -21
  75. data/sys/aos/views/games/shpps.rb +0 -21
  76. data/sys/aos/views/games/vipps.rb +0 -21
  77. data/sys/aos/views/know/library.rb +0 -19
  78. data/sys/aos/views/know/temple.rb +0 -19
  79. data/sys/aos/views/setup/settings.rb +0 -37
  80. data/sys/aos/views/welcome/waite.rb +0 -19
  81. data/sys/aos/views/welcome.rb +0 -19
  82. data/sys/aos/you.rb +0 -19
  83. data/sys/cli/config.rb +0 -669
  84. data/sys/models/log.rb +0 -20
data/sys/dom/config.rb ADDED
@@ -0,0 +1,718 @@
1
+ =begin
2
+
3
+ config.rb
4
+
5
+ dom config interface.
6
+
7
+ by i2097i
8
+
9
+ =end
10
+
11
+ module Aro
12
+
13
+ # cli entrypoint
14
+ def self.config
15
+ Aro::Config.instance.load
16
+ Aro::Config.process_config_command(ARGV)
17
+ end
18
+
19
+ class Config
20
+ include Singleton
21
+
22
+ attr_accessor :config_path, :display_lines
23
+
24
+ ARO_IVA_PREFIX = :ARO_IVA_
25
+ ARO_OVA_PREFIX = :ARO_OVA_
26
+
27
+ CONFIG_FILE = :".config"
28
+
29
+ DATE_FORMAT = "%Y:%m:%d:%H:%M:%S"
30
+
31
+ # possible envs
32
+ #
33
+ # example usage:
34
+ # Aro::Config::ENVS[:PRODUCTION]
35
+ ENVS = {
36
+ DEVELOPMENT: :development,
37
+ PRODUCTION: :production,
38
+ TEST: :test,
39
+ }
40
+
41
+ # possible formats
42
+ #
43
+ # example usage:
44
+ # Aro::Config::FORMATS[:TEXT]
45
+ FORMATS = {
46
+ TEXT: :text,
47
+ JSON: :json,
48
+ }
49
+
50
+ # possible interfaces
51
+ #
52
+ # example usage:
53
+ # Aro::Config::INTERFACES[:TERMINAL]
54
+ INTERFACES = {
55
+ TERMINAL: :terminal,
56
+ LANIMRET: :lanimret,
57
+ }
58
+
59
+ # possible dimensions
60
+ #
61
+ # example usage:
62
+ # Aro::Config::DMS[:DEV_TAROT]
63
+ DMS = {
64
+ DEV_TAROT: :dev_tarot,
65
+ RUBY_FACOT: :ruby_facot,
66
+ }
67
+
68
+ # ovar and ivar access
69
+ #
70
+ # example usage:
71
+ # Aro::Config::DEF_ACCESS[:READ]
72
+ DEF_ACCESS = {
73
+ READ: :read,
74
+ WRITE: :write
75
+ }
76
+
77
+ BOOLS = {
78
+ FALSE: false,
79
+ TRUE: true,
80
+ }
81
+
82
+ TYPES = {
83
+ BOOL: :bool,
84
+ INT: :int,
85
+ STRING: :string,
86
+ VALUES: :values
87
+ }
88
+
89
+ # types used in definition
90
+ #
91
+ # example usage:
92
+ # Aro::Config::DEF_TYPES[:INT][:validator].call(unvalid, Aro::Config::DEF[:DIMENSION])
93
+ DEF_TYPES = {
94
+ BOOL: {
95
+ name: Aro::Config::TYPES[:BOOL],
96
+ description: I18n.t("cli.config.type.bool_description"),
97
+ converter: Proc.new{|v|
98
+ if [Aro::Config::BOOLS[:TRUE].to_s, Aro::Mancy::S].include?(v)
99
+ Aro::Config::BOOLS[:TRUE]
100
+ else
101
+ Aro::Config::BOOLS[:FALSE]
102
+ end
103
+ },
104
+ validator: Proc.new{|unvalid, k, v|
105
+ Aro::Config.def_valid?(k, v) &&
106
+ Aro::Config.bool_valid?(unvalid)
107
+ }
108
+ },
109
+ INT: {
110
+ name: Aro::Config::TYPES[:INT],
111
+ description: I18n.t("cli.config.type.int_description"),
112
+ converter: Proc.new{|v| v.to_i},
113
+ validator: Proc.new{|unvalid, k, v|
114
+ Aro::V.say("validating #{k} (#{Aro::Config::DEF_TYPES[:INT][:name]})")
115
+ Aro::V.say("unvalid = #{unvalid}")
116
+ Aro::V.say("[min, max] = [#{v[:min]}, #{v[:max]}]")
117
+ int_valid = Aro::Config.def_valid?(k, v) &&
118
+ Aro::Config.int_valid?(unvalid) &&
119
+ unvalid.to_i >= v[:min] &&
120
+ unvalid.to_i <= v[:max]
121
+ Aro::V.say("unvalid(#{unvalid}) is#{int_valid ? " " : :" not ".to_s}valid.")
122
+ int_valid
123
+ }
124
+ },
125
+ STRING: {
126
+ name: Aro::Config::TYPES[:STRING],
127
+ description: I18n.t("cli.config.type.string_description"),
128
+ converter: Proc.new{|v| v.to_s},
129
+ validator: Proc.new{|unvalid, k, v|
130
+ Aro::Config.def_valid?(k, v) &&
131
+ Aro::Config.string_valid?(unvalid)
132
+ }
133
+ },
134
+ VALUES: {
135
+ name: Aro::Config::TYPES[:VALUES],
136
+ description: I18n.t("cli.config.type.values_description"),
137
+ converter: Proc.new{|v| v.to_s},
138
+ validator: Proc.new{|unvalid, k, v|
139
+ Aro::Config.def_valid?(k, v) &&
140
+ v[:possible_values].keys.include?(unvalid&.to_sym)
141
+ }
142
+ },
143
+ }
144
+
145
+ def self.bool_valid?(unvalid)
146
+ Aro::Config::BOOLS.values.map{|b| b.to_s.to_sym}.include?(unvalid&.to_s&.to_sym)
147
+ end
148
+
149
+ def self.int_valid?(unvalid)
150
+ !unvalid&.to_i.nil?
151
+ end
152
+
153
+ def self.string_valid?(unvalid)
154
+ unvalid.is_a?(String)
155
+ end
156
+
157
+ def self.def_valid?(key, deff)
158
+ def_valid = deff == Aro::Config::DEF[key]
159
+ unless def_valid
160
+ Aro::V.say("invalid def! #{key} => #{deff}")
161
+ end
162
+
163
+ def_valid
164
+ end
165
+
166
+ def validate_config
167
+ invalid_vars = []
168
+ Aro::Config::DEF.each{|k, v|
169
+ is_valid = v[:access] == Aro::Config::DEF_ACCESS[:READ]
170
+ unless is_valid
171
+ is_valid = valid_var?(Aro::Config.ivar(k), k, v)
172
+ end
173
+ invalid_vars << k unless is_valid
174
+ }
175
+ invalid_vars
176
+ end
177
+
178
+ def valid_var?(var_value, k, v)
179
+ Aro::V.say(v)
180
+ return true if v[:access] == Aro::Config::DEF_ACCESS[:READ]
181
+
182
+ Aro::Config::DEF_TYPES[
183
+ v[:type].to_s.upcase.to_sym
184
+ ][:validator].call(var_value, k, v)
185
+ end
186
+
187
+ def convert_var_for_def(k)
188
+ Aro::Config::DEF_TYPES[
189
+ Aro::Config::DEF[k][:type].upcase
190
+ ][:converter].call(ENV[Aro::Config.ivar_k(k)])
191
+ end
192
+
193
+ # adapts I18n translations to generate bash environment vars.
194
+ #
195
+ # example usage:
196
+ # Aro::Config::DEF[:Z_MAX]
197
+ DEF = {
198
+
199
+ #
200
+ # => ivars
201
+ #
202
+ ENV: {
203
+ type: Aro::Config::TYPES[:VALUES],
204
+ access: Aro::Config::DEF_ACCESS[:WRITE],
205
+ value: Aro::Config::ENVS[:PRODUCTION],
206
+ description: I18n.t("cli.config.env.description"),
207
+ possible_values: {
208
+ development: I18n.t("cli.config.env.development_description"),
209
+ production: I18n.t("cli.config.env.production_description"),
210
+ test: I18n.t("cli.config.env.test_description"),
211
+ }
212
+ },
213
+ VERBOSE: {
214
+ type: Aro::Config::TYPES[:BOOL],
215
+ access: Aro::Config::DEF_ACCESS[:WRITE],
216
+ value: Aro::Config::BOOLS[:FALSE],
217
+ description: I18n.t("cli.config.verbose_description"),
218
+ },
219
+ LOG_AOS_DB: {
220
+ type: Aro::Config::TYPES[:BOOL],
221
+ access: Aro::Config::DEF_ACCESS[:WRITE],
222
+ value: Aro::Config::BOOLS[:FALSE],
223
+ description: I18n.t("cli.config.log_aos_db_description"),
224
+ },
225
+ LOG_ARO_DB: {
226
+ type: Aro::Config::TYPES[:BOOL],
227
+ access: Aro::Config::DEF_ACCESS[:WRITE],
228
+ value: Aro::Config::BOOLS[:FALSE],
229
+ description: I18n.t("cli.config.log_aro_db_description"),
230
+ },
231
+ FORMAT: {
232
+ type: Aro::Config::TYPES[:VALUES],
233
+ implemented: false,
234
+ access: Aro::Config::DEF_ACCESS[:WRITE],
235
+ value: Aro::Config::FORMATS[:TEXT],
236
+ description: I18n.t("cli.config.format.description"),
237
+ possible_values: {
238
+ text: I18n.t("cli.config.format.text_description"),
239
+ json: I18n.t("cli.config.format.json_description")
240
+ }
241
+ },
242
+ INTERFACE: {
243
+ type: Aro::Config::TYPES[:VALUES],
244
+ implemented: false,
245
+ access: Aro::Config::DEF_ACCESS[:WRITE],
246
+ value: Aro::Config::INTERFACES[:TERMINAL],
247
+ description: I18n.t("cli.config.interface.description"),
248
+ possible_values: {
249
+ terminal: I18n.t("cli.config.interface.terminal_description"),
250
+ lanimret: I18n.t("cli.config.interface.lanimret_description")
251
+ }
252
+ },
253
+ DIMENSION: {
254
+ type: Aro::Config::TYPES[:VALUES],
255
+ access: Aro::Config::DEF_ACCESS[:WRITE],
256
+ value: Aro::Config::DMS[:DEV_TAROT],
257
+ description: I18n.t("cli.config.dimension.description"),
258
+ possible_values: {
259
+ dev_tarot: I18n.t("cli.config.dimension.dev_tarot_description"),
260
+ ruby_facot: I18n.t("cli.config.dimension.ruby_facot_description"),
261
+ }
262
+ },
263
+ HEIGHT: {
264
+ type: Aro::Config::TYPES[:INT],
265
+ access: Aro::Config::DEF_ACCESS[:WRITE],
266
+ value: Aro::Mancy::NUMERALS[:XLII],
267
+ min: Aro::Mancy::NUMERALS[:I],
268
+ max: Aro::Mancy::NUMERALS[:MMXCVII],
269
+ description: I18n.t(
270
+ "cli.config.height_description",
271
+ min: Aro::Mancy::NUMERALS[:I],
272
+ max: Aro::Mancy::NUMERALS[:VII].pow(Aro::Mancy::OS),
273
+ ),
274
+ },
275
+ WIDTH: {
276
+ type: Aro::Config::TYPES[:INT],
277
+ access: Aro::Config::DEF_ACCESS[:WRITE],
278
+ value: Aro::Mancy::NUMERALS[:C] + Aro::Mancy::NUMERALS[:XXXVII] - Aro::Mancy::S,
279
+ min: Aro::Mancy::NUMERALS[:I],
280
+ max: Aro::Mancy::NUMERALS[:MMXCVII],
281
+ description: I18n.t(
282
+ "cli.config.width_description",
283
+ min: Aro::Mancy::NUMERALS[:I],
284
+ max: Aro::Mancy::NUMERALS[:XI].pow(Aro::Mancy::OS),
285
+ ),
286
+ },
287
+ Z: {
288
+ type: Aro::Config::TYPES[:INT],
289
+ access: Aro::Config::DEF_ACCESS[:WRITE],
290
+ value: Aro::Mancy::NUMERALS[:I],
291
+ min: Aro::Mancy::NUMERALS[:I],
292
+ max: Aro::Mancy::NUMERALS[:MMXCVII],
293
+ description: I18n.t(
294
+ "cli.config.z_description",
295
+ min: Aro::Mancy::NUMERALS[:I],
296
+ max: Aro::Mancy::NUMERALS[:MMXCVII],
297
+ ),
298
+ },
299
+ Z_MAX: {
300
+ type: Aro::Config::TYPES[:INT],
301
+ access: Aro::Config::DEF_ACCESS[:WRITE],
302
+ value: Aro::Mancy::NUMERALS[:VII],
303
+ min: Aro::Mancy::NUMERALS[:I],
304
+ max: Aro::Mancy::NUMERALS[:XXII],
305
+ description: I18n.t(
306
+ "cli.config.z_max_description",
307
+ min: Aro::Mancy::NUMERALS[:I],
308
+ max: Aro::Mancy::NUMERALS[:XXII],
309
+ ),
310
+ },
311
+
312
+ #
313
+ # => ovars
314
+ #
315
+ O: {
316
+ type: Aro::Config::TYPES[:INT],
317
+ access: Aro::Config::DEF_ACCESS[:READ],
318
+ value: Aro::Mancy::O,
319
+ description: I18n.t("cli.config.aro_env.O_description"),
320
+ },
321
+ S: {
322
+ type: Aro::Config::TYPES[:INT],
323
+ access: Aro::Config::DEF_ACCESS[:READ],
324
+ value: Aro::Mancy::S,
325
+ description: I18n.t("cli.config.aro_env.S_description"),
326
+ },
327
+ OS: {
328
+ type: Aro::Config::TYPES[:INT],
329
+ access: Aro::Config::DEF_ACCESS[:READ],
330
+ value: Aro::Mancy::OS,
331
+ description: I18n.t("cli.config.aro_env.OS_description"),
332
+ },
333
+ E: {
334
+ type: Aro::Config::TYPES[:INT],
335
+ access: Aro::Config::DEF_ACCESS[:READ],
336
+ value: Aro::Mancy::E,
337
+ description: I18n.t("cli.config.aro_env.E_description"),
338
+ },
339
+ N: {
340
+ type: Aro::Config::TYPES[:INT],
341
+ access: Aro::Config::DEF_ACCESS[:READ],
342
+ value: Aro::Mancy::N,
343
+ description: I18n.t("cli.config.aro_env.N_description"),
344
+ },
345
+ PS1: {
346
+ type: Aro::Config::TYPES[:STRING],
347
+ access: Aro::Config::DEF_ACCESS[:READ],
348
+ value: Aro::Mancy::PS1,
349
+ description: I18n.t("cli.config.aro_env.PS1_description"),
350
+ },
351
+ NAME_FILE: {
352
+ type: Aro::Config::TYPES[:STRING],
353
+ access: Aro::Config::DEF_ACCESS[:READ],
354
+ value: Aro::Mancy::NAME_FILE,
355
+ description: I18n.t("cli.config.aro_env.NAME_FILE_description"),
356
+ },
357
+ I2097I: {
358
+ type: Aro::Config::TYPES[:STRING],
359
+ access: Aro::Config::DEF_ACCESS[:READ],
360
+ value: Aro::Mancy::I2097I,
361
+ description: I18n.t("cli.config.aro_env.I2097I_description"),
362
+ },
363
+ YES: {
364
+ type: Aro::Config::TYPES[:STRING],
365
+ access: Aro::Config::DEF_ACCESS[:READ],
366
+ value: Aro::Mancy::YES,
367
+ description: I18n.t("cli.config.aro_env.YES_description"),
368
+ },
369
+ ALL: {
370
+ type: Aro::Config::TYPES[:STRING],
371
+ access: Aro::Config::DEF_ACCESS[:READ],
372
+ value: Aro::Mancy::ALL,
373
+ description: I18n.t("cli.config.aro_env.ALL_description"),
374
+ },
375
+ }
376
+
377
+ def load
378
+ self.config_path = ""
379
+ if Aro::Mancy.in_aro? && Aro::Mancy.is_initialized?
380
+ self.config_path = Aro::Config.aro_config_path
381
+ elsif Aro::Dom.in_arodom?
382
+ self.config_path = Aro::Config.dom_config_path
383
+ end
384
+
385
+ unless File.exist?(Aro::Config.config_filepath)
386
+ generate_config
387
+ end
388
+
389
+ source_config
390
+ setup_env
391
+
392
+ self.display_lines = Aro::Config.base_lines
393
+ end
394
+
395
+ def self.base_lines
396
+ # print Aro::Config::DEF
397
+ lines = []
398
+ lines << "loaded config at: #{Aos::Os.osify(Aro::Config.config_filepath)}"
399
+ lines << "<Aro::Config::DEF>"
400
+ lines += Aro::Config.dump_config
401
+
402
+ # print config commands
403
+ lines << ""
404
+ lines << I18n.t("aos.constants.commands")
405
+ lines << ""
406
+ lines += Aos::Vw::Base.lines_for_cmd(Aos::Os::CMDS[:CONFIG])
407
+ lines
408
+ end
409
+
410
+ def self.aro_config_path
411
+ File.join(Dir.pwd, Aro::Db.base_aro_dir)
412
+ end
413
+
414
+ def self.dom_config_path
415
+ File.join(Aro::Dom::dom_root, Aro::Dom.room_path(:config))
416
+ end
417
+
418
+ def self.config_filepath
419
+ File.join(
420
+ self.instance.config_path,
421
+ Aro::Config::CONFIG_FILE.to_s
422
+ )
423
+ end
424
+
425
+ def self.is_test?
426
+ ENV[:ARO_ENV.to_s] == Aro::Config::ENVS[:TEST].to_s
427
+ end
428
+
429
+ def self.display_configuration
430
+ height, width = IO.console.winsize
431
+ result = {
432
+ HEIGHT: height, #Aro::Config.ivar(:HEIGHT).to_i,
433
+ WIDTH: width - Aro::Mancy::O,
434
+ DIVIDER: :".".to_s
435
+ }
436
+ # Aro::V.say(result)
437
+
438
+ result
439
+ end
440
+
441
+ def self.is_format_text?
442
+ Aro::Config.ivar(:FORMAT)&.to_sym == Aro::Config::FORMATS[:TEXT]
443
+ end
444
+
445
+ def self.process_config_command(args)
446
+ k = Aro::Config::DEF.keys.filter{|k| k == args[Aro::Mancy::S]&.upcase&.to_sym}&.first
447
+ unless k.nil?
448
+ unless args[Aro::Mancy::OS].nil?
449
+ # config <var_name> <var_value>
450
+ Aro::Config.set_ivar(args[Aro::Mancy::S], args[Aro::Mancy::OS])
451
+ end
452
+
453
+ # basic show var description and value
454
+ self.instance.display_lines = self.instance.lines_var(k, Aro::Config::DEF[k], ENV[Aro::Config.ivar_k(k)])
455
+ else
456
+ self.instance.display_lines = Aro::Config.base_lines
457
+ end
458
+
459
+ if Aro::Mancy.in_aro? && !Aro::Dom.in_arodom?
460
+ Aro::P.say(self.instance.display_lines.join("\n"))
461
+ end
462
+ end
463
+
464
+ # out vars
465
+ def self.ovar(suffix)
466
+ Aro::Config::DEF[suffix][:value]
467
+ end
468
+ # out vars
469
+ def self.ovar_k(suffix)
470
+ "#{Aro::Config::ARO_OVA_PREFIX}#{suffix}"
471
+ end
472
+
473
+ # in vars
474
+ def self.ivar(suffix)
475
+ ENV[Aro::Config.ivar_k(suffix)]
476
+ end
477
+ # in vars
478
+ def self.ivar_k(suffix)
479
+ "#{Aro::Config::ARO_IVA_PREFIX}#{suffix}"
480
+ end
481
+
482
+ def self.set_ivar(k, new_value)
483
+ k = k.upcase.to_sym
484
+
485
+ current_value = Aro::Config.ivar(k)
486
+ # ensure the var name is valid
487
+ unless current_value.nil?
488
+ Aro::Dom::P.say("validating #{k} with value #{new_value}")
489
+ if Aro::Config.instance.valid_var?(new_value, k, Aro::Config::DEF[k])
490
+ # set ENV value
491
+ ENV[Aro::Config.ivar_k(k)] = new_value
492
+ Aro::Dom::P.say("#{k} set to #{new_value}")
493
+ Aro::V.say(ENV[Aro::Config.ivar_k(k)])
494
+
495
+ # flush existing config and regen
496
+ Aro::Config.instance.generate_config(true)
497
+ Aro::Config.instance.source_config
498
+ Aro::Config.instance.setup_env
499
+ Aro::Db.configure_logger
500
+ Aos::Db.configure_logger
501
+ else
502
+ Aro::Dom::P.say("the ivar value you entered is invalid. ignoring.")
503
+ end
504
+ else
505
+ Aro::Dom::P.say("the ivar name you entered is invalid. ignoring.")
506
+ end
507
+ end
508
+
509
+ def setup_env
510
+ # do not change - update $ARO_CONFIG_ENV .aro/.config file
511
+ #
512
+ # default is production
513
+ varenv = Aro::Config.ivar(:ENV)
514
+ is_valid = valid_var?(varenv, :ENV, Aro::Config::DEF[:ENV])
515
+ ENV[:ARO_ENV.to_s] = is_valid ? varenv : Aro::Config::ENVS[:PRODUCTION].to_s
516
+ Aro::D.say("running in #{ENV[:ARO_ENV.to_s]} env.")
517
+ end
518
+
519
+ def self.dump_config
520
+ dump = []
521
+ Aro::Config::DEF.each{|k, v|
522
+ if v[:access] == Aro::Config::DEF_ACCESS[:WRITE]
523
+ dump << "#{Aro::Config.ivar_k(k).ljust(Aro::Mancy::NUMERALS[:XIV] * Aro::Mancy::OS)}=#{Aro::Config.ivar(k)}"
524
+ else
525
+ dump << "#{Aro::Config.ovar_k(k).ljust(Aro::Mancy::NUMERALS[:XIV] * Aro::Mancy::OS)}=#{Aro::Config.ovar(k)}"
526
+ end
527
+ }
528
+
529
+ dump
530
+ end
531
+
532
+ def source_config
533
+ Aro::D.say(I18n.t("cli.config.source", name: Aro::Config.config_filepath))
534
+ File.read(Aro::Config.config_filepath).split("\n").select{|line|
535
+ line.match?(/export #{Aro::Config::ARO_IVA_PREFIX}/)
536
+ }.map{|line|
537
+ line.gsub("export ", "").split("=")
538
+ }.each{|kv|
539
+ Aro::V.say("variable to set: #{kv}")
540
+ ENV[kv[0]] = kv[1] # source
541
+ Aro::V.say("value actually set: #{ENV[kv[0]]}")
542
+ }
543
+
544
+ # todo: implement
545
+ invalid_defs = validate_config
546
+ Aro::Config.dump_config.each{|l| Aro::V.say(l)}
547
+ invalid_defs.each{|k|
548
+ v = Aro::Config::DEF[k.to_sym]
549
+ if v[:access] == Aro::Config::DEF_ACCESS[:WRITE]
550
+ ENV[Aro::Config.ivar_k(k)] = v[:value]
551
+ else
552
+ ENV[Aro::Config.ovar_k(k)] = v[:value]
553
+ end
554
+ }
555
+ end
556
+
557
+ # from_memory true means write current config to file
558
+ def generate_config(from_memory = false)
559
+ # todo: localize generated config text
560
+ Aro::D.say(I18n.t("cli.config.generate", name: Aro::Config.config_filepath))
561
+
562
+ lines = []
563
+
564
+ # intro
565
+ Aro::Mancy::OS.times do
566
+ lines += lines_div
567
+ end
568
+
569
+ # header
570
+ lines += lines_div
571
+ lines += lines_newline_comment
572
+ lines += lines_config_header
573
+ lines += lines_newline_comment
574
+ lines += lines_div
575
+
576
+ lines += lines_newline_comment_os
577
+
578
+ # def_types
579
+ lines += lines_div
580
+ lines += lines_newline_comment
581
+ lines += lines_def_type_description
582
+ lines += lines_newline_comment
583
+ lines += lines_div
584
+
585
+ lines += lines_newline_comment_os
586
+
587
+ # var section
588
+ lines += lines_div
589
+ lines += lines_newline_comment
590
+ lines += lines_var_section_div
591
+ lines += lines_newline_comment
592
+ lines += lines_div
593
+
594
+ lines += lines_newline_comment_os
595
+
596
+ # vars
597
+ Aro::Config::DEF.each{|k, v|
598
+ lines += lines_var(k, v, (from_memory ? ENV[Aro::Config.ivar_k(k)] : nil))
599
+ }
600
+
601
+ lines += lines_newline_comment_os
602
+
603
+ # var section
604
+ lines += lines_div
605
+ lines += lines_newline_comment
606
+ lines += lines_var_section_div
607
+ lines += lines_newline_comment
608
+ lines += lines_div
609
+
610
+ lines += lines_newline_comment_os
611
+ # outro
612
+ Aro::Mancy::OS.times do
613
+ lines += lines_div
614
+ end
615
+ write_config(lines)
616
+ end
617
+
618
+ def write_config(lines)
619
+ File.open(Aro::Config.config_filepath, "w+") do |file|
620
+ file.write(lines.join("\n"))
621
+ file.write("\n")
622
+ end
623
+ end
624
+
625
+ def lines_div
626
+ ["#" * Aro::Mancy::NUMERALS[:VIII].pow(Aro::Mancy::OS)]
627
+ end
628
+
629
+ def lines_newline_comment
630
+ ["#"]
631
+ end
632
+
633
+ def lines_newline_comment_os
634
+ lines = []
635
+ Aro::Mancy::OS.times do
636
+ lines += lines_newline_comment
637
+ end
638
+ lines
639
+ end
640
+
641
+ def lines_config_header
642
+ [
643
+ "# #{Aro::Mancy::PS1} configuration file.",
644
+ "# this file was auto generated by the aro cli."
645
+ ]
646
+ end
647
+
648
+ def lines_var_section_div
649
+ ["# VARIABLE SECTION!"]
650
+ end
651
+
652
+ def lines_def_type_description
653
+ lines = []
654
+ lines << "# Aro::Config::DEF_TYPES"
655
+ lines << "# describes the possible types of variables."
656
+ Aro::Config::DEF_TYPES.each{|k, v|
657
+ lines << "# #{k}: #{v[:description]}"
658
+ }
659
+ lines += lines_newline_comment_os
660
+ lines << "# Aro::Config::DEF"
661
+ lines << "# define & expose an aro bash api via ENV variables."
662
+ lines << "# there are two types of bash vars in aro."
663
+ lines << "# 1) in vars (ivars). "
664
+ lines << "# => ivars enter aro from this file during aro init."
665
+ lines << "# => aro validates them and uses them unless unvalid."
666
+ lines << "# => otherwise aro will use the defaults listed below."
667
+ lines << "# 2) out vars (ovars)."
668
+ lines << "# => ovars are read-only vars that aro exposes to bash."
669
+ lines << "# => this is useful because it provides a bash interface"
670
+ lines << "# => which can be used to write programs on top of aro."
671
+ end
672
+
673
+ def lines_var(k, v, mem_v = nil)
674
+ lines = []
675
+ is_ovar = Aro::Config::DEF[k][:access] == Aro::Config::DEF_ACCESS[:READ]
676
+ if is_ovar
677
+ var_name = Aro::Config.ovar_k(k)
678
+ else
679
+ var_name = Aro::Config.ivar_k(k)
680
+ end
681
+ Aro::V.say("access for #{k} is #{Aro::Config::DEF[k][:access]}")
682
+ Aro::V.say("using var_name: #{var_name}")
683
+ lines << "# [#{var_name}] (#{is_ovar ? :ovar : :ivar})"
684
+ lines << "# => Aro::Config::DEF_TYPES: #{v[:type]}"
685
+ case v[:type]
686
+ when Aro::Config::DEF_TYPES[:BOOL][:name]
687
+ lines << "# => #{I18n.t("cli.config.type.bool_description")}"
688
+ when Aro::Config::DEF_TYPES[:INT][:name]
689
+ lines << "# => #{I18n.t("cli.config.type.int_description")}"
690
+ lines << "# => #{I18n.t("cli.config.minimum")}: #{v[:min]}"
691
+ lines << "# => #{I18n.t("cli.config.maximum")}: #{v[:max]}"
692
+ when Aro::Config::DEF_TYPES[:STRING][:name]
693
+ lines << "# => #{I18n.t("cli.config.type.string_description")}"
694
+ lines << "# => use \"double quotes\" if there are any spaces."
695
+ when Aro::Config::DEF_TYPES[:VALUES][:name]
696
+ lines << "# => #{I18n.t("cli.config.type.values_description")}"
697
+ lines << "# => #{I18n.t("cli.config.possible_values")}:"
698
+ lines += lines_newline_comment
699
+ v[:possible_values].each{|name, description|
700
+ lines << "# => #{name}"
701
+ lines += lines_newline_comment
702
+ lines << "# =>#{description}"
703
+ lines += lines_newline_comment
704
+ }
705
+ end
706
+ lines += lines_newline_comment_os
707
+ lines << "# => description:"
708
+ lines << "# => #{v[:description]}"
709
+ if is_ovar && Aro::Config::DEF_TYPES[:STRING][:name] == v[:type]
710
+ lines << "export #{var_name}=\"#{v[:value]}\""
711
+ else
712
+ lines << "export #{var_name}=#{mem_v || v[:value]}"
713
+ end
714
+ lines += lines_newline_comment_os
715
+ end
716
+
717
+ end
718
+ end