squared 0.0.5 → 0.0.6

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.
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- module Repo
4
+ module Workspace
5
5
  module Project
6
6
  class Git < Base
7
7
  include Format
@@ -13,10 +13,10 @@ module Squared
13
13
 
14
14
  class << self
15
15
  def populate(workspace, parallel: [], **)
16
- return if workspace.series[:pull].empty?
16
+ return if workspace.series.pull.empty?
17
17
 
18
18
  desc 'all[git?=rebase|stash]'
19
- task :all, [:git] do |_, args|
19
+ task 'all', [:git] do |_, args|
20
20
  sync = ->(key) { parallel.include?(key) ? :"#{key}:sync" : key }
21
21
  pull = case args.git
22
22
  when 'rebase'
@@ -48,7 +48,7 @@ module Squared
48
48
  @@tasks[REF] = {
49
49
  checkout: %i[branch detach force merge],
50
50
  commit: %i[add amend amend-orig all no-all],
51
- diff: %i[head cached branch],
51
+ diff: %i[head cached branch files],
52
52
  fetch: %i[all submodules unshallow],
53
53
  files: %i[cached modified deleted others],
54
54
  pull: %i[head rebase no-rebase commit no-commit submodules],
@@ -84,7 +84,7 @@ module Squared
84
84
  if flag == :all
85
85
  desc format_desc(action, flag, 'message?')
86
86
  task flag, [:message] do |_, args|
87
- commit(:all, message: args.fetch(:message, nil))
87
+ commit(flag, message: args.fetch(:message, nil))
88
88
  end
89
89
  else
90
90
  desc format_desc(action, flag, 'pathspec+')
@@ -96,8 +96,8 @@ module Squared
96
96
  when :stash
97
97
  if flag == :push
98
98
  desc format_desc(action, flag, 'pathspec*')
99
- task :push, [:pathspec] do |_, args|
100
- stash(:push, collect_args(args, :pathspec))
99
+ task flag, [:pathspec] do |_, args|
100
+ stash(flag, collect_args(args, :pathspec))
101
101
  end
102
102
  else
103
103
  desc format_desc(action, flag, 'commit?')
@@ -122,18 +122,25 @@ module Squared
122
122
  task flag, [:pathspec] do |_, args|
123
123
  files = collect_args(args, :pathspec)
124
124
  index = /^\d+$/.match?(files.first) && !option('index') ? files.shift.to_i : 0
125
- diff(:head, files, index: index)
125
+ diff(flag, files, index: index)
126
126
  end
127
127
  when :cached
128
128
  desc format_desc(action, flag, 'pathspec*')
129
129
  task flag, [:pathspec] do |_, args|
130
- diff(:cached, collect_args(args, :pathspec))
130
+ diff(flag, collect_args(args, :pathspec))
131
131
  end
132
132
  when :branch
133
133
  desc format_desc(action, flag, 'name,pathspec*')
134
134
  task flag, [:name, :pathspec] do |_, args|
135
135
  guard_params(action, flag, args: args, key: :name)
136
- diff(:branch, collect_args(args, :pathspec), branch: args.name)
136
+ diff(flag, collect_args(args, :pathspec), branch: args.name)
137
+ end
138
+ when :files
139
+ desc format_desc(action, flag, 'path1,path2')
140
+ task flag, [:path1, :path2] do |_, args|
141
+ guard_params(action, flag, args: args, key: :path1)
142
+ guard_params(action, flag, args: args, key: :path2)
143
+ diff(flag, [args.path1, args.path2])
137
144
  end
138
145
  end
139
146
  when :checkout
@@ -159,12 +166,12 @@ module Squared
159
166
  commit = args.commit
160
167
  end
161
168
  guard_params('checkout', :branch, args: args, key: :create, pat: /^[Bb]$/) if create
162
- checkout(:branch, branch: args.name, create: create, commit: commit, detach: detach)
169
+ checkout(flag, branch: args.name, create: create, commit: commit, detach: detach)
163
170
  end
164
171
  when :detach
165
172
  desc format_desc(action, flag, 'branch/commit?')
166
173
  task flag, [:commit] do |_, args|
167
- checkout(:detach, commit: args.commit)
174
+ checkout(flag, commit: args.commit)
168
175
  end
169
176
  else
170
177
  desc format_desc(action, flag, 'pathspec*')
@@ -177,7 +184,7 @@ module Squared
177
184
  desc format_desc(action, flag, 'ref?=HEAD,pathspec+')
178
185
  task flag, [:ref, :pathspec] do |_, args|
179
186
  guard_params(action, flag, args: args, key: :pathspec)
180
- reset(:head, collect_args(args, :pathspec), ref: args.ref)
187
+ reset(flag, collect_args(args, :pathspec), ref: args.ref)
181
188
  end
182
189
  else
183
190
  desc format_desc(action, flag, 'ref?=HEAD')
@@ -205,7 +212,7 @@ module Squared
205
212
  cmd << '--commit'
206
213
  end
207
214
  append_pull opts, OPT_PULL, flag
208
- source(sync: sync, stderr: true, exception: !workspace.multiple?)
215
+ source(sync: sync, stderr: true, exception: !workspace.series.multiple?)
209
216
  end
210
217
 
211
218
  def rebase
@@ -216,7 +223,7 @@ module Squared
216
223
  cmd = git_session 'fetch'
217
224
  cmd << '--all' if flag == :all || option('all')
218
225
  append_pull opts, OPT_FETCH, flag
219
- source(sync: invoked_sync?('fetch', flag), stderr: true, exception: !workspace.multiple?)
226
+ source(sync: invoked_sync?('fetch', flag), stderr: true, exception: !workspace.series.multiple?)
220
227
  end
221
228
 
222
229
  def stash(flag = :push, files = [], commit: nil)
@@ -224,10 +231,10 @@ module Squared
224
231
  case flag
225
232
  when :apply, :pop
226
233
  cmd << '--index' if option('index')
227
- cmd << commit if commit
234
+ cmd << commit
228
235
  else
229
- %w[all staged include-untracked].each { |val| cmd << "--#{val}" if option(val) }
230
- append_message option('message')
236
+ append_option %w[all staged include-untracked]
237
+ append_message option('message', 'm', zero: false)
231
238
  append_pathspec files
232
239
  end
233
240
  source(sync: invoked_sync?('stash', flag), exception: workspace.exception)
@@ -247,9 +254,7 @@ module Squared
247
254
  'all'
248
255
  end}"
