squared 0.5.23 → 0.6.0

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.
@@ -5,18 +5,11 @@ module Squared
5
5
  class Application
6
6
  include Common::Format
7
7
  include Utils
8
+ include Support::Variables
8
9
  include Rake::DSL
9
10
 
10
- SCRIPT_OBJ = {
11
- run: nil,
12
- script: nil,
13
- dev: nil,
14
- prod: nil,
15
- global: false,
16
- env: false
17
- }.freeze
18
11
  TASK_METADATA = Rake::TaskManager.record_task_metadata
19
- private_constant :SCRIPT_OBJ, :TASK_METADATA
12
+ private_constant :TASK_METADATA
20
13
 
21
14
  class << self
22
15
  def implement(*objs, base: false)
@@ -75,7 +68,8 @@ module Squared
75
68
  @kind_project = []
76
69
  @task_exclude = Set.new
77
70
 
78
- attr_reader :root, :home, :main, :prefix, :exception, :warning, :pipe, :verbose, :theme, :series, :closed
71
+ attr_reader :root, :home, :main, :prefix, :theme, :series, :closed
72
+ attr_accessor :exception, :pipe, :verbose, :warning
79
73
 
80
74
  def initialize(home = (ARG[:HOME] && ENV[ARG[:HOME]]) || Dir.pwd, *, main: nil, prefix: nil,
81
75
  verbose: ARG[:VERBOSE], common: ARG[:COMMON], pipe: ARG[:PIPE], exception: ARG[:FAIL], **)
@@ -92,40 +86,40 @@ module Squared
92
86
  @prefix = prefix
93
87
  @series = Application.series_wrap(self)
94
88
  @project = {}
95
- @kind = Support.hashlist
89
+ @kind = hashlist
96
90
  @extensions = []
97
91
  @envname = env_key(@main).freeze
98
- @pipe = $DEBUG ? 2 : env_pipe(pipe, (ARG[:OUT] && env(ARG[:OUT])) || 1, root: @home)
99
- @exception = env_bool exception
100
- @verbose = if $VERBOSE.nil?
101
- false
102
- elsif verbose.nil?
103
- @pipe != 0
104
- else
105
- env_bool(verbose, verbose.is_a?(String) ? @pipe != 0 : verbose, index: true)
106
- end
107
- @warning = @verbose != false
92
+ self.exception = env_bool exception
93
+ self.pipe = $DEBUG ? 2 : env_pipe(pipe, (ARG[:OUT] && env(ARG[:OUT])) || 1, root: @home)
94
+ self.verbose = if $VERBOSE.nil?
95
+ false
96
+ elsif verbose.nil?
97
+ @pipe != 0
98
+ else
99
+ env_bool(verbose, verbose.is_a?(String) ? @pipe != 0 : verbose, index: true)
100
+ end
101
+ self.warning = @verbose != false
108
102
  @closed = false
109
- if common
110
- @theme = __get__(:theme)[:workspace]
111
- ARG[:COLOR] = false if @pipe == 0 || @pipe.is_a?(Pathname)
112
- else
113
- @theme = {}
114
- end
115
- @chain = Support.hashlist
103
+ @theme = if common
104
+ ARG[:COLOR] = false if @pipe == 0 || @pipe.is_a?(Pathname)
105
+ __get__(:theme)[:workspace]
106
+ else
107
+ {}
108
+ end
109
+ @chain = hashlist
116
110
  @script = {
117
- group: Support.hashobj,
118
- ref: Support.hashobj,
111
+ group: hashobj,
112
+ ref: hashobj,
119
113
  group!: {},
120
114
  ref!: {}
121
115
  }.freeze
122
116
  @events = {
123
- group: Support.hashobj,
124
- ref: Support.hashobj
117
+ group: hashobj,
118
+ ref: hashobj
125
119
  }.freeze
126
120
  @pass = {
127
- group: Support.hashobj,
128
- ref: Support.hashobj,
121
+ group: hashobj,
122
+ ref: hashobj,
129
123
  global: {},
130
124
  pattern: []
131
125
  }.freeze
