dotanuki 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,41 +2,61 @@ h1. Dotanuki "!https://secure.travis-ci.org/pmenglund/dotanuki.png?branch=master
2
2
 
3
3
  Simple but effective executioner of commands, which will deal correctly with failed commands.
4
4
 
5
+ There are two versions of the Dotanuki gem "dotanuki":http://rubygems.org/gems/dotanuki which uses "posix-spawn":http://rubygems.org/gems/posix-spawn and a pure ruby version "dotanuki-ruby":http://rubygems.org/gems/dotanuki-ruby which uses "open4":http://rubygems.org/gems/open4 instead.
6
+
7
+ Note that if the pure ruby version can load "posix-spawn":http://rubygems.org/gems/posix-spawn it will use it.
8
+
5
9
  h1. Examples
6
10
 
7
11
  In the following example, if the <code>mkdir</code> fails, none of the other commands will be executed.
8
12
 
9
13
  <pre>
10
- class Example
11
- include Dotanuki
12
-
13
- def test
14
- commands = [
15
- "mkdir /tmp/foo",
16
- "cp /etc/hosts /tmp/foo",
17
- "cp /etc/passwd /tmp/foo"
18
- ]
19
-
20
- result = execute(commands)
21
- if result.failed?
22
- puts "execution failed: #{result.fail_message}"
23
- end
24
- end
25
- end
14
+ require "dotanuki"
15
+
16
+ class Example
17
+ include Dotanuki
18
+
19
+ def test
20
+ commands = [
21
+ "mkdir /tmp/foo",
22
+ "cp /etc/hosts /tmp/foo",
23
+ "cp /etc/passwd /tmp/foo"
24
+ ]
25
+
26
+ result = execute(commands)
27
+ if result.failed?
28
+ puts "execution failed: #{result.fail_message}"
29
+ end
30
+ end
31
+ end
26
32
  </pre>
27
33
 
28
34
  It can also be used with a <code>guard</code> block, which will raise an <code>ExecError</code> if a command fails.
29
35
 
30
36
  <pre>
31
- class Example
32
- include Dotanuki
33
-
34
- def test
35
- guard do
36
- execute "mkdir /tmp/foo"
37
- execute "cp /etc/hosts /tmp/foo"
38
- execute "cp /etc/passwd /tmp/foo"
39
- end
40
- end
41
- end
37
+ require "dotanuki"
38
+
39
+ class Example
40
+ include Dotanuki
41
+
42
+ def test
43
+ guard do
44
+ execute "mkdir /tmp/foo"
45
+ execute "cp /etc/hosts /tmp/foo"
46
+ execute "cp /etc/passwd /tmp/foo"
47
+ end
48
+ end
49
+ end
50
+ </pre>
51
+
52
+ If you want to use dotanuki in a class method, you have to use the module method
53
+
54
+ <pre>
55
+ require "dotanuki"
56
+
57
+ class Example
58
+ def self.test?
59
+ Dotanuki.execute("mkdir /tmp/foo").ok?
60
+ end
61
+ end
42
62
  </pre>
data/Rakefile CHANGED
@@ -1,10 +1,26 @@
1
1
  require 'bundler'
2
2
  require 'rspec/core/rake_task'
3
3
 
4
- Bundler::GemHelper.install_tasks
5
-
6
4
  task :default => [:spec]
7
- RSpec::Core::RakeTask.new(:spec)
5
+
6
+ desc "run rspec tests"
7
+ task :spec => [:"native:spec", :"pure:spec"]
8
+
9
+ desc "build gems"
10
+ task :build => [:"native:build", :"pure:build"]
11
+
12
+ desc "release gems"
13
+ task :release => [:"native:release", :"pure:release"]
14
+
15
+ namespace "native" do
16
+ Bundler::GemHelper.install_tasks :name => 'dotanuki'
17
+ RSpec::Core::RakeTask.new(:spec)
18
+ end
19
+
20
+ namespace "pure" do
21
+ Bundler::GemHelper.install_tasks :name => 'dotanuki-ruby'
22
+ RSpec::Core::RakeTask.new(:spec)
23
+ end
8
24
 
9
25
  desc "run autotest"