249
256
  end
250
- if (val = option('pathspec'))
251
- append_pathspec split_escape(val)
252
- end
257
+ append_pathspec
253
258
  out, banner = source(io: true)
254
259
  if banner && invoked_sync?('status')
255
260
  print_item banner
@@ -290,17 +295,16 @@ module Squared
290
295
  when :branch
291
296
  cmd << '--detach' if detach == 'd' || option('detach')
292
297
  if create
293
- cmd << "-#{create} #{branch}"
298
+ cmd << "-#{create}" << branch
294
299
  if (val = option('start-point'))
295
300
  cmd << val
296
301
  end
297
302
  else
298
303
  cmd << branch
299
304
  end
300
- cmd << commit if commit
305
+ cmd << commit
301
306
  when :detach
302
- cmd << "--#{flag}"
303
- cmd << commit if commit
307
+ cmd << "--#{flag}" << commit
304
308
  else
305
309
  cmd << "--#{flag}"
306
310
  append_ours
@@ -339,10 +343,13 @@ module Squared
339
343
 
340
344
  def diff(flag, files = [], branch: nil, index: 0)
341
345
  cmd = git_session 'diff'
342
- if /^#[0-9a-f]{5,40}\#$/.match?(sha = files.first)
343
- files.shift
344
- else
345
- sha = nil
346
+ unless flag == :files
347
+ if /^#[0-9a-f]{5,40}\#$/.match?(sha = files.first)
348
+ sha = sha[1..-2]
349
+ files.shift
350
+ else
351
+ sha = nil
352
+ end
346
353
  end
347
354
  if (val = option('unified')).to_i > 0
348
355
  cmd << "--unified=#{val}"
@@ -354,6 +361,8 @@ module Squared
354
361
  cmd << '--merge-base' if option('merge-base')
355
362
  when :branch
356
363
  cmd << branch
364
+ when :files
365
+ cmd << '--no-index'
357
366
  else
358
367
  if (val = option('index')) || index > 0
359
368
  cmd << "HEAD~#{val || index}"
@@ -361,25 +370,24 @@ module Squared
361
370
  cmd << '--merge-base'
362
371
  end
363
372
  end
364
- cmd << sha if sha
365
- append_pathspec files
366
- source
373
+ cmd << sha
374
+ append_pathspec(files, pass: flag == :files)
375
+ source(exception: cmd.include?('--exit-code'))
367
376
  end
368
377
 
369
378
  def commit(flag, files = [], message: nil, pass: false)
370
- message = option('message') if message.nil?
379
+ message ||= option('message', 'm', zero: false)
371
380
  amend = flag.to_s.start_with?('amend')
