squared 0.0.7 → 0.0.9

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.
@@ -8,6 +8,7 @@ module Squared
8
8
  module Project
9
9
  class Base
10
10
  include Common
11
+ include Format
11
12
  include System
12
13
  include Shell
13
14
  include Task
@@ -44,7 +45,6 @@ module Squared
44
45
  @@tasks = {}
45
46
 
46
47
  attr_reader :name, :project, :workspace, :group, :path, :theme
47
- attr_accessor :warning
48
48
 
49
49
  def initialize(name, path, workspace, *, group: nil, **kwargs)
50
50
  @name = name.to_s
@@ -58,8 +58,6 @@ module Squared
58
58
  @output = [kwargs[:run], nil]
59
59
  @copy = kwargs[:copy]
60
60
  @clean = kwargs[:clean]
61
- @exclude = as_a(kwargs[:exclude])
62
- @warning = workspace.warning
63
61
  @theme = if !workspace.verbose
64
62
  {}
65
63
  elsif kwargs.fetch(:common, true)
@@ -67,6 +65,9 @@ module Squared
67
65
  else
68
66
  __get__(:theme)[:project][to_sym] ||= {}
69
67
  end
68
+ @ref = []
69
+ @exclude = as_a(kwargs[:exclude], :to_sym).freeze
70
+ initialize_ref(Base.ref)
70
71
  initialize_logger(**kwargs)
71
72
  end
72
73
 
@@ -91,7 +92,7 @@ module Squared
91
92
  raise if @workspace.exception
92
93
 
93
94
  file = nil
94
- warn e if @warning
95
+ warn e if @workspace.warning
95
96
  end
96
97
  end
97
98
  log[:progname] = @name
@@ -118,23 +119,34 @@ module Squared
118
119
  end
119
120
 
120
121
  def initialize_script(ref, **)
122
+ initialize_ref(ref)
121
123
  return unless (script = workspace.script(group: group, ref: ref))
122
124
 
123
125
  @depend = script[:depend] if @depend.nil?
124
126
  @doc = script[:doc] if @doc.nil?
125
127
  @test = script[:test] if @test.nil?
126
128
  @clean = script[:clean] if @clean.nil?
129
+ @exclude = script[:exclude] if @exclude.empty? && script.key?(:exclude)
127
130
  @script = script
128
131
  end
129
132
 
133
+ def initialize_ref(ref)
134
+ @ref << ref unless @exclude.include?(ref)
135
+ end
136
+
137
+ def ref
138
+ Base.ref
139
+ end
140
+
130
141
  def populate(*)
131
- return if @exclude.include?(Base.ref)
142
+ valid = ref?(Base.ref)
143
+ series = workspace.series
132
144
 
133
145
  namespace name do
134
- workspace.series.each_key do |key|
135
- next unless Application::WORKSPACE_KEYS.include?(key) ? has?(key) : workspace.task_include?(self, key)
146
+ series.each_key do |key|
147
+ next unless series.include?(key) ? has?(key) && valid : workspace.task_extend?(self, key)
136
148
 
137
- desc message(name, key)
149
+ desc message(*name.split(':'), key)
138
150
  task key do
139
151
  __send__(key)
140
152
  end
@@ -170,7 +182,7 @@ module Squared
170
182
  build(sync: invoked_sync?('depend'))
171
183
  key = "#{name}:copy"
172
184
  if workspace.task_defined?(key)
173
- invoke key
185
+ invoke(key, exception: workspace.exception, warning: workspace.warning)
174
186
  else
175
187
  copy
176
188
  end
@@ -251,6 +263,10 @@ module Squared
251
263
  respond_to?(m = :"#{method}?") && __send__(m)
252
264
  end
253
265
 
266
+ def ref?(val)
267
+ @ref.include?(val)
268
+ end
269
+
254
270
  def build?
255
271
  !!@output[0]
256
272
  end
@@ -283,17 +299,19 @@ module Squared
283
299
  !!@dev
284
300
  end
285
301
 
286
- protected
302
+ private
287
303
 
288
304
  def run(cmd = @session, exception: workspace.exception, banner: true, sync: true, req: nil, **)
289
- return if req && !base_path(req).exist?
290
-
305
+ if req && !base_path(req).exist?
306
+ log.warn "#{req} (not found)"
307
+ return
308
+ end
291
309
  cmd = close_session(cmd)
292
310
  log.info cmd
293
311
  begin
294
312
  if cmd =~ /^\S+:(\S+:?)+$/ && workspace.task_defined?(cmd)
295
313
  print_item if sync
