Module: Tap::Utils

Included in:
Declarations::Context, Signals::Load, Tasks::Prompt
Defined in:
lib/tap/utils.rb

Overview

Windows

MSDOS has command line length limits specific to the version of Windows being run (from www.ss64.com/nt/cmd.html):

Windows NT

256 characters

Windows 2000

2046 characters

Windows XP

8190 characters

Commands longer than these limits fail, usually with something like: ‘the input line is too long’

Class Method Summary collapse

Class Method Details

.set_env(env = {}, replace = false) ⇒ Object

Sets the specified ENV variables and returns the current env. If replace is true, current ENV variables are replaced; otherwise the new env variables are simply added to the existing set.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/tap/utils.rb', line 38

def set_env(env={}, replace=false)
  current_env = {}
  ENV.each_pair do |key, value|
    current_env[key] = value
  end
  
  ENV.clear if replace
  
  env.each_pair do |key, value|
    if value.nil?
      ENV.delete(key)
    else
      ENV[key] = value
    end
  end if env
  
  current_env
end

.sh(*cmd) ⇒ Object

Run the command with system and raise an error if it fails.



79
80
81
# File 'lib/tap/utils.rb', line 79

def sh(*cmd)
  system(*cmd) or raise "Command failed with status (#{$?.exitstatus}): [#{cmd.join(' ')}]"
end

.sh_escape(str) ⇒ Object



74
75
76
# File 'lib/tap/utils.rb', line 74

def sh_escape(str)
  str.to_s.gsub("'", "\\\\'").gsub(";", '\\;')
end

.shellsplit(line, comment = "#") ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tap/utils.rb', line 19

def shellsplit(line, comment="#")
  words = []
  field = ''
  line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
    |word, sq, dq, esc, garbage, sep|
    raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage
    break if word == comment
    field << (word || sq || (dq || esc).gsub(/\\(?=.)/, ''))
    if sep
      words << field
      field = ''
    end
  end
  words
end

.with_env(env = {}, replace = false) ⇒ Object

Sets the specified ENV variables for the duration of the block. If replace is true, current ENV variables are replaced; otherwise the new env variables are simply added to the existing set.

Returns the block return.



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tap/utils.rb', line 62

def with_env(env={}, replace=false)
  current_env = nil
  begin
    current_env = set_env(env, replace)
    yield
  ensure
    if current_env
      set_env(current_env, true)
    end
  end
end