Module: Squared::Common::Shell

Included in:
Workspace::Project::Support::OptionPartition
Defined in:
lib/squared/common/shell.rb

Class Method Summary collapse

Class Method Details

.basic_option(flag, val, **kwargs) ⇒ Object



111
112
113
# File 'lib/squared/common/shell.rb', line 111

def basic_option(flag, val, **kwargs)
  shell_option(flag, val, escape: false, force: false, **kwargs)
end

.fill_option(val, **kwargs) ⇒ Object



101
102
103
104
105
# File 'lib/squared/common/shell.rb', line 101

def fill_option(val, **kwargs)
  return "-#{val}" if val.match?(/\A(?:[a-z]\d*|\d)\z/i)

  shell_escape(val.start_with?('-') ? val : "--#{val}", **kwargs)
end

.line_width(lines) ⇒ Object



96
97
98
99
# File 'lib/squared/common/shell.rb', line 96

def line_width(lines)
  ret = [lines.max_by(&:size).size, 80].max
  [ret, Rake.application.terminal_width].min
end

.quote_option(flag, val, **kwargs) ⇒ Object



107
108
109
# File 'lib/squared/common/shell.rb', line 107

def quote_option(flag, val, **kwargs)
  shell_option(flag, val, escape: false, **kwargs)
end

.shell_escape(val, quote: false, force: false, double: false, option: false, override: false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/squared/common/shell.rb', line 11

def shell_escape(val, quote: false, force: false, double: false, option: false, override: false)
  if (r = /\A(--?)([^= ]+)((=|\s+)(["'])?(?(5)(.*)\5|(.*)))?\z/m.match(val = val.to_s))
    if (data = r[2].match(/\A(["'])(.+)\1\z/))
      double = data[1] == '"'
      override = true
    elsif !r[3] || r[6]
      return val
    end
    if r[7].match?(/\A["']/)
      opt = "#{r[7]}#{r[7][0]}"
    elsif r[7].match?(/["']\z/)
      opt = "#{r[7][-1]}#{r[7]}"
    else
      return val unless r[7].match?(/\s/)

      opt = r[7]
    end
    r[1] + (data ? data[2] : r[2]) + r[4] + shell_quote(opt, double: double, force: force, override: override)
  elsif option && val =~ /\A([^=]+)=(.+)\z/m
    return val if $2.match?(/\A(["']).+\1\z/m)

    "#{$1}=%s" % if $2.include?(' ')
                   shell_quote($2, option: false)
                 else
                   Rake::Win32.windows? ? $2 : Shellwords.escape($2)
                 end
  elsif Rake::Win32.windows?
    quote ? shell_quote(val, double: double, force: force) : val
  else
    val.empty? ? '' : Shellwords.escape(val)
  end
end

.shell_option(flag, val = nil, escape: true, quote: true, option: true, force: true, double: false, merge: false, override: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/squared/common/shell.rb', line 59

def shell_option(flag, val = nil, escape: true, quote: true, option: true, force: true, double: false,
                 merge: false, override: false)
  flag = flag.to_s
  if flag =~ /\A(["'])(.+)\1\z/
    double = $1 == '"'
    flag = $2
    escape = false
    override = true
  end
  if flag[0] == '-'
    b = flag[1] == '-' ? '=' : ' '
  elsif flag.size == 1
    a = '-'
    b = merge ? '' : ' '
  else
    a = '--'
    b = '='
  end
  "#{a}#{flag}#{if val
                  "#{b}#{if escape
                           shell_escape(val, quote: quote, double: double, override: override)
                         elsif quote
                           shell_quote(val, option: option, force: force, double: double, override: override)
                         else
                           val
                         end}"
                end}"
end

.shell_quote(val, option: true, force: true, double: false, override: false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/squared/common/shell.rb', line 44

def shell_quote(val, option: true, force: true, double: false, override: false)
  val = val.to_s
  return val if (!force && !val.include?(' ')) || val.empty?

  if option && val.match?(/(?:\A|\A[^=\s]+(?:=|\s+)|#{Rake::Win32.windows? ? '[\\\/]' : '\/'})(["']).+\1\z/m)
    return val
  end

  if double || Rake::Win32.windows? || (ARG[:QUOTE] == '"' && !override)
    "\"#{val.gsub(/(?<!\\)"/, '\\"')}\""
  else
    "'#{val.gsub("'", "'\\\\''")}'"
  end
end

.shell_split(val, join: nil, **kwargs) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/squared/common/shell.rb', line 88

def shell_split(val, join: nil, **kwargs)
  ret = val.shellsplit
  ret.map! { |opt| shell_escape(opt, double: true, option: true, **kwargs) }
  return ret unless join

  ret.join(join.is_a?(::String) ? join : ' ')
end