296
- invoke(cmd, exception: exception)
314
+ invoke(cmd, exception: exception, warning: workspace.warning)
297
315
  else
298
316
  print_item format_banner(cmd, banner: banner) if sync
299
317
  shell(cmd, chdir: path, exception: exception)
@@ -309,7 +327,8 @@ module Squared
309
327
  end
310
328
 
311
329
  def env(key, default = nil, equals: nil, ignore: ['0'].freeze, suffix: nil, strict: false)
312
- a = "#{key}_#{name.gsub(/[^\w]+/, '_').upcase}"
330
+ @env ||= name.gsub(/[^\w]+/, '_').upcase
331
+ a = "#{key}_#{@env}"
313
332
  b = ''
314
333
  if suffix
315
334
  a = [a, suffix].flatten.join('_')
@@ -343,12 +362,12 @@ module Squared
343
362
  puts val unless val.empty? || (val.size == 1 && val.first.nil?)
344
363
  end
345
364
 
346
- def print_banner(*lines, styles: theme[:banner], border: theme[:border])
365
+ def print_banner(*lines, styles: theme[:banner], border: theme[:border], client: false)
347
366
  pad = 0
348
367
  if styles
349
368
  if styles.any? { |s| s.to_s.end_with?('!') }
350
369
  pad = 1
351
- elsif styles.size <= 1
370
+ elsif !client && styles.size <= 1
352
371
  styles = [:bold] + styles
353
372
  end
354
373
  end
@@ -381,22 +400,31 @@ module Squared
381
400
  ret.join("\n")
382
401
  end
383
402
 
384
- def format_desc(action, flag, opts = nil, req: 'opts*')
385
- opts = "#{req}=#{opts.join(',')}" if opts.is_a?(::Array)
386
- if !flag
403
+ def format_desc(action, flag, opts = nil, req: '', arg: 'opts*')
404
+ opts = "#{arg}=#{opts.join(',')}" if opts.is_a?(::Array)
405
+ unless flag
387
406
  flag = action
388
407
  action = ''
389
408
  end
390
- message(name, action, opts ? "#{flag}[#{opts}]" : flag)
409
+ req = opts ? "#{req}," : "[#{req}]" unless req.to_s.empty?
410
+ message(*name.split(':'), action, opts ? "#{flag}[#{req}#{opts}]" : flag.to_s + req)
391
411
  end
392
412
 
393
413
  def format_banner(cmd, banner: true, multiple: false)
394
414
  return unless banner
395
415
 
416
+ if (data = workspace.banner(group: group, ref: ref))
417
+ client = true
418
+ else
419
+ data = { command: true, order: %i[path], styles: theme[:banner], border: theme[:border] }
420
+ end
396
421
  if verbose?
397
- print_banner(cmd.sub(/^\S+/, &:upcase), path.to_s)
422
+ out = []
423
+ out << cmd.sub(/^\S+/, &:upcase) if data[:command]
424
+ data[:order].each { |val| out << val.to_s if (val = __send__(val)) }
425
+ print_banner(*out, styles: data[:styles], border: data[:border], client: client)
398
426
  elsif multiple && workspace.series.multiple?
399
- "## #{path} ##"
427
+ "## #{__send__(data[:order].first || :path)} ##"
400
428
  end
401
429
  end
402
430
 
@@ -457,8 +485,9 @@ module Squared
457
485
  workspace.pipe
458
486
  end
459
487
 
460
- def invoked_sync?(action)
461
- return true if workspace.series.sync?("#{action}:sync")
488
+ def invoked_sync?(action, flag = nil)
489
+ action = workspace.task_name(action)
490
+ return true if !flag.nil? || workspace.series.sync?("#{action}:sync")
462
491
 
463
492
  check = lambda do |val|
464
493
  if invoked?(val)
@@ -4,8 +4,6 @@ module Squared
4
4
  module Workspace
5
5
  module Project
6
6
  class Git < Base
7
- include Format
8
-
9
7
  OPT_PULL = %w[all tags prune ff-only autostash dry-run].freeze
10
8
  OPT_FETCH = %w[tags prune prune-tags depth=n dry-run].freeze
11
9
  private_constant :OPT_PULL, :OPT_FETCH
@@ -13,23 +11,26 @@ module Squared
13
11
  class << self
14
12
  include ::Rake::DSL
15
13
 
16
- def populate(workspace, parallel: [], **)
17
- return if workspace.series.pull.empty?
14
+ def populate(ws, **)
15
+ return if ws.series.pull.empty?
18
16
 
