squared 0.6.11 → 0.7.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.
@@ -27,12 +27,12 @@ module Squared
27
27
  def multiple=(val)
28
28
  case val
29
29
  when Enumerable
30
- @multiple.concat(val.to_a.map { |pat| pat.is_a?(Regexp) ? pat : pat.to_s })
30
+ @multiple.concat(val.to_a.map { |val| val.is_a?(Regexp) ? val : val.to_s })
31
31
  when String, Symbol, Pathname
32
32
  @multiple << val.to_s
33
33
  when Regexp
34
34
  @multiple << val
35
- when NilClass, FalseClass
35
+ when nil, false
36
36
  @multiple.clear
37
37
  end
38
38
  end
@@ -47,7 +47,9 @@ module Squared
47
47
 
48
48
  def slice!(*args)
49
49
  data = compact
50
- data.slice!(*args).tap { replace Set.new(data) }
50
+ ret = data.slice!(*args)
51
+ replace Set.new(data)
52
+ ret
51
53
  end
52
54
 
53
55
  def compact
@@ -109,7 +111,9 @@ module Squared
109
111
  end
110
112
 
111
113
  def done
112
- to_s.tap { clear }
114
+ ret = to_s
115
+ clear
116
+ ret
113
117
  end
114
118
 
115
119
  def merge(enum)
@@ -131,10 +135,12 @@ module Squared
131
135
  end
132
136
 
133
137
  def <<(obj)
134
- return super if extras.empty? && !extras?(obj)
138
+ unless obj.nil?
139
+ return super if extras.empty? && !extras?(obj)
135
140
 
136
- unless !extras.include?(@partition) && include?(obj) && @uniq.match?(s = obj.to_s) && !multiple?(s)
137
- extras << obj
141
+ unless !extras.include?(@partition) && include?(obj) && @uniq.match?(s = obj.to_s) && !multiple?(s)
142
+ extras << obj
143
+ end
138
144
  end
139
145
  self
140
146
  end
@@ -21,37 +21,38 @@ module Squared
21
21
  include Prompt
22
22
 
23
23
  def append(target, *args, delim: false, escape: false, quote: true, strip: nil, force: true, double: false,
24
- filter: nil, **)
25
- return if (ret = args.flatten).empty?
24
+ filter: nil, pass: nil, **)
25
+ return if (ret = args.flatten(1)).empty?
26
26
 
27
27
  target << '--' if delim && !target.include?('--')
28
28
  if strip
29
29
  pat, s = Array(strip)
30
30
  ret.map! { |val| val.is_a?(String) ? val.gsub(pat, s || '') : val }
31
31
  end
32
- ret, err = ret.partition { |val| filter.match?(val.to_s) } if filter
32
+ if filter
33
+ ret, err = ret.partition { |val| filter.match?(val.to_s) }
34
+ pass&.concat(err)
35
+ end
33
36
  if block_given?
34
- out = []
35
- err ||= []
36
- ret.each do |val|
37
- case (s = yield val)
37
+ ret = ret.each_with_object([]) do |val, out|
38
+ arg = yield val
39
+ case arg
38
40
  when String
39
- out << s
40
- when NilClass, FalseClass
41
- err << val
41
+ out << arg
42
+ when nil, false
43
+ pass&.push(val)
42
44
  else
43
45
  out << val
44
46
  end
45
47
  end
46
- ret = out
47
48
  end
48
49
  ret.map! do |val|
49
50
  next val if opt?(val)
50
51
 
51
- if !(pa = val.is_a?(Pathname)) && escape
52
- shell_escape(val, quote: quote, double: double)
53
- elsif quote || pa
52
+ if quote || val.is_a?(Pathname)
54
53
  shell_quote(val, force: force, double: double)
54
+ elsif escape
55
+ shell_escape(val, quote: quote, double: double)
55
56
  else
56
57
  val
57
58
  end
@@ -61,7 +62,7 @@ module Squared
61
62
  else
62
63
  target.concat(ret)
63
64
  end
64
- err || ret
65
+ ret
65
66
  end
66
67
 
67
68
  def clear(target, opts, pass: true, styles: nil, **kwargs)
