shr 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,7 @@ This is a port of Python's [sh](https://github.com/amoffat/sh).
10
10
 
11
11
  Installation
12
12
  ------------
13
- TODO: Write installation here
13
+ gem install shr
14
14
 
15
15
  Usage
16
16
  -----
@@ -18,5 +18,5 @@ TODO: Write usage instructions here
18
18
 
19
19
  License
20
20
  -------
21
- Released under the MIT License. See the [LICENCE][licence] file for further details.
21
+ Released under the MIT License. See the [LICENSE][license] file for further details.
22
22
  [license]: https://github.com/kukenko/shr/LICENSE.md
data/lib/shr/option.rb CHANGED
@@ -2,45 +2,43 @@
2
2
 
3
3
  module Shr
4
4
  class Option
5
- # xxx
6
5
  def initialize
7
- @stack = []
6
+ @options = []
7
+ @indicator = '-'
8
8
  end
9
+ attr_accessor :indicator
9
10
 
10
- # xxx
11
- # This method has not support Windows yet.
12
11
  def parse(options)
13
12
  options.each do |opt|
14
13
  case opt
15
14
  when String
16
- unless opt.start_with? '-'
17
- if @stack.last && @stack.last.start_with?('--')
18
- @stack.push "#{@stack.pop}=#{opt}"
19
- else
20
- @stack.push opt
21
- end
22
- else
23
- @stack.push opt
24
- end
15
+ @options << opt
25
16
  when Symbol
26
- @stack.push << "-#{opt.length > 1 ? '-' : ''}#{opt}"
17
+ @options << translate_from(opt)
27
18
  when Hash
28
19
  opt.each do |k, v|
29
20
  if v.eql?(true)
30
- @stack.push << "-#{k.length > 1 ? '-' : ''}#{k}"
21
+ @options << translate_from(k)
31
22
  else
32
23
  if k.length > 1
33
- @stack << "--#{k}=#{v}"
24
+ @options << "#{translate_from(k)}=#{v}"
34
25
  else
35
- @stack << "-#{k.length > 1 ? '-' : ''}#{k}" << v
26
+ @options << translate_from(k) << v
36
27
  end
37
28
  end
38
29
  end
39
30
  when Fixnum
40
- @stack.push << "-#{opt}"
31
+ @options << "#{@indicator}#{opt}"
41
32
  end
42
33
  end
43
- @stack
34
+ @options
35
+ end
36
+
37
+ private
38
+
39
+ def translate_from(opt)
40
+ s = opt.to_s
41
+ "#{@indicator}#{(s.length > 1 ? '-' : '') if @indicator == '-'}#{s}"
44
42
  end
45
43
  end
46
44
  end
data/lib/shr/shell.rb CHANGED
@@ -78,9 +78,17 @@ module Shr
78
78
  @command_out && !@command_out.closed?
79
79
  end
80
80
 
81
+ # xxx
81
82
  def command_line(name, args)
82
- options = Option.new.parse(args).join(' ')
83
- "#{name} #{options}".strip
83
+ option = Option.new
84
+ option.indicator = '/' if OS.windows?
85
+ options = option.parse(args).join(' ')
86
+
87
+ if OS.windows?
88
+ "cmd /c #{name} #{options}".strip
89
+ else
90
+ "#{name} #{options}".strip
91
+ end
84
92
  end
85
93
 
86
94
  def delay(name, args)
data/lib/shr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Shr
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/shr/which.rb CHANGED
@@ -12,13 +12,52 @@ module Shr
12
12
  @@path += cwd
13
13
  end
14
14
 
15
+ if OS.windows?
16
+ @@builtins = %w{
17
+ assoc
18
+ call
19
+ cd chdir
20
+ cls
21
+ color
22
+ date
23
+ del erace
24
+ dir
25
+ echo
26
+ exit
27
+ ftype
28
+ md mkdir
29
+ mklink
30
+ move
31
+ path
32
+ pause
33
+ popd
34
+ prompt
35
+ pushd
36
+ rd rmdir
37
+ ren rename
38
+ set
39
+ start
40
+ time
41
+ title
42
+ var
43
+ verify
44
+ vol
45
+ }
46
+ end
47
+
15
48
  def self.exist?(program)
