squared 0.0.7 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'json'
4
+
3
5
  module Squared
4
6
  module Workspace
5
7
  module Project
@@ -25,31 +27,44 @@ module Squared
25
27
  end
26
28
 
27
29
  @@tasks[ref] = {
28
- install: %i[force dedupe frozen],
30
+ install: %i[force frozen dedupe],
29
31
  outdated: %i[major minor patch],
30
32
  run: nil
31
33
  }.freeze
32
34
 
33
35
  attr_reader :package
34
36
 
35
- def initialize(*, **kwargs)
37
+ def initialize(*, script: nil, **kwargs)
36
38
  super
37
39
  initialize_script(Node.ref, **kwargs)
38
- if (opts = env('BUILD', strict: true))
39
- raise_error("BUILD_#{@name.upcase}", opts) if @output[0].is_a?(::Array)
40
- @output[1] = opts
41
- else
42
- @output[1] = (@script && @script[:run]) || @workspace.script
43
- end
44
40
  @dev = kwargs[:dev]
45
41
  @prod = kwargs[:prod]
46
42
  @pm = {}
47
43
  @package = base_path('package.json')
44
+ return if @output[0] == false
45
+
46
+ if @output[0].nil?
47
+ val, ext = @workspace.script(Node.ref, @group)
48
+ script_set val
49
+ unless ext
50
+ if script
51
+ script_set script
52
+ elsif (val = @script && @script[:run])
53
+ @output[0] = val
54
+ @output[1] = nil
55
+ end
56
+ end
57
+ end
58
+ @output[@output[0] || val.include?(' ') ? 0 : 1] = val if (val = env('BUILD', strict: true))
59
+ end
60
+
61
+ def ref
62
+ Node.ref
48
63
  end
49
64
 
50
65
  def populate(*)
51
66
  super
52
- return unless outdated? && !@exclude.include?(Node.ref)
67
+ return unless outdated? && ref?(Node.ref)
53
68
 
54
69
  namespace name do
55
70
  @@tasks[Node.ref].each do |action, flags|
@@ -70,10 +85,10 @@ module Squared
70
85
  when :install
71
86
  desc format_desc(action, flag)
72
87
  task flag do
73
- depend(flag, override: true)
88
+ depend(flag)
74
89
  end
75
90
  when :outdated
76
- desc format_desc(action, flag, %w[prune interactive dry-run], req: 'opts?')
91
+ desc format_desc(action, flag, %w[prune interactive dry-run], arg: 'opts?')
77
92
  task flag, [:opts] do |_, args|
78
93
  outdated(flag, opts: args.to_a)
79
94
  end
@@ -95,32 +110,35 @@ module Squared
95
110
  into = @copy[:into] if @copy.key?(:into)
96
111
  also = @copy[:also] if @copy.key?(:also)
97
112
  end
98
- items = [project == workspace.main ? nil : workspace.home].concat(as_a(also))
113
+ items = [name == workspace.main ? nil : workspace.home].concat(as_a(also))
99
114
  items.each_with_index do |dir, i|
100
115
  if i == 0
101
116
  next unless dev? & !doc?
102
117
 
103
118
  dest = dir
104
- elsif dir.is_a?(::String)
105
- dest = workspace.root_path(dir)
106
- elsif dir.is_a?(::Symbol)
107
- dest = Workspace.resolve(dir)&.path
108
- elsif dir.is_a?(Project)
109
- dest = dir.path
110
- elsif dir.is_a?(::Hash)
111
- into = nil
112
- glob = nil
113
- dir.each do |key, val|
114
- case key.to_sym
115
- when :from
116
- from = val
117
- when :glob
118
- glob = val
119
- when :subdir
120
- subdir = val
121
- when :into
122
- into = val
119
+ else
120
+ case dir
121
+ when ::String
122
+ dest = workspace.root_path(dir)
123
+ when ::Symbol
124
+ dest = Workspace.resolve(dir)&.path
125
+ when ::Hash
126
+ into = nil
127
+ glob = nil
128
+ dir.each do |key, val|
129
+ case key.to_sym
130
+ when :from
131
+ from = val
132
+ when :glob
133
+ glob = val
134
+ when :subdir
135
+ subdir = val
136
+ when :into
137
+ into = val
138
+ end
123
139
  end