@@ -75,15 +76,13 @@ module Squared
75
76
  end
76
77
 
77
78
  def delete_key(target, *args, value: false, reverse: false, count: -1)
78
- ret = []
79
- args.each do |val|
79
+ args.each_with_object([]) do |val, out|
80
80
  next if (opts = target.grep(matchopt(val, value))).empty?
81
81
 
82
82
  opts = opts.first(count) if count >= 0
83
83
  opts.send(reverse ? :reverse_each : :each) { |key| target.delete(key) }
84
- ret.concat(opts)
84
+ out.concat(opts)
85
85
  end
86
- ret
87
86
  end
88
87
 
89
88
  def strip(val)
@@ -93,7 +92,7 @@ module Squared
93
92
  end
94
93
 
95
94
  def select(list, bare: true, no: true, single: false, double: false)
96
- ret = bare ? list.grep_v(/=/) : list.grep(/=/).map! { |val| val.split('=', 2).first }
95
+ ret = bare ? list.grep_v(/=/) : list.grep(/=/).map { |val| val.split('=', 2).first }
97
96
  ret.map! { |val| val.split('|', 2).last }
98
97
  ret = ret.grep_v(/\Ano-/) unless no
99
98
  return ret if single == double
@@ -115,6 +114,18 @@ module Squared
115
114
  list
116
115
  end
117
116
 
