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.1"
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
``
53 54 55 56 57 58 |
# File 'lib/verbose-shell.rb', line 53 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
93 94 95 |
# File 'lib/verbose-shell.rb', line 93 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
96 97 98 |
# File 'lib/verbose-shell.rb', line 96 def chmod_R_formatter(method, mode, file) chmod_formatter(method, mode, file) end |
.chown_formatter(method, user, group, file) ⇒ Object
99 100 101 |
# File 'lib/verbose-shell.rb', line 99 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
102 103 104 |
# File 'lib/verbose-shell.rb', line 102 def chown_R_formatter(method, user, group, file) chown_formatter(method, user, group, file) end |
.install_D(source, dest) ⇒ Object
81 82 83 84 85 |
# File 'lib/verbose-shell.rb', line 81 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
109 110 111 112 113 |
# File 'lib/verbose-shell.rb', line 109 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
87 88 89 90 91 |
# File 'lib/verbose-shell.rb', line 87 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
105 106 107 |
# File 'lib/verbose-shell.rb', line 105 def mkdir_p_formatter(method, file) method_to_a(method) + (file.is_a?(Array) ? file : [file]) end |
.popen(*args, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/verbose-shell.rb', line 65 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 |
# 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)..lstrip else output = IO.popen(args, opts.merge({:err => [:child, :out]})) {|io| io.read} raise ShellError.new(output, args, $?.exitstatus) if $? != 0 end end |
.system_or_true(*args) ⇒ Object
48 49 50 51 |
# File 'lib/verbose-shell.rb', line 48 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
``
60 61 62 63 |
# File 'lib/verbose-shell.rb', line 60 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
42 43 44 45 46 |
# File 'lib/verbose-shell.rb', line 42 def try_system(*args) system *args rescue ShellError => e e.exit_code end |
.which?(exe) ⇒ Boolean
76 77 78 79 |
# File 'lib/verbose-shell.rb', line 76 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 |