squared 0.5.12 → 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)
@@ -29,7 +22,7 @@ module Squared
29
22
  self.impl_project = obj
30
23
  impl_series.base_set(obj)
31
24
  else
32
- kind_project.prepend(obj)
25
+ kind_project.unshift(obj)
33
26
  obj.tasks&.each { |task| impl_series.add(task, obj) }
34
27
  end
35
28
  if (args = obj.batchargs)
@@ -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
@@ -179,11 +173,14 @@ module Squared
179
173
  end
180
174
 
181
175
  def with(*val, pass: false, group: nil, **kwargs, &blk)
182
- return self if pass == true || (pass && Array(pass).map(&:to_s).any? { |s| respond_to?(s) && __send__(s) })
176
+ return self if pass == true || (pass && as_a(pass, :to_s).any? { |s| respond_to?(s) && __send__(s) })
183
177
 
184
178
  @group = nil
185
179
  @ref = nil
186
- @withargs = kwargs.empty? ? nil : kwargs
180
+ @withargs = unless kwargs.empty?
181
+ kwargs.delete(:parent)
182
+ kwargs
183
+ end
187
184
  val = as_a(group || kwargs[:ref], flat: true, compact: true) if val.empty?
188
185
  kind = val.first
189
186
  val = kind if val.size == 1
@@ -193,7 +190,7 @@ module Squared
193
190
  when Symbol
194
191
  @ref = val
195
192
  else
196
- raise_error 'missing group or ref' if block_given?
193
+ raise_error ArgumentError, 'missing group or ref' if block_given?
197
194
  end
198
195
  if block_given?
199
196
  instance_eval(&blk)
@@ -218,7 +215,7 @@ module Squared
218
215
  nil
219
216
  else
220
217
  action.map! { |val| task_name(val) }
221
- prefix ? nil : @project.keys
218
+ @project.keys unless prefix
222
219
  end
223
220
  ns = lambda do |val|
224
221
  next if (ret = as_a(val, :to_s, flat: true)).empty?
@@ -310,22 +307,21 @@ module Squared
310
307
  end
311
308
  data.order << meth
312
309
  end
313
- if group
314
- label = :group
315
- items = Array(group)
316
- else
317
- label = :ref
318
- items = Array(ref || :_)
319
- end
320
- 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 }
321
317
  self
322
318
  end
323
319
 
324
320
  def add(path, project = nil, **kwargs, &blk)
325
- kwargs = Support.hashdup(@withargs).update(kwargs) if @withargs
321
+ kwargs = hashdup(@withargs).update(kwargs) if @withargs
326
322
  ref = kwargs.key?(:ref) ? kwargs.delete(:ref) : @ref
327
323
  kwargs[:group] = @group if @group && !kwargs.key?(:group)
328
- path = root + path
324
+ path = rootpath path
329
325
  project = (project || path.basename).to_s
330
326
  name = task_name project
331
327
  index = 0
@@ -382,11 +378,10 @@ module Squared
382
378
  end
383
379
  end
384
380
  if obj.is_a?(String)
385
- begin
386
- obj = JSON.parse(homepath(obj).read, { symbolize_names: true })
381
+ obj = begin
382
+ JSON.parse(homepath(obj).read, { symbolize_names: true })
387
383
  rescue StandardError => e
388
384
  warn log_message(Logger::ERROR, e)
389
- obj = nil
390
385
  end
391
386
  end
392
387
  apply_style(data || theme, obj, args, empty: empty) if obj && (!target || data)
@@ -448,7 +443,7 @@ module Squared
448
443
  end
449
444
 
450
445
  def task_localname(val)
451
- 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
452
447
  end
453
448
 
454
449
  def task_desc(*args, **kwargs)
@@ -564,7 +559,7 @@ module Squared
564
559
 
565
560
  return ret
566
561
  end
567
- @script[:ref!][:''] || SCRIPT_OBJ
562
+ @script[:ref!][:''] ||= scriptobj
568
563
  end
569
564
 
570
565
  def script_get(*args, group: nil, ref: nil)
@@ -740,23 +735,23 @@ module Squared
740
735
  index = k if w && has.call(w, v1)
741
736
  if a && has.call(a, v1)
742
737
  if index
743
- with.call(l + 1)
738
+ with.call(l.succ)
744
739
  throw :found
