command_kit 0.1.0.pre2 → 0.2.1
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.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +15 -0
- data/.rubocop.yml +141 -0
- data/ChangeLog.md +98 -2
- data/Gemfile +3 -0
- data/README.md +189 -117
- data/Rakefile +3 -2
- data/command_kit.gemspec +4 -4
- data/examples/command.rb +1 -1
- data/gemspec.yml +10 -2
- data/lib/command_kit/arguments/argument.rb +2 -0
- data/lib/command_kit/arguments/argument_value.rb +2 -0
- data/lib/command_kit/arguments.rb +23 -4
- data/lib/command_kit/colors.rb +253 -45
- data/lib/command_kit/command.rb +6 -1
- data/lib/command_kit/command_name.rb +9 -0
- data/lib/command_kit/commands/auto_load.rb +24 -1
- data/lib/command_kit/commands/auto_require.rb +16 -0
- data/lib/command_kit/commands/command.rb +3 -0
- data/lib/command_kit/commands/help.rb +5 -2
- data/lib/command_kit/commands/parent_command.rb +7 -0
- data/lib/command_kit/commands/subcommand.rb +13 -1
- data/lib/command_kit/commands.rb +54 -9
- data/lib/command_kit/description.rb +12 -1
- data/lib/command_kit/env/home.rb +9 -0
- data/lib/command_kit/env/path.rb +16 -1
- data/lib/command_kit/env.rb +4 -0
- data/lib/command_kit/examples.rb +12 -1
- data/lib/command_kit/exception_handler.rb +4 -0
- data/lib/command_kit/help/man.rb +26 -30
- data/lib/command_kit/help.rb +7 -1
- data/lib/command_kit/inflector.rb +49 -17
- data/lib/command_kit/interactive.rb +248 -0
- data/lib/command_kit/main.rb +18 -9
- data/lib/command_kit/man.rb +44 -0
- data/lib/command_kit/open_app.rb +69 -0
- data/lib/command_kit/options/option.rb +3 -6
- data/lib/command_kit/options/option_value.rb +5 -2
- data/lib/command_kit/options/parser.rb +46 -19
- data/lib/command_kit/options/quiet.rb +3 -0
- data/lib/command_kit/options/verbose.rb +5 -0
- data/lib/command_kit/options/version.rb +6 -0
- data/lib/command_kit/options.rb +32 -7
- data/lib/command_kit/os/linux.rb +157 -0
- data/lib/command_kit/os.rb +165 -11
- data/lib/command_kit/package_manager.rb +200 -0
- data/lib/command_kit/pager.rb +80 -11
- data/lib/command_kit/printing/indent.rb +27 -4
- data/lib/command_kit/printing.rb +35 -1
- data/lib/command_kit/program_name.rb +7 -0
- data/lib/command_kit/stdio.rb +24 -0
- data/lib/command_kit/sudo.rb +40 -0
- data/lib/command_kit/terminal.rb +159 -0
- data/lib/command_kit/usage.rb +14 -0
- data/lib/command_kit/version.rb +1 -1
- data/lib/command_kit/xdg.rb +13 -0
- data/lib/command_kit.rb +1 -0
- data/spec/arguments/argument_spec.rb +2 -2
- data/spec/arguments_spec.rb +53 -27
- data/spec/colors_spec.rb +277 -13
- data/spec/command_name_spec.rb +1 -1
- data/spec/command_spec.rb +79 -5
- data/spec/commands/auto_load/subcommand_spec.rb +1 -1
- data/spec/commands/auto_load_spec.rb +34 -3
- data/spec/commands/auto_require_spec.rb +2 -2
- data/spec/commands/help_spec.rb +1 -1
- data/spec/commands/parent_command_spec.rb +1 -1
- data/spec/commands/subcommand_spec.rb +1 -1
- data/spec/commands_spec.rb +103 -29
- data/spec/description_spec.rb +1 -25
- data/spec/env/home_spec.rb +1 -1
- data/spec/env/path_spec.rb +7 -1
- data/spec/examples_spec.rb +1 -25
- data/spec/exception_handler_spec.rb +1 -1
- data/spec/help/man_spec.rb +45 -58
- data/spec/help_spec.rb +0 -25
- data/spec/inflector_spec.rb +71 -9
- data/spec/interactive_spec.rb +415 -0
- data/spec/main_spec.rb +7 -7
- data/spec/man_spec.rb +46 -0
- data/spec/open_app_spec.rb +85 -0
- data/spec/options/option_spec.rb +5 -5
- data/spec/options/option_value_spec.rb +56 -1
- data/spec/options_spec.rb +283 -1
- data/spec/os/linux_spec.rb +164 -0
- data/spec/os_spec.rb +201 -14
- data/spec/package_manager_spec.rb +806 -0
- data/spec/pager_spec.rb +76 -11
- data/spec/printing/indent_spec.rb +8 -6
- data/spec/printing_spec.rb +33 -3
- data/spec/program_name_spec.rb +1 -1
- data/spec/spec_helper.rb +0 -3
- data/spec/sudo_spec.rb +51 -0
- data/spec/{console_spec.rb → terminal_spec.rb} +65 -35
- data/spec/usage_spec.rb +2 -2
- data/spec/xdg_spec.rb +1 -1
- metadata +26 -8
- data/lib/command_kit/console.rb +0 -141
@@ -14,6 +14,8 @@ module CommandKit
|
|
14
14
|
#
|
15
15
|
# Represents an additional argument associated with an option flag.
|
16
16
|
#
|
17
|
+
# @api private
|
18
|
+
#
|
17
19
|
class OptionValue < Arguments::ArgumentValue
|
18
20
|
|
19
21
|
# Maps OptionParser types to USAGE strings.
|
@@ -39,7 +41,7 @@ module CommandKit
|
|
39
41
|
|
40
42
|
# The desired type of the argument value.
|
41
43
|
#
|
42
|
-
# @return [Class, Hash, Array, Regexp
|
44
|
+
# @return [Class, Hash, Array, Regexp]
|
43
45
|
attr_reader :type
|
44
46
|
|
45
47
|
# The default parsed value for the argument value.
|
@@ -90,10 +92,11 @@ module CommandKit
|
|
90
92
|
def self.default_usage(type)
|
91
93
|
USAGES.fetch(type) do
|
92
94
|
case type
|
93
|
-
when Class then Inflector.underscore(type.name).upcase
|
94
95
|
when Hash then type.keys.join('|')
|
95
96
|
when Array then type.join('|')
|
96
97
|
when Regexp then type.source
|
98
|
+
when Class
|
99
|
+
Inflector.underscore(Inflector.demodularize(type.name)).upcase
|
97
100
|
else
|
98
101
|
raise(TypeError,"unsupported option type: #{type.inspect}")
|
99
102
|
end
|
@@ -33,6 +33,9 @@ module CommandKit
|
|
33
33
|
include Main
|
34
34
|
include Printing
|
35
35
|
|
36
|
+
#
|
37
|
+
# @api private
|
38
|
+
#
|
36
39
|
module ModuleMethods
|
37
40
|
#
|
38
41
|
# Sets {CommandKit::Usage::ClassMethods#usage .usage} or extends
|
@@ -58,6 +61,8 @@ module CommandKit
|
|
58
61
|
# The option parser.
|
59
62
|
#
|
60
63
|
# @return [OptionParser]
|
64
|
+
#
|
65
|
+
# @api semipublic
|
61
66
|
attr_reader :option_parser
|
62
67
|
|
63
68
|
#
|
@@ -65,6 +70,8 @@ module CommandKit
|
|
65
70
|
#
|
66
71
|
# @return [OptionParser]
|
67
72
|
#
|
73
|
+
# @api public
|
74
|
+
#
|
68
75
|
def initialize(**kwargs)
|
69
76
|
super(**kwargs)
|
70
77
|
|
@@ -91,6 +98,8 @@ module CommandKit
|
|
91
98
|
# @return [Integer]
|
92
99
|
# The exit status code.
|
93
100
|
#
|
101
|
+
# @api public
|
102
|
+
#
|
94
103
|
def main(argv=[])
|
95
104
|
super(parse_options(argv))
|
96
105
|
rescue SystemExit => system_exit
|
@@ -106,24 +115,24 @@ module CommandKit
|
|
106
115
|
# @return [Array<String>]
|
107
116
|
# The remaining non-option arguments.
|
108
117
|
#
|
118
|
+
# @api semipublic
|
119
|
+
#
|
109
120
|
def parse_options(argv)
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
on_parse_error(error)
|
126
|
-
end
|
121
|
+
option_parser.parse(argv)
|
122
|
+
rescue OptionParser::InvalidOption => error
|
123
|
+
on_invalid_option(error)
|
124
|
+
rescue OptionParser::AmbiguousOption => error
|
125
|
+
on_ambiguous_option(error)
|
126
|
+
rescue OptionParser::InvalidArgument => error
|
127
|
+
on_invalid_argument(error)
|
128
|
+
rescue OptionParser::MissingArgument => error
|
129
|
+
on_missing_argument(error)
|
130
|
+
rescue OptionParser::NeedlessArgument => error
|
131
|
+
on_needless_argument(error)
|
132
|
+
rescue OptionParser::AmbiguousArgument => error
|
133
|
+
on_ambiguous_argument(error)
|
134
|
+
rescue OptionParser::ParseError => error
|
135
|
+
on_parse_error(error)
|
127
136
|
end
|
128
137
|
|
129
138
|
#
|
@@ -132,9 +141,11 @@ module CommandKit
|
|
132
141
|
# @param [OptionParser::ParseError] error
|
133
142
|
# The error from `OptionParser`.
|
134
143
|
#
|
144
|
+
# @api semipublic
|
145
|
+
#
|
135
146
|
def on_parse_error(error)
|
136
|
-
print_error(
|
137
|
-
|
147
|
+
print_error(error.message)
|
148
|
+
stderr.puts("Try '#{command_name} --help' for more information.")
|
138
149
|
exit(1)
|
139
150
|
end
|
140
151
|
|
@@ -145,6 +156,8 @@ module CommandKit
|
|
145
156
|
#
|
146
157
|
# @see on_parse_error
|
147
158
|
#
|
159
|
+
# @api semipublic
|
160
|
+
#
|
148
161
|
def on_invalid_option(error)
|
149
162
|
on_parse_error(error)
|
150
163
|
end
|
@@ -157,6 +170,8 @@ module CommandKit
|
|
157
170
|
#
|
158
171
|
# @see on_parse_error
|
159
172
|
#
|
173
|
+
# @api semipublic
|
174
|
+
#
|
160
175
|
def on_ambiguous_option(error)
|
161
176
|
on_parse_error(error)
|
162
177
|
end
|
@@ -169,6 +184,8 @@ module CommandKit
|
|
169
184
|
#
|
170
185
|
# @see on_parse_error
|
171
186
|
#
|
187
|
+
# @api semipublic
|
188
|
+
#
|
172
189
|
def on_invalid_argument(error)
|
173
190
|
on_parse_error(error)
|
174
191
|
end
|
@@ -181,6 +198,8 @@ module CommandKit
|
|
181
198
|
#
|
182
199
|
# @see on_parse_error
|
183
200
|
#
|
201
|
+
# @api semipublic
|
202
|
+
#
|
184
203
|
def on_missing_argument(error)
|
185
204
|
on_parse_error(error)
|
186
205
|
end
|
@@ -193,6 +212,8 @@ module CommandKit
|
|
193
212
|
#
|
194
213
|
# @see on_parse_error
|
195
214
|
#
|
215
|
+
# @api semipublic
|
216
|
+
#
|
196
217
|
def on_needless_argument(error)
|
197
218
|
on_parse_error(error)
|
198
219
|
end
|
@@ -205,6 +226,8 @@ module CommandKit
|
|
205
226
|
#
|
206
227
|
# @see on_parse_error
|
207
228
|
#
|
229
|
+
# @api semipublic
|
230
|
+
#
|
208
231
|
def on_ambiguous_argument(error)
|
209
232
|
on_parse_error(error)
|
210
233
|
end
|
@@ -212,6 +235,8 @@ module CommandKit
|
|
212
235
|
#
|
213
236
|
# Prints the `--help` output.
|
214
237
|
#
|
238
|
+
# @api semipublic
|
239
|
+
#
|
215
240
|
def help_options
|
216
241
|
puts option_parser
|
217
242
|
end
|
@@ -219,6 +244,8 @@ module CommandKit
|
|
219
244
|
#
|
220
245
|
# @see #help_options
|
221
246
|
#
|
247
|
+
# @api public
|
248
|
+
#
|
222
249
|
def help
|
223
250
|
help_options
|
224
251
|
end
|
@@ -18,6 +18,9 @@ module CommandKit
|
|
18
18
|
module Verbose
|
19
19
|
include Options
|
20
20
|
|
21
|
+
#
|
22
|
+
# @api private
|
23
|
+
#
|
21
24
|
module ModuleMethods
|
22
25
|
#
|
23
26
|
# Defines a `-v, --verbose` option or extends {ModuleMethods}, depending
|
@@ -47,6 +50,8 @@ module CommandKit
|
|
47
50
|
#
|
48
51
|
# @return [Boolean]
|
49
52
|
#
|
53
|
+
# @api public
|
54
|
+
#
|
50
55
|
def verbose?
|
51
56
|
@verbose
|
52
57
|
end
|
@@ -35,6 +35,8 @@ module CommandKit
|
|
35
35
|
# @return [String, nil]
|
36
36
|
# The classes version string.
|
37
37
|
#
|
38
|
+
# @api public
|
39
|
+
#
|
38
40
|
def version(new_version=nil)
|
39
41
|
if new_version
|
40
42
|
@version = new_version
|
@@ -47,6 +49,8 @@ module CommandKit
|
|
47
49
|
#
|
48
50
|
# @see ClassMethods#version
|
49
51
|
#
|
52
|
+
# @api public
|
53
|
+
#
|
50
54
|
def version
|
51
55
|
self.class.version
|
52
56
|
end
|
@@ -54,6 +58,8 @@ module CommandKit
|
|
54
58
|
#
|
55
59
|
# Prints the version.
|
56
60
|
#
|
61
|
+
# @api public
|
62
|
+
#
|
57
63
|
def print_version
|
58
64
|
puts "#{command_name} #{version}"
|
59
65
|
end
|
data/lib/command_kit/options.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'command_kit/arguments'
|
1
2
|
require 'command_kit/options/option'
|
2
3
|
require 'command_kit/options/parser'
|
3
4
|
|
@@ -22,7 +23,7 @@ module CommandKit
|
|
22
23
|
# @bar = arg.split(':')
|
23
24
|
# end
|
24
25
|
#
|
25
|
-
# ### initialize and using
|
26
|
+
# ### initialize and using instance variables
|
26
27
|
#
|
27
28
|
# option :number, value: {type: Integer},
|
28
29
|
# desc: 'Numbers' do |num|
|
@@ -36,8 +37,12 @@ module CommandKit
|
|
36
37
|
# end
|
37
38
|
#
|
38
39
|
module Options
|
40
|
+
include Arguments
|
39
41
|
include Parser
|
40
42
|
|
43
|
+
#
|
44
|
+
# @api private
|
45
|
+
#
|
41
46
|
module ModuleMethods
|
42
47
|
#
|
43
48
|
# Extends {ClassMethods} or {ModuleMethods}, depending on whether
|
@@ -68,6 +73,8 @@ module CommandKit
|
|
68
73
|
#
|
69
74
|
# @return [Hash{Symbol => Option}]
|
70
75
|
#
|
76
|
+
# @api semipublic
|
77
|
+
#
|
71
78
|
def options
|
72
79
|
@options ||= if superclass.kind_of?(ClassMethods)
|
73
80
|
superclass.options.dup
|
@@ -172,6 +179,8 @@ module CommandKit
|
|
172
179
|
# # ...
|
173
180
|
# end
|
174
181
|
#
|
182
|
+
# @api public
|
183
|
+
#
|
175
184
|
def option(name,**kwargs,&block)
|
176
185
|
options[name] = Option.new(name,**kwargs,&block)
|
177
186
|
end
|
@@ -180,6 +189,9 @@ module CommandKit
|
|
180
189
|
# Hash of parsed option values.
|
181
190
|
#
|
182
191
|
# @return [Hash{Symbol => Object}]
|
192
|
+
#
|
193
|
+
# @api semipublic
|
194
|
+
#
|
183
195
|
attr_reader :options
|
184
196
|
|
185
197
|
#
|
@@ -187,24 +199,26 @@ module CommandKit
|
|
187
199
|
# {Parser#option_parser option parser}.
|
188
200
|
#
|
189
201
|
# @param [Hash{Symbol => Object}] options
|
190
|
-
# Optional
|
202
|
+
# Optional prepopulated options hash.
|
191
203
|
#
|
192
204
|
# @note
|
193
205
|
# The {#option_parser} will populate {#options} and also call any
|
194
206
|
# {ClassMethods#option option} blocks with the parsed option values.
|
195
207
|
#
|
208
|
+
# @api public
|
209
|
+
#
|
196
210
|
def initialize(options: {}, **kwargs)
|
197
211
|
@options = options
|
198
212
|
|
199
213
|
super(**kwargs)
|
200
214
|
|
201
215
|
self.class.options.each_value do |option|
|
216
|
+
default_value = option.default_value
|
217
|
+
|
218
|
+
@options[option.name] = default_value unless default_value.nil?
|
219
|
+
|
202
220
|
option_parser.on(*option.usage,option.type,option.desc) do |arg,*captures|
|
203
|
-
@options[option.name] =
|
204
|
-
option.default_value
|
205
|
-
else
|
206
|
-
arg
|
207
|
-
end
|
221
|
+
@options[option.name] = arg
|
208
222
|
|
209
223
|
if option.block
|
210
224
|
instance_exec(*arg,*captures,&option.block)
|
@@ -212,5 +226,16 @@ module CommandKit
|
|
212
226
|
end
|
213
227
|
end
|
214
228
|
end
|
229
|
+
|
230
|
+
#
|
231
|
+
# Overrides the default {Usage#help help} method and calls {#help_options}
|
232
|
+
# and {#help_arguments}.
|
233
|
+
#
|
234
|
+
# @api public
|
235
|
+
#
|
236
|
+
def help
|
237
|
+
help_options
|
238
|
+
help_arguments
|
239
|
+
end
|
215
240
|
end
|
216
241
|
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'command_kit/os'
|
2
|
+
|
3
|
+
module CommandKit
|
4
|
+
module OS
|
5
|
+
#
|
6
|
+
# Provides methods for determining the specific type of Linux.
|
7
|
+
#
|
8
|
+
# ## Example
|
9
|
+
#
|
10
|
+
# require 'command_kit/command'
|
11
|
+
# require 'command_kit/os/linux'
|
12
|
+
#
|
13
|
+
# class Command < CommandKit::Command
|
14
|
+
#
|
15
|
+
# include CommandKit::OS::Linux
|
16
|
+
#
|
17
|
+
# def run
|
18
|
+
# if debian_linux?
|
19
|
+
# # ...
|
20
|
+
# elsif redhat_linux?
|
21
|
+
# # ...
|
22
|
+
# elsif suse_linux?
|
23
|
+
# # ...
|
24
|
+
# elsif arch_linux?
|
25
|
+
# # ...
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# @since 0.2.0
|
31
|
+
#
|
32
|
+
module Linux
|
33
|
+
#
|
34
|
+
# @api private
|
35
|
+
#
|
36
|
+
module ModuleMethods
|
37
|
+
#
|
38
|
+
# Extends {ClassMethods} or {ModuleMethods}, depending on whether
|
39
|
+
# {OS} is being included into a class or a module..
|
40
|
+
#
|
41
|
+
# @param [Class, Module] context
|
42
|
+
# The class or module which is including {OS}.
|
43
|
+
#
|
44
|
+
def included(context)
|
45
|
+
super(context)
|
46
|
+
|
47
|
+
if context.class == Module
|
48
|
+
context.extend ModuleMethods
|
49
|
+
else
|
50
|
+
context.extend ClassMethods
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
extend ModuleMethods
|
56
|
+
|
57
|
+
module ClassMethods
|
58
|
+
#
|
59
|
+
# Determines the specific Linux distro.
|
60
|
+
#
|
61
|
+
# @return [:fedora, :redhat, :debian, :suse, :arch, nil]
|
62
|
+
# Returns the type of Linux distro or `nil` if the Linux distro could
|
63
|
+
# not be determined.
|
64
|
+
#
|
65
|
+
# @api semipublic
|
66
|
+
#
|
67
|
+
def linux_distro
|
68
|
+
if File.file?('/etc/fedora-release') then :fedora
|
69
|
+
elsif File.file?('/etc/redhat-release') then :redhat
|
70
|
+
elsif File.file?('/etc/debian_version') then :debian
|
71
|
+
elsif File.file?('/etc/SuSE-release') then :suse
|
72
|
+
elsif File.file?('/etc/arch-release') then :arch
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# The Linux distro.
|
78
|
+
#
|
79
|
+
# @return [:fedora, :redhat, :debian, :suse, :arch, nil]
|
80
|
+
#
|
81
|
+
# @api public
|
82
|
+
attr_reader :linux_distro
|
83
|
+
|
84
|
+
#
|
85
|
+
# Initializes the command.
|
86
|
+
#
|
87
|
+
# @param [:fedora, :redhat, :debian, :suse, :arch, nil] linux_distro
|
88
|
+
# Overrides the default detected Linux distro.
|
89
|
+
#
|
90
|
+
# @param [Hash{Symbol => Object}] kwargs
|
91
|
+
# Additional keyword arguments.
|
92
|
+
#
|
93
|
+
# @api public
|
94
|
+
#
|
95
|
+
def initialize(linux_distro: self.class.linux_distro, **kwargs)
|
96
|
+
super(**kwargs)
|
97
|
+
|
98
|
+
@linux_distro = linux_distro
|
99
|
+
end
|
100
|
+
|
101
|
+
#
|
102
|
+
# Determines if the current OS is RedHat Linux based distro.
|
103
|
+
#
|
104
|
+
# @return [Boolean]
|
105
|
+
#
|
106
|
+
# @api public
|
107
|
+
#
|
108
|
+
def redhat_linux?
|
109
|
+
@linux_distro == :redhat
|
110
|
+
end
|
111
|
+
|
112
|
+
#
|
113
|
+
# Determines if the current OS is Fedora Linux based distro.
|
114
|
+
#
|
115
|
+
# @return [Boolean]
|
116
|
+
#
|
117
|
+
# @api public
|
118
|
+
#
|
119
|
+
def fedora_linux?
|
120
|
+
@linux_distro == :fedora
|
121
|
+
end
|
122
|
+
|
123
|
+
#
|
124
|
+
# Determines if the current OS is Debian Linux based distro.
|
125
|
+
#
|
126
|
+
# @return [Boolean]
|
127
|
+
#
|
128
|
+
# @api public
|
129
|
+
#
|
130
|
+
def debian_linux?
|
131
|
+
@linux_distro == :debian
|
132
|
+
end
|
133
|
+
|
134
|
+
#
|
135
|
+
# Determines if the current OS is SUSE Linux based distro.
|
136
|
+
#
|
137
|
+
# @return [Boolean]
|
138
|
+
#
|
139
|
+
# @api public
|
140
|
+
#
|
141
|
+
def suse_linux?
|
142
|
+
@linux_distro == :suse
|
143
|
+
end
|
144
|
+
|
145
|
+
#
|
146
|
+
# Determines if the current OS is Arch Linux based distro.
|
147
|
+
#
|
148
|
+
# @return [Boolean]
|
149
|
+
#
|
150
|
+
# @api public
|
151
|
+
#
|
152
|
+
def arch_linux?
|
153
|
+
@linux_distro == :arch
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
data/lib/command_kit/os.rb
CHANGED
@@ -4,26 +4,111 @@ module CommandKit
|
|
4
4
|
#
|
5
5
|
# ## Examples
|
6
6
|
#
|
7
|
-
#
|
7
|
+
# require 'command_kit/command'
|
8
|
+
# require 'command_kit/os'
|
8
9
|
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
10
|
+
# class Command < CommandKit::Command
|
11
|
+
#
|
12
|
+
# include CommandKit::OS
|
13
|
+
#
|
14
|
+
# def main(*argv)
|
15
|
+
# if linux?
|
16
|
+
# # ...
|
17
|
+
# elsif macos?
|
18
|
+
# # ...
|
19
|
+
# elsif freebsd?
|
20
|
+
# # ...
|
21
|
+
# elsif windows?
|
22
|
+
# # ...
|
23
|
+
# end
|
16
24
|
# end
|
25
|
+
#
|
17
26
|
# end
|
18
27
|
#
|
19
28
|
module OS
|
29
|
+
#
|
30
|
+
# @api private
|
31
|
+
#
|
32
|
+
module ModuleMethods
|
33
|
+
#
|
34
|
+
# Extends {ClassMethods} or {ModuleMethods}, depending on whether
|
35
|
+
# {OS} is being included into a class or a module..
|
36
|
+
#
|
37
|
+
# @param [Class, Module] context
|
38
|
+
# The class or module which is including {OS}.
|
39
|
+
#
|
40
|
+
def included(context)
|
41
|
+
super(context)
|
42
|
+
|
43
|
+
if context.class == Module
|
44
|
+
context.extend ModuleMethods
|
45
|
+
else
|
46
|
+
context.extend ClassMethods
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
extend ModuleMethods
|
52
|
+
|
53
|
+
module ClassMethods
|
54
|
+
#
|
55
|
+
# Determines the current OS.
|
56
|
+
#
|
57
|
+
# @return [:linux, :macos, :freebsd, :openbsd, :netbsd, :windows, nil]
|
58
|
+
# The OS type or `nil` if the OS could not be determined.
|
59
|
+
#
|
60
|
+
# @api semipublic
|
61
|
+
#
|
62
|
+
# @since 0.2.0
|
63
|
+
#
|
64
|
+
def os
|
65
|
+
if RUBY_PLATFORM.include?('linux') then :linux
|
66
|
+
elsif RUBY_PLATFORM.include?('darwin') then :macos
|
67
|
+
elsif RUBY_PLATFORM.include?('freebsd') then :freebsd
|
68
|
+
elsif RUBY_PLATFORM.include?('openbsd') then :openbsd
|
69
|
+
elsif RUBY_PLATFORM.include?('netbsd') then :netbsd
|
70
|
+
elsif Gem.win_platform? then :windows
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# The current OS.
|
76
|
+
#
|
77
|
+
# @return [:linux, :macos, :freebsd, :openbsd, :netbsd, :windows, nil]
|
78
|
+
#
|
79
|
+
# @api public
|
80
|
+
#
|
81
|
+
# @since 0.2.0
|
82
|
+
attr_reader :os
|
83
|
+
|
84
|
+
#
|
85
|
+
# Initializes the command.
|
86
|
+
#
|
87
|
+
# @param [:linux, :macos, :freebsd, :openbsd, :netbsd, :windows, nil] os
|
88
|
+
# Overrides the default OS.
|
89
|
+
#
|
90
|
+
# @param [Hash{Symbol => Object}] kwargs
|
91
|
+
# Additional keyword arguments.
|
92
|
+
#
|
93
|
+
# @api public
|
94
|
+
#
|
95
|
+
# @since 0.2.0
|
96
|
+
#
|
97
|
+
def initialize(os: self.class.os, **kwargs)
|
98
|
+
super(**kwargs)
|
99
|
+
|
100
|
+
@os = os
|
101
|
+
end
|
102
|
+
|
20
103
|
#
|
21
104
|
# Determines if the current OS is Linux.
|
22
105
|
#
|
23
106
|
# @return [Boolean]
|
24
107
|
#
|
108
|
+
# @api public
|
109
|
+
#
|
25
110
|
def linux?
|
26
|
-
|
111
|
+
@os == :linux
|
27
112
|
end
|
28
113
|
|
29
114
|
#
|
@@ -31,8 +116,75 @@ module CommandKit
|
|
31
116
|
#
|
32
117
|
# @return [Boolean]
|
33
118
|
#
|
119
|
+
# @api public
|
120
|
+
#
|
34
121
|
def macos?
|
35
|
-
|
122
|
+
@os == :macos
|
123
|
+
end
|
124
|
+
|
125
|
+
#
|
126
|
+
# Determines if the current OS is FreeBSD.
|
127
|
+
#
|
128
|
+
# @return [Boolean]
|
129
|
+
#
|
130
|
+
# @api public
|
131
|
+
#
|
132
|
+
# @since 0.2.0
|
133
|
+
#
|
134
|
+
def freebsd?
|
135
|
+
@os == :freebsd
|
136
|
+
end
|
137
|
+
|
138
|
+
#
|
139
|
+
# Determines if the current OS is OpenBSD.
|
140
|
+
#
|
141
|
+
# @return [Boolean]
|
142
|
+
#
|
143
|
+
# @api public
|
144
|
+
#
|
145
|
+
# @since 0.2.0
|
146
|
+
#
|
147
|
+
def openbsd?
|
148
|
+
@os == :openbsd
|
149
|
+
end
|
150
|
+
|
151
|
+
#
|
152
|
+
# Determines if the current OS is NetBSD.
|
153
|
+
#
|
154
|
+
# @return [Boolean]
|
155
|
+
#
|
156
|
+
# @api public
|
157
|
+
#
|
158
|
+
# @since 0.2.0
|
159
|
+
#
|
160
|
+
def netbsd?
|
161
|
+
@os == :netbsd
|
162
|
+
end
|
163
|
+
|
164
|
+
#
|
165
|
+
# Determines if the current OS is BSD based.
|
166
|
+
#
|
167
|
+
# @return [Boolean]
|
168
|
+
#
|
169
|
+
# @since 0.2.0
|
170
|
+
#
|
171
|
+
# @api public
|
172
|
+
#
|
173
|
+
def bsd?
|
174
|
+
freebsd? || openbsd? || netbsd?
|
175
|
+
end
|
176
|
+
|
177
|
+
#
|
178
|
+
# Determines if the current OS is UNIX based.
|
179
|
+
#
|
180
|
+
# @return [Boolean]
|
181
|
+
#
|
182
|
+
# @since 0.2.0
|
183
|
+
#
|
184
|
+
# @api public
|
185
|
+
#
|
186
|
+
def unix?
|
187
|
+
linux? || macos? || bsd?
|
36
188
|
end
|
37
189
|
|
38
190
|
#
|
@@ -40,8 +192,10 @@ module CommandKit
|
|
40
192
|
#
|
41
193
|
# @return [Boolean]
|
42
194
|
#
|
195
|
+
# @api public
|
196
|
+
#
|
43
197
|
def windows?
|
44
|
-
|
198
|
+
@os == :windows
|
45
199
|
end
|
46
200
|
end
|
47
201
|
end
|