squared 0.5.4 → 0.5.5

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.
@@ -94,7 +94,7 @@ module Squared
94
94
  PATH.freeze
95
95
  ARG.freeze
96
96
  VAR.each_value(&:freeze)
97
- VAR[:theme].each_value(&:freeze)
97
+ VAR[:theme].each_value { |val| val.freeze.each_value(&:freeze) }
98
98
  VAR.freeze
99
99
  end
100
100
 
@@ -143,11 +143,11 @@ module Squared
143
143
  def apply_style(data, key, args, empty: true)
144
144
  return if data.is_a?(::Symbol) && (data = __get__(:theme)[data]).nil?
145
145
 
146
- set = ->(k, v) { data[k] = check_style(v, empty: empty) }
146
+ set = ->(k, v) { data[k.to_sym] = check_style(v, empty: empty) }
147
147
  if key.is_a?(::Hash)
148
148
  key.each { |k, v| set.call(k, v || args) }
149
149
  else
150
- set.call(key.to_sym, args)
150
+ set.call(key, args)
151
151
  end
152
152
  end
153
153
 
@@ -196,7 +196,7 @@ module Squared
196
196
  end
197
197
  end
198
198
 
199
- def puts_oe(*args, pipe: 1)
199
+ def log_console(*args, pipe: 1)
200
200
  return if args.first == false && args.size == 1
201
201
 
202
202
  if pipe.is_a?(Pathname)
@@ -213,6 +213,8 @@ module Squared
213
213
  (pipe == 2 ? $stderr : $stdout).puts(*args)
214
214
  end
215
215
 
216
+ alias puts_oe log_console
217
+
216
218
  module_function
217
219
 
218
220
  def message(*args, hint: nil, empty: false, space: ARG[:SPACE])
@@ -48,7 +48,7 @@ module Squared
48
48
  list.each do |val|
49
49
  next if grep&.none? { |pat| pat.match?(line) }
50
50
 
51
- items << val.chomp
51
+ items << val.to_s.chomp
52
52
  puts '%2d. %s' % [items.size, val]
53
53
  end
54
54
  max = items.size
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rake'
4
+ require 'shellwords'
4
5
 
5
6
  module Squared
6
7
  module Common
@@ -33,7 +34,6 @@ module Squared
33
34
  elsif Rake::Win32.windows?
34
35
  $2
35
36
  else
36
- require 'shellwords'
37
37
  Shellwords.escape($2)
38
38
  end
39
39
  elsif Rake::Win32.windows?
@@ -41,7 +41,6 @@ module Squared
41
41
  elsif val.empty?
42
42
  ''
43
43
  else
44
- require 'shellwords'
45
44
  Shellwords.escape(val)
46
45
  end
47
46
  end
@@ -97,6 +96,12 @@ module Squared
97
96
  ret.join(join.is_a?(::String) ? join : ' ')
98
97
  end
99
98
 
99
+ def shell_bin(name, env: true)
100
+ key = name.upcase
101
+ shell_quote((env && ENV["PATH_#{key}"]) || PATH[key] || PATH[key.to_sym] || name,
102
+ option: false, force: false)
103
+ end
104
+
100
105
  def line_width(lines)
101
106
  ret = [lines.max_by(&:size).size, 80].max
102
107
  [ret, Rake.application.terminal_width].min
@@ -24,7 +24,7 @@ module Squared
24
24
  else
25
25
  return Kernel.send(name, *args, **kwargs)
26
26
  end
27
- return ret if ret || !e
27
+ return ret unless e && !ret && name == :system
28
28
 
29
29
  raise $?.to_s
30
30
  end
@@ -41,8 +41,8 @@ module Squared
41
41
  attr_reader :main, :name, :project, :theme
42
42
  attr_accessor :pipe
43
43
 
44
- def initialize(main, name = nil, project: nil, prefix: nil, command: nil, dump: nil, opts: {}, auto: true,
45
- common: ARG[:COMMON], pipe: ARG[:PIPE], **)
44
+ def initialize(main, name = nil, project: nil, command: nil, opts: {}, auto: true,
45
+ common: ARG[:COMMON], pipe: ARG[:PIPE], **kwargs)
46
46
  if project && (project.respond_to?(:workspace) || (project = __get__(:project)[project.to_s]))