16
49
  programs = add_extensions(program)
17
50
 
18
51
  entries = paths.product(programs).map { |list| list.join '/' }
19
- entries.find do |exe|
52
+ result = entries.find do |exe|
20
53
  File.executable?(exe) && !File.directory?(exe)
21
54
  end
55
+
56
+ if OS.windows?
57
+ result = program.to_s if @@builtins.include? program.to_s
58
+ end
59
+
60
+ result
22
61
  end
23
62
  class << self
24
63
  alias :exists? :exist?
@@ -5,76 +5,68 @@ module Shr
5
5
  describe Option do
6
6
  let(:opt) { Option.new }
7
7
 
8
+ SHORT_FORM = ['-o', 'page.html']
9
+ LONG_FORM = ['--shell=/bin/sh', '--silent']
10
+ MIXED_FORM = SHORT_FORM + LONG_FORM
11
+
8
12
  it 'has the following methods' do
9
13
  o = opt.methods
10
14
  o.should include(:parse)
11
15
  end
12
16
 
13
17
  describe '#parse' do
14
- shared_examples 'with short-form options' do
15
- it 'parses correctly' do
16
- opt.parse(options).should eq(['-o', 'page.html'])
17
- end
18
- end
19
-
20
- shared_examples 'with long-form options' do
21
- it 'parses correctly' do
22
- opt.parse(options).should eq(['--shell=/bin/sh', '--silent'])
23
- end
24
- end
25
-
26
- shared_examples 'with mixed options' do
27
- it 'parses correctly' do
28
- opt.parse(options).should eq(['-o', 'page.html', '--shell=/bin/sh', '--silent'])
29
- end
30
- end
31
-
32
18
  describe String do
33
- it_behaves_like 'with short-form options' do
34
- let(:options) { ['-o', 'page.html'] }
35
- end
36
-
37
- it_behaves_like 'with long-form options' do
38
- let(:options) { ['--shell', '/bin/sh', '--silent'] }
39
- end
40
-
41
- it_behaves_like 'with mixed options' do
42
- let(:options) { ['-o', 'page.html', '--shell', '/bin/sh', '--silent'] }
19
+ it 'remains argument unchanged' do
20
+ opt.parse(MIXED_FORM).should eq(MIXED_FORM)
43
21
  end
44
22
  end
45
23
 
46
24
  describe Symbol do
47
- it_behaves_like 'with short-form options' do
48
- let(:options) { [:o, 'page.html'] }
49
- end
25
+ context 'with a character' do
26
+ it 'tranlate argument to short-form' do
27
+ opt.parse([:o]).should eq(['-o'])
28
+ end
50
29
 
51
- it_behaves_like 'with long-form options' do
52
- let(:options) { [:shell, '/bin/sh', :silent] }
30
+ it 'may be indicated by @indicator' do
31
+ opt.indicator = '/'
32
+ opt.parse([:o]).should eq(['/o'])
33
+ end
53
34
  end
54
35
 
55
- it_behaves_like 'with mixed options' do
56
- let(:options) { [:o, 'page.html', :shell, '/bin/sh', :silent] }
36
+ context 'with some characters' do
37
+ it 'translates argument to long-form' do
38
+ opt.parse([:silent]).should eq(['--silent'])
39
+ end
40
+
41
+ it 'may be indicated by @indicator' do
42
+ opt.indicator = '/'
43
+ opt.parse([:silent]).should eq(['/silent'])
44
+ end
57
45
  end
58
46
  end
59
47
 
60
48
  describe Hash do
61
- it_behaves_like 'with short-form options' do
62
- let(:options) { [{ :o => 'page.html' }] }
63
- end
64
-
65
- it_behaves_like 'with long-form options' do
66
- let(:options) { [{ :shell => '/bin/sh', :silent => true }] }
67
- end
49
+ it { opt.parse([{ :o => 'page.html' }]).should eq(SHORT_FORM) }
50
+ it { opt.parse([{ :shell => '/bin/sh', :silent => true }]).should eq(LONG_FORM) }
68
51
 