19
- desc 'all[git?=rebase|stash]'
20
- task 'all', [:git] do |_, args|
21
- sync = ->(key) { parallel.include?(key) ? :"#{key}:sync" : key }
22
- pull = case args.git
17
+ desc ws.task_name('all[git?=rebase|stash]', desc: true)
18
+ task ws.task_name('all'), [:git] do |_, args|
19
+ sync = lambda do |key|
20
+ key = ws.task_name(key)
21
+ ws.task_defined?(ret = "#{key}:sync") ? ret : key
22
+ end
23
+ cmd = [case args.git
23
24
  when 'rebase'
24
- sync.(:rebase)
25
+ sync.('rebase')
25
26
  when 'stash'
26
- invoke sync.(:stash)
27
- sync.(:pull)
27
+ invoke(sync.('stash'), exception: ws.exception, warning: ws.warning)
28
+ sync.('pull')
28
29
  else
29
- sync.(:pull)
30
- end
31
- Common::Task.invoke(pull, exception: workspace.exception)
32
- Common::Task.invoke(workspace.dev? ? :refresh : :build, exception: workspace.exception)
30
+ sync.('pull')
31
+ end]
32
+ cmd << ws.task_name(ws.dev? && ws.series.some?(:refresh) ? 'refresh' : 'build')
33
+ Common::Task.invoke(cmd, exception: ws.exception, warning: ws.warning)
33
34
  end
34
35
  end
35
36
 
@@ -60,9 +61,18 @@ module Squared
60
61
  rev: %i[commit branch]
61
62
  }.freeze
62
63
 
64
+ def initialize(*, **)
65
+ super
66
+ initialize_ref(Git.ref)
67
+ end
68
+
69
+ def ref
70
+ Git.ref
71
+ end
72
+
63
73
  def populate(*)
64
74
  super
65
- return unless gitdir.exist? && !@exclude.include?(Git.ref)
75
+ return unless gitdir.exist? && ref?(Git.ref)
66
76
 
67
77
  namespace name do
68
78
  @@tasks[Git.ref].each do |action, flags|
@@ -121,7 +131,7 @@ module Squared
121
131
  desc format_desc(action, flag, 'index?=0,pathspec*')
122
132
  task flag, [:pathspec] do |_, args|
123
133
  files = args.to_a
124
- index = /^\d+$/.match?(files.first) && !option('index') ? files.shift.to_i : 0
134
+ index = /^\d+$/.match?(files.first) && !git_option('index') ? files.shift.to_i : 0
125
135
  diff(flag, files, index: index)
126
136
  end
127
137
  when :cached
@@ -202,12 +212,12 @@ module Squared
202
212
  cmd = git_session 'pull'
203
213
  if flag == :'no-rebase'
204
214
  cmd << '--no-rebase'
205
- elsif flag == :rebase || option('rebase')
215
+ elsif flag == :rebase || git_option('rebase')
206
216
  cmd << '--rebase'
207
217
  end
208
218
  if flag == :'no-commit'
209
219
  cmd << '--no-commit'
210
- elsif flag == :commit || option('commit')
220
+ elsif flag == :commit || git_option('commit')
211
221
  cmd << '--commit'
212
222
  end
213
223
  append_pull opts, OPT_PULL, flag
@@ -220,28 +230,28 @@ module Squared
220
230
 
221
231
  def fetch(flag = nil, opts: [])
222
232
  cmd = git_session 'fetch'
223
- cmd << '--all' if flag == :all || option('all')
233
+ cmd << '--all' if flag == :all || git_option('all')
224
234
  append_pull opts, OPT_FETCH, flag
225
235
  source(sync: invoked_sync?('fetch', flag), stderr: true, exception: !workspace.series.multiple?)
226
236
  end
227
237
 
228
- def stash(flag = :push, files = [], commit: nil)
229
- cmd = git_session 'stash', flag.to_s
238
+ def stash(flag = nil, files = [], commit: nil)
239
+ cmd = git_session 'stash', (flag || 'push').to_s
230
240
  case flag
231
241
  when :apply, :pop
232
- cmd << '--index' if option('index')
242
+ cmd << '--index' if git_option('index')
233
243
  cmd << commit
234
244
  else
235
245
  append_option %w[all staged include-untracked]
236
- append_message option('message', 'm', zero: false)
246
+ append_message git_option('message', 'm', zero: false)
237
247
  append_pathspec files
238
248
  end
239
249
  source(sync: invoked_sync?('stash', flag), exception: workspace.exception)
240
250
  end
241
251
 
242
252
  def status