47
47
  main = project.basepath(main).to_s
48
48
  @project = project
@@ -50,9 +50,9 @@ module Squared
50
50
  @required = true
51
51
  end
52
52
  @name = name || @project&.name
53
- @prefix = prefix unless @project
53
+ @prefix = kwargs[:prefix] unless @project
54
54
  @ext = File.extname(main)
55
- @dump = dump
55
+ @dump = kwargs[:dump]
56
56
  @mime = {}
57
57
  @theme = common ? __get__(:theme)[:viewer] : {}
58
58
  @pipe = env_pipe(pipe, @project ? @project.pipe : 1)
@@ -86,7 +86,7 @@ module Squared
86
86
  ['path not found', realpath]
87
87
  else
88
88
  @required = true
89
- project ? [project, 'not found'] : ['name', 'missing']
89
+ project ? [project, 'not found'] : %w[name missing]
90
90
  end
91
91
  warn log_message(Logger::WARN, msg, subject: self.class, hint: hint)
92
92
  end
@@ -94,10 +94,10 @@ module Squared
94
94
  def build
95
95
  return unless enabled?
96
96
 
97
- namespace(ns = task_name(name)) do
97
+ namespace task_name(name) do |ns|
98
98
  @mime.each do |type, items|
99
99
  items.each do |command, file, opts|
100
- next if Rake::Task.task_defined?("#{ns}:#{command}:#{type}")
100
+ next if Rake::Task.task_defined?("#{ns.scope.path}:#{command}:#{type}")
101
101
 
102
102
  namespace command do
103
103
  unless (data = @@mime_obj[type])
@@ -196,7 +196,7 @@ module Squared
196
196
  private
197
197
 
198
198
  def puts(*args)
199
- puts_oe(*args, pipe: pipe)
199
+ log_console(*args, pipe: pipe)
200
200
  end
201
201
 
202
202
  def log
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- VERSION = '0.5.4'
4
+ VERSION = '0.5.5'
5
5
  end
@@ -95,9 +95,15 @@ module Squared
95
95
  @kind = Support.hashlist
96
96
  @extensions = []
97
97
  @envname = env_key(@main).freeze
98
- @pipe = env_pipe(pipe, (ARG[:OUT] && env(ARG[:OUT])) || 1, root: @home)
98
+ @pipe = $DEBUG ? 2 : env_pipe(pipe, (ARG[:OUT] && env(ARG[:OUT])) || 1, root: @home)
99
99
  @exception = env_bool exception
100
- @verbose = env_bool(verbose, verbose.nil? || verbose.is_a?(String) ? @pipe != 0 : verbose, index: true)
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
101
107
  @warning = @verbose != false
102
108
  @closed = false
103
109
  if common
@@ -382,7 +388,15 @@ module Squared
382
388
  end
383
389
  end
384
390
  end
385
- apply_style(data || theme, obj, args, empty: empty) unless target && !data
391
+ if obj.is_a?(String)
392
+ begin
393
+ obj = JSON.parse(homepath(obj).read, { symbolize_names: true })
394
+ rescue StandardError => e
395
+ warn log_message(Logger::ERROR, e)
396
+ obj = nil
397
+ end
398
+ end
399
+ apply_style(data || theme, obj, args, empty: empty) if obj && (!target || data)
386
400
  self
387
401
  end
388
402
 
@@ -418,7 +432,7 @@ module Squared
418
432
  ret << proj if proj
419
433
  end
420
434
  end
421
- return (group || ref ? ret : ret[0]) unless block_given?
435
+ return (group || ref ? ret : ret.first) unless block_given?
422
436
 
423
437
  ret.each(&blk)
424
438
  self
@@ -673,6 +687,10 @@ module Squared
673
687
  { exception: exception, warning: warning }
674
688
  end
675
689
 
690
+ def size
691
+ @project.size
692
+ end
693
+
676
694
  def to_s
677
695
  (home? ? home : root).to_s