140
+ when Project::Base
141
+ dest = dir.path
124
142
  end
125
143
  next unless into
126
144
  end
@@ -133,57 +151,55 @@ module Squared
133
151
  end
134
152
  end
135
153
 
136
- def depend(flag = nil, override: false)
137
- if @depend && !override
154
+ def depend(flag = nil)
155
+ if @depend && !flag
138
156
  super
139
157
  elsif outdated?
140
- frozen = flag == :frozen
141
- force = flag == :force
142
- dedupe = flag == :dedupe
143
158
  if (yarn = install_type(:yarn)) > 0
144
159
  cmd = session 'yarn'
145
160
  if yarn > 1
146
- if dedupe
161
+ if flag == :dedupe
147
162
  cmd << 'dedupe'
148
163
  else
149
- cmd << 'install'
150
- if force
151
- cmd << '--check-cache'
152
- elsif frozen
153
- cmd << '--immutable'
154
- end
164
+ cmd << 'install' << if flag == :force
165
+ '--check-cache'
166
+ elsif flag == :frozen
167
+ '--immutable'
168
+ end
155
169
  end
156
170
  else
157
- cmd << 'install'
158
- if force
159
- cmd << '--force'
160
- elsif frozen
161
- cmd << '--frozen-lockfile'
162
- end
171
+ cmd << 'install' << if flag == :force
172
+ '--force'
173
+ elsif flag == :frozen
174
+ '--frozen-lockfile'
175
+ end
163
176
  cmd << '--production' if prod?
164
177
  cmd << '--ignore-engines' unless env('YARN_IGNORE_ENGINES', equals: '0')
165
178
  end
166
179
  elsif pnpm?
167
180
  cmd = session 'pnpm'
168
- if dedupe
181
+ if flag == :dedupe
169
182
  cmd << 'dedupe'
170
183
  else
171
- cmd << 'install'
172
- if force
173
- cmd << '--force'
174
- elsif frozen
175
- cmd << '--frozen-lockfile'
176
- end
184
+ cmd << 'install' << if flag == :force
185
+ '--force'
186
+ elsif flag == :frozen
187
+ '--frozen-lockfile'
188
+ end
177
189
  end
178
190
  cmd << '--prod' if prod?
179
191
  cmd << '--ignore-workspace' if env('NODE_WORKSPACES', equals: '0')
180
192
  append_nocolor
181
193
  else
182
- cmd = session 'npm', dedupe ? 'dedupe' : 'install'
183
- if force
184
- cmd << '--force'
185
- elsif frozen
186
- cmd << '--package-lock-only'
194
+ cmd = session 'npm'
195
+ if flag == :dedupe
196
+ cmd << 'dedupe'
197
+ else
198
+ cmd << 'install' << if flag == :force
199
+ '--force'
200
+ elsif flag == :frozen
201
+ '--package-lock-only'
202
+ end
187
203
  end
188
204
  cmd << '--omit=dev' if prod?
189
205
  cmd << '--workspaces=false' if env('NODE_WORKSPACES', equals: '0')
@@ -191,16 +207,15 @@ module Squared
191
207
  append_nocolor
192
208
  end
193
209
  append_loglevel
194
- run(sync: invoked_sync?('depend'))
210
+ run(sync: invoked_sync?('depend', flag))
195
211
  end
196
212
  end
197
213
 
198
214
  def outdated(rev = nil, opts: [])
199
- require 'json'
200
215
  equ = rev || (prod? ? :patch : :minor)
201
216
  cmd = pnpm? ? 'pnpm outdated' : 'npm outdated'
202
217
  log.info cmd
