Module: PG::SafeSys
- Defined in:
- lib/pg-safesys.rb,
lib/pg-safesys/version.rb
Constant Summary collapse
- VERSION =
"0.0.6"
Instance Method Summary collapse
- #close_all(*streams) ⇒ Object
- #nix_sys(*commands) ⇒ Object
- #replace_in_file(filepath, regexp, *args, &block) ⇒ Object
-
#safe_system(*cmd) ⇒ Object
NOTE: win_sys executes in separate processes, nix_sys in one shell attempting to cd, for instance, will only work for nix_sys.
- #win_sys(*commands) ⇒ Object
Instance Method Details
#close_all(*streams) ⇒ Object
53 54 55 |
# File 'lib/pg-safesys.rb', line 53 def close_all(*streams) streams.each { |s| s.close } end |
#nix_sys(*commands) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pg-safesys.rb', line 38 def nix_sys(*commands) sh = Session.new output = [] commands.each do |cmd| _out, _err = sh.execute cmd if sh.exit_status > 0 sh.close raise IOError, "\"#{ cmd }\": #{ _err }" end output << _out end sh.close output end |
#replace_in_file(filepath, regexp, *args, &block) ⇒ Object
57 58 59 60 |
# File 'lib/pg-safesys.rb', line 57 def replace_in_file(filepath, regexp, *args, &block) content = File.read(filepath).gsub(regexp, *args, &block) File.open(filepath, 'wb') { |file| file.write(content) } end |
#safe_system(*cmd) ⇒ Object
NOTE: win_sys executes in separate processes, nix_sys in one shell attempting to cd, for instance, will only work for nix_sys
16 17 18 19 |
# File 'lib/pg-safesys.rb', line 16 def safe_system(*cmd) is_windows = (!!RUBY_PLATFORM['mswin32'] || !!RUBY_PLATFORM['mingw'] || !!RUBY_PLATFORM['cygwin']) is_windows ? win_sys(*cmd) : nix_sys(*cmd) end |
#win_sys(*commands) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/pg-safesys.rb', line 21 def win_sys(*commands) output = [] commands.each do |cmd| _in, _out, _err = Open3.popen3(cmd) error = _err.read if error != "" close_all _in, _out, _err raise IOError, "\"#{ cmd }\": #{ error }" end output << _out.read close_all _in, _out, _err end output end |