shr 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/shr/builtin.rb +49 -0
- data/lib/shr/command.rb +18 -17
- data/lib/shr/shell.rb +17 -13
- data/lib/shr/version.rb +1 -1
- data/spec/lib/shr/builtin_spec.rb +51 -0
- data/spec/lib/shr/command_spec.rb +1 -1
- data/spec/lib/shr/shell_spec.rb +13 -0
- data/spec/lib/shr/shell_win_spec.rb +13 -0
- metadata +5 -2
data/lib/shr/builtin.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
module Shr
|
5
|
+
module Builtin
|
6
|
+
|
7
|
+
def capture(&block)
|
8
|
+
proc = Proc.new do |environment|
|
9
|
+
output = relay capture_stdout(&block)
|
10
|
+
if environment.key? :out
|
11
|
+
File.write(environment[:out], output.read)
|
12
|
+
end
|
13
|
+
[output, nil]
|
14
|
+
end
|
15
|
+
@commands << [:capture, proc]
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def cd!(path=nil)
|
20
|
+
proc = Proc.new do
|
21
|
+
path ? Dir.chdir(path) : Dir.chdir
|
22
|
+
[nil, nil]
|
23
|
+
end
|
24
|
+
@commands << ["#cd! #{path}".strip, proc]
|
25
|
+
force
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def capture_stdout
|
32
|
+
begin
|
33
|
+
$stdout = StringIO.new
|
34
|
+
yield
|
35
|
+
out = $stdout.string
|
36
|
+
ensure
|
37
|
+
$stdout = STDOUT
|
38
|
+
end
|
39
|
+
out
|
40
|
+
end
|
41
|
+
|
42
|
+
def relay(source)
|
43
|
+
r, w = IO.pipe
|
44
|
+
w.write source
|
45
|
+
w.close
|
46
|
+
r
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/shr/command.rb
CHANGED
@@ -30,23 +30,24 @@ module Shr
|
|
30
30
|
@command.end_with? '!'
|
31
31
|
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
33
|
+
def to_proc
|
34
|
+
Proc.new do |environment, command_out|
|
35
|
+
if release?
|
36
|
+
Open3.pipeline(self.to_s)
|
37
|
+
[nil, nil]
|
38
|
+
else
|
39
|
+
io_r, io_w = IO.pipe
|
40
|
+
options = { :out => io_w }
|
41
|
+
options[:in] = command_out if command_out
|
42
|
+
options.merge!(environment)
|
43
|
+
|
44
|
+
pid = spawn(self.to_s, options)
|
45
|
+
watcher = Process.detach(pid)
|
46
|
+
io_w.close
|
47
|
+
|
48
|
+
[io_r, watcher]
|
49
|
+
end
|
50
|
+
end
|
50
51
|
end
|
51
52
|
end
|
52
53
|
end
|
data/lib/shr/shell.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
#coding: utf-8
|
2
2
|
require 'shr/command'
|
3
|
+
require 'shr/builtin'
|
3
4
|
|
4
5
|
module Shr
|
5
6
|
class Shell
|
6
7
|
|
8
|
+
include Builtin
|
9
|
+
|
7
10
|
def initialize
|
8
11
|
@commands = []
|
9
12
|
end
|
@@ -14,7 +17,7 @@ module Shr
|
|
14
17
|
super
|
15
18
|
else
|
16
19
|
delay command
|
17
|
-
force
|
20
|
+
force if command.release?
|
18
21
|
self
|
19
22
|
end
|
20
23
|
end
|
@@ -25,7 +28,7 @@ module Shr
|
|
25
28
|
end
|
26
29
|
|
27
30
|
def inspect
|
28
|
-
command_line = @commands.join(' | ').strip
|
31
|
+
command_line = @commands.map {|c| c.first }.join(' | ').strip
|
29
32
|
force
|
30
33
|
res = "#<Shr::Shell>"
|
31
34
|
res << "<:command => #{command_line}>" if command_line.size > 0
|
@@ -61,6 +64,14 @@ module Shr
|
|
61
64
|
alias_method :<, :redirect_from
|
62
65
|
alias_method :>, :redirect_to
|
63
66
|
|
67
|
+
def bake(name, command, options=[])
|
68
|
+
Shell.class_eval do
|
69
|
+
define_method(name) do |*args|
|
70
|
+
self.method_missing(command, *(options + args))
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
64
75
|
private
|
65
76
|
|
66
77
|
def filled?
|
@@ -68,23 +79,16 @@ module Shr
|
|
68
79
|
end
|
69
80
|
|
70
81
|
def delay(command)
|
71
|
-
@commands << command
|
82
|
+
@commands << [command, command.to_proc]
|
72
83
|
end
|
73
84
|
|
74
85
|
def force(args={})
|
75
86
|
return if @commands.empty?
|
76
87
|
|
77
|
-
@commands.each do |command|
|
78
|
-
@command_out, @wait_thread = command.
|
88
|
+
@commands.each do |_, command|
|
89
|
+
@command_out, @wait_thread = command.call(args, @command_out)
|
79
90
|
end
|
80
|
-
@wait_thread.join
|
81
|
-
@commands.clear
|
82
|
-
end
|
83
|
-
|
84
|
-
def force!
|
85
|
-
return if @commands.empty?
|
86
|
-
|
87
|
-
@commands[-1].run!
|
91
|
+
@wait_thread.join if @wait_thread
|
88
92
|
@commands.clear
|
89
93
|
end
|
90
94
|
end
|
data/lib/shr/version.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
#coding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'tempfile'
|
4
|
+
|
5
|
+
module Shr
|
6
|
+
describe Builtin do
|
7
|
+
let(:sh) { Shell.new }
|
8
|
+
|
9
|
+
before(:all) do
|
10
|
+
@original = Dir.pwd
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'has the following methods' do
|
14
|
+
m = sh.methods
|
15
|
+
m.should include(:capture)
|
16
|
+
m.should include(:cd!)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#capture' do
|
20
|
+
it 'captures output from block' do
|
21
|
+
sh.capture { print 'hello capture' }.to_s.should eq("hello capture")
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'redirects output from block to file' do
|
25
|
+
tempfile = Tempfile.new('temp')
|
26
|
+
sh.capture { print 'hello capture' }.redirect_to(tempfile.path)
|
27
|
+
File.read(tempfile.path).should eq('hello capture')
|
28
|
+
tempfile.close!
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#cd!' do
|
33
|
+
it 'changes the current working directory' do
|
34
|
+
sh.cd! Dir.home
|
35
|
+
Dir.pwd.should eq(Dir.home)
|
36
|
+
Dir.chdir @original
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with no argument' do
|
40
|
+
it 'changes the current working directory to users home' do
|
41
|
+
sh.cd!
|
42
|
+
Dir.pwd.should eq(Dir.home)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
after(:all) do
|
48
|
+
Dir.chdir @original
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/spec/lib/shr/shell_spec.rb
CHANGED
@@ -16,6 +16,7 @@ module Shr
|
|
16
16
|
m.should include(:<)
|
17
17
|
m.should include(:redirect_to)
|
18
18
|
m.should include(:>)
|
19
|
+
m.should include(:bake)
|
19
20
|
end
|
20
21
|
|
21
22
|
unless OS.windows?
|
@@ -89,6 +90,18 @@ module Shr
|
|
89
90
|
end
|
90
91
|
end
|
91
92
|
|
93
|
+
describe '#bake' do
|
94
|
+
it 'bakes new command' do
|
95
|
+
sh.bake(:sort_r, :sort, [:r])
|
96
|
+
|
97
|
+
files = []
|
98
|
+
sh.ls(@tmpdir).sort_r.each do |file|
|
99
|
+
files << file.strip
|
100
|
+
end
|
101
|
+
files.should eq(['ruby.rb', 'python.py', 'perl.pl'])
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
92
105
|
after(:all) do
|
93
106
|
FileUtils.remove_entry_secure @tmpdir
|
94
107
|
end
|
@@ -72,6 +72,19 @@ module Shr
|
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
|
+
describe '#bake' do
|
76
|
+
it 'bakes new command' do
|
77
|
+
sh.bake(:dir_B, :dir, [:B])
|
78
|
+
sh.bake(:sort_R, :sort, [:R])
|
79
|
+
|
80
|
+
files = []
|
81
|
+
sh.dir_B(@tmpdir.gsub('/', '\\')).sort_R.each do |file|
|
82
|
+
files << file.strip
|
83
|
+
end
|
84
|
+
files.should eq(['ruby.rb', 'python.py', 'perl.pl'])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
75
88
|
after(:all) do
|
76
89
|
FileUtils.remove_entry_secure @tmpdir
|
77
90
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: os
|
@@ -41,12 +41,14 @@ files:
|
|
41
41
|
- README.md
|
42
42
|
- Rakefile
|
43
43
|
- lib/shr.rb
|
44
|
+
- lib/shr/builtin.rb
|
44
45
|
- lib/shr/command.rb
|
45
46
|
- lib/shr/option.rb
|
46
47
|
- lib/shr/shell.rb
|
47
48
|
- lib/shr/version.rb
|
48
49
|
- lib/shr/which.rb
|
49
50
|
- shr.gemspec
|
51
|
+
- spec/lib/shr/builtin_spec.rb
|
50
52
|
- spec/lib/shr/command_spec.rb
|
51
53
|
- spec/lib/shr/option_spec.rb
|
52
54
|
- spec/lib/shr/shell_spec.rb
|
@@ -79,6 +81,7 @@ signing_key:
|
|
79
81
|
specification_version: 3
|
80
82
|
summary: Make controlling subprocess easier in Ruby.
|
81
83
|
test_files:
|
84
|
+
- spec/lib/shr/builtin_spec.rb
|
82
85
|
- spec/lib/shr/command_spec.rb
|
83
86
|
- spec/lib/shr/option_spec.rb
|
84
87
|
- spec/lib/shr/shell_spec.rb
|