Class: Wunderbar::BuilderClass
- Inherits:
-
BuilderBase
- Object
- BuilderBase
- Wunderbar::BuilderClass
- Defined in:
- lib/wunderbar/builder.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#system(command, opts = {}) {|:stdin, echo| ... } ⇒ Object
execute a system command, echoing stdin, stdout, and stderr.
- #websocket(*args, &block) ⇒ Object
Methods inherited from BuilderBase
#get_binding, #set_variables_from_params
Instance Method Details
#system(command, opts = {}) {|:stdin, echo| ... } ⇒ Object
execute a system command, echoing stdin, stdout, and stderr
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/wunderbar/builder.rb', line 38 def system(command, opts={}) if command.respond_to? :flatten flat = command.flatten secret = command - flat begin # if available, use escape as it does prettier quoting require 'escape' echo = Escape.shell_command(command.compact - secret) rescue LoadError # std-lib function that gets the job done require 'shellwords' echo = Shellwords.join(command.compact - secret) end command = flat.compact.map(&:dup).map(&:untaint) else echo = command command = [command] end patterns = opts[:hilite] || [] patterns=[patterns] if String === patterns or Regexp === patterns patterns.map! do |pattern| String === pattern ? Regexp.new(Regexp.escape(pattern)) : pattern end yield :stdin, echo unless opts[:echo] == false require 'open3' require 'thread' semaphore = Mutex.new Open3.popen3(*command) do |pin, pout, perr, wait| [ Thread.new do until pout.eof? out_line = pout.readline.chomp semaphore.synchronize do if patterns.any? {|pattern| out_line =~ pattern} yield :hilight, out_line else yield :stdout, out_line end end end end, Thread.new do until perr.eof? err_line = perr.readline.chomp semaphore.synchronize do yield :stderr, err_line end end end, Thread.new do if opts[:stdin].respond_to? :read require 'fileutils' FileUtils.copy_stream opts[:stdin], pin elsif opts[:stdin] pin.write opts[:stdin].to_s end pin.close end ].each {|thread| thread.join} wait and wait.value.exitstatus end end |
#websocket(*args, &block) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/wunderbar/builder.rb', line 29 def websocket(*args, &block) if Hash === args.last args.last[:locals] = Hash[instance_variables. map { |name| [name.to_s.sub('@',''), instance_variable_get(name)] } ] end Wunderbar.websocket(*args, &block) end |