372
381
  if !message && !amend
373
382
  return if pass
374
383
 
375
- raise ArgumentError, message('commit', 'GIT_MESSAGE="description"', hint: 'missing')
384
+ raise_error('commit', 'GIT_MESSAGE="description"', hint: 'missing')
376
385
  end
377
386
  pathspec = if flag == :all || (amend && files.size == 1 && files.first == '*')
378
387
  '--all'
379
388
  else
380
389
  files = source_path(as_a(files))
381
- raise ArgumentError, message('commit', 'pathspec', hint: 'missing') if files.empty?
382
-
390
+ raise_error('commit', 'pathspec', hint: 'missing') if files.empty?
383
391
  "-- #{files.join(' ')}"
384
392
  end
385
393
  if !push?
@@ -392,7 +400,7 @@ module Squared
392
400
  break
393
401
  end
394
402
  end
395
- raise ArgumentError, message('commit', 'work tree is not usable') unless push?
403
+ raise_error('commit', 'work tree is not usable') unless push?
396
404
 
397
405
  cmd = git_session 'commit'
398
406
  cmd << '--dry-run' if option('dry-run')
@@ -438,7 +446,7 @@ module Squared
438
446
  cmd = close_session(cmd)
439
447
  log.info cmd
440
448
  begin
441
- banner = format_banner(cmd.gsub(trailing_slash(path), ''), banner: banner, multiple: true)
449
+ banner = format_banner(cmd.gsub(File.join(path, ''), ''), banner: banner, multiple: true)
442
450
  cmd = cmd.sub(/^git\b/, "git --work-tree #{shell_quote(path)} --git-dir #{shell_quote(gitdir)}")
443
451
  if io
444
452
  [IO.popen(cmd), banner]
@@ -472,7 +480,7 @@ module Squared
472
480
  log.error e
473
481
  raise if exception
474
482
 
475
- emphasize e, message('git', cmd)
483
+ warn e
476
484
  end
477
485
  end
478
486
 
@@ -513,8 +521,9 @@ module Squared
513
521
  end
514
522
  end
515
523
 
516
- def source_path(files)
517
- files.select { |val| source_path?(val) }.map { |val| val == '.' ? '.' : shell_quote(base_path(val.strip)) }
524
+ def source_path(files, pass: false)
525
+ files = files.select { |val| source_path?(val) } unless pass
526
+ files.map { |val| val == '.' ? '.' : shell_quote(base_path(val.strip)) }
518
527
  end
519
528
 
520
529
  def source_path?(val)
@@ -541,12 +550,15 @@ module Squared
541
550
  val.empty? ? 'HEAD' : val
542
551
  end
543
552
 
544
- def append_pathspec(files, expect: false)
545
- files = source_path(files)
546
- if files.empty?
547
- raise ArgumentError, message('pathspec not within worktree', hint: 'invalid') if expect
548
- else
553
+ def append_pathspec(files = [], expect: false, pass: false)
554
+ if files.empty? && (val = option('pathspec'))
555
+ files = split_escape(val)
556
+ end
557
+ files = source_path(files, pass: pass)
558
+ if !files.empty?
549
559
  @session << "-- #{files.join(' ')}"
560
+ elsif expect
561
+ raise_error(pass ? 'pathspec not present' : 'pathspec not within worktree', hint: 'invalid')
550
562
  end
551
563
  end
552
564
 
@@ -555,9 +567,7 @@ module Squared
555
567
  end
556
568
 
557
569
  def append_head
558
- return unless (val = option('head')) || (val = option('tree-ish'))
559
-
560
- @session << val
570
+ @session << (option('head') || option('tree-ish'))
561
571
  end
562
572
 
563
573
  def append_ours
@@ -576,11 +586,19 @@ module Squared
576
586
  end
577
587
  end
578
588
 
579
- def option(val, equals: nil)
580
- ret = ENV.fetch("GIT_#{val.gsub(/\W/, '_').upcase}", '')
581
- return ret == equals.to_s unless equals.nil?
589
+ def append_option(list)
590
+ list.each { |val| @session << "--#{val}" if option(val) }
591
+ end
582
592
 
583
- ret unless ret.empty? || ret == '0'
593
+ def option(*args, equals: nil, zero: true)
594
+ for val in args
595
+ break if (ret = ENV["GIT_#{val.gsub(/\W/, '_').upcase}"])
596
+ end
597
+ if !equals.nil?
598
+ ret == equals.to_s
599
+ elsif !ret.nil? && !ret.empty? && !(ret == '0' && zero)
600
+ ret
601
+ end
584
602
  end
