module SilentStream::Extracted
Extracted
from: github.com/rails/rails/blob/4-2-stable/activesupport/lib/active_support/core_ext/kernel/reporting.rb
Constants
- SILENT_STREAM_NULL_DEVICE
- SILENT_STREAM_REGEXP_HAS_MATCH
- SILENT_STREAM_WINDOWS_REGEXP
Public Instance Methods
capture(stream) { || ... }
click to toggle source
Captures the given stream and returns it:
stream = capture(:stdout) { puts 'notice' } stream # => "notice\n" stream = capture(:stderr) { warn 'error' } stream # => "error\n"
even for subprocesses:
stream = capture(:stdout) { system('echo notice') } stream # => "notice\n" stream = capture(:stderr) { system('echo error 1>&2') } stream # => "error\n"
This method is not thread-safe.
# File lib/silent_stream.rb, line 109 def capture(stream) stream = stream.to_s captured_stream = Tempfile.new(stream) stream_io = eval("$#{stream}") origin_stream = stream_io.dup stream_io.reopen(captured_stream) yield stream_io.rewind captured_stream.read ensure captured_stream.close captured_stream.unlink stream_io.reopen(origin_stream) end
quietly() { || ... }
click to toggle source
Silences both STDOUT and STDERR, even for subprocesses.
quietly { system 'bundle install' }
This method is not thread-safe.
# File lib/silent_stream.rb, line 134 def quietly silence_stream(STDOUT) do silence_stream(STDERR) do yield end end end
silence_stderr() { || ... }
click to toggle source
This method is not thread-safe.
# File lib/silent_stream.rb, line 65 def silence_stderr silence_stream(STDERR) { yield } end
silence_stream(stream) { || ... }
click to toggle source
Silences any stream for the duration of the block.
silence_stream(STDOUT) do puts 'This will never be seen' end puts 'But this will'
This method is not thread-safe.
# File lib/silent_stream.rb, line 78 def silence_stream(stream) old_stream = stream.dup begin stream.reopen(SILENT_STREAM_NULL_DEVICE, 'a+') rescue Exception => e stream.puts "[SilentStream] Unable to silence. #{e.class}: #{e.message}" end stream.sync = true yield ensure stream.reopen(old_stream) old_stream.close end
Private Instance Methods
windows_os_test()
click to toggle source
# File lib/silent_stream.rb, line 146 def windows_os_test # When available, in Ruby 2.4+, we use Regexp#match? which does not update # the $~ global object and may be 3x faster than alternative match tests if SILENT_STREAM_REGEXP_HAS_MATCH SILENT_STREAM_WINDOWS_REGEXP.match?(RbConfig::CONFIG['host_os']) else SILENT_STREAM_WINDOWS_REGEXP =~ (RbConfig::CONFIG['host_os']) end end