simple_shell 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ gem "rdoc", "~> 3.12"
11
+ gem "bundler", "~> 1.0.0"
12
+ gem "jeweler", "~> 1.8.3"
13
+ gem "rcov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.3)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.6.5)
12
+ rake (0.9.2.2)
13
+ rcov (0.9.10)
14
+ rdoc (3.12)
15
+ json (~> 1.4)
16
+ rspec (2.8.0)
17
+ rspec-core (~> 2.8.0)
18
+ rspec-expectations (~> 2.8.0)
19
+ rspec-mocks (~> 2.8.0)
20
+ rspec-core (2.8.0)
21
+ rspec-expectations (2.8.0)
22
+ diff-lcs (~> 1.1.2)
23
+ rspec-mocks (2.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.0.0)
30
+ jeweler (~> 1.8.3)
31
+ rcov
32
+ rdoc (~> 3.12)
33
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Hartog C. de Mik
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = simple_shell
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to simple_shell
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Hartog C. de Mik. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "simple_shell"
18
+ gem.homepage = "http://github.com/coffeeaddict/simple_shell"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A very simple shell operations gem}
21
+ gem.description = %Q{A very simple, smart & elegant shell operations gem}
22
+ gem.email = "hartog@organisedminds.com"
23
+ gem.authors = ["Hartog C. de Mik"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "simple_shell #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,145 @@
1
+ require 'open3'
2
+
3
+ # A very simple shell operations implementation
4
+ #
5
+ # == Synopsis
6
+ #
7
+ # shell = SimpleShell.new
8
+ # res = shell.ls
9
+ # puts res.out
10
+ # puts res.err
11
+ # puts res.S? (as $? wont work :->)
12
+ #
13
+ # commands = shell.in("/other/path") do |sh|
14
+ # sh.ls
15
+ # sh.rm "-r", "bar"
16
+ # end
17
+ # puts commands.first.out
18
+ #
19
+ class SimpleShell
20
+ attr_reader :commands
21
+
22
+ def self.noisy
23
+ @noisy ||= false
24
+ end
25
+
26
+ def self.noisy=(noise)
27
+ @noisy = noise
28
+ end
29
+
30
+ # Create a new shell instance. This is tied to a base dir, which defaults to
31
+ # Dir.pwd
32
+ #
33
+ # SimpleShell.new
34
+ # SimpleShell.new("/path/to/base")
35
+ #
36
+ def initialize(base=Dir.pwd)
37
+ @base = base
38
+ @commands = []
39
+ end
40
+
41
+ # open up a sub shell at another directory
42
+ #
43
+ # shell.in("/path/to/other") do |sub_shell|
44
+ # sub_shell.system("ls")
45
+ # sub_shell.in("/path/to/third") do |sub_sub_shell|
46
+ # ...
47
+ # end
48
+ #
49
+ def in(dir, &block)
50
+ dir = File.join(@base, dir) if !File.exists?(dir) || dir !~ /^\//
51
+
52
+ shell = SimpleShell.new(dir)
53
+ yield shell
54
+ return shell.commands
55
+ end
56
+
57
+ # perform a command on the shell
58
+ #
59
+ # shell.system("ls")
60
+ # shell.do("ls")
61
+ # shell.command("ls")
62
+ #
63
+ # It is expected that arguments are provided as an array for security/safety
64
+ # purposes:
65
+ #
66
+ # shell.system("rm", "-r", "-f", "foo/")
67
+ #
68
+ # Returns a Command instance
69
+ #
70
+ def system(*args, &block)
71
+ command = nil
72
+ Dir.chdir(@base) do
73
+ command = Command.new(@base)
74
+ command.execute(*args, &block)
75
+ end
76
+
77
+ @commands << command
78
+
79
+ return command
80
+ end
81
+ alias_method :do, :system
82
+ alias_method :command, :system
83
+
84
+ # the S? of the last command
85
+ def S?
86
+ @commands.last.S?
87
+ end
88
+
89
+ # funkyness for neat little shell access
90
+ #
91
+ # shell.ls # = shell.system("ls")
92
+ # shell.rm %w(-rf foo/) # = shell.system("rm", "-rf", "foo/")
93
+ #
94
+ def method_missing(name, *args, &block)
95
+ super
96
+ rescue NoMethodError => e
97
+ if args[0].is_a? Array
98
+ args = args[0]
99
+ end
100
+
101
+ self.do(name.to_s, *args, &block)
102
+ end
103
+
104
+ # A command and it's output
105
+ #
106
+ # stdout and stderr are
107
+ class Command
108
+ attr_reader :out, :err, :S
109
+
110
+ def initialize(base)
111
+ @base = base
112
+ @out = ""
113
+ @err = ""
114
+ @S = -1
115
+ end
116
+
117
+ def execute(command, *args, &block)
118
+ $stderr.puts("#{command}, #{args}, #{@base}") if SimpleShell.noisy
119
+
120
+ Open3.popen3("#{command}", *(args.collect { |a| "#{a}" }) , :chdir => @base) do |stdin, stdout, stderr, thread|
121
+ if block_given?
122
+ yield stdin
123
+ end
124
+
125
+ stdin.close
126
+
127
+ # TODO : perhaps use blocks to put stuff into stdin
128
+
129
+ @out = stdout.read.chomp
130
+ @err = stderr.read.chomp
131
+ @S = thread.value rescue 0
132
+ end
133
+ end
134
+
135
+ # cheap copy of $?
136
+ def S?
137
+ @S
138
+ end
139
+
140
+ # we just want to know the output of the command
141
+ def to_s
142
+ @out
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "simple_shell"
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Hartog C. de Mik"]
12
+ s.date = "2012-02-01"
13
+ s.description = "A very simple, smart & elegant shell operations gem"
14
+ s.email = "hartog@organisedminds.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/simple_shell.rb",
29
+ "simple_shell.gemspec",
30
+ "spec/simple_shell_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = "http://github.com/coffeeaddict/simple_shell"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.10"
37
+ s.summary = "A very simple shell operations gem"
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
44
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
45
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
47
+ s.add_development_dependency(%q<rcov>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
50
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
51
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
53
+ s.add_dependency(%q<rcov>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
57
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
60
+ s.add_dependency(%q<rcov>, [">= 0"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,66 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SimpleShell do
4
+ let (:shell) { SimpleShell.new }
5
+
6
+ it "should keep track of perfomed commands" do
7
+ shell.ls
8
+ shell.ls
9
+
10
+ shell.commands.count.should == 2
11
+ end
12
+
13
+ it "should remember the last exit status" do
14
+ shell.ls "/path/non/existant"
15
+ shell.S?.to_i.should_not == 0
16
+
17
+ shell.ls
18
+ shell.S?.to_i.should == 0
19
+ end
20
+
21
+ it "should perform system commands" do
22
+ c = shell.system("ls -la")
23
+
24
+ c.out.should_not be_empty
25
+ c.err.should be_empty
26
+ c.S?.to_i.should == 0
27
+ end
28
+
29
+ it "should allow a sub shell" do
30
+ shell.ls
31
+ shell.ls
32
+
33
+ commands = shell.in("/tmp") do |sub|
34
+ sub.ls
35
+ end
36
+
37
+ commands.should be_kind_of(Array)
38
+ commands.count.should == 1
39
+
40
+ shell.commands.count.should == 2
41
+ end
42
+
43
+ it "should allow a sub shell with relative paths" do
44
+ shell.mkdir "-p", "here"
45
+ shell.in("here") do |sh|
46
+ sh.do :touch, "here_file"
47
+ end
48
+
49
+ shell.ls("here").out.split("\n").should include("here_file")
50
+ shell.rm "-rf", "here"
51
+ end
52
+
53
+
54
+ it "should use blocks to write on stdin" do
55
+ res = shell.wc("-w") do |pipe|
56
+ pipe.puts "I was echoed"
57
+ end
58
+
59
+ res.out.should == "3"
60
+ end
61
+
62
+ it "should be settable to noisy" do
63
+ SimpleShell.noisy = true
64
+ SimpleShell.noisy.should be_true
65
+ end
66
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'simple_shell'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_shell
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hartog C. de Mik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-01 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &89298180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *89298180
25
+ - !ruby/object:Gem::Dependency
26
+ name: rdoc
27
+ requirement: &89297920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.12'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *89297920
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &89297670 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *89297670
47
+ - !ruby/object:Gem::Dependency
48
+ name: jeweler
49
+ requirement: &89297410 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.8.3
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *89297410
58
+ - !ruby/object:Gem::Dependency
59
+ name: rcov
60
+ requirement: &89297110 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *89297110
69
+ description: A very simple, smart & elegant shell operations gem
70
+ email: hartog@organisedminds.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ files:
77
+ - .document
78
+ - .rspec
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.rdoc
83
+ - Rakefile
84
+ - VERSION
85
+ - lib/simple_shell.rb
86
+ - simple_shell.gemspec
87
+ - spec/simple_shell_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: http://github.com/coffeeaddict/simple_shell
90
+ licenses:
91
+ - MIT
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ segments:
103
+ - 0
104
+ hash: 701496477
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.10
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: A very simple shell operations gem
117
+ test_files: []