easysh 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/easysh.rb +44 -18
- data/test/test_easysh.rb +55 -1
- metadata +3 -3
data/lib/easysh.rb
CHANGED
@@ -17,7 +17,6 @@
|
|
17
17
|
# sh.ls.sort
|
18
18
|
# sh.ls.chars.to_a.sample(5)
|
19
19
|
# sh.cat('/dev/urandom').bytes.first(10)
|
20
|
-
# sh.ls._l.map{|l| l.split}
|
21
20
|
# sh.ps._e._o('euser,comm').map(&:split).group_by(&:first)
|
22
21
|
#
|
23
22
|
# # chaining commands
|
@@ -48,10 +47,10 @@
|
|
48
47
|
# # exit status
|
49
48
|
# p = sh.which('bash')
|
50
49
|
# puts p
|
51
|
-
# p.
|
50
|
+
# p.status # => #<Process::Status: pid 5931 exit 0>
|
52
51
|
# p = sh.which.nonexists
|
53
52
|
# puts p
|
54
|
-
# p.
|
53
|
+
# p.status # => #<Process::Status: pid 6156 exit 1>
|
55
54
|
#
|
56
55
|
#
|
57
56
|
# # instant mode
|
@@ -64,10 +63,10 @@
|
|
64
63
|
# # => Linux
|
65
64
|
#
|
66
65
|
#
|
67
|
-
class EasySH < Struct.new(:cmd, :opt, :chain, :instant)
|
66
|
+
class EasySH < Struct.new(:cmd, :opt, :chain, :instant) # :no-doc:
|
68
67
|
include Enumerable
|
69
68
|
|
70
|
-
attr_reader :
|
69
|
+
attr_reader :status
|
71
70
|
|
72
71
|
def method_missing name, *args, &block # :no-doc:
|
73
72
|
begin
|
@@ -79,14 +78,14 @@ class EasySH < Struct.new(:cmd, :opt, :chain, :instant)
|
|
79
78
|
r = if name.is_a? EasySH
|
80
79
|
self.class.new [*cmd, *name.cmd], Hash[*opt, *name.opt], chain, instant
|
81
80
|
else
|
82
|
-
args = [name.to_s.gsub(/^_+/) {|s| '-' * s.size}, *args]
|
81
|
+
args = [name && name.to_s.gsub(/^_+/) {|s| '-' * s.size}, *args].compact
|
83
82
|
*args, opt = *args if args.last.is_a?(Hash) && args.last.keys.find{|k| k.is_a? Integer}
|
84
83
|
args = args.map do |a|
|
85
84
|
case a
|
86
85
|
when Symbol
|
87
86
|
"-#{a.length > 1 ? '-' : ''}#{a}"
|
88
87
|
when Hash
|
89
|
-
a.map { |k,v| k.length > 1 ? "--#{k}=#{v}" : "-#{k}
|
88
|
+
a.map { |k,v| k.length > 1 ? "--#{k}=#{v}" : ["-#{k}", v.to_s] }
|
90
89
|
else
|
91
90
|
a.to_s
|
92
91
|
end
|
@@ -142,7 +141,7 @@ class EasySH < Struct.new(:cmd, :opt, :chain, :instant)
|
|
142
141
|
end
|
143
142
|
ensure
|
144
143
|
pipes.flatten.each { |io| io.close unless io.closed? }
|
145
|
-
@
|
144
|
+
@status = Process.wait2(lpid)[-1] rescue nil
|
146
145
|
['TERM', 'KILL'].each { |sig| Process.kill sig, *pids rescue nil }
|
147
146
|
Process.waitall rescue nil
|
148
147
|
end
|
@@ -163,16 +162,43 @@ class EasySH < Struct.new(:cmd, :opt, :chain, :instant)
|
|
163
162
|
to_io { |i| while b = i.getbyte; yield b; end }
|
164
163
|
end
|
165
164
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
165
|
+
def [] i, *args
|
166
|
+
case i
|
167
|
+
when Integer
|
168
|
+
return to_a[i] if args.empty?
|
169
|
+
return to_a[i, *args] if args.size == 1 && args[0].is_a?(Integer)
|
170
|
+
when Range
|
171
|
+
return to_a[i] if args.empty?
|
172
|
+
when Regexp
|
173
|
+
return to_s[i, *args]
|
174
|
+
end
|
175
|
+
method_missing nil, i, *args
|
176
|
+
end
|
177
|
+
|
178
|
+
def to_i
|
179
|
+
to_s if status.nil?
|
180
|
+
(status && status.exitstatus) || 0
|
181
|
+
end
|
182
|
+
|
183
|
+
def successful?
|
184
|
+
to_i == 0
|
185
|
+
end
|
186
|
+
|
187
|
+
def failed?
|
188
|
+
! successful?
|
189
|
+
end
|
190
|
+
|
191
|
+
alias :call :method_missing
|
192
|
+
alias :to_ary :to_a
|
193
|
+
alias :lines :each_line
|
194
|
+
alias :each :each_line
|
195
|
+
alias :lines :each_line
|
196
|
+
alias :chars :each_char
|
197
|
+
alias :bytes :each_byte
|
198
|
+
alias :read :to_s
|
199
|
+
alias :! :to_s
|
200
|
+
alias :exitstatus :status
|
201
|
+
alias :exitcode :to_i
|
176
202
|
|
177
203
|
def pretty_print(q)
|
178
204
|
q.text self.inspect
|
data/test/test_easysh.rb
CHANGED
@@ -37,6 +37,33 @@ class EasySHTest < Test::Unit::TestCase
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
def test_alternatives
|
41
|
+
ref_cmd = sh['ls', '-l'].cmd
|
42
|
+
assert_equal ref_cmd, sh.ls['-l'].cmd
|
43
|
+
assert_equal ref_cmd, sh.ls[:l].cmd
|
44
|
+
assert_equal ref_cmd, sh.ls(:l).cmd
|
45
|
+
assert_equal ref_cmd, sh.ls._l.cmd
|
46
|
+
assert_equal ref_cmd, sh['ls']._l.cmd
|
47
|
+
|
48
|
+
ref_cmd = sh['ls', '--all', '--color=auto'].cmd
|
49
|
+
assert_equal ref_cmd, sh.ls.__all[:color => :auto].cmd
|
50
|
+
assert_equal ref_cmd, sh.ls.__all(:color => :auto).cmd
|
51
|
+
assert_equal ref_cmd, sh.ls[:all][:color => :auto].cmd
|
52
|
+
assert_equal ref_cmd, sh.ls(:all)[:color => :auto].cmd
|
53
|
+
|
54
|
+
ref_cmd = sh['tail', '-f', '-n', '3'].cmd
|
55
|
+
assert_equal ref_cmd, sh.tail._f._n['3'].cmd
|
56
|
+
assert_equal ref_cmd, sh.tail._f._n(3).cmd
|
57
|
+
assert_equal ref_cmd, sh.tail._f[n: 3].cmd
|
58
|
+
assert_equal ref_cmd, sh.tail._f[:n, 3].cmd
|
59
|
+
assert_equal ref_cmd, sh.tail._f(n: 3).cmd
|
60
|
+
assert_equal ref_cmd, sh.tail._f(:n, 3).cmd
|
61
|
+
assert_equal ref_cmd, sh.tail(:f, n: 3).cmd
|
62
|
+
assert_equal ref_cmd, sh.tail(:f, :n, 3).cmd
|
63
|
+
assert_equal ref_cmd, sh.tail[:f, n: 3].cmd
|
64
|
+
assert_equal ref_cmd, sh.tail[:f, :n, 3].cmd
|
65
|
+
end
|
66
|
+
|
40
67
|
def test_enumerator
|
41
68
|
with_tmpfile [*10..25].join("\n") do |tmppath, content|
|
42
69
|
kat = sh.cat
|
@@ -97,9 +124,36 @@ class EasySHTest < Test::Unit::TestCase
|
|
97
124
|
with_tmpfile do |tmppath, content|
|
98
125
|
assert_fd_count_equal do
|
99
126
|
assert_equal ((kat < tmppath) | kat).to_s, content
|
100
|
-
((sh.echo('hello') | kat) > tmppath)
|
127
|
+
((sh.echo('hello') | kat) > tmppath).to_s
|
101
128
|
assert_equal File.read(tmppath).chomp, 'hello'
|
102
129
|
end
|
103
130
|
end
|
104
131
|
end
|
132
|
+
|
133
|
+
def test_lines_regex
|
134
|
+
assert_fd_count_equal do
|
135
|
+
assert_equal sh.echo["a\nb"][1], 'b'
|
136
|
+
assert_equal sh.echo["a\nb"][-1], 'b'
|
137
|
+
assert_equal sh.echo["a\nb\nc"][0..1], ['a', 'b']
|
138
|
+
assert_equal sh.echo["a\nb\nc"][1, 2], ['b', 'c']
|
139
|
+
assert_equal sh.echo["a\nb\nc"][1, 2, 3].class, sh.class
|
140
|
+
assert_equal sh.echo["a\nb\nc"][1..2, 3].class, sh.class
|
141
|
+
assert_equal sh.echo["hello\nabcword"][/b.*o/], 'bcwo'
|
142
|
+
assert_equal sh.echo["hello\nabcword"][/b(.*)o/, 1], 'cw'
|
143
|
+
assert_equal sh.echo("Hello world\nThis is a test")[/T.*$/], "This is a test"
|
144
|
+
assert_equal sh.echo("Hello world\nThis is a test")[/T.* ([^ ]*)$/, 1], "test"
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_status
|
149
|
+
assert_equal sh.true.exitcode, 0
|
150
|
+
assert_not_equal sh.false.exitstatus, 0
|
151
|
+
assert_equal sh.true.successful?, true
|
152
|
+
assert_equal sh.false.failed?, true
|
153
|
+
sh1 = sh.true
|
154
|
+
assert_equal sh1.status, nil
|
155
|
+
sh1.to_s
|
156
|
+
assert_not_equal sh1.status, nil
|
157
|
+
assert_equal sh1.exitcode, 0
|
158
|
+
end
|
105
159
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easysh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
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: 2012-09-
|
12
|
+
date: 2012-09-19 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Synatactic sugars about shell executables, redirects and pipes
|
15
15
|
email: quark@zju.edu.cn
|
@@ -19,7 +19,7 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- lib/easysh.rb
|
21
21
|
- test/test_easysh.rb
|
22
|
-
homepage: https://github.com/quark-zju/
|
22
|
+
homepage: https://github.com/quark-zju/easysh
|
23
23
|
licenses: []
|
24
24
|
post_install_message:
|
25
25
|
rdoc_options: []
|