585
603
 
586
604
  def git_session(*cmd)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- module Repo
4
+ module Workspace
5
5
  module Project
6
6
  class Node < Git
7
7
  REF = :node
@@ -35,20 +35,19 @@ module Squared
35
35
 
36
36
  attr_reader :package
37
37
 
38
- def initialize(name, path, workspace, *, **kwargs)
38
+ def initialize(name, path, workspace, *, dev: nil, prod: nil, **kwargs)
39
39
  super
40
40
  initialize_script(REF)
41
41
  if (opts = env('BUILD', strict: true))
42
- raise ArgumentError, message("BUILD_#{@name.upcase}", opts) if @output[0].is_a?(::Array)
43
-
42
+ raise_error("BUILD_#{@name.upcase}", opts) if @output[0].is_a?(::Array)
44
43
  @output[1] = opts
45
44
  else
46
45
  @output[1] = (@script && @script[:run]) || workspace.script
47
46
  end
48
- @package = base_path('package.json')
49
- @dev = workspace.bool_match(env('BUILD', suffix: 'DEV'), kwargs.delete(:dev))
50
- @prod = workspace.bool_match(env('BUILD', suffix: 'PROD'), kwargs.delete(:prod))
47
+ @dev = dev
48
+ @prod = prod
51
49
  @pm = {}
50
+ @package = base_path('package.json')
52
51
  end
53
52
 
54
53
  def populate(*)
@@ -108,7 +107,7 @@ module Squared
108
107
  elsif dir.is_a?(::String)
109
108
  dest = workspace.root_path(dir)
110
109
  elsif dir.is_a?(::Symbol)
111
- dest = Repo.resolve(dir)&.path
110
+ dest = Workspace.resolve(dir)&.path
112
111
  elsif dir.is_a?(Project)
113
112
  dest = dir.path
114
113
  elsif dir.is_a?(::Hash)
@@ -195,7 +194,7 @@ module Squared
195
194
  append_nocolor
196
195
  end
197
196
  append_loglevel
198
- run(exception: workspace.exception)
197
+ run(exception: workspace.exception, sync: invoked_sync?('depend'))
199
198
  end
200
199
  end
201
200
 
@@ -203,37 +202,37 @@ module Squared
203
202
  require 'json'
204
203
  rev ||= prod? ? :patch : :minor
205
204
  cmd = pnpm? ? 'pnpm outdated' : 'npm outdated'
206
- print_item format_banner(message("#{cmd}#{opts.include?('dry-run') ? ' --dry-run' : ''}"), multiple: true)
205
+ if invoked_sync?('outdated')
206
+ print_item format_banner(message("#{cmd}#{opts.include?('dry-run') ? ' --dry-run' : ''}"), multiple: true)
207
+ end
207
208
  pwd = Dir.pwd
208
209
  Dir.chdir(path)
209
210
  log.info cmd
210
211
  data = `#{cmd} --json --loglevel=error`
211
212
  Dir.chdir(pwd)
212
213
  json = JSON.parse(doc = package.read)
213
- pat = /^(\d+)(\.)(\d+)(\.)(\d+)$/
214
214
  dep1 = json['dependencies'] || {}
215
215
  dep2 = json['devDependencies'] || {}
216
216
  found = []
217
217
  avail = []
218
218
  if !data.empty?
219
- validate = ->(ver) { ver.match?(pat) }
220
219
  JSON.parse(data).each_pair do |key, val|
221
220
  val = val.find { |item| item['dependent'] == json['name'] } if val.is_a?(Array)
222
221
  next unless val && (file = dep1[key] || dep2[key])
223
222
 
224
223
  ch = file[0]
225
- unless ch.match?(/[~^]/)
224
+ unless ch =~ /[~^]/
226
225
  avail << [key, file, val['latest'], true]
227
226
  next
228
227
  end
229
228
  file = file[1..-1]
230
229
  cur = val['current']
231
- want = if rev == :major && validate.(val['latest'])
230
+ want = if rev == :major && val['latest'] =~ SEM_VER
232
231
  [val['latest'], val['wanted']].max { |a, b| a <=> b }
233
232
  else
234
233
  val['wanted']
235
234
  end
236
- next unless (cur != want || file != want) && (validate.(want) || !validate.(file))
235
+ next unless (cur != want || file != want) && (want.match?(SEM_VER) || !file.match?(SEM_VER))
237
236
 
238
237
  a, b = file.split('.')
239
238
  c, d = want.split('.')
