/
util.rb
56 lines (48 loc) · 1.61 KB
/
util.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require "fpm/namespace"
# Some utility functions
module FPM::Util
# Raised if safesystem cannot find the program to run.
class ExecutableNotFound < StandardError; end
# Raised if a safesystem program exits nonzero
class ProcessFailed < StandardError; end
# Is the given program in the system's PATH?
def program_in_path?(program)
# Scan path to find the executable
# Do this to help the user get a better error message.
envpath = ENV["PATH"].split(":")
return envpath.select { |p| File.executable?(File.join(p, program)) }.any?
end # def program_in_path
# Run a command safely in a way that gets reports useful errors.
def safesystem(*args)
program = args[0]
# Scan path to find the executable
# Do this to help the user get a better error message.
if !program.include?("/") and !program_in_path?(program)
raise ExecutableNotFound.new(program)
end
@logger.debug("Running command", :args => args)
success = system(*args)
if !success
raise ProcessFailed.new("#{program} failed (exit code #{$?.exitstatus})" \
". Full command was:#{args.inspect}")
end
return success
end # def safesystem
# Get the recommended 'tar' command for this platform.
def tar_cmd
# Rely on gnu tar for solaris and OSX.
case %x{uname -s}.chomp
when "SunOS"
return "gtar"
when "Darwin"
return "gnutar"
else
return "tar"
end
end # def tar_cmd
# Run a block with a value.
# Useful in lieu of assigning variables
def with(value, &block)
block.call(value)
end # def with
end # module FPM::Util