@@ -178,29 +172,15 @@ module Squared
178
172
  self
179
173
  end
180
174
 
181
- def with(*val, hide: nil, group: nil, **kwargs, &blk)
182
- if hide.nil? && kwargs.key?(:pass)
183
- pass = kwargs[:pass]
184
- case pass
185
- when true, false
186
- hide = pass
187
- kwargs.delete(:pass)
188
- else
189
- hide, pass = Array(pass).partition { |s| respond_to?(s) || s.to_s.end_with?('?') }
190
- if pass.empty?
191
- kwargs.delete(:pass)
192
- elsif hide.empty?
193
- hide = nil
194
- else
195
- kwargs[:pass] = pass
196
- end
197
- end
198
- end
199
- return self if hide == true || (hide && Array(hide).any? { |s| respond_to?(s) && __send__(s) rescue nil })
175
+ def with(*val, pass: false, group: nil, **kwargs, &blk)
176
+ return self if pass == true || (pass && as_a(pass, :to_s).any? { |s| respond_to?(s) && __send__(s) })
200
177
 
201
178
  @group = nil
202
179
  @ref = nil
203
- @withargs = kwargs.empty? ? nil : kwargs
180
+ @withargs = unless kwargs.empty?
181
+ kwargs.delete(:parent)
182
+ kwargs
183
+ end
204
184
  val = as_a(group || kwargs[:ref], flat: true, compact: true) if val.empty?
205
185
  kind = val.first
206
186
  val = kind if val.size == 1
@@ -210,7 +190,7 @@ module Squared
210
190
  when Symbol
211
191
  @ref = val
212
192
  else
213
- raise_error 'missing group or ref' if block_given?
193
+ raise_error ArgumentError, 'missing group or ref' if block_given?
214
194
  end
215
195
  if block_given?
216
196
  instance_eval(&blk)
@@ -235,7 +215,7 @@ module Squared
235
215
  nil
236
216
  else
237
217
  action.map! { |val| task_name(val) }
238
- prefix ? nil : @project.keys
218
+ @project.keys unless prefix
239
219
  end
240
220
  ns = lambda do |val|
241
221
  next if (ret = as_a(val, :to_s, flat: true)).empty?
@@ -291,9 +271,9 @@ module Squared
291
271
 
292
272
  def pass(name, group: @group, ref: @ref, &blk)
293
273
  data = if group
294
- @pass[:group][group.to_s]
274
+ @pass[:group][group]
295
275
  elsif ref
296
- @pass[:ref][ref.to_sym]
276
+ @pass[:ref][ref]
297
277
  else
298
278
  @pass[:global]
299
279
  end
@@ -327,22 +307,21 @@ module Squared
327
307
  end
328
308
  data.order << meth
329
309
  end
330
- if group
331
- label = :group
332
- items = Array(group)
333
- else
334
- label = :ref
335
- items = Array(ref || :_)
336
- end
337
- items.each { |val| @banner[label][val.to_sym] = data }
310
+ Array(if group
311
+ label = :group
312
+ group
313
+ else
314
+ label = :ref
315
+ ref || :_
316
+ end).each { |val| @banner[label][val.to_sym] = data }
338
317
  self
339
318
  end
340
319
 
341
320
  def add(path, project = nil, **kwargs, &blk)
342
- kwargs = Support.hashdup(@withargs).update(kwargs) if @withargs
321
+ kwargs = hashdup(@withargs).update(kwargs) if @withargs
343
322
  ref = kwargs.key?(:ref) ? kwargs.delete(:ref) : @ref
344
323
  kwargs[:group] = @group if @group && !kwargs.key?(:group)
345
- path = root + path
324
+ path = rootpath path
346
325
  project = (project || path.basename).to_s
347
326
  name = task_name project
348
327
  index = 0
@@ -352,9 +331,9 @@ module Squared
352
331
  end