@@ -244,7 +243,7 @@ module Squared
244
243
  when :minor
245
244
  upgrade = ch == '^' && (a == '0' ? c == '0' && b == d : a == c)
246
245
  when :patch
247
- upgrade = a == c && b == d
246
+ upgrade = a == c && b == d && Regexp.last_match(5)
248
247
  end
249
248
  if upgrade
250
249
  next if file == want
@@ -304,7 +303,7 @@ module Squared
304
303
  a = sub_style(a, :bold)
305
304
  sub_style(c, :green, :bold)
306
305
  else
307
- sub_style(c, :green, :bold, pat: pat, index: d)
306
+ sub_style(c, :green, :bold, pat: SEM_VER, index: d)
308
307
  end
309
308
  puts "#{pad_ord.(i, found)}. #{a}#{b.ljust(col2)}#{c}"
310
309
  end
@@ -352,7 +351,7 @@ module Squared
352
351
  elsif args.is_a?(::String)
353
352
  cmd << args
354
353
  else
355
- raise ArgumentError, message("#{cmd.first} script name", hint: args.nil? ? 'missing' : 'invalid')
354
+ raise_error("#{cmd.first} script name", hint: args.nil? ? 'missing' : 'invalid')
356
355
  end
357
356
  cmd.join(' ')
358
357
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- module Repo
4
+ module Workspace
5
5
  module Project
6
6
  class Python < Git
7
7
  REF = :python
@@ -94,9 +94,9 @@ module Squared
94
94
  append_general opts, OPT_FORCE
95
95
  end
96
96
  cmd << (type == 1 ? '-r requirements.txt' : '.')
97
- run(exception: workspace.exception)
97
+ run(exception: workspace.exception, sync: invoked_sync?('depend'))
98
98
  when 3
99
- run_s "#{@bin} setup.py install"
99
+ run_s("#{@bin} setup.py install", sync: invoked_sync?('depend'))
100
100
  end
101
101
  end
102
102
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- module Repo
4
+ module Workspace
5
5
  module Project
6
6
  class Ruby < Git
7
7
  REF = :ruby
@@ -35,11 +35,11 @@ module Squared
35
35
 
36
36
  attr_reader :gemfile
37
37
 
38
- def initialize(name, path, workspace, *, **kwargs)
38
+ def initialize(name, path, workspace, *, version: nil, autodetect: false, **kwargs)
39
39
  super
40
40
  initialize_build(REF, **kwargs)
41
- @version = env('BUILD', suffix: 'VERSION', strict: true) || kwargs.delete(:version)
42
- @autodetect = kwargs.key?(:autodetect) ? kwargs.delete(:autodetect) : false
41
+ @version = env('BUILD', suffix: 'VERSION', strict: true) || version
42
+ @autodetect = autodetect
43
43
  index = GEMFILE.index { |file| base_path(file).exist? } || 0
44
44
  @gemfile = base_path(GEMFILE[index])
45
45
  return if !@output[0].nil? || !@copy.nil? || @version || @autodetect || (file = rakefile).nil?
@@ -47,7 +47,7 @@ module Squared
47
47
  begin
48
48
  pat = %r{\brequire\s+(["'])bundler/gem_tasks\1}
49
49
  File.foreach(file) do |line|
50
- next unless line.match?(pat)
50
+ next unless line =~ pat
51
51
 
52
52
  @output[0] = 'bundle exec rake build'
53
53
  @copy = 'bundle exec rake install'
@@ -116,8 +116,7 @@ module Squared
116
116
  elsif outdated?
117
117
  case flag
118
118
  when :redownload, :local, :'prefer-local'
119
- cmd = bundle_session 'install'
120
- cmd << "--#{flag}"
119
+ cmd = bundle_session 'install', "--#{flag}"
121
120
  if (val = env('BUNDLE_TRUST_POLICY'))
122
121
  cmd << "--trust-policy=#{case val
123
122
  when '0'
@@ -218,7 +217,7 @@ module Squared
218
217
  end
219
218
  return false unless @autodetect
220
219
 
221
- unsafe = ->(hint) { raise ArgumentError, message('failed to parse', hint: hint) }
220
+ unsafe = ->(hint) { raise_error('failed to parse', hint: hint) }
222
221
  begin
223
222
  out = `gem -C #{shell_quote(path)} list --local -d #{project}`
224
223
  data = /#{Regexp.escape(project)} \(([^)]+)\)/.match(out)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Squared
4
- module Repo
4
+ module Workspace
5
5
  module Project
6
6
  class << self
7
7
  attr_accessor :line_width