69
- it_behaves_like 'with mixed options' do
70
- let(:options) { [{ :o => 'page.html', :shell => '/bin/sh', :silent => true }] }
52
+ context 'with indicator' do
53
+ it 'may be indicated by @indicator' do
54
+ opt.indicator = '/'
55
+ opt.parse([{ :o => 'page.html' }]).should eq(['/o', 'page.html'])
56
+ end
71
57
  end
72
58
  end
73
59
 
74
60
  describe Fixnum do
75
- it 'parses short-form options' do
76
- options = [1, 2, 3]
77
- opt.parse(options).should eq(['-1', '-2', '-3'])
61
+ it 'translates argument to short-form' do
62
+ opt.parse([1, 2, 3]).should eq(['-1', '-2', '-3'])
63
+ end
64
+
65
+ context 'with indicator' do
66
+ it 'may be indicated by @indicator' do
67
+ opt.indicator = '/'
68
+ opt.parse([1, 2, 3]).should eq(['/1', '/2', '/3'])
69
+ end
78
70
  end
79
71
  end
80
72
  end
@@ -74,8 +74,8 @@ module Shr
74
74
 
75
75
  describe '#exitstatus' do
76
76
  it "shows an exit status of process" do
77
- sh.ruby(:e, "'exit 0'").exitstatus.should eq(0)
78
- sh.ruby(:e, "'exit 1'").exitstatus.should eq(1)
77
+ sh.ruby('-e', "'exit 0'").exitstatus.should eq(0)
78
+ sh.ruby('-e', "'exit 1'").exitstatus.should eq(1)
79
79
  end
80
80
  end
81
81
 
@@ -10,18 +10,44 @@ module Shr
10
10
  end
11
11
 
12
12
  describe '#exist?' do
13
- it 'returns true if any of executables are found' do
14
- Which.exist?(:pwd).should be_true
13
+ shared_examples 'if any of executables are found' do
14
+ it { Which.exist?(command).should be_true }
15
15
  end
16
16
 
17
- it 'returns false if any of executables are not found' do
18
- Which.exist?(:not_found).should be_false
17
+ shared_examples 'if any of executables are not found' do
18
+ it { Which.exist?(command).should be_false }
19
19
  end
20
20
 
21
- context 'with argument String' do
22
- it 'returns true if any of executables are found' do
23
- Which.exist?('pwd').should be_true
21
+ unless ENV['OS'] == 'Windows_NT'
22
+ it_behaves_like 'if any of executables are found' do
23
+ let(:command) { :pwd }
24
24
  end
25
+
26
+ context 'with String' do
27
+ it_behaves_like 'if any of executables are found' do
28
+ let(:command) { 'pwd' }
29
+ end
30
+ end
31
+ else
32
+ it_behaves_like 'if any of executables are found' do
33
+ let(:command) { :notepad }
34
+ end
35
+
36
+ context 'with builtin command' do
37
+ it_behaves_like 'if any of executables are found' do
38
+ let(:command) { :chdir }
39
+ end
40
+ end
41
+
42
+ context 'with String' do
43
+ it_behaves_like 'if any of executables are found' do
44
+ let(:command) { 'chdir' }
45
+ end
46
+ end
47
+ end
48
+
49
+ it_behaves_like 'if any of executables are not found' do
50
+ let(:command) { :not_found }
25
51
  end
26
52
  end
27
53
  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.0.1
4
+ version: 0.1.1
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: 2013-01-06 00:00:00.000000000 Z
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: os
16
- requirement: &70190202007440 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70190202007440
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  description: Make controlling subprocess easier in Ruby.
26
31
  email:
27
32
  - m.kukenko@gmail.com
@@ -66,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
71
  version: '0'
67
72
  requirements: []
68
73
  rubyforge_project:
69
- rubygems_version: 1.8.11
74
+ rubygems_version: 1.8.24
70
75
  signing_key:
71
76
  specification_version: 3
72
77
  summary: Make controlling subprocess easier in Ruby.