117
+ def parse_option(val)
118
+ val = val.strip
119
+ return [val, nil, ''] unless (i = val.index('='))
120
+
121
+ last = val[i.succ..-1].strip
122
+ if last =~ /\A(["'])(.*)\1\z/m
123
+ last = $2
124
+ quote = $1
125
+ end
126
+ [val[0..i.pred], last, quote || '']
127
+ end
128
+
118
129
  def parse_arg!(name, val)
119
130
  return unless val.is_a?(String)
120
131
 
@@ -141,7 +152,8 @@ module Squared
141
152
  end
142
153
 
143
154
  def pattern?(val)
144
- val.match?(/\A\^|\$\z/) || val.match?(/[.)][*+?]|\(\?:|\\[dsw\d]|\[.+\]|\{\d+,?\d*\}/i)
155
+ val.start_with?('\A', '^') || val.end_with?('\z', '$') ||
156
+ val.match?(/[.)][*+?]|\(\?:|\\[dsw\d]|\[.+\]|\{\d+,?\d*\}/i)
145
157
  end
146
158
 
147
159
  private
@@ -159,13 +171,13 @@ module Squared
159
171
  end
160
172
 
161
173
  def shortopt(*group)
162
- group.map! { |s| Regexp.escape(s.delete_prefix('-')) }
163
- "-(?:#{group.join('|')})(?:\\z|[^ =]| +[^ -])"
174
+ g = group.map { |s| Regexp.escape(s.delete_prefix('-')) }
175
+ "-(?:#{g.join('|')})(?:\\z|[^ =]| +[^ -])"
164
176
  end
165
177
 
166
178
  def longopt(*group, value)
167
- group.map! { |s| Regexp.escape(s.delete_prefix('--')) }
168
- "--(?:#{group.join('|')})(?:#{value ? '=[^ ]| +[^ -]' : '[= ]|\z'})"
179
+ g = group.map { |s| Regexp.escape(s.delete_prefix('--')) }
180
+ "--(?:#{g.join('|')})(?:#{value ? '=[^ ]| +[^ -]' : '[= ]|\z'})"
169
181
  end
170
182
  end
171
183
 
@@ -336,7 +348,7 @@ module Squared
336
348
  push opt
337
349
  skip = true if args
338
350
  end
339
- skip = true if first&.any? { |pat| pat.is_a?(Regexp) ? opt.match?(pat) : !opt.include?(pat) }
351
+ skip = true if first&.any? { |s| s.is_a?(Regexp) ? opt.match?(s) : !opt.include?(s) }
340
352
  end
341
353
  end
342
354
  @values = @values.empty? ? /\A\s+\z/ : /\A(#{@values.join('|')})#{sep}(.+)\z/m
@@ -356,21 +368,21 @@ module Squared
356
368
 
357
369
  def append(*args, **kwargs, &blk)
358
370
  args = extras if args.empty?
359
- out = OptionPartition.append(target, *args, **kwargs, &blk)
360
- errors.concat(out) if out && (block_given? || kwargs[:filter])
371
+ pass = kwargs[:pass] ||= []
372
+ OptionPartition.append(target, *args, **kwargs, &blk)
373
+ errors.concat(pass)
361
374
  self
362
375
  end
363
376
 
364
- def append_any(*args, escape: false, **kwargs)
365
- quote = kwargs.fetch(:quote, true)
366
- (args.empty? ? extras : args.flatten).each do |val|
377
+ def append_any(*args, escape: false, quote: true, **kwargs)
378
+ (args.empty? ? extras : args.flatten(1)).each do |val|
367
379
  if block_given?
368
380
  temp = val
369
381
  val = yield val
370
382
  if val.is_a?(Array)
371
383
  found << temp
372
384
  k, v, q = val
373
- add_option(k, v, escape: escape, double: q == '"', merge: q == true, **kwargs)
385
+ add_option(k, v, escape: escape, quote: quote, double: q == '"', merge: q == true, **kwargs)
374
386
  next
375
387
  end
376
388
  end
@@ -378,10 +390,10 @@ module Squared
378
390
 
379
391
  if exist?(val)
380
392
  add_path(val, **kwargs)
381
- elsif escape
382
- add shell_escape(val, **kwargs)
383
393
  elsif quote
384
394
  add_quote(val, **kwargs)
395
+ elsif escape
396
+ add shell_escape(val, **kwargs)
385
397
  else
386
398
  add val
387
399
  end
@@ -400,7 +412,7 @@ module Squared
400
412
  g = ["\"((?:[^\"]|(?<=\\\\)\"(?!$#{'| ' if windows?}))*)\""]
401
413
  g << "'((?:[^']|'\\\\'')*)'" unless windows?
402
414
  g << "(#{s})"
403
- args.map! do |opt|
415
+ list = args.map do |opt|
404
416
  if opt.size == 1
405
417
  /(?:\A| )-#{opt} ?([^ ]+)/
406
418
  else
@@ -409,7 +421,7 @@ module Squared
409
421
  end
410
422
  ret = []
411
423
  target.each do |opt|
412
- args.each do |pat|
424
+ list.each do |pat|
413
425
  next unless opt =~ pat
414
426
 
415
427
  ret << ($1 || $2 || $3)
@@ -495,10 +507,10 @@ module Squared
495
507
 
496
508
  def add_path(*args, option: nil, force: true, double: false, **kwargs)
497
509
  if args.empty?
498
- args = grep(String)
499
- found.concat(args)
500
- args.map! { |val| path + val } if path
501
- append(args, force: force, **kwargs)
510
+ list = select { |val| val.is_a?(String) }
511
+ found.concat(list)
512
+ list.map! { |val| path + val } if path
513
+ append(list, force: force, **kwargs)
502
514
  else
503
515
  val = path ? path.join(*args) : File.join(*args)
504
516
  if option
@@ -511,9 +523,7 @@ module Squared
511
523
  end
512
524
 
513
525
  def add_quote(*args, **kwargs)
514
- kwargs.delete(:quote)
515
- merge(args.compact
516
- .map! { |val| val == '--' || OptionPartition.opt?(val) ? val : shell_quote(val, **kwargs) })
526
+ merge(args.compact.map { |s| s == '--' || OptionPartition.opt?(s) ? s : shell_quote(s, **kwargs) })
517
527
  self
518
528
  end
519
529
 
@@ -522,8 +532,8 @@ module Squared
522
532
  self
523
533
  end
524
534
 
525
- def add_first(fallback = nil, prefix: nil, path: false, escape: false, reverse: false, expect: false,
526
- **kwargs)
535
+ def add_first(fallback = nil, prefix: nil, path: false, escape: false, quote: false, reverse: false,
536
+ expect: false, **kwargs)
527
537
  val = (reverse ? pop : shift) || fallback
528
538
  if val
529
539
  temp = val
@@ -531,10 +541,10 @@ module Squared
531
541
  unless block_given? && !(val = yield val).is_a?(String)
532
542
  if path
533
543
  add_path(val, **kwargs)
544
+ elsif quote
545
+ add_quote(val, **kwargs)
534
546
  elsif escape
535
547
  add shell_escape(val, **kwargs)
536
- elsif kwargs[:quote]
537
- add_quote(val, **kwargs)
538
548
  else
539
549
  add val
540
550
  end
@@ -546,6 +556,12 @@ module Squared
546
556
  self
547
557
  end
548
558
 
559
+ def detach
560
+ ret = extras
561
+ @extras = []
562
+ ret
563
+ end
564
+
549
565
  def delim
550
566
  add '--'
551
567
  self
@@ -554,7 +570,7 @@ module Squared
554
570
  def last(val = nil, &blk)
555
571
  unless block_given?
556
572
  case val
557
- when NilClass
573
+ when nil
558
574
  return extras.last
559
575
  when Numeric
560
576
  return extras.last(val)
@@ -568,7 +584,7 @@ module Squared
568
584
  ret = find_all(&blk)
569
585
  unless ret.empty?
570
586
  ret = case val
571
- when NilClass
587
+ when nil
572
588
  ret.first(1)
573
589
  when Numeric
574
590
  ret.first(val)
@@ -586,8 +602,6 @@ module Squared
586
602
  def splice(*exclude, quote: true, delim: true, path: false, pattern: false, &blk)
587
603
  temp, other = if block_given?
588
604
  partition(&blk)
589
- elsif exclude.first.is_a?(Symbol)
590
- partition(&exclude.first)
591
605
  else
592
606
  exclude.map! { |pat| Regexp.new(pat) }
593
607
  partition do |val|
@@ -666,10 +680,11 @@ module Squared
666
680
  end
667
681
 
668
682
  def uniq!(list)
669
- unless (list = uniq(list)).empty?
670
- concat list
671
- self
672
- end
683
+ list = uniq list
684
+ return if list.empty?
685
+
686
+ concat list
687
+ self
673
688
  end
674
689
 
675
690
  private
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Squared
4
+ module Workspace
5
+ module Project
6
+ module Support
7
+ module Utils
8
+ private
9
+
10
+ def rand_s(size)
11
+ if RUBY_VERSION >= '3.1'
12
+ require 'random/formatter'
13
+ Random.new.alphanumeric(size)
14
+ else
15
+ (0...size).map { rand(97..122).chr }.join
16
+ end
17
+ end
18
+
19
+ def merge_list(target, list)
20
+ list = Array(list)
21
+ case target
22
+ when Array
23
+ target.concat(list)
24
+ when Set
25
+ target.merge(list)
26
+ else
27
+ Array(target).concat(list)
28
+ end
29
+ end
30
+
31
+ def matchmap(list, prefix = nil)
32
+ list.map do |val|
33
+ next val if val.is_a?(Regexp)
34
+
35
+ val = ".*#{val}" if prefix && !val.sub!(/\A(\^|\\A)/, '')
36
+ Regexp.new("#{prefix}#{val == '*' ? '.+' : val}")
37
+ end
38
+ end
39
+
40
+ def matchany?(list, *args, empty: true)
41
+ return empty if list.empty?
42
+
43
+ list.any? { |pat| args.any?(pat) }
44
+ end
45
+
46
+ def has_value?(target, *args)
47
+ args.any? { |obj,| target.include?(obj) }
48
+ end
49
+
50
+ def has_value!(target, *args, first: false)
51
+ ret = nil
52
+ args.each do |val|
53
+ if target.respond_to?(:delete?)
54
+ ret = target if target.delete?(val)
55
+ elsif target.respond_to?(:delete)
56
+ ret = target if target.delete(val)
57
+ elsif target.include?(val)
58
+ ret = target
59
+ end
60
+ break if ret && first
61
+ end
62
+ ret
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,11 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Squared
4
- module Workspace
5
- module Project
6
- end
7
- end
8
- end
9
-
10
3
  require_relative 'project/base'
11
4
  require_relative 'project/git'