Module: Squared::Common::Shell

Defined in:
lib/squared/common/shell.rb

Class Method Summary collapse

Class Method Details

.basic_option(flag, val) ⇒ Object



70
71
72
# File 'lib/squared/common/shell.rb', line 70

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

.double_quote(val) ⇒ Object



78
79
80
# File 'lib/squared/common/shell.rb', line 78

def double_quote(val)
  val.gsub(/(?<!\\)"/, '\\"')
end

.fill_option(val) ⇒ Object



60
61
62
63
64
# File 'lib/squared/common/shell.rb', line 60

def fill_option(val)
  return "-#{val}" if val =~ /\A[a-z](?:\d*)\z/i

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

.quote_option(flag, val) ⇒ Object



66
67
68
# File 'lib/squared/common/shell.rb', line 66

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

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



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/squared/common/shell.rb', line 11

def shell_escape(val, quote: false, force: false)
  if (data = /\A(--?[^= ]+)((=|\s+)(["'])?(.+?)(["'])?)?\z/m.match(val = val.to_s))
    return val if !data[2] || (!data[4] && !data[5].match?(/\s/))

    join = ->(opt) { data[1] + data[3] + shell_quote(opt) }
    if data[4] == data[6]
      data[4] ? val : join.(data[5])
    else
      join.(data[4] + data[5] + data[6])
    end
  elsif Rake::Win32.windows?
    quote ? shell_quote(val, force: force) : val
  else
    val.empty? ? '' : Shellwords.escape(val)
  end
end

.shell_option(flag, val = nil, escape: true, quote: true, force: true) ⇒ Object



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

def shell_option(flag, val = nil, escape: true, quote: true, force: true)
  flag = flag.to_s
  if flag[0] == '-'
    b = flag[1] == '-' ? '=' : ' '
  else
    a, b = flag.size == 1 ? ['-', ' '] : ['--', '=']
  end
  "#{a}#{flag}#{if val
                  "#{b}#{if escape
                           shell_escape(val, quote: quote)
                         else
                           quote ? shell_quote(val, force: force) : val
                         end}"
                end}"
end

.shell_quote(val, force: true) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/squared/common/shell.rb', line 28

def shell_quote(val, force: true)
  val = val.to_s
  return val if !force && !val.include?(' ')
  return val if option && val =~ /(?:^|\S=|[^=]\s+|#{Rake::Win32.windows? ? '[\\\/]' : '\/'})(["']).+\1\z/m

  Rake::Win32.windows? || ARG[:QUOTE] == '"' ? "\"#{double_quote(val)}\"" : "'#{single_quote(val)}'"
end

.shell_split(val, escape: true, quote: false, join: nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/squared/common/shell.rb', line 36

def shell_split(val, escape: true, quote: false, join: nil)
  val = Shellwords.split(val)
  val = val.map { |opt| shell_escape(opt, quote: quote) } if escape
  return val unless join

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

.single_quote(val) ⇒ Object



74
75
76
# File 'lib/squared/common/shell.rb', line 74

def single_quote(val)
  val.gsub("'", "'\\\\''")
end

.split_escape(val, char: ',') ⇒ Object



82
83
84
# File 'lib/squared/common/shell.rb', line 82

def split_escape(val, char: ',')
  val.split(/\s*(?<!\\)#{char}\s*/)
end