203
- if store_pwd || invoked_sync?("outdated#{rev ? ":#{rev}" : ''}")
218
+ if store_pwd || invoked_sync?("outdated#{rev && ":#{rev}"}")
204
219
  print_item format_banner("#{cmd}#{opts.include?('dry-run') ? ' --dry-run' : ''}", multiple: true)
205
220
  end
206
221
  data = `#{cmd} --json --loglevel=error`
@@ -300,10 +315,10 @@ module Squared
300
315
  elsif modified == cur
301
316
  'FAIL'
302
317
  elsif d == 1
303
- a = sub_style(a, styles: theme[:active])
318
+ a = sub_style(a, styles: theme[:major])
304
319
  sub_style(c, :green, :bold)
305
320
  else
306
- sub_style(c, :green, :bold, pat: SEM_VER, index: d)
321
+ sub_style(c, :green, pat: SEM_VER, index: d)
307
322
  end
308
323
  puts "#{pad_ord.(i, found)}. #{a}#{b.ljust(col2)}#{c}"
309
324
  end
@@ -339,10 +354,6 @@ module Squared
339
354
  end
340
355
  end
341
356
 
342
- def run_script(cmd)
343
- cmd.each { |val| run_s compose(val) }
344
- end
345
-
346
357
  def compose(args)
347
358
  args ||= @output[1]
348
359
  cmd = [if yarn?
@@ -394,13 +405,10 @@ module Squared
394
405
  else
395
406
  1
396
407
  end
408
+ elsif (ver = read_packagemanager || env('NODE_INSTALL')) && ver.start_with?('yarn')
409
+ ver == 'yarn' || ver.include?('@1') ? 1 : 3
397
410
  else
398
- ver = env('NODE_INSTALL')
399
- if ver&.start_with?('yarn')
400
- ver.include?('@1') ? 1 : 3
401
- else
402
- 0
403
- end
411
+ 0
404
412
  end) > 0
405
413
  end
406
414
 
@@ -421,19 +429,23 @@ module Squared
421
429
  4
422
430
  end
423
431
  else
424
- env('NODE_INSTALL')&.start_with?('pnpm') ? 4 : 0
432
+ (read_packagemanager || env('NODE_INSTALL'))&.start_with?('pnpm') ? 4 : 0
425
433
  end) > 0
426
434
  end
427
435
 
428
436
  def dev?
429
- !Node.prod? && workspace.dev?(script: @output[1], pat: @dev, **runargs)
437
+ !Node.prod? && workspace.dev?(pat: @dev, **runargs)
430
438
  end
431
439
 
432
440
  def prod?
433
- Node.prod? || workspace.prod?(script: @output[1], pat: @prod, **runargs)
441
+ Node.prod? || workspace.prod?(pat: @prod, **runargs)
434
442
  end
435
443
 
436
- protected
444
+ private
445
+
446
+ def run_script(cmd)
447
+ cmd.each { |val| run_s compose(val) }
448
+ end
437
449
 
438
450
  def append_loglevel(cmd = @session)
439
451
  return unless (level = env('NODE_LOGLEVEL'))
@@ -466,16 +478,35 @@ module Squared
466
478
  end
467
479
  end
468
480
 
469
- private
470
-
471
481
  def confirm_outdated(rev, pkg, ver)
472
482
  m = ver == :major
473
483
  confirm("Upgrade to #{rev}? #{sub_style("#{pkg} #{ver}", styles: theme[:inline])} [#{m ? 'y/N' : 'Y/n'}] ",
474
484
  default: m ? 'N' : 'Y', timeout: 60)
475
485
  end
476
486
 
487
+ def read_packagemanager
488
+ if @pm[:_].nil?
489
+ doc = JSON.parse(package.read)
490
+ @pm[:_] = (val = doc['packageManager']) ? val[0..(val.index('+') || 0) - 1] : false
491
+ end
492
+ rescue StandardError => e
493
+ log.warn e if package.exist?
494
+ @pm[:_] = false
495
+ nil
496
+ else
497
+ @pm[:_] || nil
498
+ end
499
+
500
+ def script_set(val)
501
+ @output[1] = if val.is_a?(::Array)
502
+ val[Node.prod? ? 1 : 0]
503
+ else
504
+ val
505
+ end
506
+ end
507
+
477
508
  def runargs
478
- { ref: Node.ref, group: group, global: !@script || @script[:run].nil? }
509
+ { script: @output[1], ref: Node.ref, group: group, global: @output[0].nil? && !(@script && @script[:run]) }
479
510
  end
480
511
  end
481
512
  end
@@ -7,9 +7,8 @@ module Squared
7
7
  REQUIREMENTS = %w[requirements.txt pyproject.toml setup.py].freeze
8
8
  OPT_USER = %w[pre dry-run].freeze
9
9
  OPT_FORCE = [*OPT_USER, 'user'].freeze
10
- OPT_UPGRADE = [*OPT_FORCE, 'eager'].freeze
11
10
  OPT_GENERAL = %w{venv isolated no-cache-dir [v]erbose}.freeze
12
- private_constant :REQUIREMENTS, :OPT_USER, :OPT_FORCE, :OPT_UPGRADE, :OPT_GENERAL
11
+ private_constant :REQUIREMENTS, :OPT_USER, :OPT_FORCE, :OPT_GENERAL
13
12
 
14
13
  class << self
15
14
  def populate(*); end
@@ -42,12 +41,16 @@ module Squared
42
41
  end
43
42
 
44
43
  @@tasks[ref] = {
45
- install: %i[user upgrade force]
44
+ install: %i[user target upgrade force]
46
45
  }.freeze
47
46
 
47
+ def ref
48
+ Python.ref
49
+ end
50
+
48
51
  def populate(*)
49
52
  super
50
- return unless outdated? && !@exclude.include?(Python.ref)
53
+ return unless outdated? && ref?(Python.ref)
51
54
 
52
55
  namespace name do
53
56
  @@tasks[Python.ref].each do |action, flags|
@@ -56,16 +59,26 @@ module Squared
56
59
  case action
57
60
  when :install
58
61
  list = case flag
62
+ when :target
63
+ req = 'dir'
64
+ OPT_USER + %w[upgrade eager]
59
65
  when :upgrade
60
- OPT_UPGRADE
66
+ OPT_FORCE + ['eager']
61
67
  when :force
62
68
  OPT_FORCE
63
69
  else
64
70
  OPT_USER
65
71
  end
66
- desc format_desc(action, flag, list + OPT_GENERAL)
67
- task flag do |_, args|
68
- depend(flag, opts: args.to_a, override: true)
72
+ desc format_desc(action, flag, list + OPT_GENERAL, req: req)
73
+ if flag == :target
74
+ task flag, [:dir, :opts] do |_, args|
75
+ guard_params(action, flag, args: args, key: :dir)
76
+ depend(flag, dir: args.dir, opts: args.to_a)
77
+ end
78
+ else
79
+ task flag do |_, args|
80
+ depend(flag, opts: args.to_a)
81
+ end
69
82
  end
70
83
  end
71
84
  end
@@ -74,33 +87,42 @@ module Squared
74
87
  end
75
88
  end
76
89
 
77
- def depend(flag = nil, opts: [], override: false)
78
- if @depend && !override
90
+ def depend(flag = nil, dir: nil, opts: [])
91
+ if @depend && !flag
79
92
  super
80
93
  elsif outdated?
94
+ sync = invoked_sync?('depend', flag)
81
95
  case (type = install_type)
82
96
  when 1, 2
83
97
  cmd = pip_session 'install'
84
98
  case flag
85
99
  when :user
86
100
  cmd << '--user'
87
- append_general opts, OPT_USER
101
+ append_pip opts, OPT_USER
102
+ when :target
103
+ cmd << "--target=#{shell_escape(base_path(dir).to_s, quote: true)}"
104
+ append_pip opts, OPT_USER + ['upgrade']
105
+ append_eager opts
88
106
  when :upgrade
89
107
  cmd << '--upgrade'
90
- append_general opts, OPT_UPGRADE
108
+ append_pip opts, OPT_FORCE
109
+ append_eager opts
91
110
  when :force
92
111
  cmd << '--force-reinstall'
93
- append_general opts, OPT_FORCE
112
+ append_pip opts, OPT_FORCE
94
113
  end
95
114
  cmd << (type == 1 ? '-r requirements.txt' : '.')
96
- run(sync: invoked_sync?('depend'))
115
+ run(sync: sync)
97
116
  when 3
98
- run_s("#{@bin} setup.py install", sync: invoked_sync?('depend'))
117
+ run_s("#{@bin} setup.py install", sync: sync)
99
118
  end
100
119
  end
101
120
  end
102
121
 
103
- def outdated(*); end
122
+ def outdated(*)
123
+ pip_session 'list', '--outdated'
124
+ run
125
+ end
104
126
 
105
127
  def install_type(*)
106
128
  requirements.exist? ? @reqindex + 1 : 0
@@ -116,14 +138,11 @@ module Squared
116
138
  session('pip', *cmd)
117
139
  end
118
140
 
119
- def append_general(opts, list = [])
120
- list += OPT_GENERAL
141
+ def append_pip(opts, list = [])
121
142
  opts.each do |opt|
122
- next unless (v = opt.match(/^v+$/)) || list.include?(opt)
143
+ next unless list.include?(opt) || OPT_GENERAL.include?(opt) || (v = opt.match(/^v+$/))
123
144
 
124
145
  @session << case opt
125
- when 'eager'
126
- '--upgrade-strategy=eager'
127
146
  when 'venv'
128
147
  '--require-virtualenv'
129
148
  else
@@ -135,8 +154,15 @@ module Squared
135
154
  if (val = env('PIP_PROXY'))
136
155
  @session << "--proxy=#{shell_escape(val, quote: true)}"
137
156
  end
157
+ if (val = env('PIP_LOG'))
158
+ @session << "--log=#{shell_escape(base_path(val).to_s, quote: true)}"
159
+ end
138
160
  append_nocolor
139
161
  end
162
+
163
+ def append_eager(opts)
164
+ @session << '--upgrade-strategy=eager' if opts.include?('eager')
165
+ end
140
166
  end
141
167
  end
142
168
  end
@@ -61,9 +61,13 @@ module Squared
61
61
  end
62
62
  end
63
63
 
64
+ def ref
65
+ Ruby.ref
66
+ end
67
+
64
68
  def populate(*)
65
69
  super
66
- return unless outdated? && !@exclude.include?(Ruby.ref)
70
+ return unless outdated? && ref?(Ruby.ref)
67
71
 
68
72
  namespace name do
69
73
  @@tasks[Ruby.ref].each do |action, flags|
@@ -93,19 +97,19 @@ module Squared
93
97
  task flag, [:name] do |_, args|
94
98
  name = args.to_a
95
99
  guard_params(action, flag, args: name)
96
- depend(flag, opts: name, override: true)
100
+ depend(flag, opts: name)
97
101
  end
98
102
  when :with, :without
99
103
  desc format_desc(action, flag, 'group+')
100
104
  task flag, [:group] do |_, args|
101
105
  group = args.to_a
102
106
  guard_params(action, flag, args: group)
103
- depend(flag, opts: group, override: true)
107
+ depend(flag, opts: group)
104
108
  end
105
109
  else
106
110
  desc format_desc(action, flag, OPT_INSTALL)
107
111
  task flag, [:opts] do |_, args|
108
- depend(flag, opts: args.to_a, override: true)
112
+ depend(flag, opts: args.to_a)
109
113
  end
110
114
  end
111
115
  when :update, :outdated
@@ -142,11 +146,17 @@ module Squared
142
146
  end
143
147
  end
144
148
 
145
- def depend(flag = nil, opts: [], override: false)
146
- if @depend && !override
149
+ def depend(flag = nil, opts: [])
150
+ if @depend && !flag
147
151
  super
148
152
  elsif outdated?
149
153
  case flag
154
+ when :gem
155
+ gem_session 'install'
156
+ append_value opts
157
+ when :with, :without
158
+ gem_session 'install'
159
+ append_repeat flag, opts
150
160
  when :redownload, :local, :'prefer-local'
151
161
  cmd = bundle_session 'install', "--#{flag}"
152
162
  if (val = env('BUNDLE_TRUST_POLICY'))
@@ -166,17 +176,11 @@ module Squared
166
176
  end}"
