Module: BashLike
- Defined in:
- lib/dorkbox.rb
Constant Summary collapse
- ALWAYS_PRINT_STDOUT =
false
- ALWAYS_ECHO_CMDLINE =
true
- EXCEPTION_ON_NONZERO_STATUS =
true
- LOGFILE =
nil
- LOGFILE_MAX_LINES =
5000
Instance Method Summary collapse
- #`(cmdline) ⇒ Object
- #c(cmdline, print_each_line = ALWAYS_PRINT_STDOUT) ⇒ Object
- #log(message) ⇒ Object
Instance Method Details
#`(cmdline) ⇒ Object
12 13 14 15 |
# File 'lib/dorkbox.rb', line 12 def `(cmdline) c(cmdline, true) return $exit == 0 ? true : false end |
#c(cmdline, print_each_line = ALWAYS_PRINT_STDOUT) ⇒ Object
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 |
# File 'lib/dorkbox.rb', line 17 def c(cmdline, print_each_line=ALWAYS_PRINT_STDOUT) $stdout.puts "+ #{cmdline}" if ALWAYS_ECHO_CMDLINE Open3.popen3(cmdline) { |stdin, stdout, stderr, wait_thr| all_out = '' all_err = '' while wait_thr.alive? if !(stderr.ready? || stdout.ready?) sleep(0.1) next end current_out = stdout.read(stdout.nread) current_err = stderr.read(stderr.nread) all_out = all_out + current_out all_err = all_err + current_err $stdout.puts current_out if (print_each_line && !current_out.empty?) $stderr.puts current_err if !current_err.empty? end status = wait_thr.value # do it once more to catch everything that was buffered current_out = stdout.read() current_err = stderr.read() all_out = all_out + current_out all_err = all_err + current_err $stdout.puts current_out if (print_each_line && !current_out.empty?) $stderr.puts current_err if !current_err.empty? $out = all_out $err = all_err $exit = status.exitstatus raise StandardError.new("ERROR: #{cmdline.split()[0]} -> exit status #{status.exitstatus}") if (status.exitstatus != 0 && EXCEPTION_ON_NONZERO_STATUS) all_out } end |
#log(message) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dorkbox.rb', line 50 def log() if $global_log.nil? and !LOGFILE.nil? # remove the exceeding lines if File.exists?(LOGFILE) f = File.open(LOGFILE, "r") lines = f.readlines() retain_from = [lines.size, LOGFILE_MAX_LINES].min retain_lines = lines[-retain_from..-1] f.close() else retain_lines=[] end $global_log = File.open(LOGFILE, "w") $global_log.write(retain_lines.join('')) end = "[#{Time.now.to_s}] #{}" $global_log.puts() unless $global_log.nil? $stdout.puts() end |