353
332
  proj = ((if !ref.is_a?(Class)
354
333
  Application.find(ref, path: path)
355
- elsif ref < Application.impl_project
334
+ elsif ref < Project::Base
356
335
  ref
357
- end) || @kind[name]&.last || Application.impl_project).new(self, path, name, **kwargs)
336
+ end) || @kind[name]&.last || Project::Base).new(self, path, name, **kwargs)
358
337
  proj.__send__(:index_set, @project.size)
359
338
  @project[name] = proj
360
339
  __get__(:project)[name] = proj unless kwargs[:private]
@@ -399,11 +378,10 @@ module Squared
399
378
  end
400
379
  end
401
380
  if obj.is_a?(String)
402
- begin
403
- obj = JSON.parse(homepath(obj).read, { symbolize_names: true })
381
+ obj = begin
382
+ JSON.parse(homepath(obj).read, { symbolize_names: true })
404
383
  rescue StandardError => e
405
384
  warn log_message(Logger::ERROR, e)
406
- obj = nil
407
385
  end
408
386
  end
409
387
  apply_style(data || theme, obj, args, empty: empty) if obj && (!target || data)
@@ -465,7 +443,7 @@ module Squared
465
443
  end
466
444
 
467
445
  def task_localname(val)
468
- prefix && val.is_a?(String) ? val.sub(/\A#{Regexp.escape(prefix)}:/, '') : val.to_s
446
+ prefix && val.is_a?(String) ? val.sub(/^#{Regexp.escape(prefix)}:/, '') : val.to_s
469
447
  end
470
448
 
471
449
  def task_desc(*args, **kwargs)
@@ -514,7 +492,7 @@ module Squared
514
492
  if (base = task_base?(key))
515
493
  tasks << key if obj.has?(key, baseref)
516
494
  elsif (batch = series.batch_get(key))
517
- obj.allref do |ref|
495
+ obj.allref.each do |ref|
518
496
  next unless obj.has?(key, ref) && (data = batch[ref])
519
497
 
520
498
  data.each do |val|
@@ -533,7 +511,7 @@ module Squared
533
511
  if tasks.empty?
534
512
  return [] if (base && !obj.ref?(baseref)) || !(data = series.alias_get(key))
535
513
 
536
- obj.allref do |ref|
514
+ obj.allref.each do |ref|
537
515
  next unless obj.has?(key, ref) && (alt = data[ref])
538
516
 
539
517
  ret = task_resolve obj, alt
@@ -581,7 +559,7 @@ module Squared
581
559
 
582
560
  return ret
583
561
  end
584
- @script[:ref!][:''] || SCRIPT_OBJ
562
+ @script[:ref!][:''] ||= scriptobj
585
563
  end
586
564
 
587
565
  def script_get(*args, group: nil, ref: nil)
@@ -597,7 +575,7 @@ module Squared
597
575
  return ret if group && (ret = @banner[:group][group.to_sym])
598
576
 
599
577
  ref.reverse_each { |val| return ret if (ret = @banner[:ref][val]) }
600
- @banner[:ref][:_]
578
+ @banner[:ref][:'']
601
579
  end
602
580
 
603
581
  def enabled?
@@ -757,23 +735,23 @@ module Squared
757
735
  index = k if w && has.call(w, v1)
758
736
  if a && has.call(a, v1)
759
737
  if index
760
- with.call(l + 1)
738
+ with.call(l.succ)
761
739
  throw :found
762
740
  else
763
- index = k + 1
741
+ index = k.succ
764
742
  end
765
743
  elsif b && has.call(b, v1)
766
744
  if index
767
745
  with.call(l)
768
746
  throw :found
769
747
  else
770
- index = k - 1
748
+ index = k.pred
771
749
  end
772
750
  elsif index
773
751
  if a || b
774
752
  tasks.each_with_index do |v2, m|
775
753
  if a && has.call(a, v2)
776
- with.call(m + 1)
754
+ with.call(m.succ)
777
755
  throw :found
778
756
  elsif b && has.call(b, v2)
779
757
  with.call(m)
@@ -783,7 +761,7 @@ module Squared
783
761
  if !pass
784
762
  pass = [i, data]
785
763
  elsif pass.include?(data)
786
- if i == pass.first + 1
764
+ if i == pass.first.succ
787
765
  pass.delete(data)
788
766
  pass = nil if pass.size == 1
789
767
  end
@@ -795,7 +773,7 @@ module Squared
795
773
  else
796
774
  next
797
775
  end
798
- step = index == -1 ? -1 : index + 1
776
+ step = index == -1 ? -1 : index.succ
799
777
  throw :found
800
778
  end
801
779
  end
@@ -823,18 +801,15 @@ module Squared
823
801
  format_desc key
824
802
  task key do
825
803
  unless failed.empty? && group.empty?
826
- puts(log_message(Logger::ERROR, *(failed + group.map { |val| val.action }.flatten),
827
- subject: 'failed placement', hint: false), pipe: 2)
804
+ group.each { |val| failed += val.action }
805
+ puts log_message(Logger::ERROR, *failed, subject: 'failed placement', hint: false), pipe: 2
828
806
  end
829
- cols = level.flatten(1).map(&:size).max
830
- level.each_with_index do |grp, n|
831
- title = "Step #{n.succ}#{if !sync.include?(grp) || (grp.size == 1 && series.parallel.include?(grp.first))
832
- ''
833
- else
807
+ level.each_with_index do |grp, i|
808
+ title = "Step #{i.succ}#{if sync.include?(grp) && !(grp.size == 1 && series.parallel.include?(grp.first))
834
809
  ' (sync)'
835
810
  end}"
836
- emphasize(grp, title: title, cols: [cols, title.size].max, border: theme[:border],
837
- sub: [{ pat: /\A(Step \d+)(.*)\z/, styles: theme[:header] }])
811
+ emphasize(grp, title: title, cols: level.flatten(1).push(title), border: theme[:border],
812
+ sub: opt_style(theme[:header], /\A(Step \d+)(.*)\z/))
838
813
  end
839
814
  end
840
815
  end
@@ -852,12 +827,11 @@ module Squared
852
827
  end
853
828
  if group
854
829
  label = :group
855
- items = as_a(group, :to_sym)
830
+ as_a group, :to_sym
856
831
  else
857
832
  label = :ref
858
- items = as_a(ref, :to_sym)
859
- end
860
- items.each do |name|
833
+ as_a ref, :to_sym
834
+ end.each do |name|
861
835
  @script[label][name][task] = val
862
836
  @events[label][name][task] = on if on.is_a?(Hash)
863
837
  end
@@ -896,26 +870,34 @@ module Squared
896
870
 
897
871
  path.each_child do |c|
898
872
  name = c.basename.to_s
899
- next if c.to_s == __FILE__ || (@main == name && c.directory? && c.empty?) || pass.any? { |val| val == name }
900
-
901
- return false
873
+ unless c.to_s == __FILE__ || (@main == name && c.directory? && c.empty?) || pass.any? { |val| val == name }
874
+ return false
875
+ end
902
876
  end
903
877
  true
904
878
  end
905
879
 
906
- def script?(state, target: nil, pat: nil, group: nil, ref: baseref, global: false)
880
+ def script?(state, target: nil, pat: nil, group: nil, ref: baseref, global: false, script: true)
907
881
  data = script_find ref, group
882
+ type = script ? :script : :run
908
883
  if global
909
- target = data[:script] || data[:run] if target.nil?
884
+ target = data[type] if target.nil?
910
885
  pat = data[state] if pat.nil?
911
886
  end
912
- return false if state == :prod && data[:dev] == true && data[:global]
887
+ return false if state == :prod && data[:dev] == true && data[:global][type]
913
888
 
914
889
  target && pat.is_a?(Regexp) ? Array(target).any?(pat) : pat == true
915
890
  end
916
891
 
917
892
  def scriptobj
918
- SCRIPT_OBJ.dup
893
+ {
894
+ run: nil,
895
+ script: nil,
896
+ dev: nil,
897
+ prod: nil,
898
+ global: {},
899
+ env: {}
900
+ }
919
901
  end
920
902
  end
921
903
  end