squared 0.5.26 → 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.
@@ -12,18 +12,19 @@ module Squared
12
12
  include Rake::DSL
13
13
 
14
14
  class << self
15
- def parse(gem, namespace, ext = [gem])
15
+ def parse(gem, namespace, ext = [pkg])
16
16
  require gem
17
- [eval(namespace), Array(ext)].tap do |data|
18
- data.last.each { |key| @@mime_obj[key] = data }
19
- end
17
+ obj = eval namespace
18
+ Array(ext).each { |val| @@mime_obj[val] = [obj, ext] }
20
19
  rescue LoadError, NameError => e
21
20
  warn e
22
21
  nil
22
+ else
23
+ @@mime_obj[ext.first]
23
24
  end
24
25
 
25
- def link(project, main = project.dependfile&.basename, name = nil, **kwargs, &blk)
26
- return unless project.enabled? && main
26
+ def link(project, main = project.dependfile.basename, name = nil, **kwargs, &blk)
27
+ return unless project.enabled?
27
28
 
28
29
  ret = new(main, name, project: project, **kwargs)
29
30
  ret.instance_eval(&blk) if block_given?
@@ -85,7 +86,7 @@ module Squared
85
86
  ['path not found', realpath]
86
87
  else
87
88
  @required = true
88
- project ? [project, 'not found'] : %w[name missing]
89
+ project ? [project, 'missing'] : %w[name missing]
89
90
  end
90
91
  warn log_message(Logger::WARN, msg, subject: self.class, hint: hint)
91
92
  end
@@ -105,7 +106,7 @@ module Squared
105
106
  next unless (data = Viewer.parse(type, type.upcase, ext))
106
107
  end
107
108
  obj, ext = data
108
- target = file || target? ? file || realpath : nil
109
+ target = file || (realpath if target?)
109
110
 
110
111
  task_desc(command, *ext, target: target)
111
112
  task type, [:keys] do |_, args|
@@ -159,7 +160,7 @@ module Squared
159
160
  end
160
161
 
161
162
  def also(path, type = nil, name: nil, **kwargs)
162
- return self unless (file = basepath(path)).exist? && !@mime.frozen?
163
+ return self if @mime.frozen? || !(file = basepath(path)).exist?
163
164
 
164
165
  ext = mimetype file
165
166
  type ||= ext
@@ -204,7 +205,7 @@ module Squared
204
205
 
205
206
  def read_keys(reader, type, file, keys, ext: [type], opts: {})
206
207
  if file && (mime = mimetype(file)) && basepath(file).exist?
207
- raise_error(file, mime, hint: 'invalid') unless ext.include?(mime)
208
+ raise_error file, mime, hint: 'invalid' unless ext.include?(mime)
208
209
  else
209
210
  if ext.include?(mime)
210
211
  alt = file
@@ -219,7 +220,7 @@ module Squared
219
220
  args = { hint: 'no keys' }
220
221
  end
221
222
  unless file
222
- args ||= { hint: 'not found', kind: LoadError }
223
+ args ||= { kind: Errno::ENOENT }
223
224
  raise_error(reader.name, "#{File.basename(alt, '.*')}.#{ext.first}", **args)
224
225
  end
225
226
  end
@@ -236,22 +237,18 @@ module Squared
236
237
  title = Pathname.new(file)
237
238
  .realpath
238
239
  .to_s
239
- .sub(/\A#{Regexp.escape(File.join(Dir.pwd, ''))}/, '')
240
+ .sub(/^#{Regexp.escape(File.join(Dir.pwd, ''))}/, '')
240
241
  emphasize(lines, title: title, sub: unless stdin?
241
242
  [
242
- { pat: /\A((?:[^:]|(?<! ):(?! ))+)\z/, styles: theme[:banner] },
243
- { pat: /\A(.*?)(<[^>]+>)(.+)\z/m, styles: theme[:undefined], index: 2 },
244
- { pat: /\A((?~ : ))( : (?!undefined).+)\z/m, styles: theme[:key] },
245
- { pat: /\A((?~: ): )(-?[\d.]+)(\s*)\z/m, styles: theme[:number],
246
- index: 2 },
247
- { pat: /\A((?~: ): ")(.+)("\s*)\z/m, styles: theme[:string], index: 2 },
248
- { pat: /\A((?~: ): \{)(.+)(}\s*)\z/m, styles: theme[:hash], index: 2 },
249
- { pat: /\A((?~: ): \[)(.+)(\]\s*)\z/m, styles: theme[:array],
250
- index: 2 },
251
- { pat: /\A((?~: ): )(true|false)(\s*)\z/m, styles: theme[:boolean],
252
- index: 2 },
253
- { pat: /\A((?~: ): (?!undefined))([^"\[{].*)\z/m, styles: theme[:value],
254
- index: 2 }
243
+ opt_style(theme[:banner], /\A((?:[^:]|(?<! ):(?! ))+)\z/),
244
+ opt_style(theme[:undefined], /\A(.*?)(<[^>]+>)(.+)\z/m, 2),
245
+ opt_style(theme[:key], /\A((?~ : ))( : (?!undefined).+)\z/m),
246
+ opt_style(theme[:number], /\A((?~: ): )(-?[\d.]+)(\s*)\z/m, 2),
247
+ opt_style(theme[:string], /\A((?~: ): ")(.+)("\s*)\z/m, 2),
248
+ opt_style(theme[:hash], /\A((?~: ): \{)(.+)(\}\s*)\z/m, 2),
249
+ opt_style(theme[:array], /\A((?~: ): \[)(.+)(\]\s*)\z/m, 2),
250
+ opt_style(theme[:boolean], /\A((?~: ): )(true|false)(\s*)\z/m, 2),
251
+ opt_style(theme[:value], /\A((?~: ): (?!undefined))([^"\[{].*)\z/m, 2)
255
252
  ]
256
253
  end, border: theme[:border])
257
254
  end
@@ -262,7 +259,7 @@ module Squared
262
259
  symbolize = opts[:symbolize_names]
263
260
  keys.each do |key|
264
261
  begin
265
- items = key.split('.').flat_map { |name| name =~ /^(.+)\[(\d+)\]$/ ? [$1, $2.to_i] : name }
262
+ items = key.split('.')
266
263
  items = items.map(&:to_sym) if symbolize
267
264
  val = data.dig(*items)
268
265
  if val.nil?
@@ -293,7 +290,7 @@ module Squared
293
290
  if stdin?
294
291
  puts out.map!(&:last).join("\n")
295
292
  else
296
- out.map! { |a, b| '%-*s : %s' % [pad, a, b] }
293
+ out.map! { |item| '%-*s : %s' % [pad, item[0], item[1]] }
297
294
  end
298
295
  end
299
296
 
@@ -308,7 +305,7 @@ module Squared
308
305
  def task_desc(command, *ext, target: nil)
309
306
  return unless Rake::TaskManager.record_task_metadata
310
307
 
311
- val = "#{ext.first}[#{target ? '' : "file?=#{File.basename(main)}.#{ext.last},"}keys+]"
308
+ val = "#{ext.first}[#{"file?=#{File.basename(main)}.#{ext.last}," if target}keys+]"
312
309
  args = *name.split(':').push(command, val)
313
310
  if project
314
311
  project.workspace.task_desc(*args)
@@ -322,9 +319,7 @@ module Squared
322
319
  end
323
320
 
324
321
  def warning?
325
- return true unless project
326
-
327
- project.workspace.warning
322
+ project ? project.workspace.warning : true
328
323
  end
329
324
 
330
325
  def stdin?
@@ -349,9 +344,7 @@ module Squared
349
344
  end
350
345
 
351
346
  def basepath(*args)
352
- return Pathname.pwd.join(*args) unless project
353
-
354
- project.basepath(*args)
347
+ project ? project.basepath(*args) : Pathname.pwd.join(*args)
355
348
  end
356
349
  end
357
350
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- VERSION = '0.5.26'
4
+ VERSION = '0.6.0'
5
5
  end
@@ -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,46 +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 = if $DEBUG
99
- 2
100
- elsif (out = ARG[:OUT]) && out.include?(File::SEPARATOR)
101
- env_pipe(out, pipe, root: home)
102
- else
103
- env_pipe(pipe, (out && env(out)) || 1, root: home)
104
- end
105
- @exception = env_bool exception
106
- @verbose = if $VERBOSE.nil?
107
- false
108
- elsif verbose.nil?
109
- @pipe != 0
110
- else
111
- env_bool(verbose, verbose.is_a?(String) ? @pipe != 0 : verbose, index: true)
112
- end
113
- @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
114
102
  @closed = false
115
- if common
116
- @theme = __get__(:theme)[:workspace]
117
- ARG[:COLOR] = false if @pipe == 0 || @pipe.is_a?(Pathname)
118
- else
119
- @theme = {}
120
- end
121
- @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
122
110
  @script = {
123
- group: Support.hashobj,
124
- ref: Support.hashobj,
111
+ group: hashobj,
112
+ ref: hashobj,
125
113
  group!: {},
126
114
  ref!: {}
127
115
  }.freeze
128
116
  @events = {
129
- group: Support.hashobj,
130
- ref: Support.hashobj
117
+ group: hashobj,
118
+ ref: hashobj
131
119
  }.freeze
132
120
  @pass = {
133
- group: Support.hashobj,
134
- ref: Support.hashobj,
121
+ group: hashobj,
122
+ ref: hashobj,
135
123
  global: {},
136
124
  pattern: []
137
125
  }.freeze
@@ -184,29 +172,15 @@ module Squared
184
172
  self
185
173
  end
186
174
 
187
- def with(*val, hide: nil, group: nil, **kwargs, &blk)
188
- if hide.nil? && kwargs.key?(:pass)
189
- pass = kwargs[:pass]
190
- case pass
191
- when true, false
192
- hide = pass
193
- kwargs.delete(:pass)
194
- else
195
- hide, pass = Array(pass).partition { |s| respond_to?(s) || s.to_s.end_with?('?') }
196
- if pass.empty?
197
- kwargs.delete(:pass)
198
- elsif hide.empty?
199
- hide = nil
200
- else
201
- kwargs[:pass] = pass
202
- end
203
- end
204
- end
205
- 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) })
206
177
 
207
178
  @group = nil
208
179
  @ref = nil
209
- @withargs = kwargs.empty? ? nil : kwargs
180
+ @withargs = unless kwargs.empty?
181
+ kwargs.delete(:parent)
182
+ kwargs
183
+ end
210
184
  val = as_a(group || kwargs[:ref], flat: true, compact: true) if val.empty?
211
185
  kind = val.first
212
186
  val = kind if val.size == 1
@@ -216,7 +190,7 @@ module Squared
216
190
  when Symbol
217
191
  @ref = val
218
192
  else
219
- raise_error 'missing group or ref' if block_given?
193
+ raise_error ArgumentError, 'missing group or ref' if block_given?
220
194
  end
221
195
  if block_given?
222
196
  instance_eval(&blk)
@@ -241,7 +215,7 @@ module Squared
241
215
  nil
242
216
  else
243
217
  action.map! { |val| task_name(val) }
244
- prefix ? nil : @project.keys
218
+ @project.keys unless prefix
245
219
  end
246
220
  ns = lambda do |val|
247
221
  next if (ret = as_a(val, :to_s, flat: true)).empty?
@@ -297,9 +271,9 @@ module Squared
297
271
 
298
272
  def pass(name, group: @group, ref: @ref, &blk)
299
273
  data = if group
300
- @pass[:group][group.to_s]
274
+ @pass[:group][group]
301
275
  elsif ref
302
- @pass[:ref][ref.to_sym]
276
+ @pass[:ref][ref]
303
277
  else
304
278
  @pass[:global]
305
279
  end
@@ -333,22 +307,21 @@ module Squared
333
307
  end
334
308
  data.order << meth
335
309
  end
336
- if group
337
- label = :group
338
- items = Array(group)
339
- else
340
- label = :ref
341
- items = Array(ref || :_)
342
- end
343
- 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 }
344
317
  self
345
318
  end
346
319
 
347
320
  def add(path, project = nil, **kwargs, &blk)
348
- kwargs = Support.hashdup(@withargs).update(kwargs) if @withargs
321
+ kwargs = hashdup(@withargs).update(kwargs) if @withargs
349
322
  ref = kwargs.key?(:ref) ? kwargs.delete(:ref) : @ref
350
323
  kwargs[:group] = @group if @group && !kwargs.key?(:group)
351
- path = root + path
324
+ path = rootpath path
352
325
  project = (project || path.basename).to_s
353
326
  name = task_name project
354
327
  index = 0
@@ -358,9 +331,9 @@ module Squared
358
331
  end
359
332
  proj = ((if !ref.is_a?(Class)
360
333
  Application.find(ref, path: path)
361
- elsif ref < Application.impl_project
334
+ elsif ref < Project::Base
362
335
  ref
363
- end) || @kind[name]&.last || Application.impl_project).new(self, path, name, **kwargs)
336
+ end) || @kind[name]&.last || Project::Base).new(self, path, name, **kwargs)
364
337
  proj.__send__(:index_set, @project.size)
365
338
  @project[name] = proj
366
339
  __get__(:project)[name] = proj unless kwargs[:private]
@@ -405,11 +378,10 @@ module Squared
405
378
  end
406
379
  end
407
380
  if obj.is_a?(String)
408
- begin
409
- obj = JSON.parse(homepath(obj).read, { symbolize_names: true })
381
+ obj = begin
382
+ JSON.parse(homepath(obj).read, { symbolize_names: true })
410
383
  rescue StandardError => e
411
384
  warn log_message(Logger::ERROR, e)
412
- obj = nil
413
385
  end
414
386
  end
415
387
  apply_style(data || theme, obj, args, empty: empty) if obj && (!target || data)
@@ -471,7 +443,7 @@ module Squared
471
443
  end
472
444
 
473
445
  def task_localname(val)
474
- 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
475
447
  end
476
448
 
477
449
  def task_desc(*args, **kwargs)
@@ -520,7 +492,7 @@ module Squared
520
492
  if (base = task_base?(key))
521
493
  tasks << key if obj.has?(key, baseref)
522
494
  elsif (batch = series.batch_get(key))
523
- obj.allref do |ref|
495
+ obj.allref.each do |ref|
524
496
  next unless obj.has?(key, ref) && (data = batch[ref])
525
497
 
526
498
  data.each do |val|
@@ -539,7 +511,7 @@ module Squared
539
511
  if tasks.empty?
540
512
  return [] if (base && !obj.ref?(baseref)) || !(data = series.alias_get(key))
541
513
 
542
- obj.allref do |ref|
514
+ obj.allref.each do |ref|
543
515
  next unless obj.has?(key, ref) && (alt = data[ref])
544
516
 
545
517
  ret = task_resolve obj, alt
@@ -587,7 +559,7 @@ module Squared
587
559
 
588
560
  return ret
589
561
  end
590
- @script[:ref!][:''] || SCRIPT_OBJ
562
+ @script[:ref!][:''] ||= scriptobj
591
563
  end
592
564
 
593
565
  def script_get(*args, group: nil, ref: nil)
@@ -603,7 +575,7 @@ module Squared
603
575
  return ret if group && (ret = @banner[:group][group.to_sym])
604
576
 
605
577
  ref.reverse_each { |val| return ret if (ret = @banner[:ref][val]) }
606
- @banner[:ref][:_]
578
+ @banner[:ref][:'']
607
579
  end
608
580
 
609
581
  def enabled?
@@ -763,23 +735,23 @@ module Squared
763
735
  index = k if w && has.call(w, v1)
764
736
  if a && has.call(a, v1)
765
737
  if index
766
- with.call(l + 1)
738
+ with.call(l.succ)
767
739
  throw :found
768
740
  else
769
- index = k + 1
741
+ index = k.succ
770
742
  end
771
743
  elsif b && has.call(b, v1)
772
744
  if index
773
745
  with.call(l)
774
746
  throw :found
775
747
  else
776
- index = k - 1
748
+ index = k.pred
777
749
  end
778
750
  elsif index
779
751
  if a || b
780
752
  tasks.each_with_index do |v2, m|
781
753
  if a && has.call(a, v2)
782
- with.call(m + 1)
754
+ with.call(m.succ)
783
755
  throw :found
784
756
  elsif b && has.call(b, v2)
785
757
  with.call(m)
@@ -789,7 +761,7 @@ module Squared
789
761
  if !pass
790
762
  pass = [i, data]
791
763
  elsif pass.include?(data)
792
- if i == pass.first + 1
764
+ if i == pass.first.succ
793
765
  pass.delete(data)
794
766
  pass = nil if pass.size == 1
795
767
  end
@@ -801,7 +773,7 @@ module Squared
801
773
  else
802
774
  next
803
775
  end
804
- step = index == -1 ? -1 : index + 1
776
+ step = index == -1 ? -1 : index.succ
805
777
  throw :found
806
778
  end
807
779
  end
@@ -829,18 +801,15 @@ module Squared
829
801
  format_desc key
830
802
  task key do
831
803
  unless failed.empty? && group.empty?
832
- puts(log_message(Logger::ERROR, *(failed + group.map { |val| val.action }.flatten),
833
- 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
834
806
  end
835
- cols = level.flatten(1).map(&:size).max
836
- level.each_with_index do |grp, n|
837
- title = "Step #{n.succ}#{if !sync.include?(grp) || (grp.size == 1 && series.parallel.include?(grp.first))
838
- ''
839
- 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))
840
809
  ' (sync)'
841
810
  end}"
842
- emphasize(grp, title: title, cols: [cols, title.size].max, border: theme[:border],
843
- 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/))
844
813
  end
845
814
  end
846
815
  end
@@ -850,7 +819,7 @@ module Squared
850
819
  log_console(*args, pipe: kwargs[:pipe] || pipe)
851
820
  end
852
821
 
853
- def script_command(task, val, group, ref, on = nil, &blk)
822
+ def script_command(task, val, group, ref, on, &blk)
854
823
  if block_given?
855
824
  val = Struct::RunData.new(val, blk)
856
825
  elsif !val
@@ -858,12 +827,11 @@ module Squared
858
827
  end
859
828
  if group
860
829
  label = :group
861
- items = as_a(group, :to_sym)
830
+ as_a group, :to_sym
862
831
  else
863
832
  label = :ref
864
- items = as_a(ref || :_, :to_sym)
865
- end
866
- items.each do |name|
833
+ as_a ref, :to_sym
834
+ end.each do |name|
867
835
  @script[label][name][task] = val
868
836
  @events[label][name][task] = on if on.is_a?(Hash)
869
837
  end
@@ -894,8 +862,6 @@ module Squared
894
862
  nil
895
863
  elsif ref && target[:ref].key?(ref)
896
864
  target[:ref][ref]
