aruba 0.8.1 → 0.9.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/.travis.yml +3 -1
- data/Gemfile +13 -10
- data/History.md +25 -7
- data/README.md +6 -1
- data/Rakefile +16 -3
- data/aruba.gemspec +10 -2
- data/bin/aruba +7 -0
- data/cucumber.yml +11 -2
- data/features/api/command/extract_text.feature +97 -0
- data/features/api/command/unescape_text.feature +119 -0
- data/features/api/core/expand_path.feature +16 -0
- data/features/api/{cd.feature → filesystem/cd.feature} +2 -1
- data/features/api/filesystem/create_directory.feature +57 -0
- data/features/api/filesystem/disk_usage.feature +151 -0
- data/features/api/filesystem/does_exist.feature +62 -0
- data/features/api/filesystem/is_absolute.feature +48 -0
- data/features/api/filesystem/is_directory.feature +53 -0
- data/features/api/filesystem/is_file.feature +54 -0
- data/features/api/filesystem/is_relative.feature +48 -0
- data/features/cli/console.feature +52 -0
- data/features/configuration/console_history_file.feature +38 -0
- data/features/configuration/exit_timeout.feature +2 -1
- data/features/configuration/fixtures_path_prefix.feature +0 -1
- data/features/configuration/home_directory.feature +0 -1
- data/features/configuration/io_timeout.feature +0 -1
- data/features/configuration/keep_ansi.feature +1 -2
- data/features/configuration/log_level.feature +38 -0
- data/features/configuration/physical_block_size.feature +53 -0
- data/features/configuration/remove_ansi_escape_sequences.feature +38 -0
- data/features/configuration/root_directory.feature +0 -1
- data/features/configuration/working_directory.feature +2 -1
- data/features/fixtures/cli-app/README.md +5 -0
- data/features/fixtures/cli-app/lib/cli/app.rb +1 -1
- data/features/fixtures/cli-app/spec/spec_helper.rb +1 -2
- data/features/fixtures/cli-app/spec/support/aruba.rb +1 -0
- data/features/fixtures/empty-app/lib/cli/app.rb +1 -1
- data/features/fixtures/empty-app/spec/spec_helper.rb +1 -1
- data/features/step_definitions/aruba_dev_steps.rb +12 -1
- data/features/support/env.rb +1 -1
- data/lib/aruba/announcer.rb +1 -14
- data/lib/aruba/api.rb +6 -6
- data/lib/aruba/api/command.rb +74 -12
- data/lib/aruba/api/core.rb +18 -11
- data/lib/aruba/api/deprecated.rb +60 -72
- data/lib/aruba/api/filesystem.rb +41 -16
- data/lib/aruba/aruba_logger.rb +77 -0
- data/lib/aruba/aruba_path.rb +33 -0
- data/lib/aruba/basic_configuration.rb +2 -2
- data/lib/aruba/cli.rb +11 -0
- data/lib/aruba/config.rb +13 -3
- data/lib/aruba/config/jruby.rb +5 -2
- data/lib/aruba/console.rb +66 -0
- data/lib/aruba/console/help.rb +33 -0
- data/lib/aruba/contracts/is_power_of_two.rb +15 -0
- data/lib/aruba/cucumber.rb +27 -10
- data/lib/aruba/cucumber/hooks.rb +15 -8
- data/lib/aruba/disk_usage_calculator.rb +7 -0
- data/lib/aruba/file_size.rb +52 -0
- data/lib/aruba/in_process.rb +1 -1
- data/lib/aruba/jruby.rb +1 -1
- data/lib/aruba/matchers/command.rb +1 -1
- data/lib/aruba/matchers/directory.rb +1 -1
- data/lib/aruba/matchers/environment.rb +1 -1
- data/lib/aruba/matchers/file.rb +1 -1
- data/lib/aruba/matchers/path.rb +1 -1
- data/lib/aruba/matchers/path/match_path_pattern.rb +1 -1
- data/lib/aruba/matchers/rspec_matcher_include_regexp.rb +1 -1
- data/lib/aruba/platform.rb +14 -242
- data/lib/aruba/{creators → platforms}/aruba_file_creator.rb +3 -3
- data/lib/aruba/{creators → platforms}/aruba_fixed_size_file_creator.rb +2 -2
- data/lib/aruba/platforms/simple_table.rb +36 -0
- data/lib/aruba/platforms/unix_command_string.rb +24 -0
- data/lib/aruba/platforms/unix_environment_variables.rb +130 -0
- data/lib/aruba/platforms/unix_platform.rb +226 -0
- data/lib/aruba/platforms/unix_which.rb +83 -0
- data/lib/aruba/platforms/windows_command_string.rb +23 -0
- data/lib/aruba/platforms/windows_environment_variables.rb +83 -0
- data/lib/aruba/platforms/windows_platform.rb +39 -0
- data/lib/aruba/platforms/windows_which.rb +104 -0
- data/lib/aruba/process_monitor.rb +3 -3
- data/lib/aruba/processes/basic_process.rb +23 -3
- data/lib/aruba/processes/debug_process.rb +2 -2
- data/lib/aruba/processes/in_process.rb +1 -1
- data/lib/aruba/processes/spawn_process.rb +10 -4
- data/lib/aruba/reporting.rb +1 -1
- data/lib/aruba/rspec.rb +8 -6
- data/lib/aruba/runtime.rb +8 -5
- data/lib/aruba/version.rb +1 -1
- data/spec/aruba/api_spec.rb +25 -23
- data/spec/aruba/aruba_path_spec.rb +37 -0
- data/spec/aruba/jruby_spec.rb +1 -1
- data/spec/aruba/matchers/file_spec.rb +2 -2
- data/spec/aruba/matchers/path_spec.rb +3 -3
- data/spec/aruba/platform/simple_table_spec.rb +23 -0
- data/spec/aruba/platform/windows_environment_variables_spec.rb +500 -0
- data/spec/aruba/spawn_process_spec.rb +3 -1
- metadata +90 -12
- data/lib/aruba/contracts/is_a.rb +0 -21
- data/lib/aruba/environment.rb +0 -121
@@ -15,11 +15,11 @@ module Aruba
|
|
15
15
|
# @param [TrueClass, FalseClass] check_presence (false)
|
16
16
|
# Check if file exist
|
17
17
|
def write(path, content, check_presence = false)
|
18
|
-
fail "Expected #{path} to be present" if check_presence && !Aruba
|
18
|
+
fail "Expected #{path} to be present" if check_presence && !Aruba.platform.file?(path)
|
19
19
|
|
20
|
-
Aruba
|
20
|
+
Aruba.platform.mkdir(File.dirname(path))
|
21
21
|
|
22
|
-
if RUBY_VERSION < '1.9'
|
22
|
+
if RUBY_VERSION < '1.9.3'
|
23
23
|
File.open(path, 'w') { |f| f << content }
|
24
24
|
else
|
25
25
|
File.write(path, content)
|
@@ -19,9 +19,9 @@ module Aruba
|
|
19
19
|
# @param [TrueClass, FalseClass] check_presence (false)
|
20
20
|
# Check if file exist
|
21
21
|
def write(path, size, check_presence)
|
22
|
-
fail "Expected #{path} to be present" if check_presence && !Aruba
|
22
|
+
fail "Expected #{path} to be present" if check_presence && !Aruba.platform.file?(path)
|
23
23
|
|
24
|
-
Aruba
|
24
|
+
Aruba.platform.mkdir(File.dirname(path))
|
25
25
|
|
26
26
|
File.open(path, "wb"){ |f| f.seek(size - 1); f.write("\0") }
|
27
27
|
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Aruba
|
2
|
+
module Platforms
|
3
|
+
class SimpleTable
|
4
|
+
private
|
5
|
+
|
6
|
+
attr_reader :hash
|
7
|
+
|
8
|
+
public
|
9
|
+
|
10
|
+
def initialize(hash)
|
11
|
+
@hash = hash
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
longest_key = hash.keys.map(&:to_s).max_by(&:length)
|
16
|
+
return [] if longest_key.nil?
|
17
|
+
|
18
|
+
name_size = longest_key.length
|
19
|
+
|
20
|
+
if RUBY_VERSION < '2'
|
21
|
+
rows = []
|
22
|
+
|
23
|
+
hash.each do |k,v|
|
24
|
+
rows << format("# %-#{name_size}s => %s", k, v)
|
25
|
+
end
|
26
|
+
|
27
|
+
rows.sort
|
28
|
+
else
|
29
|
+
hash.each_with_object([]) do |(k,v), a|
|
30
|
+
a << format("# %-#{name_size}s => %s", k, v)
|
31
|
+
end.sort
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
require 'shellwords'
|
3
|
+
|
4
|
+
module Aruba
|
5
|
+
module Platforms
|
6
|
+
# This is a command which should be run
|
7
|
+
class UnixCommandString < SimpleDelegator
|
8
|
+
def initialize(cmd)
|
9
|
+
__setobj__ cmd
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_a
|
13
|
+
Shellwords.split __getobj__
|
14
|
+
end
|
15
|
+
|
16
|
+
if RUBY_VERSION < '1.9'
|
17
|
+
def to_s
|
18
|
+
__getobj__.to_s
|
19
|
+
end
|
20
|
+
alias_method :inspect, :to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
module Aruba
|
2
|
+
module Platforms
|
3
|
+
class UnixEnvironmentVariables
|
4
|
+
# We need to use this, because `nil` is a valid value as default
|
5
|
+
UNDEFINED = Object.new
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
attr_reader :env
|
10
|
+
|
11
|
+
public
|
12
|
+
|
13
|
+
def initialize(env = ENV.to_hash)
|
14
|
+
@env = Marshal.load(Marshal.dump(env))
|
15
|
+
end
|
16
|
+
|
17
|
+
# Update environment with other en
|
18
|
+
#
|
19
|
+
# @param [#to_hash, #to_h] other_env
|
20
|
+
# Another environment object or hash
|
21
|
+
#
|
22
|
+
# @yield
|
23
|
+
# Pass block to env
|
24
|
+
def update(other_env, &block)
|
25
|
+
if RUBY_VERSION <= '1.9.3'
|
26
|
+
# rubocop:disable Style/EachWithObject
|
27
|
+
other_env = other_env.to_hash.inject({}) { |a, (k, v)| a[k] = v.to_s; a }
|
28
|
+
# rubocop:enable Style/EachWithObject
|
29
|
+
else
|
30
|
+
other_env = other_env.to_h.each_with_object({}) { |(k, v), a| a[k] = v.to_s }
|
31
|
+
end
|
32
|
+
|
33
|
+
env.update(other_env, &block)
|
34
|
+
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
# Fetch variable from environment
|
39
|
+
#
|
40
|
+
# @param [#to_s] name
|
41
|
+
# The name of the variable
|
42
|
+
#
|
43
|
+
# @param [Object] default
|
44
|
+
# The default value used, if the variable is not defined
|
45
|
+
def fetch(name, default = UNDEFINED)
|
46
|
+
if default == UNDEFINED
|
47
|
+
env.fetch name.to_s
|
48
|
+
else
|
49
|
+
env.fetch name.to_s, default
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Check if variable exist
|
54
|
+
#
|
55
|
+
# @param [#to_s] name
|
56
|
+
# The name of the variable
|
57
|
+
def key?(name)
|
58
|
+
env.key? name.to_s
|
59
|
+
end
|
60
|
+
|
61
|
+
# Get value of variable
|
62
|
+
#
|
63
|
+
# @param [#to_s] name
|
64
|
+
# The name of the variable
|
65
|
+
def [](name)
|
66
|
+
env[name.to_s]
|
67
|
+
end
|
68
|
+
|
69
|
+
# Set value of variable
|
70
|
+
#
|
71
|
+
# @param [#to_s] name
|
72
|
+
# The name of the variable
|
73
|
+
#
|
74
|
+
# @param [#to_s] value
|
75
|
+
# The value of the variable
|
76
|
+
def []=(name, value)
|
77
|
+
env[name.to_s] = value.to_s
|
78
|
+
|
79
|
+
self
|
80
|
+
end
|
81
|
+
|
82
|
+
# Append value to variable
|
83
|
+
#
|
84
|
+
# @param [#to_s] name
|
85
|
+
# The name of the variable
|
86
|
+
#
|
87
|
+
# @param [#to_s] value
|
88
|
+
# The value of the variable
|
89
|
+
def append(name, value)
|
90
|
+
name = name.to_s
|
91
|
+
env[name] = env[name].to_s + value.to_s
|
92
|
+
|
93
|
+
self
|
94
|
+
end
|
95
|
+
|
96
|
+
# Prepend value to variable
|
97
|
+
#
|
98
|
+
# @param [#to_s] name
|
99
|
+
# The name of the variable
|
100
|
+
#
|
101
|
+
# @param [#to_s] value
|
102
|
+
# The value of the variable
|
103
|
+
def prepend(name, value)
|
104
|
+
name = name.to_s
|
105
|
+
env[name] = value.to_s + env[name].to_s
|
106
|
+
|
107
|
+
self
|
108
|
+
end
|
109
|
+
|
110
|
+
# Convert to hash
|
111
|
+
#
|
112
|
+
# @return [Hash]
|
113
|
+
# A new hash from environment
|
114
|
+
def to_h
|
115
|
+
if RUBY_VERSION < '2.0'
|
116
|
+
Marshal.load(Marshal.dump(env.to_hash))
|
117
|
+
else
|
118
|
+
Marshal.load(Marshal.dump(env.to_h))
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Reset environment
|
123
|
+
def clear
|
124
|
+
env.clear
|
125
|
+
|
126
|
+
self
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,226 @@
|
|
1
|
+
require 'rbconfig'
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
require 'aruba/platforms/simple_table'
|
5
|
+
require 'aruba/platforms/unix_command_string'
|
6
|
+
require 'aruba/platforms/unix_which'
|
7
|
+
|
8
|
+
module Aruba
|
9
|
+
# This abstracts OS-specific things
|
10
|
+
module Platforms
|
11
|
+
# WARNING:
|
12
|
+
# All methods found here are not considered part of the public API of aruba.
|
13
|
+
#
|
14
|
+
# Those methods can be changed at any time in the feature or removed without
|
15
|
+
# any further notice.
|
16
|
+
#
|
17
|
+
# This includes all methods for the UNIX platform
|
18
|
+
class UnixPlatform
|
19
|
+
def self.match?
|
20
|
+
!FFI::Platform.windows?
|
21
|
+
end
|
22
|
+
|
23
|
+
def environment_variables
|
24
|
+
UnixEnvironmentVariables.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def command_string
|
28
|
+
UnixCommandString
|
29
|
+
end
|
30
|
+
|
31
|
+
def detect_ruby(cmd)
|
32
|
+
if cmd =~ /^ruby\s/
|
33
|
+
cmd.gsub(/^ruby\s/, "#{current_ruby} ")
|
34
|
+
else
|
35
|
+
cmd
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def deprecated(msg)
|
40
|
+
warn(format('%s. Called by %s', msg, caller[1]))
|
41
|
+
end
|
42
|
+
|
43
|
+
def current_ruby
|
44
|
+
::File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
|
45
|
+
end
|
46
|
+
|
47
|
+
# @deprecated
|
48
|
+
# Add newline at the end
|
49
|
+
def ensure_newline(str)
|
50
|
+
deprecated('The use of "#ensure_newline" is deprecated. It will be removed soon')
|
51
|
+
|
52
|
+
str.chomp << "\n"
|
53
|
+
end
|
54
|
+
|
55
|
+
def require_matching_files(pattern, base)
|
56
|
+
if RUBY_VERSION < '1.9.3'
|
57
|
+
::Dir.glob(::File.expand_path(pattern, base)).each { |f| require File.join(File.dirname(f), File.basename(f, '.rb')) }
|
58
|
+
else
|
59
|
+
::Dir.glob(::File.expand_path(pattern, base)).each { |f| require_relative f }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Create directory and subdirectories
|
64
|
+
def mkdir(dir_name)
|
65
|
+
dir_name = ::File.expand_path(dir_name)
|
66
|
+
|
67
|
+
::FileUtils.mkdir_p(dir_name) unless ::File.directory?(dir_name)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Remove file, directory + sub-directories
|
71
|
+
def rm(paths, options = {})
|
72
|
+
paths = Array(paths).map { |p| ::File.expand_path(p) }
|
73
|
+
|
74
|
+
FileUtils.rm_r(paths, options)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Get current working directory
|
78
|
+
def getwd
|
79
|
+
Dir.getwd
|
80
|
+
end
|
81
|
+
|
82
|
+
# Change to directory
|
83
|
+
def chdir(dir_name, &block)
|
84
|
+
dir_name = ::File.expand_path(dir_name.to_s)
|
85
|
+
|
86
|
+
begin
|
87
|
+
if RUBY_VERSION <= '1.9.3'
|
88
|
+
old_env = ENV.to_hash.dup
|
89
|
+
else
|
90
|
+
old_env = ENV.to_h.dup
|
91
|
+
end
|
92
|
+
|
93
|
+
ENV['OLDPWD'] = getwd
|
94
|
+
ENV['PWD'] = dir_name
|
95
|
+
::Dir.chdir(dir_name, &block)
|
96
|
+
ensure
|
97
|
+
ENV.clear
|
98
|
+
ENV.update old_env
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Touch file, directory
|
103
|
+
def touch(args, options)
|
104
|
+
FileUtils.touch(args, options)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Copy file/directory
|
108
|
+
def cp(args, options)
|
109
|
+
FileUtils.cp_r(args, options)
|
110
|
+
end
|
111
|
+
|
112
|
+
# Change mode of file/directory
|
113
|
+
def chmod(mode, args, options)
|
114
|
+
FileUtils.chmod_R(mode, args, options)
|
115
|
+
end
|
116
|
+
|
117
|
+
# Exists and is file
|
118
|
+
def file?(f)
|
119
|
+
File.file? f
|
120
|
+
end
|
121
|
+
|
122
|
+
# Exists and is directory
|
123
|
+
def directory?(f)
|
124
|
+
File.directory? f
|
125
|
+
end
|
126
|
+
|
127
|
+
# Path Exists
|
128
|
+
def exist?(f)
|
129
|
+
File.exist? f
|
130
|
+
end
|
131
|
+
|
132
|
+
# Path is executable
|
133
|
+
def executable_file?(f)
|
134
|
+
File.file?(f) && File.executable?(f)
|
135
|
+
end
|
136
|
+
|
137
|
+
# Expand path
|
138
|
+
def expand_path(path, base)
|
139
|
+
File.expand_path(path, base)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Is absolute path
|
143
|
+
def absolute_path?(path)
|
144
|
+
Pathname.new(path).absolute?
|
145
|
+
end
|
146
|
+
|
147
|
+
# Is relative path
|
148
|
+
def relative_path?(path)
|
149
|
+
Pathname.new(path).relative?
|
150
|
+
end
|
151
|
+
|
152
|
+
# Check if command is relative
|
153
|
+
#
|
154
|
+
# @return [TrueClass, FalseClass]
|
155
|
+
# true
|
156
|
+
# * bin/command.sh
|
157
|
+
#
|
158
|
+
# false
|
159
|
+
# * /bin/command.sh
|
160
|
+
# * command.sh
|
161
|
+
def relative_command?(path)
|
162
|
+
p = Pathname.new(path)
|
163
|
+
p.relative? && p.basename != p
|
164
|
+
end
|
165
|
+
|
166
|
+
# Check if command is relative
|
167
|
+
#
|
168
|
+
# @return [TrueClass, FalseClass]
|
169
|
+
# true
|
170
|
+
# * command.sh
|
171
|
+
#
|
172
|
+
# false
|
173
|
+
# * /bin/command.sh
|
174
|
+
# * bin/command.sh
|
175
|
+
def command?(path)
|
176
|
+
p = Pathname.new(path)
|
177
|
+
p.relative? && p.basename == p
|
178
|
+
end
|
179
|
+
|
180
|
+
# Write to file
|
181
|
+
def write_file(path, content)
|
182
|
+
if RUBY_VERSION < '1.9.3'
|
183
|
+
File.open(path, 'wb') do |f|
|
184
|
+
f.print content
|
185
|
+
end
|
186
|
+
else
|
187
|
+
File.write(path, content)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
# Unescape string
|
192
|
+
#
|
193
|
+
# @param [String] string
|
194
|
+
# The string which should be unescaped, e.g. the output of a command
|
195
|
+
#
|
196
|
+
# @return
|
197
|
+
# The string stripped from escape sequences
|
198
|
+
def unescape(string, keep_ansi = true)
|
199
|
+
deprecated('The use of "Aruba.platform.unescape" is deprecated. Please use "#unescape_text" and "#extract_text" instead')
|
200
|
+
|
201
|
+
string = string.gsub('\n', "\n").gsub('\"', '"').gsub('\e', "\e")
|
202
|
+
string = string.gsub(/\e\[\d+(?>(;\d+)*)m/, '') unless keep_ansi
|
203
|
+
string
|
204
|
+
end
|
205
|
+
|
206
|
+
# Transform hash to a string table which can be output on stderr/stdout
|
207
|
+
def simple_table(hash)
|
208
|
+
SimpleTable.new(hash).to_s
|
209
|
+
end
|
210
|
+
|
211
|
+
# Resolve path for command using the PATH-environment variable
|
212
|
+
#
|
213
|
+
# Mostly taken from here: https://github.com/djberg96/ptools
|
214
|
+
#
|
215
|
+
# @param [#to_s] program
|
216
|
+
# The name of the program which should be resolved
|
217
|
+
#
|
218
|
+
# @param [String] path
|
219
|
+
# The PATH, a string concatenated with ":", e.g. /usr/bin/:/bin on a
|
220
|
+
# UNIX-system
|
221
|
+
def which(program, path = ENV['PATH'])
|
222
|
+
UnixWhich.new.call(program, path)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'aruba/platform'
|
2
|
+
|
3
|
+
module Aruba
|
4
|
+
module Platforms
|
5
|
+
# Implement `which(command)` for UNIX/Linux
|
6
|
+
class UnixWhich
|
7
|
+
# Bail out because this should never be reached
|
8
|
+
class DefaultWhich
|
9
|
+
def self.match?(*)
|
10
|
+
true
|
11
|
+
end
|
12
|
+
|
13
|
+
def call(program, path)
|
14
|
+
fail %(Invalid input program "#{program}" and/or path "#{path}".)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Find path for absolute or relative command
|
19
|
+
class AbsoluteOrRelativePathWhich
|
20
|
+
def self.match?(program)
|
21
|
+
Aruba.platform.absolute_path?(program) || Aruba.platform.relative_command?(program)
|
22
|
+
end
|
23
|
+
|
24
|
+
def call(program, path)
|
25
|
+
return File.expand_path(program) if Aruba.platform.executable_file?(program)
|
26
|
+
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Find path for command
|
32
|
+
class ProgramWhich
|
33
|
+
def self.match?(program)
|
34
|
+
Aruba.platform.command?(program)
|
35
|
+
end
|
36
|
+
|
37
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
38
|
+
# rubocop:disable Metrics/MethodLength
|
39
|
+
def call(program, path)
|
40
|
+
# Iterate over each path glob the dir + program.
|
41
|
+
path.split(File::PATH_SEPARATOR).each do |dir|
|
42
|
+
dir = Aruba.platform.expand_path(dir, Dir.getwd)
|
43
|
+
next unless Aruba.platform.exist?(dir) # In case of bogus second argument
|
44
|
+
|
45
|
+
found = Dir[File.join(dir, program)].first
|
46
|
+
return found if found && Aruba.platform.executable_file?(found)
|
47
|
+
end
|
48
|
+
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
52
|
+
# rubocop:enable Metrics/MethodLength
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
attr_reader :whiches
|
58
|
+
|
59
|
+
public
|
60
|
+
|
61
|
+
def initialize
|
62
|
+
@whiches = []
|
63
|
+
@whiches << AbsoluteOrRelativePathWhich
|
64
|
+
@whiches << ProgramWhich
|
65
|
+
@whiches << DefaultWhich
|
66
|
+
end
|
67
|
+
|
68
|
+
# Find fully quallified path for program
|
69
|
+
#
|
70
|
+
# @param [String] program
|
71
|
+
# Name of program
|
72
|
+
#
|
73
|
+
# @param [String] path
|
74
|
+
# ENV['PATH']
|
75
|
+
def call(program, path = ENV['PATH'])
|
76
|
+
raise ArgumentError, "ENV['PATH'] cannot be empty" if path.nil? || path.empty?
|
77
|
+
program = program.to_s
|
78
|
+
|
79
|
+
whiches.find { |w| w.match? program }.new.call(program, path)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|