243
- cmd = git_session 'status', option('long') ? '--long' : '--short'
244
- if (val = option('ignore-submodules'))
253
+ cmd = git_session 'status', git_option('long') ? '--long' : '--short'
254
+ if (val = git_option('ignore-submodules'))
245
255
  cmd << "--ignore-submodules=#{case val
246
256
  when '0', 'none'
247
257
  'none'
@@ -259,12 +269,15 @@ module Squared
259
269
  print_item banner
260
270
  banner = nil
261
271
  end
262
- ret = write_lines(out, banner: banner, sub: [
263
- { pat: /^(.)([A-Z])(.+)$/, styles: :red, index: 2 },
264
- { pat: /^([A-Z])(.+)$/, styles: :green },
265
- { pat: /^(\?\?)(.+)$/, styles: :red },
266
- { pat: /^(## )(.+)(\.{3})(.+)$/, styles: [nil, :green, nil, :red], index: -1 }
267
- ])
272
+ sub = if verbose?
273
+ [
274
+ { pat: /^(.)([A-Z])(.+)$/, styles: :red, index: 2 },
275
+ { pat: /^([A-Z])(.+)$/, styles: :green },
276
+ { pat: /^(\?\?)(.+)$/, styles: :red },
277
+ { pat: /^(## )(.+)(\.{3})(.+)$/, styles: [nil, :green, nil, :red], index: -1 }
278
+ ]
279
+ end
280
+ ret = write_lines(out, banner: banner, sub: sub)
268
281
  list_result(ret, 'files', action: 'modified')
269
282
  end
270
283
 
@@ -282,7 +295,7 @@ module Squared
282
295
  append_submodules flag
283
296
  else
284
297
  cmd << '--mixed'
285
- cmd << '--no-refresh' if option('refresh', equals: '0')
298
+ cmd << '--no-refresh' if git_option('refresh', equals: '0')
286
299
  end
287
300
  append_commit ref
288
301
  end
@@ -293,10 +306,10 @@ module Squared
293
306
  cmd = git_session 'checkout'
294
307
  case flag
295
308
  when :branch
296
- cmd << '--detach' if detach == 'd' || option('detach')
309
+ cmd << '--detach' if detach == 'd' || git_option('detach')
297
310
  if create
298
311
  cmd << "-#{create}" << branch
299
- if (val = option('start-point'))
312
+ if (val = git_option('start-point'))
300
313
  cmd << val
301
314
  end
302
315
  else
@@ -351,22 +364,22 @@ module Squared
351
364
  sha = nil
352
365
  end
353
366
  end
354
- if (val = option('unified')).to_i > 0
367
+ if (val = git_option('unified')).to_i > 0
355
368
  cmd << "--unified=#{val}"
356
369
  end
357
370
  append_nocolor
358
371
  case flag
359
372
  when :cached
360
373
  cmd << '--cached'
361
- cmd << '--merge-base' if option('merge-base')
374
+ cmd << '--merge-base' if git_option('merge-base')
362
375
  when :branch
363
376
  cmd << branch
364
377
  when :files
365
378
  cmd << '--no-index'
366
379
  else
367
- if (val = option('index')) || index > 0
380
+ if (val = git_option('index')) || index > 0
368
381
  cmd << "HEAD~#{val || index}"
369
- elsif sha && option('merge-base')
382
+ elsif sha && git_option('merge-base')
370
383
  cmd << '--merge-base'
371
384
  end
372
385
  end
@@ -376,7 +389,7 @@ module Squared
376
389
  end
377
390
 
378
391
  def commit(flag, files = [], message: nil, pass: false)
379
- message ||= option('message', 'm', zero: false)
392
+ message ||= git_option('message', 'm', zero: false)
380
393
  amend = flag.to_s.start_with?('amend')
381
394
  if !message && !amend
382
395
  return if pass
@@ -391,7 +404,7 @@ module Squared
391
404
  "-- #{files.join(' ')}"
392
405
  end
393
406
  if !push?
394
- source('git branch -vv --list', io: true, banner: false)[0].each do |val|
407
+ source('git branch -vv --list', io: true, banner: false).first.each do |val|
395
408
  origin = %r{^\* [^\[]+(?<= )\[([\w.-/]+?)/([\w.-]+)\] }.match(val)
396
409
  next unless origin
397
410
 
@@ -403,7 +416,7 @@ module Squared
403
416
  raise_error('commit', 'work tree is not usable') unless push?
404
417
 
405
418
  cmd = git_session 'commit'
406
- cmd << '--dry-run' if option('dry-run')
419
+ cmd << '--dry-run' if git_option('dry-run')
407
420
  if amend
408
421
  cmd << '--amend'
409
422
  else
@@ -411,7 +424,7 @@ module Squared
411
424
  end
412
425
  if message
413
426
  append_message message
414
- elsif flag == :'amend-orig' || option('no-edit')
427
+ elsif flag == :'amend-orig' || git_option('no-edit')
415
428
  cmd << '--no-edit'
416
429
  end
417
430
  a = ['git add --verbose']
@@ -440,14 +453,14 @@ module Squared
440
453
  source(stdout: true)
441
454
  end
442
455
 
443
- protected
456
+ private
444
457
 
445
458
  def source(cmd = @session, exception: true, banner: true, io: false, sync: true, stdout: false, stderr: false)
446
459
  cmd = close_session(cmd)
447
460
  log.info cmd
461
+ banner = format_banner(cmd.gsub(File.join(path, ''), ''), banner: banner, multiple: true)
462
+ cmd = cmd.sub(/^git\b/, "git --work-tree #{shell_quote(path)} --git-dir #{shell_quote(gitdir)}")
448
463
  begin
449
- banner = format_banner(cmd.gsub(File.join(path, ''), ''), banner: banner, multiple: true)
450
- cmd = cmd.sub(/^git\b/, "git --work-tree #{shell_quote(path)} --git-dir #{shell_quote(gitdir)}")
451
464
  if io
452
465
  [IO.popen(cmd), banner]
453
466
  elsif pipe? ? sync : stdout
@@ -480,7 +493,7 @@ module Squared
480
493
  log.error e
481
494
  raise if exception
482
495
 
483
- warn e
496
+ warn e if workspace.warning
484
497
  end
485
498
  end
486
499
 
@@ -526,14 +539,6 @@ module Squared
526
539
  files.map { |val| val == '.' ? '.' : shell_quote(base_path(val.strip)) }
527
540
  end
528
541
 
529
- def source_path?(val)
530
- return val.to_s.start_with?(File.join(path, '').to_s) if Pathname.new(val).absolute?
531
-
532
- !val.match?(%r{^\.\.[/\\]})
533
- end
534
-
535
- private
536
-
537
542
  def append_pull(opts, list, flag = nil)
538
543
  append_submodules flag
539
544
  opts.each do |opt|
@@ -551,7 +556,7 @@ module Squared
551
556
  end
552
557
 
553
558
  def append_pathspec(files = [], expect: false, pass: false)
554
- if files.empty? && (val = option('pathspec'))
559
+ if files.empty? && (val = git_option('pathspec'))
555
560
  files = split_escape(val)
556
561
  end
557
562
  files = source_path(files, pass: pass)
@@ -567,30 +572,30 @@ module Squared
567
572
  end
568
573
 
569
574
  def append_head
570
- @session << (option('head') || option('tree-ish'))
575
+ @session << (git_option('head') || git_option('tree-ish'))
571
576
  end
572
577
 
573
578
  def append_ours
574
- if option('ours')
579
+ if git_option('ours')
575
580
  @session << '--ours'
576
- elsif option('theirs')
581
+ elsif git_option('theirs')
577
582
  @session << '--theirs'
578
583
  end
579
584
  end
580
585
 
581
586
  def append_submodules(flag = nil)
582
- if option('recurse-submodules', equals: '0')
587
+ if git_option('recurse-submodules', equals: '0')
583
588
  @session << '--no-recurse-submodules'
584
- elsif flag == :submodules || option('recurse-submodules')
589
+ elsif flag == :submodules || git_option('recurse-submodules')
585
590
  @session << '--recurse-submodules'
586
591
  end
587
592
  end
588
593
 
589
594
  def append_option(list)
590
- list.each { |val| @session << "--#{val}" if option(val) }
595
+ list.each { |val| @session << "--#{val}" if git_option(val) }
591
596
  end
592
597
 
593
- def option(*args, equals: nil, zero: true)
598
+ def git_option(*args, equals: nil, zero: true)
594
599
  for val in args
595
600
  break if (ret = ENV["GIT_#{val.gsub(/\W/, '_').upcase}"])
596
601
  end
@@ -606,11 +611,13 @@ module Squared
606
611
  end
607
612
 
608
613
  def gitdir
609
- @gitdir ||= base_path('.git')
614
+ base_path('.git')
610
615
  end
611
616
 
612
- def invoked_sync?(action, flag = nil)
613
- !flag.nil? || super(action)
617
+ def source_path?(val)
618
+ return val.to_s.start_with?(File.join(path, '').to_s) if Pathname.new(val).absolute?
619
+
620
+ !val.match?(%r{^\.\.[/\\]})
614
621
  end
615
622
 
616
623
  def push?