Module: Squared::Common::Shell

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

Class Method Summary collapse

Class Method Details

.double_quote(val) ⇒ Object



48
49
50
# File 'lib/squared/common/shell.rb', line 48

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

.fill_option(val) ⇒ Object



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

def fill_option(val)
  return "-#{val}" if val.size == 1 || val =~ /^[a-z]\d+$/i

  val = "--#{val}" unless val.start_with?('-')
  shell_escape(val).sub('\\=', '=')
end

.shell_escape(val, quote: false) ⇒ Object



11
12
13
14
15
# File 'lib/squared/common/shell.rb', line 11

def shell_escape(val, quote: false)
  return Shellwords.escape(val) unless ::Rake::Win32.windows?

  quote ? shell_quote(opt, force: false) : opt
end

.shell_quote(val, force: true) ⇒ Object



17
18
19
20
21
22
# File 'lib/squared/common/shell.rb', line 17

def shell_quote(val, force: true)
  ret = val.to_s.strip
  return ret if (!force && !ret.include?(' ')) || ret =~ /(?:^|\S=|[^=]\s+)(["']).+\1$/m

  ::Rake::Win32.windows? ? "\"#{double_quote(ret)}\"" : "'#{single_quote(ret)}'"
end

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/squared/common/shell.rb', line 24

def shell_split(val, quote: false, join: false)
  ret = Shellwords.split(val).map do |opt|
    if (data = /^(--?[^= ]+)(=|\s+)?(["'])?(.+?)\3?$/m.match(opt))
      next opt unless data[2] && !data[3]

      data[1] + data[2] + shell_quote(data[4], force: !::Rake::Win32.windows?)
    else
      shell_escape(opt, quote: quote)
    end
  end
  join ? ret.join(' ') : ret
end

.single_quote(val) ⇒ Object



44
45
46
# File 'lib/squared/common/shell.rb', line 44

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

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



52
53
54
# File 'lib/squared/common/shell.rb', line 52

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