167
177
  end
168
178
  append_bundle opts, OPT_INSTALL
169
- when :gem
170
- gem_session 'install'
171
- append_value opts
172
- when :with, :without
173
- gem_session 'install'
174
- append_repeat flag, opts
175
179
  else
176
180
  bundle_session 'install'
177
181
  append_bundle
178
182
  end
179
- run_rb
183
+ run_rb(sync: invoked_sync?('depend', flag))
180
184
  end
181
185
  end
182
186
 
@@ -207,7 +211,7 @@ module Squared
207
211
  append_bundle opts, OPT_OUTDATED
208
212
  cmd = cmd.done
209
213
  log.info cmd
210
- print_item format_banner(cmd) if store_pwd || invoked_sync?("outdated#{rev ? ":#{rev}" : ''}")
214
+ print_item format_banner(cmd) if store_pwd || invoked_sync?("outdated#{rev && ":#{rev}"}")
211
215
  start = 0
212
216
  found = 0
213
217
  major = 0
@@ -224,41 +228,29 @@ module Squared
224
228
  l = lat.join
225
229
  styles = []
226
230
  data.each do |val|
227
- break unless styles && (req = /(>?=|~>|!=|<=?) (#{Regexp.escape(val.join)})/.match(line))
231
+ break unless (req = /(>=?|=|~>|!=|<=?) (#{Regexp.escape(val.join)})/.match(line))
228
232
 
229
233
  v = semver(val).join
230
234
  case req[1]
231
235
  when '>', '>='
232
- if c == l
233
- styles = nil
234
- elsif l < v
235
- styles = %i[red underline]
236
- break
237
- elsif semmajor(cur, lat)
238
- styles = %i[green underline]
239
- major += 1
240
- elsif cur[3] != lat[3]
236
+ if semmajor(cur, lat)
241
237
  styles = %i[green bold]
238
+ major += 1
242
239
  else
243
- styles[0] = :green
240
+ styles[0] = cur[2] == lat[2] ? :yellow : :green
244
241
  end
245
242
  when '<', '<='
246
243
  if c <= v
247
244
  if semmajor(cur, lat)
248
- styles = %i[green underline]
245
+ styles = %i[green bold]
249
246
  major += 1
250
247
  else
251
248
  styles[0] = :yellow
252
249
  end
253
- elsif l >= v
254
- styles = nil
255
250
  end
256
251
  when '!='
257
- if c == v
258
- styles = %i[red underline]
259
- break
260
- elsif l == v
261
- styles = nil
252
+ if c == l
253
+ styles.clear
262
254
  else
263
255
  styles[1] = :bold
264
256
  end
@@ -266,27 +258,23 @@ module Squared
266
258
  if c < v && cur[0] == val[0] && !semmajor(cur, val)
267
259
  styles[0] = :yellow
268
260
  elsif semmajor(lat, val)
269
- styles = nil
270
- else
271
- styles[1] = :bold
272
- end
273
- when '='
274
- if c == v
275
261
  styles[1] = :underline
276
262
  else
277
- styles[0] = :red
263
+ styles[1] = :bold
278
264
  end
279
265
  end
280
266
  end
281
- next unless styles && !styles.empty?
282
-
283
- case styles[0]
284
- when :yellow, :green
285
- line = sub_style(line, pat: /^(\S+)(.+)$/, styles: theme[:active])
286
- found += 1
267
+ unless styles.empty?
268
+ case styles[0]
269
+ when :green
270
+ line = sub_style(line, pat: /^(\S+)(.+)$/, styles: theme[styles[1] == :bold ? :major : :active])
271
+ found += 1
272
+ when :yellow
273
+ found += 1
274
+ end
275
+ line = sub_style(line, pat: /^((?:\S+\s+){2})(#{Regexp.escape(l)})(.+)$/,
276
+ styles: styles.compact, index: 2)
287
277
  end
288
- line = sub_style(line, pat: /^((?:\S+\s+){2})(#{Regexp.escape(l)})(.+)$/,
289
- styles: styles.compact, index: 2)
290
278
  end
291
279
  end
292
280
  puts "#{start.to_s.rjust(2)}. #{line}"
@@ -393,8 +381,8 @@ module Squared
393
381
 
394
382
  private
395
383
 
396
- def run_rb
397
- run(banner: !@session.include?('--quiet'))
384
+ def run_rb(sync: true)
385
+ run(sync: sync, banner: !@session.include?('--quiet'))
398
386
  end
399
387
 
400
388
  def append_bundle(opts = nil, list = nil)
@@ -413,7 +401,7 @@ module Squared
413
401
  end
414
402
 
415
403
  def append_repeat(flag, opts)
416
- opts.each { |val| @session << "--#{flag}=#{shell_escape(val)}" }
404
+ opts.each { |val| @session << "--#{flag}=#{shell_escape(val, quote: true)}" }
417
405
  end
418
406
 
419
407
  def append_value(opts)