command-runner 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -59,6 +59,18 @@ line.pass.no_command? # => true
59
59
 
60
60
  it'll tell you.
61
61
 
62
+ It calls the given block...
63
+
64
+ ```Ruby
65
+ line = Command::Runner.new("echo", "{something}")
66
+
67
+ line.pass(something: "hello") do |message|
68
+ message.stdout
69
+ end # => "hello\n"
70
+ ```
71
+
72
+ and return the value.
73
+
62
74
  ## Compatibility
63
75
  It works on
64
76
 
@@ -89,14 +89,16 @@ module Command
89
89
  # @note This method may not raise a {NoCommandError} and instead
90
90
  # return a {Message} with the error code +127+, even if the
91
91
  # command doesn't exist.
92
+ # @yield [Message] when the command finishes.
92
93
  # @raise [NoCommandError] on no command.
93
94
  # @param interops [Hash<Symbol, Object>] the interpolations to
94
95
  # make.
95
96
  # @param options [Hash<Symbol, Object>] the options for the
96
97
  # backend.
97
- # @return [Message]
98
- def pass!(interops = {}, options = {})
99
- backend.call(*[contents(interops), options.delete(:env) || {}, options].flatten)
98
+ # @return [Message, Object] message if no block was given, the
99
+ # return value of the block otherwise.
100
+ def pass!(interops = {}, options = {}, &block)
101
+ backend.call(*[contents(interops), options.delete(:env) || {}, options].flatten, &block)
100
102
 
101
103
  rescue Errno::ENOENT
102
104
  raise NoCommandError, @command
@@ -106,13 +108,20 @@ module Command
106
108
  # defaults to no interpolations. Calls {#pass!}, but does not
107
109
  # raise an error.
108
110
  #
111
+ # @yield (see #pass!)
109
112
  # @param (see #pass!)
110
113
  # @return (see #pass!)
111
- def pass(interops = {}, options = {})
112
- pass! interops, options
114
+ def pass(interops = {}, options = {}, &block)
115
+ pass! interops, options, &block
113
116
 
114
117
  rescue NoCommandError
115
- Message.error(:line => contents(interops))
118
+ message = Message.error(contents(interops).join(' '))
119
+
120
+ if block_given?
121
+ block.call(message)
122
+ else
123
+ message
124
+ end
116
125
  end
117
126
 
118
127
  # The command line being run by the runner. Interpolates the
@@ -25,7 +25,8 @@ module Command
25
25
  # @param env [Hash] the enviornment to run the command
26
26
  # under.
27
27
  # @param options [Hash] the options to run the command under.
28
- # @return [Message] information about the process that ran.
28
+ # @return [Message, Object] message if no block is given, the
29
+ # result of the block call otherwise.
29
30
  def call(command, arguments, env = {}, options = {})
30
31
  super
31
32
  output = ""
@@ -38,7 +39,7 @@ module Command
38
39
  end_time = Time.now
39
40
  end
40
41
 
41
- Message.new :process_id => $?.pid,
42
+ message = Message.new :process_id => $?.pid,
42
43
  :exit_code => $?.exitstatus,
43
44
  :finished => true,
44
45
  :time => (end_time - start_time).abs,
@@ -48,6 +49,12 @@ module Command
48
49
  :line => [command, arguments].join(' '),
49
50
  :executed => true,
50
51
  :status => $?
52
+
53
+ if block_given?
54
+ block.call(message)
55
+ else
56
+ message
57
+ end
51
58
  end
52
59
 
53
60
  private
@@ -28,17 +28,20 @@ module Command
28
28
  # @abstract
29
29
  # @note Does nothing.
30
30
  # @raise [Errno::ENOENT] if the command doesn't exist.
31
+ # @yield [message] when the command finishes.
31
32
  # @param command [String] the command to run.
32
33
  # @param arguments [String] the arguments to pass to the
33
34
  # command.
34
35
  # @param env [Hash] the enviornment to run the command
35
36
  # under.
36
37
  # @param options [Hash] the options to run the command under.