678
696
  end
@@ -795,8 +813,8 @@ module Squared
795
813
  format_desc key
796
814
  task key do
797
815
  unless failed.empty? && group.empty?
798
- puts log_message(Logger::ERROR, *(failed + group.map { |val| val.action }.flatten),
799
- subject: 'failed placement', hint: false)
816
+ puts(log_message(Logger::ERROR, *(failed + group.map { |val| val.action }.flatten),
817
+ subject: 'failed placement', hint: false), pipe: 2)
800
818
  end
801
819
  cols = level.flatten(1).map(&:size).max
802
820
  level.each_with_index do |grp, n|
@@ -812,8 +830,8 @@ module Squared
812
830
  end
813
831
  end
814
832
 
815
- def puts(*args)
816
- puts_oe(*args, pipe: pipe)
833
+ def puts(*args, **kwargs)
834
+ log_console(*args, pipe: kwargs[:pipe] || pipe)
817
835
  end
818
836
 
819
837
  def script_command(task, val, group, ref, on, &blk)
@@ -19,9 +19,9 @@ module Squared
19
19
  include ::Comparable
20
20
 
21
21
  VAR_SET = %i[parent global script index envname desc dependfile dependindex theme archive env dev prod graph
22
- pass only exclude].freeze
22
+ pass only exclude asdf].freeze
23
23
  BLK_SET = %i[run depend doc lint test copy clean].freeze
