Class: ObjectiveCommand::Runners::Runner

Inherits:
Object
  • Object
show all
Includes:
Abstract, Hookable, Hooker, Mockable
Defined in:
lib/objective_command/runners/runner.rb

Direct Known Subclasses

Exec, NoRun

Defined Under Namespace

Classes: DebugHooker, FailureHooker, VerboseHooker

Constant Summary collapse

@@running_options =
{
  :verbose => :make_verbose,
  :v       => :make_verbose,
  :raise   => :raise_on_failures,
  :r       => :raise_on_failures,
  :debug   => :make_debug,
  :d       => :make_debug,
  :mock    => :make_mock,
  :m       => :make_mock,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mockable

#make_mock

Methods included from Hooker

#hook_method

Methods included from Hookable

#disable_hook, #hook_trigger, #hooker_subscribe, #subscribe_hook

Constructor Details

#initialize(*opts) ⇒ Runner

Construction methods.



49
50
51
52
53
54
# File 'lib/objective_command/runners/runner.rb', line 49

def initialize *opts
  @command_data_factory = Datas::Factory.new
  @open_mode = :w
  hooker_subscribe self
  apply_options(*opts)
end

Instance Attribute Details

#command_data_factoryObject

Accessors



21
22
23
# File 'lib/objective_command/runners/runner.rb', line 21

def command_data_factory
  @command_data_factory
end

Instance Method Details

#apply_options(*options) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/objective_command/runners/runner.rb', line 151

def apply_options ( *options )
  options.flatten.each do |k|
    option = @@running_options[k]
    if option.nil?
      raise ArgumentError, "Bad runner option: #{k}"
    else
      send option
    end
  end
  self
end

#fork(data, &block) ⇒ Object



163
164
165
# File 'lib/objective_command/runners/runner.rb', line 163

def fork data, &block
  block.call
end

#initialize_copy(copy) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/objective_command/runners/runner.rb', line 57

def initialize_copy ( copy )
  super
  if defined? @hookers
    @hookers = @hookers.dup
  else
    @hookers = []
  end
end

#make_debugObject



127
128
129
130
# File 'lib/objective_command/runners/runner.rb', line 127

def make_debug
  hooker_subscribe DebugHooker.new
  self
end

#make_verboseObject



121
122
123
124
# File 'lib/objective_command/runners/runner.rb', line 121

def make_verbose
  hooker_subscribe VerboseHooker.new
  self
end

#raise_on_failuresObject



133
134
135
136
# File 'lib/objective_command/runners/runner.rb', line 133

def raise_on_failures
  hooker_subscribe FailureHooker.new
  self
end

#run(aCommand) ⇒ Object

Methods.



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
113
114
115
116
117
118
# File 'lib/objective_command/runners/runner.rb', line 71

def run ( aCommand )
  unless aCommand.is_a? Commands::Command
    raise ArgumentError, "Need a OCmd::Commands::Command not #{aCommand}"
  end

  hook_trigger :display_command, aCommand

  data = @command_data_factory.create
  begin
    data.open_mode = @open_mode
    data.input = aCommand.input unless aCommand.input.nil?
    data.output = aCommand.output unless aCommand.output.nil?
    data.error = aCommand.error unless aCommand.error.nil?
    hook_trigger :data, aCommand, data

    hook_trigger :fork, data do
      begin
        hook_trigger :son, aCommand, data
        hook_trigger :before_open, data
        aCommand = aCommand.instanciate_args
        STDIN.reopen(data.input.to_io_for_ocmd) unless data.input.nil?
        STDOUT.reopen(data.output.to_io_for_ocmd) unless data.output.nil?
        STDERR.reopen(data.error.to_io_for_ocmd) unless data.error.nil?
        STDOUT.flush
        STDERR.flush

        hook_trigger :before_chdir, data
        Dir.chdir(aCommand.dir) unless aCommand.dir.nil?

        hook_trigger :before_exec, aCommand, data
        hook_trigger :exec, aCommand, data
      rescue Exception => ex
        hook_trigger :exception_raised_during_exec, ex
      end
    end

    hook_trigger :waitpid, data
    if data.status.nil? or not data.status.success?
      hook_trigger :failure, aCommand, data
    end

    hook_trigger :before_return, aCommand, data
  rescue Exception => ex
    hook_trigger :abort, data
    raise ex
  end
  data
end