10
26
  task :autotest do
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "dotanuki/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "dotanuki-ruby"
7
+ s.version = Dotanuki::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Martin Englund"]
10
+ s.email = ["martin@englund.nu"]
11
+ s.homepage = "https://github.com/pmenglund/dotanuki"
12
+ s.summary = %q{Command executioner}
13
+ s.description = %q{A pure ruby command executioner which doesn't blindly stumble on when a command fails'}
14
+
15
+ s.rubyforge_project = "dotanuki"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "open4"
23
+
24
+ s.add_development_dependency "rspec"
25
+ s.add_development_dependency "guard-rspec"
26
+ s.add_development_dependency "simplecov"
27
+ s.add_development_dependency "rake"
28
+
29
+ end
@@ -1,9 +1,15 @@
1
- require 'posix/spawn'
2
-
3
1
  # Module intented to be included into classes which execute system commands
4
2
  # @author Martin Englund
5
3
  module Dotanuki
6
4
 
5
+ begin
6
+ require 'posix/spawn'
7
+ POSIX_SPAWN = true
8
+ rescue LoadError
9
+ require 'open4'
10
+ POSIX_SPAWN = false
11
+ end
12
+
7
13
  # Error raised when an execution error occurs
8
14
  class ExecError < StandardError
9
15
  # Create a new ExecError
@@ -47,6 +53,11 @@ module Dotanuki
47
53
  status != 0
48
54
  end
49
55
 
56
+ # Returns true if a command has not failed
57
+ def ok?
58
+ status == 0
59
+ end
60
+
50
61
  # Returns stderr for the command that failed
51
62
  def fail_message
52
63
  stderr[@failed_index]
@@ -118,12 +129,20 @@ module Dotanuki
118
129
 
119
130
  [commands].flatten.each do |command|
120
131
  begin
121
- child = POSIX::Spawn::Child.new(command)
122
- stdout = child.out.strip
123
- stderr = child.err.strip
124
- result.add(stdout, stderr, child.status.exitstatus)
132
+ if POSIX_SPAWN
133
+ child = POSIX::Spawn::Child.new(command)
134
+ stdout = child.out.strip
135
+ stderr = child.err.strip
136
+ status = child.status
137
+ else
138
+ status = Open4::popen4(command) do |pid, stdin, out, err|
139
+ stdout = out.read.strip
140
+ stderr = err.read.strip
141
+ end
142
+ end
143
+ result.add(stdout, stderr, status.exitstatus)
125
144
 
126
- if child.status.exitstatus != 0
145
+ if status.exitstatus != 0
127
146
  if options[:on_error] == :exception || @guard
128
147
  @guard << result if @guard
129
148
  raise ExecError, stderr
@@ -1,4 +1,4 @@
1
1
  module Dotanuki
2
2
  # gem version
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
@@ -122,6 +122,7 @@ describe Dotanuki do
122
122
 
123
123
  it "should supply correct info on a failing command" do
124
124
  r = execute(COMMAND_FAILED)
125
+ r.ok?.should be_false
125
126
  r.failed?.should be_true
126
127
  r.fail_message.should_not be_empty
127
128
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotanuki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-16 00:00:00.000000000Z
12
+ date: 2012-02-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: posix-spawn
16
- requirement: &70167238543040 !ruby/object:Gem::Requirement
16
+ requirement: &70268770922200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70167238543040
24
+ version_requirements: *70268770922200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70167238542300 !ruby/object:Gem::Requirement
27
+ requirement: &70268770921340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70167238542300
35
+ version_requirements: *70268770921340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: guard-rspec
38
- requirement: &70167238541620 !ruby/object:Gem::Requirement
38
+ requirement: &70268770920880 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70167238541620
46
+ version_requirements: *70268770920880
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: simplecov
49
- requirement: &70167238540880 !ruby/object:Gem::Requirement
49
+ requirement: &70268770920460 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70167238540880
57
+ version_requirements: *70268770920460
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &70167238540160 !ruby/object:Gem::Requirement
60
+ requirement: &70268770919920 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70167238540160
68
+ version_requirements: *70268770919920
69
69
  description: A command executioner which doesn't blindly stumble on when a command
70
70
  fails'
71
71
  email:
@@ -83,6 +83,7 @@ files:
83
83
  - LICENSE
84
84
  - README.textile
85
85
  - Rakefile
86
+ - dotanuki-ruby.gemspec
86
87
  - dotanuki.gemspec
87
88
  - lib/dotanuki.rb
88
89
  - lib/dotanuki/version.rb