897
- else
898
- target[:ref][:_]
899
865
  end
900
866
  end
901
867
 
@@ -904,26 +870,34 @@ module Squared
904
870
 
905
871
  path.each_child do |c|
906
872
  name = c.basename.to_s
907
- next if c.to_s == __FILE__ || (@main == name && c.directory? && c.empty?) || pass.any? { |val| val == name }
908
-
909
- return false
873
+ unless c.to_s == __FILE__ || (@main == name && c.directory? && c.empty?) || pass.any? { |val| val == name }
874
+ return false
875
+ end
910
876
  end
911
877
  true
912
878
  end
913
879
 
914
- 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)
915
881
  data = script_find ref, group
882
+ type = script ? :script : :run
916
883
  if global
917
- target = data[:script] || data[:run] if target.nil?
884
+ target = data[type] if target.nil?
918
885
  pat = data[state] if pat.nil?
919
886
  end
920
- return false if state == :prod && data[:dev] == true && data[:global]
887
+ return false if state == :prod && data[:dev] == true && data[:global][type]
921
888
 
922
889
  target && pat.is_a?(Regexp) ? Array(target).any?(pat) : pat == true
923
890
  end
924
891
 
925
892
  def scriptobj
926
- SCRIPT_OBJ.dup
893
+ {
894
+ run: nil,
895
+ script: nil,
896
+ dev: nil,
897
+ prod: nil,
898
+ global: {},
899
+ env: {}
900
+ }
927
901
  end
928
902
  end
929
903
  end