Class: Xcode::Shell::Command

Inherits:
Object
  • Object
show all
Includes:
TerminalOutput
Defined in:
lib/xcode/shell/command.rb

Constant Summary

Constants included from TerminalOutput

TerminalOutput::LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TerminalOutput

#color_output=, #color_output?, #format_lhs, included, #log_level, log_level=, #print, #print_input, #print_output, #print_system, #print_task, #puts, terminal_supports_colors?

Constructor Details

#initialize(cmd, environment = {}) ⇒ Command

Returns a new instance of Command.



21
22
23
24
25
26
27
28
29
30
# File 'lib/xcode/shell/command.rb', line 21

def initialize(cmd, environment={})
  @cmd = cmd.to_s
  @args = []
  @env = environment
  @show_output = true
  @pipe = nil
  @output = []
  @output_dir = Dir.tmpdir
  @log_to_file = false
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



19
20
21
# File 'lib/xcode/shell/command.rb', line 19

def args
  @args
end

#cmdObject

Returns the value of attribute cmd.



19
20
21
# File 'lib/xcode/shell/command.rb', line 19

def cmd
  @cmd
end

#envObject

Returns the value of attribute env.



19
20
21
# File 'lib/xcode/shell/command.rb', line 19

def env
  @env
end

#log_to_fileObject

Returns the value of attribute log_to_file.



19
20
21
# File 'lib/xcode/shell/command.rb', line 19

def log_to_file
  @log_to_file
end

#outputObject

Returns the value of attribute output.



19
20
21
# File 'lib/xcode/shell/command.rb', line 19

def output
  @output
end

#output_dirObject

Returns the value of attribute output_dir.



19
20
21
# File 'lib/xcode/shell/command.rb', line 19

def output_dir
  @output_dir
end

#show_outputObject

Returns the value of attribute show_output.



19
20
21
# File 'lib/xcode/shell/command.rb', line 19

def show_output
  @show_output
end

Instance Method Details

#<<(arg) ⇒ Object



32
33
34
# File 'lib/xcode/shell/command.rb', line 32

def <<(arg)
  @args << arg
end

#==(obj) ⇒ Object



48
49
50
51
52
# File 'lib/xcode/shell/command.rb', line 48

def ==(obj)
  return false unless obj.is_a? Xcode::Shell::Command
  # to_s==obj.to_s
  Set.new(obj.to_a) == Set.new(self.to_a)
end

#attach(pipe) ⇒ Object

Attach an output pipe.

This can be any object which responds to puts and close



58
59
60
61
# File 'lib/xcode/shell/command.rb', line 58

def attach(pipe)
  @pipe = pipe
  @show_output = false
end

#execute(&block) ⇒ Object

Execute the given command



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
105
106
107
108
109
110
111
112
# File 'lib/xcode/shell/command.rb', line 66

def execute(&block) #:yield: output
  print_output self.to_s, :debug
  # print_task 'shell', self.to_s, :debug if show_output
  begin
    output_file_name = File.join(@output_dir, "xcoder-#{@cmd}-#{Time.now.strftime('%Y%m%d-%H%M%S')}")
    
    File.open(output_file_name, "w") do |file|          
      PTY.spawn(to_s) do |r, w, child_pid|
        r.sync
        r.each_line do |line|
          file << line
          
          print_input line.gsub(/\n$/,''), :debug if @show_output 

          if @pipe.nil?
            # DEPRECATED
            yield(line) if block_given?
          else
            @pipe << line
          end
          
          @output << line
        end 
        Process.wait(child_pid)
      end
    end
    
    raise ExecutionError.new("Error (#{$?.exitstatus}) executing '#{to_s}'", @output) if $?.exitstatus>0        

    if @log_to_file
      print_system "Captured output to #{output_file_name}", :notice                      
    else
      File.delete(output_file_name)
    end
    
    @output
  rescue Xcode::Shell::ExecutionError => e          
    print_system "Captured output to #{output_file_name}", :notice          
    print_system "Cropped #{e.output.count - 10} lines", :notice if @output.count>10
    @output.last(10).each do |line|
      print_output line.strip, :error
    end
    raise e      
  ensure
    @pipe.close unless @pipe.nil?
  end
end

#to_aObject



40
41
42
43
44
45
46
# File 'lib/xcode/shell/command.rb', line 40

def to_a
  out = []
  out << @cmd
  out+=@args
  out+=(@env.map {|k,v| "#{k}=#{v}"})
  out
end

#to_sObject



36
37
38
# File 'lib/xcode/shell/command.rb', line 36

def to_s
  "#{to_a.join(' ')}"
end