rprogram 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog.md +9 -1
- data/LICENSE.txt +1 -1
- data/README.md +6 -10
- data/gemspec.yml +1 -2
- data/lib/rprogram/argument.rb +31 -0
- data/lib/rprogram/non_option.rb +6 -36
- data/lib/rprogram/option.rb +32 -43
- data/lib/rprogram/program.rb +9 -10
- data/lib/rprogram/rprogram.rb +6 -4
- data/lib/rprogram/system.rb +38 -31
- data/lib/rprogram/task.rb +14 -12
- data/lib/rprogram/version.rb +1 -1
- data/rprogram.gemspec +38 -83
- data/spec/classes/aliased_program.rb +2 -0
- data/spec/classes/named_program.rb +2 -0
- data/spec/non_option_spec.rb +10 -12
- data/spec/option_examples.rb +3 -5
- data/spec/option_list_spec.rb +7 -11
- data/spec/option_spec.rb +31 -33
- data/spec/program_spec.rb +8 -7
- data/spec/rprogram_spec.rb +7 -7
- data/spec/system_spec.rb +3 -2
- data/spec/task_spec.rb +43 -36
- metadata +5 -34
- data/lib/rprogram/yard.rb +0 -1
data/ChangeLog.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
### 0.3.2 / 2012-07-15
|
2
|
+
|
3
|
+
* Require Ruby >= 1.8.7.
|
4
|
+
* Removed env as a dependency.
|
5
|
+
* Added {RProgram::Argument}.
|
6
|
+
* Removed orphaned `rprogram/yard.rb` file.
|
7
|
+
* Style improvements.
|
8
|
+
|
1
9
|
### 0.3.1 / 2012-05-27
|
2
10
|
|
3
11
|
* Replaced ore-tasks with
|
@@ -76,7 +84,7 @@
|
|
76
84
|
* Renamed `PRogram::Compat.PATHS` to `RProgram::Compat.paths`.
|
77
85
|
* Refactored {RProgram::Option#arguments}.
|
78
86
|
* Removed `RProgram::Option#format`.
|
79
|
-
* Refactored
|
87
|
+
* Refactored `RProgram::NonOption#arguments`.
|
80
88
|
* Renamed `RProgram::NonOption#leading` to {RProgram::NonOption#leading?}.
|
81
89
|
* Removed `RProgram::NonOption#tailing`.
|
82
90
|
* Added {RProgram::NonOption#tailing?}.
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -49,8 +49,8 @@ First, create the class to represent the options of the program, using
|
|
49
49
|
long_option :flag => '--includes', :multiple => true
|
50
50
|
|
51
51
|
# options with multiple values can have a custom separator character
|
52
|
-
long_option :flag
|
53
|
-
:multiple
|
52
|
+
long_option :flag => '--ops',
|
53
|
+
:multiple => true,
|
54
54
|
:separator => ','
|
55
55
|
|
56
56
|
# define any non-options (aka additional arguments)
|
@@ -89,23 +89,19 @@ Finally, run your program with options or a block:
|
|
89
89
|
|
90
90
|
MyProgram.my_run do |my_prog|
|
91
91
|
my_prog.includes = ['one', 'two', 'three']
|
92
|
-
my_prog.mode
|
92
|
+
my_prog.mode = :safe
|
93
93
|
|
94
94
|
my_prog.output = 'output.txt'
|
95
|
-
my_prog.files
|
95
|
+
my_prog.files = ['test1.txt', 'test2.txt']
|
96
96
|
end
|
97
97
|
# => true
|
98
98
|
|
99
|
-
## Requirements
|
100
|
-
|
101
|
-
* [env](https://github.com/postmodern/env) ~> 0.1, >= 0.1.2
|
102
|
-
|
103
99
|
## Install
|
104
100
|
|
105
|
-
$
|
101
|
+
$ gem install rprogram
|
106
102
|
|
107
103
|
## License
|
108
104
|
|
109
|
-
Copyright (c) 2007-
|
105
|
+
Copyright (c) 2007-2012 Hal Brodigan
|
110
106
|
|
111
107
|
See {file:LICENSE.txt} for license information.
|
data/gemspec.yml
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
module RProgram
|
2
|
+
class Argument
|
3
|
+
|
4
|
+
#
|
5
|
+
# Formats a value into an Array of arguments.
|
6
|
+
#
|
7
|
+
# @param [Hash, Array, String] value
|
8
|
+
# The value to format.
|
9
|
+
#
|
10
|
+
# @return [Array]
|
11
|
+
# The formatted arguments.
|
12
|
+
#
|
13
|
+
def arguments(value)
|
14
|
+
value = case value
|
15
|
+
when Hash
|
16
|
+
value.map do |key,sub_value|
|
17
|
+
if sub_value == true then key.to_s
|
18
|
+
elsif sub_value then "#{key}=#{sub_value}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
when false, nil
|
22
|
+
[]
|
23
|
+
else
|
24
|
+
Array(value)
|
25
|
+
end
|
26
|
+
|
27
|
+
return value.compact
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/rprogram/non_option.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
require 'rprogram/argument'
|
2
|
+
|
1
3
|
module RProgram
|
2
|
-
class NonOption
|
4
|
+
class NonOption < Argument
|
3
5
|
|
4
6
|
# Name of the argument(s)
|
5
7
|
attr_reader :name
|
@@ -28,12 +30,9 @@ module RProgram
|
|
28
30
|
def initialize(options={})
|
29
31
|
@name = options[:name]
|
30
32
|
|
31
|
-
@tailing = if options[:leading]
|
32
|
-
|
33
|
-
|
34
|
-
options[:tailing]
|
35
|
-
else
|
36
|
-
true
|
33
|
+
@tailing = if options[:leading] then !options[:leading]
|
34
|
+
elsif options[:tailing] then options[:tailing]
|
35
|
+
else true
|
37
36
|
end
|
38
37
|
|
39
38
|
@multiple = (options[:multiple] || false)
|
@@ -59,34 +58,5 @@ module RProgram
|
|
59
58
|
!(@tailing)
|
60
59
|
end
|
61
60
|
|
62
|
-
#
|
63
|
-
# Formats the arguments for the non-option.
|
64
|
-
#
|
65
|
-
# @param [Hash, Array, String, nil] value
|
66
|
-
# The value to use for the arguments of the non-option.
|
67
|
-
#
|
68
|
-
# @return [Array]
|
69
|
-
# The arguments for the non-option.
|
70
|
-
#
|
71
|
-
def arguments(value)
|
72
|
-
return [] unless value
|
73
|
-
|
74
|
-
if value.kind_of?(Hash)
|
75
|
-
value = value.map do |key,sub_value|
|
76
|
-
if sub_value == true
|
77
|
-
key.to_s
|
78
|
-
elsif sub_value
|
79
|
-
"#{key}=#{sub_value}"
|
80
|
-
end
|
81
|
-
end
|
82
|
-
elsif value.kind_of?(Array)
|
83
|
-
value = value.flatten
|
84
|
-
else
|
85
|
-
value = [value]
|
86
|
-
end
|
87
|
-
|
88
|
-
return value.compact
|
89
|
-
end
|
90
|
-
|
91
61
|
end
|
92
62
|
end
|
data/lib/rprogram/option.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
+
require 'rprogram/argument'
|
2
|
+
|
1
3
|
module RProgram
|
2
|
-
class Option
|
4
|
+
class Option < Argument
|
3
5
|
|
4
6
|
# Flag of the option
|
5
7
|
attr_reader :flag
|
@@ -54,26 +56,24 @@ module RProgram
|
|
54
56
|
def initialize(options={},&block)
|
55
57
|
@flag = options[:flag]
|
56
58
|
|
57
|
-
@equals
|
58
|
-
@multiple
|
59
|
-
@separator = if options[:separator]
|
60
|
-
|
61
|
-
elsif options[:equals]
|
62
|
-
' '
|
59
|
+
@equals = (options[:equals] || false)
|
60
|
+
@multiple = (options[:multiple] || false)
|
61
|
+
@separator = if options[:separator] then options[:separator]
|
62
|
+
elsif options[:equals] then ' '
|
63
63
|
end
|
64
64
|
@sub_options = (options[:sub_options] || false)
|
65
65
|
|
66
66
|
@formatter = if block
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
67
|
+
block
|
68
|
+
else
|
69
|
+
Proc.new do |opt,value|
|
70
|
+
if opt.equals
|
71
|
+
["#{opt.flag}=#{value.first}"]
|
72
|
+
else
|
73
|
+
[opt.flag] + value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
77
|
end
|
78
78
|
|
79
79
|
#
|
@@ -86,38 +86,27 @@ module RProgram
|
|
86
86
|
# The formatted arguments of the option.
|
87
87
|
#
|
88
88
|
def arguments(value)
|
89
|
-
|
90
|
-
|
89
|
+
case value
|
90
|
+
when true
|
91
|
+
[@flag]
|
92
|
+
when false, nil
|
93
|
+
[]
|
94
|
+
else
|
95
|
+
value = super(value)
|
91
96
|
|
92
|
-
|
97
|
+
if @multiple
|
98
|
+
args = []
|
93
99
|
|
94
|
-
|
95
|
-
|
96
|
-
if sub_value == true
|
97
|
-
key.to_s
|
98
|
-
elsif sub_value
|
99
|
-
"#{key}=#{sub_value}"
|
100
|
+
value.each do |arg|
|
101
|
+
args += Array(@formatter.call(self,[arg]))
|
100
102
|
end
|
101
|
-
}
|
102
|
-
elsif value.kind_of?(Array)
|
103
|
-
value.flatten!
|
104
|
-
else
|
105
|
-
value = [value]
|
106
|
-
end
|
107
103
|
|
108
|
-
|
104
|
+
return args
|
105
|
+
else
|
106
|
+
value = [value.join(@separator)] if @separator
|
109
107
|
|
110
|
-
|
111
|
-
return value.inject([]) do |args,value|
|
112
|
-
arg = @formatter.call(self,[value])
|
113
|
-
|
114
|
-
args += arg if arg
|
115
|
-
args
|
108
|
+
return Array(@formatter.call(self,value))
|
116
109
|
end
|
117
|
-
else
|
118
|
-
value = [value.join(@separator)] if @separator
|
119
|
-
|
120
|
-
return (@formatter.call(self,value) || [])
|
121
110
|
end
|
122
111
|
end
|
123
112
|
|
data/lib/rprogram/program.rb
CHANGED
@@ -32,17 +32,17 @@ module RProgram
|
|
32
32
|
# @example
|
33
33
|
# Program.new('/usr/bin/ls')
|
34
34
|
#
|
35
|
-
def initialize(path
|
35
|
+
def initialize(path)
|
36
36
|
path = File.expand_path(path)
|
37
37
|
|
38
38
|
unless File.file?(path)
|
39
|
-
raise(ProgramNotFound,"program #{path.dump} does not exist"
|
39
|
+
raise(ProgramNotFound,"program #{path.dump} does not exist")
|
40
40
|
end
|
41
41
|
|
42
42
|
@path = path
|
43
43
|
@name = File.basename(path)
|
44
44
|
|
45
|
-
|
45
|
+
yield self if block_given?
|
46
46
|
end
|
47
47
|
|
48
48
|
#
|
@@ -94,7 +94,7 @@ module RProgram
|
|
94
94
|
# alias_program 'vim', 'vi'
|
95
95
|
#
|
96
96
|
def self.alias_program(*aliases)
|
97
|
-
@program_aliases = aliases.map
|
97
|
+
@program_aliases = aliases.map(&:to_s)
|
98
98
|
end
|
99
99
|
|
100
100
|
#
|
@@ -229,9 +229,9 @@ module RProgram
|
|
229
229
|
path ||= System.find_program_by_names(*self.program_names)
|
230
230
|
|
231
231
|
unless path
|
232
|
-
names = self.program_names.map
|
232
|
+
names = self.program_names.map(&:dump).join(', ')
|
233
233
|
|
234
|
-
raise(ProgramNotFound,"programs #{names} were not found"
|
234
|
+
raise(ProgramNotFound,"programs #{names} were not found")
|
235
235
|
end
|
236
236
|
|
237
237
|
return self.new(path,*arguments,&block)
|
@@ -339,10 +339,9 @@ module RProgram
|
|
339
339
|
# For valid `:sudo` options.
|
340
340
|
#
|
341
341
|
def sudo(*arguments)
|
342
|
-
options =
|
343
|
-
|
344
|
-
else
|
345
|
-
{}
|
342
|
+
options = case arguments.last
|
343
|
+
when Hash then arguments.pop
|
344
|
+
else {}
|
346
345
|
end
|
347
346
|
|
348
347
|
task = SudoTask.new(options.delete(:sudo) || {})
|
data/lib/rprogram/rprogram.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
module RProgram
|
2
|
+
@debug = false
|
3
|
+
|
2
4
|
#
|
3
5
|
# @return [true, false]
|
4
6
|
# Specifies whether debugging messages are enabled for RProgram.
|
5
7
|
# Defaults to false, if not set.
|
6
8
|
#
|
7
|
-
def
|
8
|
-
|
9
|
+
def self.debug
|
10
|
+
@debug
|
9
11
|
end
|
10
12
|
|
11
13
|
#
|
@@ -17,7 +19,7 @@ module RProgram
|
|
17
19
|
# @return [true, false]
|
18
20
|
# The new value of RProgram.debug.
|
19
21
|
#
|
20
|
-
def
|
21
|
-
|
22
|
+
def self.debug=(value)
|
23
|
+
@debug = value
|
22
24
|
end
|
23
25
|
end
|
data/lib/rprogram/system.rb
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
require 'rprogram/exceptions/program_not_found'
|
2
2
|
require 'rprogram/rprogram'
|
3
3
|
|
4
|
-
require '
|
4
|
+
require 'pathname'
|
5
5
|
|
6
6
|
module RProgram
|
7
7
|
#
|
8
8
|
# @since 0.3.0
|
9
9
|
#
|
10
10
|
module System
|
11
|
-
extend Env::Variables
|
12
11
|
|
13
12
|
@arch, @platform = RUBY_PLATFORM.split('-',2)
|
14
13
|
@platform ||= @arch
|
@@ -25,7 +24,7 @@ module RProgram
|
|
25
24
|
#
|
26
25
|
# @since 0.3.0
|
27
26
|
#
|
28
|
-
def
|
27
|
+
def self.arch
|
29
28
|
@arch
|
30
29
|
end
|
31
30
|
|
@@ -39,7 +38,7 @@ module RProgram
|
|
39
38
|
# System.platform
|
40
39
|
# # => "linux"
|
41
40
|
#
|
42
|
-
def
|
41
|
+
def self.platform
|
43
42
|
@platform
|
44
43
|
end
|
45
44
|
|
@@ -51,7 +50,7 @@ module RProgram
|
|
51
50
|
#
|
52
51
|
# @since 0.3.0
|
53
52
|
#
|
54
|
-
def
|
53
|
+
def self.windows?
|
55
54
|
if @platform
|
56
55
|
@platform.include?('mingw') || @platform.include?('mswin')
|
57
56
|
else
|
@@ -67,8 +66,8 @@ module RProgram
|
|
67
66
|
#
|
68
67
|
# @since 0.3.0
|
69
68
|
#
|
70
|
-
def
|
71
|
-
RUBY_VERSION
|
69
|
+
def self.ruby_1_8?
|
70
|
+
RUBY_VERSION.start_with?('1.8.')
|
72
71
|
end
|
73
72
|
|
74
73
|
#
|
@@ -79,9 +78,20 @@ module RProgram
|
|
79
78
|
#
|
80
79
|
# @since 0.3.0
|
81
80
|
#
|
82
|
-
def
|
83
|
-
|
84
|
-
|
81
|
+
def self.jruby?
|
82
|
+
const_defined?(:RUBY_ENGINE) && const_get(:RUBY_ENGINE) == 'jruby'
|
83
|
+
end
|
84
|
+
|
85
|
+
#
|
86
|
+
# The directories to search for programs.
|
87
|
+
#
|
88
|
+
# @return [Array<Pathname>]
|
89
|
+
# The directories containing programs.
|
90
|
+
#
|
91
|
+
def self.paths
|
92
|
+
@paths ||= ENV['PATH'].split(File::PATH_SEPARATOR).map do |dir|
|
93
|
+
Pathname.new(dir)
|
94
|
+
end
|
85
95
|
end
|
86
96
|
|
87
97
|
#
|
@@ -97,11 +107,9 @@ module RProgram
|
|
97
107
|
# System.find_program('as')
|
98
108
|
# #=> #<Pathname:/usr/bin/as>
|
99
109
|
#
|
100
|
-
def
|
110
|
+
def self.find_program(name)
|
101
111
|
# add the `.exe` suffix to the name, if running on Windows
|
102
|
-
if windows?
|
103
|
-
name = "#{name}.exe"
|
104
|
-
end
|
112
|
+
name = "#{name}.exe" if windows?
|
105
113
|
|
106
114
|
paths.each do |dir|
|
107
115
|
full_path = dir.join(name).expand_path
|
@@ -125,7 +133,7 @@ module RProgram
|
|
125
133
|
# System.find_program_by_names("gas","as")
|
126
134
|
# # => #<Pathname:/usr/bin/as>
|
127
135
|
#
|
128
|
-
def
|
136
|
+
def self.find_program_by_names(*names)
|
129
137
|
names.each do |name|
|
130
138
|
if (path = find_program(name))
|
131
139
|
return path
|
@@ -171,24 +179,25 @@ module RProgram
|
|
171
179
|
#
|
172
180
|
# @raise [RuntimeError]
|
173
181
|
# Passing `:popen`, `:env` or exec options is not supported
|
174
|
-
#
|
182
|
+
# on Ruby 1.8.x.
|
175
183
|
#
|
176
184
|
# @see http://rubydoc.info/stdlib/core/1.9.2/Kernel#spawn-instance_method
|
177
185
|
# For acceptable options.
|
178
186
|
#
|
179
|
-
def
|
187
|
+
def self.run(*arguments)
|
180
188
|
# extra tailing options and ENV variables from arguments
|
181
|
-
|
189
|
+
case arguments.last
|
190
|
+
when Hash
|
182
191
|
options = arguments.pop
|
183
|
-
env
|
184
|
-
popen
|
192
|
+
env = (options.delete(:env) || {})
|
193
|
+
popen = options.delete(:popen)
|
185
194
|
else
|
186
195
|
options = {}
|
187
|
-
env
|
196
|
+
env = {}
|
188
197
|
end
|
189
198
|
|
190
199
|
# all arguments must be Strings
|
191
|
-
arguments = arguments.map
|
200
|
+
arguments = arguments.map(&:to_s)
|
192
201
|
|
193
202
|
# print debugging information
|
194
203
|
if RProgram.debug
|
@@ -222,13 +231,11 @@ module RProgram
|
|
222
231
|
end
|
223
232
|
|
224
233
|
# re-add ENV variables and exec options
|
225
|
-
arguments.unshift(env)
|
234
|
+
arguments.unshift(env) unless env.empty?
|
226
235
|
arguments.push(options) unless options.empty?
|
227
236
|
|
228
|
-
if popen
|
229
|
-
|
230
|
-
else
|
231
|
-
Kernel.system(*arguments)
|
237
|
+
if popen then IO.popen(arguments,popen)
|
238
|
+
else Kernel.system(*arguments)
|
232
239
|
end
|
233
240
|
end
|
234
241
|
|
@@ -240,7 +247,7 @@ module RProgram
|
|
240
247
|
#
|
241
248
|
# @since 0.3.0
|
242
249
|
#
|
243
|
-
def
|
250
|
+
def self.sudo_path
|
244
251
|
@sudo ||= find_program('sudo')
|
245
252
|
end
|
246
253
|
|
@@ -255,7 +262,7 @@ module RProgram
|
|
255
262
|
#
|
256
263
|
# @since 0.3.0
|
257
264
|
#
|
258
|
-
def
|
265
|
+
def self.sudo_path=(path)
|
259
266
|
@sudo = Pathname.new(path)
|
260
267
|
end
|
261
268
|
|
@@ -267,7 +274,7 @@ module RProgram
|
|
267
274
|
#
|
268
275
|
# @since 0.3.0
|
269
276
|
#
|
270
|
-
def
|
277
|
+
def self.sudo?
|
271
278
|
!sudo_path.nil?
|
272
279
|
end
|
273
280
|
|
@@ -305,7 +312,7 @@ module RProgram
|
|
305
312
|
#
|
306
313
|
# @see run
|
307
314
|
#
|
308
|
-
def
|
315
|
+
def self.sudo(*arguments)
|
309
316
|
unless sudo?
|
310
317
|
raise(ProgramNotFound,'could not find the "sudo" program')
|
311
318
|
end
|