toys 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,101 +0,0 @@
1
- # Copyright 2018 Daniel Azuma
2
- #
3
- # All rights reserved.
4
- #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are met:
7
- #
8
- # * Redistributions of source code must retain the above copyright notice,
9
- # this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above copyright notice,
11
- # this list of conditions and the following disclaimer in the documentation
12
- # and/or other materials provided with the distribution.
13
- # * Neither the name of the copyright holder, nor the names of any other
14
- # contributors to this software, may be used to endorse or promote products
15
- # derived from this software without specific prior written permission.
16
- #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
- # POSSIBILITY OF SUCH DAMAGE.
28
- ;
29
-
30
- module Toys
31
- module Utils
32
- ##
33
- # A helper module that provides methods to do module lookups. This is
34
- # used to obtain named helpers, middleware, and templates from the
35
- # respective modules.
36
- #
37
- # You generally do not need to use these module methods directly. Instead
38
- # use the convenience methods {Toys::Helpers.lookup},
39
- # {Toys::Middleware.lookup}, or {Toys::Templates.lookup}.
40
- #
41
- module ModuleLookup
42
- class << self
43
- ##
44
- # Convert the given string to a path element. Specifically, converts
45
- # to `lower_snake_case`.
46
- #
47
- # @param [String,Symbol] str String to convert.
48
- # @return [String] Converted string
49
- #
50
- def to_path_name(str)
51
- str.to_s.gsub(/([a-zA-Z])([A-Z])/) { |_m| "#{$1}_#{$2.downcase}" }.downcase
52
- end
53
-
54
- ##
55
- # Convert the given string to a module name. Specifically, converts
56
- # to `UpperCamelCase`.
57
- #
58
- # @param [String,Symbol] str String to convert.
59
- # @return [String] Converted string
60
- #
61
- def to_module_name(str)
62
- str.to_s.gsub(/(^|_)([a-zA-Z0-9])/) { |_m| $2.upcase }
63
- end
64
-
65
- ##
66
- # Obtain a named module from the given collection. Raises an exception
67
- # on failure.
68
- #
69
- # @param [String,Symbol] collection The collection to search. Typical
70
- # values are `:helpers`, `:middleware`, and `:templates`.
71
- # @param [String,Symbol] name The name of the module to return.
72
- #
73
- # @return [Module] The specified module
74
- # @raise [LoadError] No Ruby file containing the given module could
75
- # be found.
76
- # @raise [NameError] The given module was not defined.
77
- #
78
- def lookup!(collection, name)
79
- require "toys/#{to_path_name(collection)}/#{to_path_name(name)}"
80
- ::Toys.const_get(to_module_name(collection)).const_get(to_module_name(name))
81
- end
82
-
83
- ##
84
- # Obtain a named module from the given collection. Returns `nil` on
85
- # failure.
86
- #
87
- # @param [String,Symbol] collection The collection to search. Typical
88
- # values are `:helpers`, `:middleware`, and `:templates`.
89
- # @param [String,Symbol] name The name of the module to return.
90
- #
91
- # @return [Module,nil] The specified module, or `nil` if not found.
92
- #
93
- def lookup(collection, name)
94
- lookup!(collection, name)
95
- rescue ::NameError, ::LoadError
96
- nil
97
- end
98
- end
99
- end
100
- end
101
- end
@@ -1,163 +0,0 @@
1
- # Copyright 2018 Daniel Azuma
2
- #
3
- # All rights reserved.
4
- #
5
- # Redistribution and use in source and binary forms, with or without
6
- # modification, are permitted provided that the following conditions are met:
7
- #
8
- # * Redistributions of source code must retain the above copyright notice,
9
- # this list of conditions and the following disclaimer.
10
- # * Redistributions in binary form must reproduce the above copyright notice,
11
- # this list of conditions and the following disclaimer in the documentation
12
- # and/or other materials provided with the distribution.
13
- # * Neither the name of the copyright holder, nor the names of any other
14
- # contributors to this software, may be used to endorse or promote products
15
- # derived from this software without specific prior written permission.
16
- #
17
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
- # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
- # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
- # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21
- # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
- # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
- # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
- # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
- # POSSIBILITY OF SUCH DAMAGE.
28
- ;
29
-
30
- module Toys
31
- module Utils
32
- ##
33
- # A helper class that generates usage documentation for a tool.
34
- #
35
- # This class generates full usage documentation, including description,
36
- # switches, and arguments. It is used by middleware that implements help
37
- # and related options.
38
- #
39
- class Usage
40
- ##
41
- # Create a usage helper given an execution context.
42
- #
43
- # @param [Toys::Context] context The current execution context.
44
- # @return [Toys::Utils::Usage]
45
- #
46
- def self.from_context(context)
47
- new(context[Context::TOOL], context[Context::BINARY_NAME], context[Context::LOADER])
48
- end
49
-
50
- ##
51
- # Create a usage helper.
52
- #
53
- # @param [Toys::Tool] tool The tool for which to generate documentation.
54
- # @param [String] binary_name The name of the binary. e.g. `"toys"`.
55
- # @param [Toys::Loader] loader A loader that can provide subcommands.
56
- #
57
- # @return [Toys::Utils::Usage]
58
- #
59
- def initialize(tool, binary_name, loader)
60
- @tool = tool
61
- @binary_name = binary_name
62
- @loader = loader
63
- end
64
-
65
- ##
66
- # Generate a usage string.
67
- #
68
- # @param [Boolean] recursive If true, and the tool is a group tool,
69
- # display all subcommands recursively. Defaults to false.
70
- # @return [String] A usage string.
71
- #
72
- def string(recursive: false)
73
- optparse = ::OptionParser.new
74
- optparse.banner = @tool.includes_executor? ? tool_banner : group_banner
75
- unless @tool.effective_long_desc.empty?
76
- optparse.separator("")
77
- optparse.separator(@tool.effective_long_desc)
78
- end
79
- add_switches(optparse)
80
- if @tool.includes_executor?
81
- add_positional_arguments(optparse)
82
- elsif !@tool.alias?
83
- add_command_list(optparse, recursive)
84
- end
85
- optparse.to_s
86
- end
87
-
88
- private
89
-
90
- #
91
- # Returns the banner string for a normal tool
92
- #
93
- def tool_banner
94
- banner = ["Usage:", @binary_name] + @tool.full_name
95
- banner << "[<options...>]" unless @tool.switch_definitions.empty?
96
- @tool.required_arg_definitions.each do |arg_info|
97
- banner << "<#{arg_info.canonical_name}>"
98
- end
99
- @tool.optional_arg_definitions.each do |arg_info|
100
- banner << "[<#{arg_info.canonical_name}>]"
101
- end
102
- if @tool.remaining_args_definition
103
- banner << "[<#{@tool.remaining_args_definition.canonical_name}...>]"
104
- end
105
- banner.join(" ")
106
- end
107
-
108
- #
109
- # Returns the banner string for a group
110
- #
111
- def group_banner
112
- (["Usage:", @binary_name] + @tool.full_name + ["<command>", "[<options...>]"]).join(" ")
113
- end
114
-
115
- #
116
- # Add switches from the tool to the given optionparser. Causes the
117
- # optparser to generate documentation for those switches.
118
- #
119
- def add_switches(optparse)
120
- return if @tool.switch_definitions.empty?
121
- optparse.separator("")
122
- optparse.separator("Options:")
123
- @tool.switch_definitions.each do |switch|
124
- optparse.on(*switch.optparse_info)
125
- end
126
- end
127
-
128
- #
129
- # Add documentation for the tool's positional arguments, to the given
130
- # option parser.
131
- #
132
- def add_positional_arguments(optparse)
133
- args_to_display = @tool.required_arg_definitions + @tool.optional_arg_definitions
134
- args_to_display << @tool.remaining_args_definition if @tool.remaining_args_definition
135
- return if args_to_display.empty?
136
- optparse.separator("")
137
- optparse.separator("Positional arguments:")
138
- args_to_display.each do |arg_info|
139
- optparse.separator(" #{arg_info.canonical_name.ljust(31)} #{arg_info.doc.first}")
140
- (arg_info.doc[1..-1] || []).each do |d|
141
- optparse.separator(" #{d}")
142
- end
143
- end
144
- end
145
-
146
- #
147
- # Add documentation for the tool's subcommands, to the given option
148
- # parser.
149
- #
150
- def add_command_list(optparse, recursive)
151
- name_len = @tool.full_name.length
152
- subtools = @loader.list_subtools(@tool.full_name, recursive: recursive)
153
- return if subtools.empty?
154
- optparse.separator("")
155
- optparse.separator("Commands:")
156
- subtools.each do |subtool|
157
- tool_name = subtool.full_name.slice(name_len..-1).join(" ").ljust(31)
158
- optparse.separator(" #{tool_name} #{subtool.effective_desc}")
159
- end
160
- end
161
- end
162
- end
163
- end