Module: Squared::Common::Shell

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

Class Method Summary collapse

Class Method Details

.double_quote(val) ⇒ Object



35
36
37
# File 'lib/squared/common/shell.rb', line 35

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

.fill_option(val) ⇒ Object



24
25
26
27
28
29
# File 'lib/squared/common/shell.rb', line 24

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

.sanitize_args(*opts) ⇒ Object



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

def sanitize_args(*opts)
  opts.map { |val| val.include?(' ') ? shell_quote(val) : shell_escape(val) }.join(' ')
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(val, force: false) : val
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 =~ /(?:^|=)(["']).+\1$/m

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

.single_quote(val) ⇒ Object



31
32
33
# File 'lib/squared/common/shell.rb', line 31

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

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



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

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