toys 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +0 -2
- data/CHANGELOG.md +20 -0
- data/README.md +4 -24
- data/bin/toys +1 -1
- data/{lib/toys/builtins → builtins}/do.rb +1 -1
- data/{lib/toys/builtins → builtins}/system.rb +0 -0
- data/lib/toys.rb +9 -14
- data/lib/toys/standard_cli.rb +151 -0
- data/lib/toys/version.rb +2 -2
- metadata +19 -28
- data/lib/toys/cli.rb +0 -271
- data/lib/toys/config_dsl.rb +0 -432
- data/lib/toys/context.rb +0 -278
- data/lib/toys/errors.rb +0 -42
- data/lib/toys/helpers.rb +0 -52
- data/lib/toys/helpers/exec.rb +0 -469
- data/lib/toys/helpers/file_utils.rb +0 -39
- data/lib/toys/loader.rb +0 -423
- data/lib/toys/middleware.rb +0 -55
- data/lib/toys/middleware/base.rb +0 -51
- data/lib/toys/middleware/set_verbosity.rb +0 -54
- data/lib/toys/middleware/show_group_usage.rb +0 -68
- data/lib/toys/middleware/show_tool_usage.rb +0 -64
- data/lib/toys/middleware/show_usage_errors.rb +0 -57
- data/lib/toys/template.rb +0 -123
- data/lib/toys/templates.rb +0 -55
- data/lib/toys/templates/clean.rb +0 -80
- data/lib/toys/templates/gem_build.rb +0 -115
- data/lib/toys/templates/minitest.rb +0 -108
- data/lib/toys/templates/rubocop.rb +0 -81
- data/lib/toys/templates/yardoc.rb +0 -95
- data/lib/toys/tool.rb +0 -831
- data/lib/toys/utils/module_lookup.rb +0 -101
- data/lib/toys/utils/usage.rb +0 -163
data/lib/toys/middleware.rb
DELETED
@@ -1,55 +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
|
-
require "toys/utils/module_lookup"
|
31
|
-
|
32
|
-
module Toys
|
33
|
-
##
|
34
|
-
# Namespace for common middleware
|
35
|
-
#
|
36
|
-
module Middleware
|
37
|
-
##
|
38
|
-
# Return a middleware class by name.
|
39
|
-
#
|
40
|
-
# Currently recognized middleware names are:
|
41
|
-
#
|
42
|
-
# * `:group_default` : Provides a default implementation for a group.
|
43
|
-
# * `:set_verbosity` : Switches for affecting log verbosity.
|
44
|
-
# * `:show_tool_help` : A switch that causes a tool to print its usage
|
45
|
-
# documentation.
|
46
|
-
# * `:show_usage_errors` : Displays the usage error if one occurs.
|
47
|
-
#
|
48
|
-
# @param [String,Symbol] name Name of the middleware class to return
|
49
|
-
# @return [Class,nil] The class, or `nil` if not found
|
50
|
-
#
|
51
|
-
def self.lookup(name)
|
52
|
-
Utils::ModuleLookup.lookup(:middleware, name)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
data/lib/toys/middleware/base.rb
DELETED
@@ -1,51 +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 Middleware
|
32
|
-
##
|
33
|
-
# A base middleware with a no-op implementation.
|
34
|
-
#
|
35
|
-
class Base
|
36
|
-
##
|
37
|
-
# The base middleware does not affect tool configuration.
|
38
|
-
#
|
39
|
-
def config(_tool)
|
40
|
-
yield
|
41
|
-
end
|
42
|
-
|
43
|
-
##
|
44
|
-
# The base middleware does not affect tool execution.
|
45
|
-
#
|
46
|
-
def execute(_context)
|
47
|
-
yield
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,54 +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
|
-
require "toys/middleware/base"
|
31
|
-
|
32
|
-
module Toys
|
33
|
-
module Middleware
|
34
|
-
##
|
35
|
-
# A middleware that provides switches for editing the verbosity
|
36
|
-
#
|
37
|
-
class SetVerbosity < Base
|
38
|
-
##
|
39
|
-
# This middleware adds `--verbose` and `--quiet` flags.
|
40
|
-
#
|
41
|
-
def config(tool)
|
42
|
-
tool.add_switch(Context::VERBOSITY, "-v", "--verbose",
|
43
|
-
doc: "Increase verbosity",
|
44
|
-
handler: ->(_val, cur) { cur + 1 },
|
45
|
-
only_unique: true)
|
46
|
-
tool.add_switch(Context::VERBOSITY, "-q", "--quiet",
|
47
|
-
doc: "Decrease verbosity",
|
48
|
-
handler: ->(_val, cur) { cur - 1 },
|
49
|
-
only_unique: true)
|
50
|
-
yield
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,68 +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
|
-
require "toys/middleware/base"
|
31
|
-
require "toys/utils/usage"
|
32
|
-
|
33
|
-
module Toys
|
34
|
-
module Middleware
|
35
|
-
##
|
36
|
-
# A middleware that provides a default implementation for groups. If a
|
37
|
-
# tool has no executor, this middleware assumes it to be a group, and it
|
38
|
-
# provides a default executor that displays group usage documentation.
|
39
|
-
#
|
40
|
-
class ShowGroupUsage < Base
|
41
|
-
##
|
42
|
-
# This middleware adds a "--no-recursive" flag to groups. This flag, when
|
43
|
-
# set, shows only immediate subcommands rather than all recursively.
|
44
|
-
#
|
45
|
-
def config(tool)
|
46
|
-
if tool.includes_executor?
|
47
|
-
yield
|
48
|
-
else
|
49
|
-
tool.add_switch(:_no_recursive, "--no-recursive",
|
50
|
-
doc: "Show immediate rather than all subcommands",
|
51
|
-
only_unique: true)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
##
|
56
|
-
# This middleware displays the usage documentation for groups. It has
|
57
|
-
# no effect on tools that have their own executor.
|
58
|
-
#
|
59
|
-
def execute(context)
|
60
|
-
if context[Context::TOOL].includes_executor?
|
61
|
-
yield
|
62
|
-
else
|
63
|
-
puts(Utils::Usage.from_context(context).string(recursive: !context[:_no_recursive]))
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
@@ -1,64 +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
|
-
require "toys/middleware/base"
|
31
|
-
require "toys/utils/usage"
|
32
|
-
|
33
|
-
module Toys
|
34
|
-
module Middleware
|
35
|
-
##
|
36
|
-
# A middleware that shows usage documentation
|
37
|
-
#
|
38
|
-
class ShowToolUsage < Base
|
39
|
-
##
|
40
|
-
# This middleware adds a `--help` flag that triggers display of help.
|
41
|
-
#
|
42
|
-
def config(tool)
|
43
|
-
if tool.includes_executor?
|
44
|
-
tool.add_switch(:_help, "-?", "--help",
|
45
|
-
doc: "Show help message",
|
46
|
-
only_unique: true)
|
47
|
-
end
|
48
|
-
yield
|
49
|
-
end
|
50
|
-
|
51
|
-
##
|
52
|
-
# If the `--help` flag is present, this middleware causes the tool to
|
53
|
-
# display its usage documentation and exit, rather than executing.
|
54
|
-
#
|
55
|
-
def execute(context)
|
56
|
-
if context[:_help]
|
57
|
-
puts(Utils::Usage.from_context(context).string(recursive: context[:_recursive]))
|
58
|
-
else
|
59
|
-
yield
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
@@ -1,57 +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
|
-
require "toys/middleware/base"
|
31
|
-
require "toys/utils/usage"
|
32
|
-
|
33
|
-
module Toys
|
34
|
-
module Middleware
|
35
|
-
##
|
36
|
-
# A middleware that shows usage errors
|
37
|
-
#
|
38
|
-
class ShowUsageErrors < Base
|
39
|
-
##
|
40
|
-
# If a usage error happens, e.g. an unrecognized switch or an unfulfilled
|
41
|
-
# required argument, this middleware causes the tool to display the error
|
42
|
-
# and usage documentation and exit with a nonzero result. Otherwise, it
|
43
|
-
# does nothing.
|
44
|
-
#
|
45
|
-
def execute(context)
|
46
|
-
if context[Context::USAGE_ERROR]
|
47
|
-
puts(context[Context::USAGE_ERROR])
|
48
|
-
puts("")
|
49
|
-
puts(Utils::Usage.from_context(context).string)
|
50
|
-
context.exit(-1)
|
51
|
-
else
|
52
|
-
yield
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
data/lib/toys/template.rb
DELETED
@@ -1,123 +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
|
-
##
|
32
|
-
# A template definition. Template classes should include this module.
|
33
|
-
#
|
34
|
-
# A template is a configurable set of DSL code that can be run in a toys
|
35
|
-
# configuration to automate tool defintion. For example, toys provides a
|
36
|
-
# "minitest" template that generates a "test" tool that invokes minitest.
|
37
|
-
# Templates will often support configuration; for example the minitest
|
38
|
-
# template lets you configure the paths to the test files.
|
39
|
-
#
|
40
|
-
# ## Usage
|
41
|
-
#
|
42
|
-
# To create a template, define a class and include this module.
|
43
|
-
# The class defines the "configuration" of the template. If your template
|
44
|
-
# has options/parameters, you should provide a constructor, and methods
|
45
|
-
# appropriate to edit those options. The arguments given to the
|
46
|
-
# {Toys::ConfigDSL#expand} method are passed to your constructor, and your
|
47
|
-
# template object is passed to any block given to {Toys::ConfigDSL#expand}.
|
48
|
-
#
|
49
|
-
# Next, in your template class, call the `to_expand` method, which is defined
|
50
|
-
# in {Toys::Template::ClassMethods#to_expand}. Pass this a block which
|
51
|
-
# defines the implementation of the template. Effectively, the contents of
|
52
|
-
# this block are "inserted" into the user's configuration. The template
|
53
|
-
# object is passed to the block so you have access to the template options.
|
54
|
-
#
|
55
|
-
# ## Example
|
56
|
-
#
|
57
|
-
# This is a simple template that generates a "hello" tool. The tool simply
|
58
|
-
# prints a `"Hello, #{name}!"` greeting. The name is set as a template
|
59
|
-
# option; it is defined when the template is expanded in a toys
|
60
|
-
# configuration.
|
61
|
-
#
|
62
|
-
# # Define a template by creating a class that includes Toys::Template.
|
63
|
-
# class MyHelloTemplate
|
64
|
-
# include Toys::Template
|
65
|
-
#
|
66
|
-
# # A user of the template may pass an optional name as a parameter to
|
67
|
-
# # `expand`, or leave it as the default of "world".
|
68
|
-
# def initialize(name: "world")
|
69
|
-
# @name = name
|
70
|
-
# end
|
71
|
-
#
|
72
|
-
# # The template is passed to the expand block, so a user of the
|
73
|
-
# # template may also call this method to set the name.
|
74
|
-
# attr_accessor :name
|
75
|
-
#
|
76
|
-
# # The following block is inserted when the template is expanded.
|
77
|
-
# to_expand do |template|
|
78
|
-
# desc "Prints a greeting to #{template.name}"
|
79
|
-
# tool "templated-greeting" do
|
80
|
-
# execute do
|
81
|
-
# puts "Hello, #{template.name}!"
|
82
|
-
# end
|
83
|
-
# end
|
84
|
-
# end
|
85
|
-
# end
|
86
|
-
#
|
87
|
-
# Now you can use the template in your `.toys.rb` file like this:
|
88
|
-
#
|
89
|
-
# expand(MyHelloTemplate, name: "rubyists")
|
90
|
-
#
|
91
|
-
# or alternately:
|
92
|
-
#
|
93
|
-
# expand(MyHelloTemplate) do |template|
|
94
|
-
# template.name = "rubyists"
|
95
|
-
# end
|
96
|
-
#
|
97
|
-
# And it will create a tool called "templated-greeting".
|
98
|
-
#
|
99
|
-
module Template
|
100
|
-
## @private
|
101
|
-
def self.included(mod)
|
102
|
-
mod.extend(ClassMethods)
|
103
|
-
end
|
104
|
-
|
105
|
-
##
|
106
|
-
# Class methods that will be added to a template class.
|
107
|
-
#
|
108
|
-
module ClassMethods
|
109
|
-
##
|
110
|
-
# Provide the block that implements the template.
|
111
|
-
#
|
112
|
-
def to_expand(&block)
|
113
|
-
@expander = block
|
114
|
-
end
|
115
|
-
|
116
|
-
##
|
117
|
-
# You may alternately set the expander block using this accessor.
|
118
|
-
# @return [Proc]
|
119
|
-
#
|
120
|
-
attr_accessor :expander
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|