toys 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +54 -0
- data/LICENSE.md +16 -24
- data/README.md +263 -57
- data/bin/toys +17 -25
- data/builtins/do.rb +38 -38
- data/builtins/system.rb +111 -30
- data/docs/guide.md +783 -321
- data/lib/toys.rb +32 -34
- data/lib/toys/standard_cli.rb +69 -90
- data/lib/toys/templates/clean.rb +19 -27
- data/lib/toys/templates/gem_build.rb +78 -41
- data/lib/toys/templates/minitest.rb +25 -31
- data/lib/toys/templates/rake.rb +21 -29
- data/lib/toys/templates/rdoc.rb +27 -35
- data/lib/toys/templates/rspec.rb +30 -36
- data/lib/toys/templates/rubocop.rb +21 -29
- data/lib/toys/templates/yardoc.rb +36 -44
- data/lib/toys/version.rb +18 -26
- data/share/bash-completion-remove.sh +26 -0
- data/share/bash-completion.sh +26 -0
- metadata +14 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b496ae16cf08a819091313e3b0ae83db93f7f7fa47430845fae2c254d35cf17
|
4
|
+
data.tar.gz: 2d23c96333efb627ee8a89ff91acc841aabaa08e413f3aaa89cc693993edeb82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8df0872e8a6753a0a8e3a79640b45e96486b8bdf1d5ea7b9bebff4cb7575932ba1fddd634b9deaa637d1e88507d161c563dae885ab6b357bf156c8522ee384a1
|
7
|
+
data.tar.gz: 4034d1dfec8d5aa21314adcebce48d8351f978f90eb1ea28333f2a12b8007adde48711baadb41f2299a0180ab1a4ee548fed9a753828861e6d284195d84f5887
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,59 @@
|
|
1
1
|
# Release History
|
2
2
|
|
3
|
+
### 0.8.0 / 2019-06-20
|
4
|
+
|
5
|
+
This is a major update with significant new features and a bunch of fixes.
|
6
|
+
It does include a few minor backward-incompatible changes. All signifiant
|
7
|
+
features planned for beta are now implemented.
|
8
|
+
|
9
|
+
Highlights:
|
10
|
+
|
11
|
+
* Tab completion is available for Bash! See the README for instructions on installing it. Tab completion covers tool names, flags, flag values, and positional arguments. Tools can also customize the completion for their own flag and argument values.
|
12
|
+
* Toys now integrates with `did_you_mean` to provide suggestions for misspelled tools, flags, and arguments (when run on Ruby 2.4 or later.)
|
13
|
+
* Tools can now provide their own interrupt handler to respond to user `CTRL-C`. And the default handler no longer displays an unsightly stack trace. Tools can also provide their own handler for usage errors.
|
14
|
+
* A new argument parsing engine, supporting additional features such as optional enforcing that flags appear before positional arguments, as well as a bunch of fixes, especially around acceptors and optional flag values.
|
15
|
+
* Changed the license from BSD to MIT to better match how most libraries in the Ruby community are licensed.
|
16
|
+
|
17
|
+
Details:
|
18
|
+
|
19
|
+
* CHANGED: Relicensed under the MIT License.
|
20
|
+
* CHANGED: Toys now requires Ruby 2.4 or later.
|
21
|
+
* ADDED: Tab completion for bash. Args and flags can provide their own completion information.
|
22
|
+
* ADDED: The usage error screen displays alternative suggestions when an argument is misspelled. (Requires Ruby 2.4 or later.)
|
23
|
+
* ADDED: Tools can provide an interrupt handler.
|
24
|
+
* ADDED: Tools can enforce that flags must be given before positional args. In particular, `toys do` now uses this feature, which eliminates most of the need to use `--` to get flags to work for subtools.
|
25
|
+
* ADDED: Tools can control whether their flags can be invoked by partial matches.
|
26
|
+
* ADDED: Function and range based acceptors.
|
27
|
+
* ADDED: Flag handlers can accept the symbolic names `:set` and `:push` for common cases.
|
28
|
+
* ADDED: The `:gem_build` template includes an `:install_gem` option. It also allows customization of gem output path.
|
29
|
+
* ADDED: The `acceptor` directive takes an optional `type_desc` argument.
|
30
|
+
* ADDED: The `accept` directives under flag and positional arg blocks in the DSL can now take blocks and `type_desc` values.
|
31
|
+
* ADDED: Context keys `UNMATCHED_ARGS`, `UNMATCHED_POSITIONAL`, and `UNMATCHED_FLAGS` that provide arguments that were not handled during arg parsing.
|
32
|
+
* ADDED: The Exec util and mixin support specifying a callback for process results.
|
33
|
+
* ADDED: The Exec util and mixin provide a way to identify processes by name.
|
34
|
+
* CHANGED: Toys now implements its own argument parsing and standard acceptors rather than relying on OptionParser. For the most part, OptionParser behavior is preserved, except in cases where there is clearly a bug.
|
35
|
+
* CHANGED: Flags create a short form flag by default if the name has one character.
|
36
|
+
* CHANGED: Flags with explicit value-less syntax are no longer given a value even if they specify a default or an acceptor.
|
37
|
+
* CHANGED: Renamed the `TOOL_DEFINITION` context key to `TOOL`, and removed the `tool_definition` convenience method.
|
38
|
+
* CHANGED: Removed the `BINARY_NAME` and `LOADER` context keys, and removed and the corresponding convenience methods. Get these values from the CLI if needed.
|
39
|
+
* CHANGED: Renamed the `USAGE_ERROR` context key to `USAGE_ERRORS`, and the corresponding convenience method to `usage_errors`. The value is now a (possibly empty) array of `Toys::ArgParser::UsageError` objects rather than a string that isn't machine-parseable.
|
40
|
+
* CHANGED: The root tool no longer defines remaining_args.
|
41
|
+
* CHANGED: Renamed `to_expand` to `on_expand` in template definitions.
|
42
|
+
* CHANGED: Renamed `to_initialize` to `on_initialize`, and `to_include` to `on_include` in mixin definitions.
|
43
|
+
* CHANGED: Default descriptions for flag groups is now handled by the `set_default_descriptions` middleware rather than hard-coded in FlagGroup.
|
44
|
+
* CHANGED: Exec reports failure to start processes in the result object rather than, e.g. raising ENOENT.
|
45
|
+
* IMPROVED: Toys no longer displays a stack trace if a tool is interrupted.
|
46
|
+
* IMPROVED: Error messages for flag groups are more complete.
|
47
|
+
* IMPROVED: All context data, including well-known data, is available to be modified by flags and args.
|
48
|
+
* FIXED: Flags with optional values are properly set to `true` (rather than left at `nil`) if no value is provided.
|
49
|
+
* FIXED: Prevented toys-core from being ousted from the load path if a toys file invoked bundler setup.
|
50
|
+
* FIXED: Acceptors no longer raise errors when run on missing optional values.
|
51
|
+
* FIXED: When reporting errors in toys files, the line number was off by 2.
|
52
|
+
* FIXED: The `--usage` help flag now honors `--all` and `--no-recursive`.
|
53
|
+
* FIXED: The terminal now handles nil streams, as advertised.
|
54
|
+
|
55
|
+
Additionally, a significant amount of internal reorganization and cleanup happened in the toys-core gem. See the changelog for toys-core for more details.
|
56
|
+
|
3
57
|
### 0.7.0 / 2019-01-23
|
4
58
|
|
5
59
|
* ADDED: A template for creating tools that invoke RSpec.
|
data/LICENSE.md
CHANGED
@@ -1,29 +1,21 @@
|
|
1
1
|
# License
|
2
2
|
|
3
|
-
Copyright
|
3
|
+
Copyright 2019 Daniel Azuma
|
4
4
|
|
5
|
-
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
6
11
|
|
7
|
-
|
8
|
-
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
9
14
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
derived from this software without specific prior written permission.
|
18
|
-
|
19
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
22
|
-
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
23
|
-
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
24
|
-
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
25
|
-
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
-
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
27
|
-
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28
|
-
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
29
|
-
POSSIBILITY OF SUCH DAMAGE.
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
21
|
+
IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,50 +1,62 @@
|
|
1
1
|
# Toys
|
2
2
|
|
3
3
|
Toys is a configurable command line tool. Write commands in config files using
|
4
|
-
a simple DSL, and Toys will provide the command line
|
5
|
-
all the details such as argument parsing, online help, and error reporting.
|
4
|
+
a simple DSL, and Toys will provide the command line executable and take care
|
5
|
+
of all the details such as argument parsing, online help, and error reporting.
|
6
6
|
|
7
7
|
Toys is designed for software developers, IT professionals, and other power
|
8
8
|
users who want to write and organize scripts to automate their workflows. It
|
9
|
-
can also be used as a Rake
|
10
|
-
interface for your project's build tasks.
|
9
|
+
can also be used as a replacement for Rake, providing a more natural command
|
10
|
+
line interface for your project's build tasks.
|
11
11
|
|
12
12
|
Unlike most command line frameworks, Toys is *not primarily* designed to help
|
13
|
-
you build and ship a custom command line
|
14
|
-
*can* use it in that way
|
15
|
-
|
16
|
-
https://www.rubydoc.info/gems/toys-core
|
13
|
+
you build and ship a custom command line executable written in Ruby. However,
|
14
|
+
you *can* use it in that way with the "toys-core" API, available as a separate
|
15
|
+
gem. For more info on using toys-core, see
|
16
|
+
[its documentation](https://www.rubydoc.info/gems/toys-core).
|
17
17
|
|
18
|
-
##
|
18
|
+
## Introductory tutorial
|
19
19
|
|
20
|
-
Here's a
|
20
|
+
Here's a tutorial to help you get a feel of what Toys can do.
|
21
21
|
|
22
22
|
### Install Toys
|
23
23
|
|
24
|
+
Toys requires Ruby 2.3 or later. (JRuby is not currently supported.)
|
25
|
+
|
24
26
|
Install the **toys** gem using:
|
25
27
|
|
26
|
-
gem install toys
|
28
|
+
$ gem install toys
|
27
29
|
|
28
|
-
This installs the `toys`
|
29
|
-
You can run the
|
30
|
+
This installs the `toys` executable, along with some builtin tools and
|
31
|
+
libraries. You can run the executable immediately:
|
30
32
|
|
31
|
-
toys
|
33
|
+
$ toys
|
32
34
|
|
33
|
-
This displays overall help for
|
34
|
-
|
35
|
+
This displays overall help for Toys. If you have `less` installed, Toys will
|
36
|
+
use it to display the help screen. Press `q` to exit.
|
35
37
|
|
36
38
|
You may notice that the help screen lists some tools that are preinstalled.
|
37
39
|
Let's run one of them:
|
38
40
|
|
39
|
-
toys system version
|
41
|
+
$ toys system version
|
42
|
+
|
43
|
+
The `system version` tool displays the current version of the toys gem.
|
44
|
+
|
45
|
+
Toys also provides optional tab completion for bash. To install it, execute the
|
46
|
+
following command in your shell, or add it to your bash configuration file
|
47
|
+
(e.g. `~/.bashrc`).
|
40
48
|
|
41
|
-
|
49
|
+
$(toys system bash-completion install)
|
50
|
+
|
51
|
+
Toys does not yet specially implement tab completion for zsh or other shells.
|
52
|
+
However, if you are using zsh, installing bash completion using `bashcompinit`
|
53
|
+
*mostly* works.
|
42
54
|
|
43
55
|
### Write your first tool
|
44
56
|
|
45
57
|
You can define tools by creating a *Toys file*. Go into any directory, and,
|
46
58
|
using your favorite editor, create a new file called `.toys.rb` (note the
|
47
|
-
leading period). Copy the following into the file, and save it:
|
59
|
+
leading period). Copy the following text into the file, and save it:
|
48
60
|
|
49
61
|
tool "greet" do
|
50
62
|
desc "My first tool!"
|
@@ -56,46 +68,227 @@ leading period). Copy the following into the file, and save it:
|
|
56
68
|
|
57
69
|
This defines a tool named "greet". Try running it:
|
58
70
|
|
59
|
-
toys greet
|
71
|
+
$ toys greet
|
60
72
|
|
61
73
|
The tool also recognizes a flag on the command line. Try this:
|
62
74
|
|
63
|
-
toys greet --whom=ruby
|
75
|
+
$ toys greet --whom=ruby
|
64
76
|
|
65
77
|
Toys provides a rich set of features for defining command line arguments and
|
66
78
|
flags. It can also validate arguments. Try this:
|
67
79
|
|
68
|
-
toys greet --bye
|
80
|
+
$ toys greet --bye
|
69
81
|
|
70
82
|
Notice that Toys automatically generated a usage summary for your tool. It also
|
71
83
|
automatically generates a full help screen, which you can view using the
|
72
84
|
`--help` flag:
|
73
85
|
|
74
|
-
toys greet --help
|
86
|
+
$ toys greet --help
|
87
|
+
|
88
|
+
Toys searches up the directory hierarchy for Toys files. So it will find this
|
89
|
+
`.toys.rb` if you are located in this directory or any subdirectory. It will
|
90
|
+
also read multiple files if it finds them, so you can "scope" your tools more
|
91
|
+
specifically or generally by locating them in your directory hierarchy.
|
92
|
+
|
93
|
+
If you want to define "global" tools that apply anywhere, write a Toys file
|
94
|
+
either in your home directory, or in the system configuration directory
|
95
|
+
(usually `/etc`). Toys always searches these locations.
|
75
96
|
|
76
|
-
###
|
97
|
+
### A more sophisticated example
|
77
98
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
define tools scoped to particular projects. You can also define "global" tools
|
83
|
-
by creating a `.toys.rb` file in your home directory.
|
99
|
+
Let's take a look at another example that exercises some of the features you're
|
100
|
+
likely to see in real-world usage. Add the following to your `.toys.rb` file.
|
101
|
+
(You don't need to replace the greet tool you just wrote; just add this new
|
102
|
+
tool to the end of the file.)
|
84
103
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
104
|
+
tool "new-repo" do
|
105
|
+
desc "Create a new git repo"
|
106
|
+
|
107
|
+
optional_arg :name, desc: "Name of the directory to create"
|
108
|
+
|
109
|
+
include :exec, exit_on_nonzero_status: true
|
110
|
+
include :fileutils
|
111
|
+
include :terminal
|
112
|
+
|
113
|
+
def run
|
114
|
+
if name.nil?
|
115
|
+
response = ask "Please enter a directory name: "
|
116
|
+
set :name, response
|
117
|
+
end
|
118
|
+
if File.exist? name
|
119
|
+
puts "Aborting because #{name} already exists", :red, :bold
|
120
|
+
exit 1
|
121
|
+
end
|
122
|
+
logger.info "Creating new repo in directory #{name}..."
|
123
|
+
mkdir name
|
124
|
+
cd name do
|
125
|
+
create_repo
|
126
|
+
end
|
127
|
+
puts "Created repo in #{name}", :green, :bold
|
128
|
+
end
|
91
129
|
|
92
|
-
|
130
|
+
def create_repo
|
131
|
+
exec "git init"
|
132
|
+
File.write ".gitignore", <<~CONTENT
|
133
|
+
tmp
|
134
|
+
.DS_STORE
|
135
|
+
CONTENT
|
136
|
+
# You can add additional files here.
|
137
|
+
exec "git add ."
|
138
|
+
exec "git commit -m 'Initial commit'"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
Now you should have an additional tool called `new-repo` available. Type:
|
143
|
+
|
144
|
+
$ toys
|
145
|
+
|
146
|
+
The help screen lists both the `greet` tool we started with, and the new
|
147
|
+
`new-repo` tool. This new tool creates a directory containing a newly created
|
148
|
+
git repo. (It assumes you have `git` available on your path.) Try running it:
|
149
|
+
|
150
|
+
$ toys new-repo foo
|
151
|
+
|
152
|
+
That should create a directory `foo`, initialize a git repository within it,
|
153
|
+
and make a commit.
|
154
|
+
|
155
|
+
Notice that this tool accepts a positional command line argument. Toys supports
|
156
|
+
any combination of flags and required and optional arguments. This tool's
|
157
|
+
argument is declared with a description string, which you can see if you view
|
158
|
+
the tool's help:
|
159
|
+
|
160
|
+
$ toys new-repo --help
|
161
|
+
|
162
|
+
The argument is marked as "optional" which means you can omit it. Notice that
|
163
|
+
the tool's code detects that it has been omitted and responds by prompting you
|
164
|
+
interactively for a directory name. You can also mark a positional argument as
|
165
|
+
"required", which causes Toys to report a usage error if it is omitted.
|
166
|
+
|
167
|
+
Next, notice this tool includes two methods, `create_repo` as well as `run`.
|
168
|
+
The "entrypoint" for a tool is always the `run` method, but each tool is
|
169
|
+
actually a class under the hood, and you can add any helper methods you want.
|
170
|
+
You can even define and include modules if you want to share code across tools.
|
171
|
+
|
172
|
+
For our tool, notice that the three "include" lines are taking symbols rather
|
173
|
+
than modules. These symbols are the names of some of Toys's built-in helper
|
174
|
+
*mixins*, which are configurable modules that enhance your tool. They may
|
175
|
+
provide methods your tool can call, or invoke other behavior. In our example:
|
176
|
+
|
177
|
+
* The `:exec` mixin provides a variety of methods for running external
|
178
|
+
commands. In this example, we use the `exec` method to run shell
|
179
|
+
commands, but you can also signal and control these commands, capture
|
180
|
+
and redirect streams, and so forth. Note that we pass the
|
181
|
+
`:exit_on_nonzero_status` option, which configures the `:exec` mixin to
|
182
|
+
abort the tool automatically if any of the external commands fails (similar
|
183
|
+
to `set -e` in bash). This is a common pattern when writing tools that
|
184
|
+
invoke external commands. (If you want more control, the `:exec` mixin also
|
185
|
+
provides ways to respond to result codes individually.)
|
186
|
+
* The `:fileutils` mixin provides the methods of the Ruby `FileUtils`
|
187
|
+
library, such as `mkdir` and `cd` used in this example. It's effectively
|
188
|
+
shorthand for `require "fileutils"; include ::FileUtils`.
|
189
|
+
* The `:terminal` mixin provides styled output, as you can see with the style
|
190
|
+
codes being passed to `puts`. It also provides some user interaction
|
191
|
+
commands such as `ask`, as well as spinners and other controls. You can see
|
192
|
+
operation of the `:terminal` mixin in the tool's output, which is styled
|
193
|
+
either green (for success) or red (on error) when running on a supported
|
194
|
+
tty.
|
195
|
+
|
196
|
+
Now try running this:
|
197
|
+
|
198
|
+
$ toys new-repo bar --verbose
|
199
|
+
|
200
|
+
You'll notice some diagnostic log output. Toys provides a standard Ruby Logger
|
201
|
+
for each tool, and you can use it to emit diagnostic logs directly as
|
202
|
+
demonstrated in the example. Some other Toys features might also emit log
|
203
|
+
entries: the `:exec` mixin, for example, by default logs every external command
|
204
|
+
it runs (although this can be customized).
|
205
|
+
|
206
|
+
By default, only warnings and higher severity logs are displayed, but you can
|
207
|
+
change that by applying the `--verbose` or `--quiet` flags as we have done
|
208
|
+
here. These flags, like `--help`, are provided automatically to every tool.
|
209
|
+
|
210
|
+
### A better Rake?
|
211
|
+
|
212
|
+
Let's look at one more example. Traditionally, Ruby developers often use
|
213
|
+
Rakefiles to write scripts for tasks such as build, test, and deploy. And Toys
|
214
|
+
is similar to Rake in how it uses directory-scoped files to define tools.
|
215
|
+
|
216
|
+
But Rake is really designed for dependency management, not for writing scripts.
|
217
|
+
As a result, some features, such as passing arguments to a task, are very
|
218
|
+
clumsy with Rake.
|
219
|
+
|
220
|
+
If you have a project with a Rakefile, move into that directory and create a
|
221
|
+
new file called `.toys.rb` in that same directory (next to the Rakefile). Add
|
222
|
+
the following line to your `.toys.rb` file:
|
223
|
+
|
224
|
+
expand :rake
|
225
|
+
|
226
|
+
This syntax is called a "template expansion." It's a way to generate tools
|
227
|
+
programmatically. In this case, Toys provides the `:rake` template, which reads
|
228
|
+
your Rakefile and generates Toys tools corresponding to all your Rake tasks!
|
229
|
+
Now if you run:
|
230
|
+
|
231
|
+
$ toys
|
232
|
+
|
233
|
+
You'll see that you now have tools associated with each of your Rake tasks. So
|
234
|
+
if you have a `rake test` task, you can run it using `toys test`.
|
235
|
+
|
236
|
+
Note that if you normally run Rake with Bundler (e.g. `bundle exec rake test`),
|
237
|
+
you may need to add Toys to your Gemfile and use Bundler to invoke Toys (i.e.
|
238
|
+
`bundle exec toys test`). This is because Toys is just calling the Rake API to
|
239
|
+
run your task, and the Rake task might require the bundle. However, when Toys
|
240
|
+
is not wrapping Rake, typical practice is actually *not* to use Bundler. Toys
|
241
|
+
provides its own mechanisms to activate and even install needed gems for you.
|
242
|
+
|
243
|
+
So far, we've made Toys a front-end for your Rake tasks. This may be useful by
|
244
|
+
itself. Toys lets you pass command line arguments "normally" to tools, whereas
|
245
|
+
Rake requires a weird square bracket syntax (which may also require escaping
|
246
|
+
depending on your shell.) Toys also provides more sophisticated online help
|
247
|
+
than Rake does.
|
248
|
+
|
249
|
+
But you also might find Toys a more natural way to *write* tasks, and indeed
|
250
|
+
you can often rewrite an entire Rakefile as a Toys file and get quite a bit of
|
251
|
+
benefit in readability and maintainability. For an example, see the
|
252
|
+
[Toys file for the Toys repo itself](https://github.com/dazuma/toys/blob/master/toys/.toys.rb).
|
253
|
+
It contains the Toys scripts that I use to develop, test, and release Toys
|
254
|
+
itself. Yes, Toys is self-hosted. You'll notice most of this Toys file consists
|
255
|
+
of template expansions. Toys provides templates for a lot of common build,
|
256
|
+
test, and release tasks for Ruby projects.
|
257
|
+
|
258
|
+
If you're feeling adventurous, try translating some of your Rake tasks into
|
259
|
+
native Toys tools. You can do so in your existing `.toys.rb` file. Keep the
|
260
|
+
`expand :rake` line at the *end* of the file, and locate your tools (whether
|
261
|
+
simple tools or template expansions) before it. That way, your Toys-native
|
262
|
+
tools will take precedence, and `expand :rake` will proxy out to Rake only for
|
263
|
+
the remaining tasks that haven't been ported explicitly.
|
264
|
+
|
265
|
+
### Learning more
|
266
|
+
|
267
|
+
This introduction should be enough to get you started. However, Toys is a deep
|
268
|
+
tool with many more features, all explained in detail in the
|
93
269
|
[User Guide](https://www.rubydoc.info/gems/toys/file/docs/guide.md).
|
94
270
|
|
271
|
+
For example, Toys lets you create tool namespaces and "subtools", and search
|
272
|
+
for tools by name and description. There are various ways to validate and
|
273
|
+
interpret command line arguments. You can create your own mixins and templates,
|
274
|
+
and take advantage of a variety of third-party libraries such as Highline and
|
275
|
+
TTY. Finally, if your `.toys.rb` files are growing too large or complicated,
|
276
|
+
you can replace them with `.toys` directories that contain tool definitions in
|
277
|
+
separate files. Such directories are versatile, letting you organize your tool
|
278
|
+
definitions, along with shared code, normal Ruby classes, and even data files
|
279
|
+
for use by tools.
|
280
|
+
|
281
|
+
Unlike most command line frameworks, Toys is *not primarily* designed to help
|
282
|
+
you build and ship a custom command line executable written in Ruby. However,
|
283
|
+
you *can* use it in that way with the "toys-core" API, available as a separate
|
284
|
+
gem. You would effectively write your command line executable using the same
|
285
|
+
Toys DSL that you use to write `.toys.rb` files. For more info on using
|
286
|
+
toys-core, see [its documentation](https://www.rubydoc.info/gems/toys-core).
|
287
|
+
|
95
288
|
## Why Toys?
|
96
289
|
|
97
|
-
I wrote Toys because I was accumulating dozens of ad hoc Ruby
|
98
|
-
had written to automate various tasks in my workflow, everything from
|
290
|
+
I originally wrote Toys because I was accumulating dozens of *ad hoc* Ruby
|
291
|
+
scripts I had written to automate various tasks in my workflow, everything from
|
99
292
|
refreshing credentials, to displaying git history in my favorite format, to
|
100
293
|
running builds and tests of complex multi-component projects. It was becoming
|
101
294
|
difficult to remember which scripts did what, and what arguments each required,
|
@@ -104,26 +297,39 @@ them. Furthermore, when writing new scripts, I was repeating the same
|
|
104
297
|
OptionParser boilerplate and common functionality.
|
105
298
|
|
106
299
|
Toys was designed to address those problems by providing a framework for
|
107
|
-
writing and organizing your own command line scripts. You provide the actual
|
108
|
-
functionality, and Toys takes care of all the other
|
109
|
-
good command line tool. It provides a streamlined
|
110
|
-
handling command line flags and positional
|
111
|
-
organize shared code. It automatically
|
112
|
-
usage information at a glance,
|
113
|
-
you find the script you need
|
300
|
+
writing *and organizing* your own command line scripts. You provide the actual
|
301
|
+
functionality by writing Toys files, and Toys takes care of all the other
|
302
|
+
details expected from a good command line tool. It provides a streamlined
|
303
|
+
interface for defining and handling command line flags and positional
|
304
|
+
arguments, and sensible ways to organize shared code. It automatically
|
305
|
+
generates help text so you can see usage information at a glance, provides a
|
306
|
+
search feature to help you find the script you need, and generates tab
|
307
|
+
completion for your shell.
|
114
308
|
|
115
309
|
Toys can also be used to share scripts. For example, it can be used instead of
|
116
|
-
Rake to provide build and test scripts for a project
|
117
|
-
|
118
|
-
command line conventions. The Toys github repo itself comes with
|
119
|
-
|
310
|
+
Rake to provide build and test scripts for a project. Unlike Rake tasks,
|
311
|
+
scripts written for Toys can be invoked and passed arguments and flags using
|
312
|
+
familiar unix command line conventions. The Toys github repo itself comes with
|
313
|
+
Toys scripts instead of Rakefiles.
|
120
314
|
|
121
315
|
## License
|
122
316
|
|
123
|
-
Copyright
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
317
|
+
Copyright 2019 Daniel Azuma
|
318
|
+
|
319
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
320
|
+
of this software and associated documentation files (the "Software"), to deal
|
321
|
+
in the Software without restriction, including without limitation the rights
|
322
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
323
|
+
copies of the Software, and to permit persons to whom the Software is
|
324
|
+
furnished to do so, subject to the following conditions:
|
325
|
+
|
326
|
+
The above copyright notice and this permission notice shall be included in
|
327
|
+
all copies or substantial portions of the Software.
|
328
|
+
|
329
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
330
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
331
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
332
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
333
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
334
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
335
|
+
IN THE SOFTWARE.
|