Class: VerboseShell
- Inherits:
-
Object
- Object
- VerboseShell
- Defined in:
- lib/verbose-shell.rb,
lib/verbose-shell/version.rb
Defined Under Namespace
Classes: ShellError
Constant Summary collapse
- VERSION =
"0.3.2"
Class Attribute Summary collapse
-
.verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
-
.capture(*args) ⇒ Object
``.
- .chmod_formatter(method, mode, file) ⇒ Object
- .chmod_R_formatter(method, mode, file) ⇒ Object
- .chown_formatter(method, user, group, file) ⇒ Object
- .chown_R_formatter(method, user, group, file) ⇒ Object
- .install_D(source, dest) ⇒ Object
- .method_missing(method, *args, &block) ⇒ Object
- .method_to_a(method) ⇒ Object
- .mkdir_p_formatter(method, file) ⇒ Object
- .popen(*args, &block) ⇒ Object
- .system(*args) ⇒ Object
- .system_or_true(*args) ⇒ Object
- .system_trace(*args) ⇒ Object
-
.try_capture(*args) ⇒ Object
``.
- .try_system(*args) ⇒ Object
- .which?(exe) ⇒ Boolean
Class Attribute Details
.verbose ⇒ Object
Returns the value of attribute verbose.
23 24 25 |
# File 'lib/verbose-shell.rb', line 23 def verbose @verbose end |
Class Method Details
.capture(*args) ⇒ Object
``
54 55 56 57 58 59 |
# File 'lib/verbose-shell.rb', line 54 def capture(*args) # `` system_trace *args ret = IO.popen(args + (@verbose == 0 ? [{:err => "/dev/null"}] : []), "r") {|io| io.read}.strip raise ShellError.new(ret, args, $?.exitstatus) unless $? == 0 ret end |
.chmod_formatter(method, mode, file) ⇒ Object
94 95 96 |
# File 'lib/verbose-shell.rb', line 94 def chmod_formatter(method, mode, file) method_to_a(method) + [mode.class == String ? mode : sprintf("%04o", mode)] + (file.class == Array ? file : [file]) end |
.chmod_R_formatter(method, mode, file) ⇒ Object
97 98 99 |
# File 'lib/verbose-shell.rb', line 97 def chmod_R_formatter(method, mode, file) chmod_formatter(method, mode, file) end |
.chown_formatter(method, user, group, file) ⇒ Object
100 101 102 |
# File 'lib/verbose-shell.rb', line 100 def chown_formatter(method, user, group, file) method_to_a(method) + [[user,group && ":#{group}"].select{|x|x}.join('')] + (file.class == Array ? file : [file]) end |
.chown_R_formatter(method, user, group, file) ⇒ Object
103 104 105 |
# File 'lib/verbose-shell.rb', line 103 def chown_R_formatter(method, user, group, file) chown_formatter(method, user, group, file) end |
.install_D(source, dest) ⇒ Object
82 83 84 85 86 |
# File 'lib/verbose-shell.rb', line 82 def install_D(source, dest) system_trace 'install', '-D', source, dest FileUtils.mkdir_p(File.dirname(dest)) FileUtils.install(source, dest) end |
.method_missing(method, *args, &block) ⇒ Object
110 111 112 113 114 |
# File 'lib/verbose-shell.rb', line 110 def method_missing(method, *args, &block) system_trace *(self.respond_to?("#{method}_formatter") ? self.send("#{method}_formatter", method, *args) : method_to_a(method) + args) FileUtils.send(method, *args, &block) end |
.method_to_a(method) ⇒ Object
88 89 90 91 92 |
# File 'lib/verbose-shell.rb', line 88 def method_to_a(method) a = method.to_s.split('_') a[1] = "-#{a[1]}" if a[1] a end |
.mkdir_p_formatter(method, file) ⇒ Object
106 107 108 |
# File 'lib/verbose-shell.rb', line 106 def mkdir_p_formatter(method, file) method_to_a(method) + (file.is_a?(Array) ? file : [file]) end |
.popen(*args, &block) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/verbose-shell.rb', line 66 def popen(*args, &block) env = args.first.is_a?(Hash) && args.unshift opts = args.last.is_a?(Hash) && args.pop mode = args.length == 2 ? args.pop : 'r' cmd = args[0] system_trace *cmd ret = IO.popen(*([env, cmd, mode, opts].select {|v| v}.to_a), &block) raise ShellError.new('', cmd, $?.exitstatus) if block_given? and $? != 0 ret end |
.system(*args) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/verbose-shell.rb', line 31 def system(*args) system_trace *args args, opts = args.last.is_a?(Hash) ? [args[0..-2], args.last.dup] : [args, {}] if opts.delete(:loud) or @verbose > 0 Kernel.system(*args, opts) or raise ShellError.new('', args, $?.exitstatus) else output = IO.popen(args, opts.merge({:err => [:child, :out]})) {|io| io.read} raise ShellError.new(output, args, $?.exitstatus) if $? != 0 end $?.exitstatus end |
.system_or_true(*args) ⇒ Object
49 50 51 52 |
# File 'lib/verbose-shell.rb', line 49 def system_or_true(*args) warn "system_or_true is deprecated, use try_system", category: :deprecated, uplevel: 1 try_system(*args) end |
.system_trace(*args) ⇒ Object
26 27 28 29 |
# File 'lib/verbose-shell.rb', line 26 def system_trace(*args) return unless @verbose > 0 puts "+ "+args.map{|a| a =~ /\s/ ? '"'+a+'"' : a}.join(' ') # TODO: Fix quoting so it's more shell-like end |
.try_capture(*args) ⇒ Object
``
61 62 63 64 |
# File 'lib/verbose-shell.rb', line 61 def try_capture(*args) # `` system_trace *args ret = IO.popen(args + (@verbose == 0 ? [{:err => "/dev/null"}] : []), "r") {|io| io.read}.strip end |
.try_system(*args) ⇒ Object
43 44 45 46 47 |
# File 'lib/verbose-shell.rb', line 43 def try_system(*args) system *args rescue ShellError => e e.exit_code end |
.which?(exe) ⇒ Boolean
77 78 79 80 |
# File 'lib/verbose-shell.rb', line 77 def which?(exe) system_trace 'which', exe [exe, *ENV['PATH'].split(File::PATH_SEPARATOR).map {|p| File.join(p, exe)}].find {|f| File.executable?(f)} end |