Module: Squared::Common::Shell

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

Instance Method Summary collapse

Instance Method Details

#double_quote(val) ⇒ Object



23
24
25
# File 'lib/squared/common/shell.rb', line 23

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

#sanitize_args(*opts) ⇒ Object



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

def sanitize_args(*opts)
  opts.map { |val| val.include?(' ') ? shell_quote(val) : shell_escape(val) }.join(' ')
end

#shell_escape(val) ⇒ Object



6
7
8
9
10
# File 'lib/squared/common/shell.rb', line 6

def shell_escape(val)
  return val if ::Rake::Win32.windows?

  Shellwords.escape(val)
end

#shell_quote(val, force: true) ⇒ Object



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

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

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

#single_quote(val) ⇒ Object



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

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

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



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

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

#trailing_slash(val) ⇒ Object



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

def trailing_slash(val)
  val.to_s.chomp(::File::SEPARATOR) + ::File::SEPARATOR
end