745
740
  else
746
- index = k + 1
741
+ index = k.succ
747
742
  end
748
743
  elsif b && has.call(b, v1)
749
744
  if index
750
745
  with.call(l)
751
746
  throw :found
752
747
  else
753
- index = k - 1
748
+ index = k.pred
754
749
  end
755
750
  elsif index
756
751
  if a || b
757
752
  tasks.each_with_index do |v2, m|
758
753
  if a && has.call(a, v2)
759
- with.call(m + 1)
754
+ with.call(m.succ)
760
755
  throw :found
761
756
  elsif b && has.call(b, v2)
762
757
  with.call(m)
@@ -766,7 +761,7 @@ module Squared
766
761
  if !pass
767
762
  pass = [i, data]
768
763
  elsif pass.include?(data)
769
- if i == pass.first + 1
764
+ if i == pass.first.succ
770
765
  pass.delete(data)
771
766
  pass = nil if pass.size == 1
772
767
  end
@@ -778,14 +773,14 @@ module Squared
778
773
  else
779
774
  next
780
775
  end
781
- step = index == -1 ? -1 : index + 1
776
+ step = index == -1 ? -1 : index.succ
782
777
  throw :found
783
778
  end
784
779
  end
785
780
  end
786
781
  end
787
782
  if step == -1
788
- level.prepend(data.action)
783
+ level.unshift(data.action)
789
784
  step = 0
790
785
  elsif step > 0
791
786
  (level[step -= 1] ||= []).concat(data.action)
@@ -806,18 +801,15 @@ module Squared
806
801
  format_desc key
807
802
  task key do
808
803
  unless failed.empty? && group.empty?
809
- puts(log_message(Logger::ERROR, *(failed + group.map { |val| val.action }.flatten),
810
- 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
811
806
  end
812
- cols = level.flatten(1).map(&:size).max
813
- level.each_with_index do |grp, n|
814
- title = "Step #{n.succ}#{if !sync.include?(grp) || (grp.size == 1 && series.parallel.include?(grp.first))
815
- ''
816
- 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))
817
809
  ' (sync)'
818
810
  end}"
819
- emphasize(grp, title: title, cols: [cols, title.size].max, border: theme[:border],
820
- 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/))
821
813
  end
822
814
  end
823
815
  end
@@ -835,12 +827,11 @@ module Squared
835
827
  end
836
828
  if group
837
829
  label = :group
838
- items = as_a(group, :to_sym)
830
+ as_a group, :to_sym
839
831
  else
840
832
  label = :ref
841
- items = as_a(ref, :to_sym)
842
- end
843
- items.each do |name|
833
+ as_a ref, :to_sym
834
+ end.each do |name|
844
835
  @script[label][name][task] = val
845
836
  @events[label][name][task] = on if on.is_a?(Hash)
846
837
  end
@@ -879,26 +870,34 @@ module Squared
879
870
 
880
871
  path.each_child do |c|
881
872
  name = c.basename.to_s
882
- next if c.to_s == __FILE__ || (@main == name && c.directory? && c.empty?) || pass.any? { |val| val == name }
883
-
884
- return false
873
+ unless c.to_s == __FILE__ || (@main == name && c.directory? && c.empty?) || pass.any? { |val| val == name }
874
+ return false
875
+ end
885
876
  end
886
877
  true
887
878
  end
888
879
 
889
- 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)
890
881
  data = script_find ref, group
882
+ type = script ? :script : :run
891
883
  if global
892
- target = data[:script] || data[:run] if target.nil?
884
+ target = data[type] if target.nil?
893
885
  pat = data[state] if pat.nil?
894
886
  end
895
- return false if state == :prod && data[:dev] == true && data[:global]
887
+ return false if state == :prod && data[:dev] == true && data[:global][type]
896
888
 
897
889
  target && pat.is_a?(Regexp) ? Array(target).any?(pat) : pat == true
898
890
  end
899
891
 
900
892
  def scriptobj
901
- SCRIPT_OBJ.dup
893
+ {
894
+ run: nil,
895
+ script: nil,
896
+ dev: nil,
897
+ prod: nil,
898
+ global: {},
899
+ env: {}
900
+ }
902
901
  end
903
902
  end
904
903
  end