37
- # @return [Message] information about the process that ran.
38
- def call(command, arguments, env = {}, options = {})
38
+ # @return [Message, Object] message if no block is given, the
39
+ # result of the block call otherwise.
40
+ def call(command, arguments, env = {}, options = {}, &block)
39
41
  @ran << [command, arguments]
40
42
 
41
- Message.new :env => env, :options => options
43
+ message = Message.new :env => env, :options => options, :line =>
44
+ [command, arguments].join(' ')
42
45
  end
43
46
 
44
47
  # Determines whether or not the given command and arguments were
@@ -6,7 +6,9 @@ module Command
6
6
  class Spawn < Fake
7
7
 
8
8
  # Returns whether or not this backend is available on this
9
- # platform.
9
+ # platform. Process.spawn doesn't work the way we want it to
10
+ # on JRuby 1.9 mode, so we prevent this from being used on
11
+ # that platform.
10
12
  #
11
13
  # @return [Boolean]
12
14
  def self.available?
@@ -20,10 +22,14 @@ module Command
20
22
 
21
23
  # Run the given command and arguments, in the given environment.
22
24
  #
25
+ # @note The block is called in another thread, so in ruby
26
+ # versions other than MRI, make sure your code is
27
+ # thread-safe.
23
28
  # @raise [Errno::ENOENT] if the command doesn't exist.
29
+ # @yield (see Fake#call)
24
30
  # @param (see Fake#call)
25
31
  # @return (see Fake#call)
26
- def call(command, arguments, env = {}, options = {})
32
+ def call(command, arguments, env = {}, options = {}, &block)
27
33
  super
28
34
  stderr_r, stderr_w = IO.pipe
29
35
  stdout_r, stdout_w = IO.pipe
@@ -49,7 +55,7 @@ module Command
49
55
 
50
56
  [stdout_w, stderr_w].each(&:close)
51
57
 
52
- Message.new :process_id => process_id,
58
+ message = Message.new :process_id => process_id,
53
59
  :exit_code => status.exitstatus,
54
60
  :finished => true,
55
61
  :time => (start_time - end_time).abs,
@@ -60,6 +66,12 @@ module Command
60
66
  :line => line,
61
67
  :executed => true,
62
68
  :status => status
69
+
70
+ if block_given?
71
+ block.call(message)
72
+ else
73
+ message
74
+ end
63
75
  end
64
76
  end
65
77
 
@@ -11,7 +11,7 @@ module Command
11
11
  # +Errno::ENOENT+ error.
12
12
  # @ return [Message]
13
13
  def self.error(line)
14
- Message.new(:line => line, :exit_code => 127)
14
+ new(:line => line, :exit_code => 127)
15
15
  end
16
16
 
17
17
  # Initialize the message with the given data about the process.
@@ -3,7 +3,7 @@ module Command
3
3
  class Runner
4
4
 
5
5
  # The current version of Runner.
6
- VERSION = "0.3.0".freeze
6
+ VERSION = "0.4.0".freeze
7
7
 
8
8
  end
9
9
  end
@@ -47,7 +47,7 @@ describe Command::Runner do
47
47
  end
48
48
  end
49
49
 
50
- context "selects backends" do
50
+ context "selecting backends" do
51
51
  it "selects the best backend" do
52
52
  Command::Runner::Backends::PosixSpawn.stub(:available?).and_return(false)
53
53
  Command::Runner::Backends::Spawn.stub(:available?).and_return(true)
@@ -58,14 +58,23 @@ describe Command::Runner do
58
58
  end
59
59
  end
60
60
 
61
- context "running bad commands" do
61
+ context "bad commands" do
62
62
  let(:command) { "some-non-existant-command" }
63
63
  let(:arguments) { "" }
64
64
 
65
- it "raises exceptions on non-existant commands" do
65
+ before :each do
66
66
  subject.backend = Command::Runner::Backends::Backticks.new
67
+ end
68
+
69
+ its(:pass) { should be_no_command }
70
+
71
+ it "calls the block given" do
72
+ subject.backend = Command::Runner::Backends::Backticks.new
73
+ subject.pass do |message|
74
+ message.should be_no_command
67
75
 
68
- subject.pass.should be_no_command
76
+ message.line.should == "some-non-existant-command "
77
+ end
69
78
  end
70
79
  end
71
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command-runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: