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