shell_command 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ doc/
data/.pryrc ADDED
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift './lib'
2
+ require 'shell_command'
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ruby-head
6
+ #- jruby-19mode (Appears to have popen3(…) from 1.8.7)
7
+ #- rbx-19mode (Appears to not implement Open3.spawn(…))
8
+
9
+ notifications:
10
+ irc: "irc.freenode.org#flowof.info"
11
+ recipients:
12
+ - rob@flowof.info
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ -m markdown
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in shell_command.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (C) 2011 by Robert Gleeson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ __OVERVIEW__
2
+
3
+
4
+ | Project | shell\_command
5
+ |:----------------|:--------------------------------------------------
6
+ | Homepage | https://github.com/robgleeson/shell\_command
7
+ | Wiki | https://github.com/robgleeson/shell\_command/wiki
8
+ | Documentation | http://rubydoc.info/gems/shell\_command/frames
9
+ | Author | Rob Gleeson
10
+
11
+ __DESCRIPTION__
12
+
13
+ shell\_command tries to provide a better interface for communicating with
14
+ commands you spawn on the shell. The examples will give you a much better idea
15
+ of what I mean.
16
+
17
+ __USE CASES__
18
+
19
+ * Run a command, and find out if it was successful (e.g: exited with 0).
20
+ * Run a command, and access stdout or stderr.
21
+
22
+ __EXAMPLES__
23
+
24
+ The first example lets you handle a failure yourself, but the second example
25
+ assumes you want a exception raised on failure.
26
+
27
+ __1.__
28
+
29
+ ShellCommand.run "ls" do |command|
30
+ if command.success?
31
+ puts command.stdout
32
+ else
33
+ raise RuntimeError, "The command 'ls' failed."
34
+ end
35
+ end
36
+
37
+ __2.__
38
+
39
+ begin
40
+ ShellCommand.run! "ls"
41
+ rescue ShellCommand::Exception => e
42
+ p e.message
43
+ end
44
+
45
+ __SUPPORTED PLATFORMS__
46
+
47
+ JRuby, and Rubinius do not implement `Open3.popen3(…)` properly yet. :(
48
+ As soon as they do they will be supported in 1.9 mode.
49
+
50
+ * CRuby 1.9+
51
+
52
+ __LICENSE__
53
+
54
+ See LICENSE.txt
55
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.name = :test
6
+ t.test_files = FileList["test/*.rb"]
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,86 @@
1
+ require "shell_command/version"
2
+ require 'open3'
3
+
4
+ module ShellCommand
5
+
6
+ class Exception < StandardError
7
+ end
8
+
9
+ #
10
+ # @attr [Process::Status] status
11
+ # The status of the command.
12
+ #
13
+ # @attr [String] stderr
14
+ # The output written by the command to standard error.
15
+ #
16
+ # @attr [String] stdout
17
+ # The output written by the command to standard output.
18
+ #
19
+ class Command < Struct.new(:status, :stderr, :stdout)
20
+
21
+ def method_missing method, *args, &block
22
+ if status.respond_to?(method)
23
+ status.send(method, *args, &block)
24
+ else
25
+ super
26
+ end
27
+ end
28
+
29
+ def respond_to_missing? method, include_private = false
30
+ status.respond_to?(method, include_private)
31
+ end
32
+
33
+ end
34
+
35
+ #
36
+ # @param [String] *args
37
+ # A command and its arguments to execute (if any).
38
+ #
39
+ # @yieldparam [ShellCommand::Command] command
40
+ # Yields {ShellCommand::Command} if a block is given.
41
+ #
42
+ # @return [ShellCommand::Command]
43
+ # Returns a {ShellCommand::Command}
44
+ #
45
+ def run *args
46
+ if args.empty?
47
+ raise ArgumentError, 'Wrong number of arguments (0 for 1)'
48
+ end
49
+
50
+ Open3.popen3(*args) do |_, stdout, stderr, thr|
51
+ status = thr.value
52
+ command = Command.new(status, stderr.read, stdout.read)
53
+
54
+ if block_given?
55
+ yield(command)
56
+ else
57
+ command
58
+ end
59
+ end
60
+ end
61
+ module_function :run
62
+
63
+ #
64
+ # @param
65
+ # (see #run)
66
+ #
67
+ # @raise [ShellCommand::Exception]
68
+ # If the command is unsuccessful (e.g: > 0 exit status code)
69
+ #
70
+ # @return
71
+ # (see #run)
72
+ #
73
+ def run! *args
74
+ command = run(*args)
75
+
76
+ if command.success?
77
+ command
78
+ else
79
+ raise ShellCommand::Exception,
80
+ "The command '#{args.join(' ')}' has failed."
81
+ end
82
+ end
83
+ module_function :run!
84
+
85
+ end
86
+
@@ -0,0 +1,3 @@
1
+ module ShellCommand
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "shell_command/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "shell_command"
7
+ s.version = ShellCommand::VERSION
8
+ s.authors = ["Rob Gleeson"]
9
+ s.email = ["rob@flowof.info"]
10
+ s.homepage = "https://github.com/robgleeson/shell_command"
11
+
12
+ s.summary = %q{shell_command tries to provide a better interface for
13
+ communicating with commands you spawn on the shell}
14
+
15
+ s.description = s.summary
16
+
17
+ s.rubyforge_project = "shell_command"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+
24
+ s.required_ruby_version = '~> 1.9.1'
25
+
26
+ s.add_development_dependency "rake"
27
+ s.add_development_dependency "minitest"
28
+
29
+ end
data/test/setup.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'shell_command'
2
+ require 'minitest/spec'
3
+ require 'minitest/autorun'
4
+
5
+ alias context describe
6
+
@@ -0,0 +1,56 @@
1
+ context ShellCommand do
2
+
3
+ context 'run' do
4
+ it 'must successfully execute "echo".' do
5
+ command = ShellCommand.run "echo -n Hi!"
6
+ command.success?.must_equal(true)
7
+ end
8
+
9
+ it 'must give access to the standard output of "echo"' do
10
+ command = ShellCommand.run "echo -n Hi!"
11
+ command.stdout.must_equal("Hi!")
12
+ end
13
+
14
+ it 'must give access to the standard error output of "echo"' do
15
+ command = ShellCommand.run "ls /i_do_not_exist"
16
+ command.stderr.empty?.must_equal(false)
17
+ end
18
+
19
+ it 'must call a block, if given.' do
20
+ mock = MiniTest::Mock.new
21
+ mock.expect(:ok, nil)
22
+
23
+ ShellCommand.run "ls" do |command|
24
+ mock.ok
25
+ end
26
+
27
+ mock.verify
28
+ end
29
+
30
+ it "must yield a parameter, if asked." do
31
+ ShellCommand.run "ls" do |command|
32
+ command.must_be_instance_of(ShellCommand::Command)
33
+ end
34
+ end
35
+
36
+ it 'must raise a ArgumentError if given no arguments.' do
37
+ proc {
38
+ ShellCommand.run
39
+ }.must_raise(ArgumentError)
40
+ end
41
+ end
42
+
43
+ context 'run!' do
44
+ it 'must raise a exception when a command fails.' do
45
+ proc {
46
+ ShellCommand.run! "ls /opskddiofjfsiodjf"
47
+ }.must_raise(ShellCommand::Exception)
48
+ end
49
+
50
+ it 'must give a Command when a command is successful.' do
51
+ command = ShellCommand.run! "ls"
52
+ command.must_be_instance_of(ShellCommand::Command)
53
+ end
54
+ end
55
+
56
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shell_command
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rob Gleeson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70283541504560 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70283541504560
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &70283541504100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70283541504100
36
+ description: shell_command tries to provide a better interface for communicating
37
+ with commands you spawn on the shell
38
+ email:
39
+ - rob@flowof.info
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - .pryrc
46
+ - .travis.yml
47
+ - .yardopts
48
+ - Gemfile
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/shell_command.rb
53
+ - lib/shell_command/version.rb
54
+ - shell_command.gemspec
55
+ - test/setup.rb
56
+ - test/test_shell_command.rb
57
+ homepage: https://github.com/robgleeson/shell_command
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.9.1
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: -941004093862676109
78
+ requirements: []
79
+ rubyforge_project: shell_command
80
+ rubygems_version: 1.8.15
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: shell_command tries to provide a better interface for communicating with
84
+ commands you spawn on the shell
85
+ test_files:
86
+ - test/setup.rb
87
+ - test/test_shell_command.rb