24
- SEM_VER = /\b(\d+)(?:(\.)(\d+))?(?:(\.)(\d+))?-?(\S+)?\b/.freeze
24
+ SEM_VER = /\b(\d+)(?:(\.)(\d+))?(?:(\.)(\d+))?[-.]?(\S+)?\b/.freeze
25
25
  URI_SCHEME = %r{\A([a-z][a-z\d+-.]*)://[^@:\[\]\\^<>|\s]}i.freeze
26
26
  TASK_METADATA = Rake::TaskManager.record_task_metadata
27
27
  private_constant :VAR_SET, :BLK_SET, :SEM_VER, :URI_SCHEME, :TASK_METADATA
@@ -66,11 +66,17 @@ module Squared
66
66
 
67
67
  @@tasks = {}
68
68
  @@graph = { _: [] }
69
+ @@asdf = if File.exist?("#{Dir.home}/.asdf/asdf.sh")
70
+ [Pathname.new("#{Dir.home}/.asdf"), 15]
71
+ elsif ENV['ASDF_DATA_DIR']
72
+ [Pathname.new(ENV['ASDF_DATA_DIR']), 16]
73
+ end
69
74
  @@print_order = 0
70
75
 
71
76
  subtasks({
72
77
  'graph' => %i[run print].freeze,
73
- 'unpack' => %i[zip tar gem ext].freeze
78
+ 'unpack' => %i[zip tar gem ext].freeze,
79
+ 'asdf' => %i[set exec current]
74
80
  })
75
81
 
76
82
  attr_reader :name, :project, :workspace, :path, :theme, :group, :parent, :dependfile,
@@ -114,6 +120,7 @@ module Squared
114
120
  only_set kwargs[:only]
115
121
  exclude_set kwargs[:exclude]
116
122
  archive_set kwargs[:archive]
123
+ asdf_set kwargs[:asdf]
117
124
  theme_set common
118
125
  initialize_ref Base.ref
119
126
  end
@@ -318,7 +325,7 @@ module Squared
318
325
  flags.each do |flag|
319
326
  case action
320
327
  when 'graph'
321
- next unless graph?
328
+ break unless graph?
322
329
 
323
330
  format_desc action, flag, '(-)project*'
324
331
  task flag do |_, args|
@@ -369,6 +376,32 @@ module Squared
369
376
  end
370
377
  unpack(path + dir, uri: tag, digest: digest, ext: ext, force: force)
371
378
  end
379
+ when 'asdf'
380
+ break unless @asdf
381
+
382
+ case flag
383
+ when :set
384
+ format_desc action, flag, 'version,opts*=u|home,p|parent'
385
+ task flag, [:version] do |_, args|
386
+ args = if (version = args.version)
387
+ args.extras
388
+ else
389
+ version, opts = choice_index('Select a version',
390
+ @asdf[1].children.map(&:basename).push('latest'),
391
+ force: true, accept: [['Confirm?', false, true]],
392
+ values: ['Options'])
393
+ OptionPartition.strip(opts)
394
+ end
395
+ asdf(flag, args, version: version)
396
+ end
397
+ else
398
+ format_desc(action, flag, flag == :exec ? 'command' : nil)
399
+ task flag do |_, args|
400
+ args = args.to_a
401
+ args << readline('Enter command', force: true) if args.empty? && flag == :exec
402
+ asdf flag, args
403
+ end
404
+ end
372
405
  end
373
406
  end
374
407
  end
@@ -451,13 +484,13 @@ module Squared
451
484
  if args.first.is_a?(Struct)
452
485
  f, blk = args.first.to_a
453
486
  args[0] = instance_eval(&blk) || f
454
- return unless args[0]
487
+ return unless args.first
455
488
  end
456
489
  if args.all? { |val| val.is_a?(Array) }
457
490
  cmd = []
458
491
  var = {}
459
492
  args.each do |val|
460
- next instance_exec(*val[1..-1], &val[0]) if val.first.is_a?(Proc)
493
+ next instance_exec(*val[1..-1], &val.first) if val.first.is_a?(Proc)
461
494
 
462
495
  a, b, c, d, e = val
463
496
  case b
@@ -761,6 +794,33 @@ module Squared
761
794
  end
762
795
  end
763
796
 
797
+ def asdf(flag, opts = [], version: nil)
798
+ return unless @asdf
799
+
800
+ cmd = session 'asdf', flag
801
+ name = @asdf.first
802
+ legacy = @@asdf[1] == 15
803
+ case flag
804
+ when :set
805
+ u = has_value?(opts, %w[u home])
806
+ cmd << if legacy
807
+ cmd.delete(flag)
808
+ u ? 'global' : 'local'
809
+ elsif has_value?(opts, %w[p parent])
810
+ '--parent'
811
+ elsif u
812
+ '--home'
813
+ end
814
+ cmd << name << version
815
+ when :exec
816
+ cmd.merge(opts)
817
+ when :current
818
+ cmd << '--no-header' unless legacy
819
+ cmd << name
820
+ end
821
+ print_success if success?(run(from: :"asdf:#{flag}")) && flag == :set
822
+ end
823
+
764
824
  def first(key, *args, **kwargs, &blk)
765
825
  event(:first, key, *args, **kwargs, &blk)
766
826
  end
@@ -836,6 +896,8 @@ module Squared
836
896
  parent_set val
837
897
  when :archive
838
898
  archive_set val
899
+ when :asdf
900
+ asdf_set val
839
901
  when :run
840
902
  run_set(*args, **kwargs)
841
903
  when :script
@@ -945,7 +1007,7 @@ module Squared
945
1007
  def log
946
1008
  return @log unless @log.is_a?(Array)
947
1009
 
948
- @log = Logger.new(enabled? ? @log[0] : nil, **@log[1])
1010
+ @log = Logger.new(enabled? ? @log.first : nil, **@log.last)
949
1011
  end
950
1012
 
951
1013
  def allref
@@ -986,8 +1048,8 @@ module Squared
986
1048
 
987
1049
  private
988
1050
 
989
- def puts(*args)
990
- puts_oe(*args, pipe: pipe)
1051
+ def puts(*args, **kwargs)
1052
+ log_console(*args, pipe: kwargs[:pipe] || pipe)
991
1053
  end
992
1054
 
993
1055
  def run(cmd = @session, var = nil, exception: self.exception, sync: true, from: nil, banner: true, chdir: path,
@@ -996,10 +1058,8 @@ module Squared
996
1058
  warn log_message(Logger::WARN, 'no command given', subject: project, hint: from || 'unknown', pass: true)
997
1059
  return
998
1060
  end
999
- i = interactive && !(@session && option('y'))
1000
1061
  cmd = cmd.target if cmd.is_a?(OptionPartition)
1001
- cmd = session_done cmd
1002
- if i
1062
+ if interactive && (!@session || !option('y'))
1003
1063
  title, y = case interactive
1004
1064
  when Array
1005
1065
  interactive
@@ -1008,8 +1068,9 @@ module Squared
1008
1068
  else
1009
1069
  ['Run', 'Y']
1010
1070
  end
1011
- exit 1 unless confirm("#{title}? [#{sub_style(cmd, styles: theme[:inline])}]", y)
1071
+ exit 1 unless confirm("#{title}? [#{sub_style(cmd.to_s, styles: theme[:inline])}]", y)
1012
1072
  end
1073
+ cmd = session_done cmd
1013
1074
  log&.info cmd
1014
1075
  on :first, from
1015
1076
  begin
@@ -1226,12 +1287,12 @@ module Squared
1226
1287
  end
1227
1288
 
1228
1289
  def session(*cmd, prefix: cmd.first, main: true, path: true, options: true)
1229
- prefix = stripext(prefix.to_s).upcase
1230
- if path && (val = ENV["PATH_#{prefix}"] || PATH[prefix] || PATH[prefix.to_sym])
1290
+ prefix = stripext prefix.to_s
1291
+ if path && (val = shell_bin(prefix))
1231
1292
  cmd[0] = shell_quote(val, force: false)
1232
1293
  end
1233
1294
  ret = JoinSet.new(cmd.flatten(1))
1234
- if options && (val = env("#{prefix}_OPTIONS"))
1295
+ if options && (val = env("#{prefix.upcase}_OPTIONS"))
1235
1296
  split_escape(val).each { |opt| ret.last(fill_option(opt), /\A(--?[^ =]+)[ =].+\z/m) }
1236
1297
  end
1237
1298
  main ? @session = ret : ret
@@ -1410,9 +1471,9 @@ module Squared
1410
1471
  unless items.empty?
1411
1472
  pad = items.size.to_s.size
1412
1473
  items.each_with_index do |val, i|
1413
- next unless matchany?(val[0], reg)
1474
+ next unless matchany?(val.first, reg)
1414
1475
 
1415
- out << ('%*d. %s' % [pad, i.succ, each ? each.call(val) : val[0]])
1476
+ out << ('%*d. %s' % [pad, i.succ, each ? each.call(val) : val.first])
1416
1477
  end
1417
1478
  end
1418
1479
  sub = [headerstyle]
@@ -1760,7 +1821,14 @@ module Squared
1760
1821
  return -1 if b.empty?
1761
1822
  return 1 if a.empty?
1762
1823
 
1763
- a, b = [a.first, b.first].map! { |c| [c[0], c[2], c[4] || '0', c[5] ? '-1' : '0'] }
1824
+ a, b = [a.first, b.first].map! do |c|
1825
+ begin
1826
+ d = Integer(c[5]).to_s
1827
+ rescue StandardError
1828
+ d = c[5] ? '-1' : '0'
1829
+ end
1830
+ [c[0], c[2], c[4] || '0', d]
1831
+ end
1764
1832
  a.each_with_index do |c, index|
1765
1833
  next if c == (d = b[index])
1766
1834
 
@@ -1952,6 +2020,13 @@ module Squared
1952
2020
  end
1953
2021
  end
1954
2022
 
2023
+ def asdf_set(val)
2024
+ @asdf = if @@asdf && val
2025
+ dir = @@asdf[0].join('installs', val)
2026
+ [val, dir] if dir.exist? && !dir.empty?
2027
+ end
2028
+ end
2029
+
1955
2030
  def theme_set(common)
1956
2031
  @theme = if !verbose
1957
2032
  {}
@@ -147,7 +147,7 @@ module Squared
147
147
  end
148
148
  end
149
149
  when 'bake'
150
- next unless bake?
150
+ break unless bake?
151
151
 
152
152
  case flag
153
153
  when :build
@@ -168,7 +168,7 @@ module Squared
168
168
  end
169
169
  end
170
170
  when 'compose'
171
- next unless compose?
171
+ break unless compose?
172
172
 
173
173
  case flag
174
174
  when :build, :up