Module: Shella::Shell
- Defined in:
- lib/shella/shell.rb
Class Method Summary collapse
-
.call(file) ⇒ Object
execute another scrit from this.
-
.method_missing(method, *args, &block) ⇒ Object
the core functionality : any command send to the module is redirected to the shell.
-
.popen(cmd) ⇒ Object
execute a shell command and get the sdin, stdout and stderr output.
-
.rmdir(filename) ⇒ Object
delete a directory and its contents.
-
.set(env_var) ⇒ Object
set an environment variable.
Class Method Details
.call(file) ⇒ Object
execute another scrit from this
14 15 16 17 18 19 20 21 |
# File 'lib/shella/shell.rb', line 14 def self.call(file) File.open(file) do |f| f.each_line do |line| command, args = line.split(" ", 2) self.send(command, args) end end end |
.method_missing(method, *args, &block) ⇒ Object
the core functionality : any command send to the module is redirected to the shell
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/shella/shell.rb', line 69 def self.method_missing(method, *args, &block) command = "#{method.to_s.gsub(/_/, ' ')} #{args[0]}" begin popen(command) do |stdin, stdout, stderr| puts stdout.read if not stdout.eof? puts stderr.read if not stderr.eof? end rescue Errno::ENOENT puts "unknown command : #{command}" end end |
.popen(cmd) ⇒ Object
execute a shell command and get the sdin, stdout and stderr output
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/shella/shell.rb', line 35 def self.popen(cmd) @env ||= ENV pw = IO::pipe # pipe[0] for read, pipe[1] for write pr = IO::pipe pe = IO::pipe pid = spawn( @env, cmd, STDIN=>pw[0], STDOUT=>pr[1], STDERR=>pe[1], :unsetenv_others => true ) wait_thr = Process.detach(pid) pw[0].close pr[1].close pe[1].close pi = [pw[1], pr[0], pe[0], wait_thr] pw[1].sync = true if defined? yield begin return yield(*pi) ensure [pw[1], pr[0], pe[0]].each{|p| p.close unless p.closed?} wait_thr.join end end pi end |
.rmdir(filename) ⇒ Object
delete a directory and its contents
24 25 26 27 28 29 30 31 32 |
# File 'lib/shella/shell.rb', line 24 def self.rmdir(filename) if File.exists? filename if File.directory? filename FileUtils.rm_r filename else FileUtils.rm filename end end end |
.set(env_var) ⇒ Object
set an environment variable
6 7 8 9 10 11 |
# File 'lib/shella/shell.rb', line 6 def self.set(env_var) var, value = env_var.split('=', 2) @env ||= {} @env.merge! ENV @env[var] = value end |