toys 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE.md +29 -0
- data/README.md +36 -0
- data/bin/toys +31 -2
- data/lib/toys.rb +45 -1
- data/lib/toys/builder.rb +162 -0
- data/lib/toys/builtins/system.rb +32 -10
- data/lib/toys/cli.rb +83 -27
- data/lib/toys/context.rb +65 -13
- data/lib/toys/errors.rb +37 -2
- data/lib/toys/helpers/exec.rb +262 -31
- data/lib/toys/helpers/file_utils.rb +34 -4
- data/lib/toys/lookup.rb +152 -81
- data/lib/toys/template.rb +43 -14
- data/lib/toys/templates/clean.rb +67 -0
- data/lib/toys/templates/gem_build.rb +68 -36
- data/lib/toys/templates/minitest.rb +75 -29
- data/lib/toys/templates/rubocop.rb +66 -0
- data/lib/toys/templates/yardoc.rb +79 -0
- data/lib/toys/tool.rb +400 -215
- data/lib/toys/version.rb +34 -1
- metadata +13 -36
- data/lib/toys/parser.rb +0 -138
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f036d1f723d27ed317a4aaac039147badbc21c55
|
4
|
+
data.tar.gz: 72974b402f863e4f07da29411393021c80728128
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d1b8ab307213db7030cceee42c910bb21fa253cdc953654a7bcb611c2cd0ec71c2509c75b5e18bc833394d08b4d4c1bf8f3e8a707c486fe16de277cc6e7a0f1
|
7
|
+
data.tar.gz: 82177adaf1bd37799aaa83a031690fc8e908a0292d3ad618cb51afbc192f269e148a6a192727ed91cc95dd3e042e1660e3d47f338731f4f6db6ebc57b960d44f
|
data/LICENSE.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# License
|
2
|
+
|
3
|
+
Copyright 2018 Daniel Azuma
|
4
|
+
|
5
|
+
All rights reserved.
|
6
|
+
|
7
|
+
Redistribution and use in source and binary forms, with or without
|
8
|
+
modification, are permitted provided that the following conditions are met:
|
9
|
+
|
10
|
+
* Redistributions of source code must retain the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer.
|
12
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
14
|
+
and/or other materials provided with the distribution.
|
15
|
+
* Neither the name of the copyright holder, nor the names of any other
|
16
|
+
contributors to this software, may be used to endorse or promote products
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# Toys
|
2
|
+
|
3
|
+
Toys is a Ruby library and command line tool that lets you build your own
|
4
|
+
command line suite of tools (with commands and subcommands) using a Ruby DSL.
|
5
|
+
You can define commands globally or configure special commands scoped to
|
6
|
+
individual directories.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
This software is distributed as a Ruby gem and may be installed using:
|
11
|
+
|
12
|
+
```
|
13
|
+
gem install toys
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
(TODO)
|
19
|
+
|
20
|
+
## Contributing
|
21
|
+
|
22
|
+
While we appreciate contributions, please note that this software is currently
|
23
|
+
highly experimental, and the code is evolving very rapidly. Please contact the
|
24
|
+
author before embarking on a major pull request. More detailed contribution
|
25
|
+
guidelines will be provided when the software stabilizes further.
|
26
|
+
|
27
|
+
The source can be found on Github at
|
28
|
+
[https://github.com/dazuma/toys](https://github.com/dazuma/toys)
|
29
|
+
|
30
|
+
## License
|
31
|
+
|
32
|
+
Copyright 2018 Daniel Azuma
|
33
|
+
|
34
|
+
This software is licensed under the 3-clause BSD license.
|
35
|
+
|
36
|
+
See the LICENSE.md file for more information.
|
data/bin/toys
CHANGED
@@ -1,6 +1,35 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
3
|
+
# Copyright 2018 Daniel Azuma
|
4
|
+
#
|
5
|
+
# All rights reserved.
|
6
|
+
#
|
7
|
+
# Redistribution and use in source and binary forms, with or without
|
8
|
+
# modification, are permitted provided that the following conditions are met:
|
9
|
+
#
|
10
|
+
# * Redistributions of source code must retain the above copyright notice,
|
11
|
+
# this list of conditions and the following disclaimer.
|
12
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
# this list of conditions and the following disclaimer in the documentation
|
14
|
+
# and/or other materials provided with the distribution.
|
15
|
+
# * Neither the name of the copyright holder, nor the names of any other
|
16
|
+
# contributors to this software, may be used to endorse or promote products
|
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.
|
30
|
+
;
|
31
|
+
|
32
|
+
$LOAD_PATH.unshift(::File.absolute_path(::File.join(::File.dirname(__dir__), "lib")))
|
4
33
|
require "toys"
|
5
34
|
|
6
|
-
Toys::
|
35
|
+
::Toys::CLI.create_standard.run(::ARGV)
|
data/lib/toys.rb
CHANGED
@@ -1,11 +1,55 @@
|
|
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
|
+
##
|
31
|
+
# Toys is a Ruby library and command line tool that lets you build your own
|
32
|
+
# command line suite of tools (with commands and subcommands) using a Ruby DSL.
|
33
|
+
# You can define commands globally or configure special commands scoped to
|
34
|
+
# individual directories.
|
35
|
+
#
|
1
36
|
module Toys
|
37
|
+
##
|
38
|
+
# Namespace for common helper modules
|
39
|
+
#
|
40
|
+
module Helpers; end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Namespace for common templates
|
44
|
+
#
|
45
|
+
module Templates; end
|
2
46
|
end
|
3
47
|
|
48
|
+
require "toys/builder"
|
4
49
|
require "toys/cli"
|
5
50
|
require "toys/context"
|
6
51
|
require "toys/errors"
|
7
52
|
require "toys/lookup"
|
8
|
-
require "toys/parser"
|
9
53
|
require "toys/template"
|
10
54
|
require "toys/tool"
|
11
55
|
require "toys/version"
|
data/lib/toys/builder.rb
ADDED
@@ -0,0 +1,162 @@
|
|
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
|
+
# The object context in effect in a toys configuration file
|
33
|
+
#
|
34
|
+
class Builder
|
35
|
+
def initialize(path, tool, remaining_words, priority, lookup)
|
36
|
+
@path = path
|
37
|
+
@tool = tool
|
38
|
+
@remaining_words = remaining_words
|
39
|
+
@priority = priority
|
40
|
+
@lookup = lookup
|
41
|
+
end
|
42
|
+
|
43
|
+
def name(word, alias_of: nil, &block)
|
44
|
+
word = word.to_s
|
45
|
+
subtool = @lookup.get_tool(@tool.full_name + [word], @priority)
|
46
|
+
return self if subtool.nil?
|
47
|
+
if alias_of
|
48
|
+
if block
|
49
|
+
raise ToolDefinitionError, "Cannot take a block with alias_of"
|
50
|
+
end
|
51
|
+
subtool.make_alias_of_word(alias_of.to_s)
|
52
|
+
return self
|
53
|
+
end
|
54
|
+
next_remaining = Lookup.next_remaining_words(@remaining_words, word)
|
55
|
+
Builder.build(@path, subtool, next_remaining, @priority, @lookup, block)
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def alias_as(word)
|
60
|
+
if @tool.root?
|
61
|
+
raise ToolDefinitionError, "Cannot make an alias of the root tool"
|
62
|
+
end
|
63
|
+
alias_name = @tool.full_name.slice(0..-2) + [word.to_s]
|
64
|
+
alias_tool = @lookup.get_tool(alias_name, @priority)
|
65
|
+
alias_tool.make_alias_of(@tool.simple_name) if alias_tool
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
def alias_of(word)
|
70
|
+
@tool.make_alias_of(word.to_s)
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
def include(path)
|
75
|
+
@tool.yield_definition do
|
76
|
+
@lookup.include_path(path, @tool.full_name, @remaining_words, @priority)
|
77
|
+
end
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
def expand(template_class, *args)
|
82
|
+
unless template_class.is_a?(::Class)
|
83
|
+
template_class = template_class.to_s
|
84
|
+
file_name =
|
85
|
+
template_class
|
86
|
+
.gsub(/([a-zA-Z])([A-Z])/) { |_m| "#{$1}_#{$2.downcase}" }
|
87
|
+
.downcase
|
88
|
+
require "toys/templates/#{file_name}"
|
89
|
+
const_name = template_class.gsub(/(^|_)([a-zA-Z0-9])/) { |_m| $2.upcase }
|
90
|
+
template_class = Templates.const_get(const_name)
|
91
|
+
end
|
92
|
+
template = template_class.new(*args)
|
93
|
+
yield template if block_given?
|
94
|
+
instance_exec(template, &template_class.expander)
|
95
|
+
self
|
96
|
+
end
|
97
|
+
|
98
|
+
def long_desc(desc)
|
99
|
+
@tool.long_desc = desc
|
100
|
+
self
|
101
|
+
end
|
102
|
+
|
103
|
+
def short_desc(desc)
|
104
|
+
@tool.short_desc = desc
|
105
|
+
self
|
106
|
+
end
|
107
|
+
|
108
|
+
def switch(key, *switches, accept: nil, default: nil, doc: nil)
|
109
|
+
@tool.add_switch(key, *switches, accept: accept, default: default, doc: doc)
|
110
|
+
self
|
111
|
+
end
|
112
|
+
|
113
|
+
def required_arg(key, accept: nil, doc: nil)
|
114
|
+
@tool.add_required_arg(key, accept: accept, doc: doc)
|
115
|
+
self
|
116
|
+
end
|
117
|
+
|
118
|
+
def optional_arg(key, accept: nil, default: nil, doc: nil)
|
119
|
+
@tool.add_optional_arg(key, accept: accept, default: default, doc: doc)
|
120
|
+
self
|
121
|
+
end
|
122
|
+
|
123
|
+
def remaining_args(key, accept: nil, default: [], doc: nil)
|
124
|
+
@tool.set_remaining_args(key, accept: accept, default: default, doc: doc)
|
125
|
+
self
|
126
|
+
end
|
127
|
+
|
128
|
+
def execute(&block)
|
129
|
+
@tool.executor = block
|
130
|
+
self
|
131
|
+
end
|
132
|
+
|
133
|
+
def helper(name, &block)
|
134
|
+
@tool.add_helper(name, &block)
|
135
|
+
self
|
136
|
+
end
|
137
|
+
|
138
|
+
def use(mod)
|
139
|
+
@tool.use_module(mod)
|
140
|
+
self
|
141
|
+
end
|
142
|
+
|
143
|
+
def _binding
|
144
|
+
binding
|
145
|
+
end
|
146
|
+
|
147
|
+
def self.build(path, tool, remaining_words, priority, lookup, source)
|
148
|
+
builder = new(path, tool, remaining_words, priority, lookup)
|
149
|
+
tool.defining_from(path) do
|
150
|
+
case source
|
151
|
+
when String
|
152
|
+
# rubocop:disable Security/Eval
|
153
|
+
eval(source, builder._binding, path, 1)
|
154
|
+
# rubocop:enable Security/Eval
|
155
|
+
when ::Proc
|
156
|
+
builder.instance_eval(&source)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
tool
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
data/lib/toys/builtins/system.rb
CHANGED
@@ -1,20 +1,43 @@
|
|
1
|
-
|
2
|
-
|
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
|
+
;
|
3
29
|
|
30
|
+
short_desc "A collection of system commands for toys"
|
4
31
|
|
5
32
|
name "version" do
|
6
|
-
|
7
33
|
short_desc "Print current toys version"
|
8
34
|
|
9
35
|
execute do
|
10
|
-
puts Toys::VERSION
|
36
|
+
puts ::Toys::VERSION
|
11
37
|
end
|
12
|
-
|
13
38
|
end
|
14
39
|
|
15
|
-
|
16
40
|
name "update" do
|
17
|
-
|
18
41
|
short_desc "Update toys if a newer version is available"
|
19
42
|
|
20
43
|
use :exec
|
@@ -22,8 +45,8 @@ name "update" do
|
|
22
45
|
execute do
|
23
46
|
version_info = capture("gem query -q -r -e toys")
|
24
47
|
if version_info =~ /toys\s\((.+)\)/
|
25
|
-
latest_version = Gem::Version.new($1)
|
26
|
-
cur_version = Gem::Version.new(Toys::VERSION)
|
48
|
+
latest_version = ::Gem::Version.new($1)
|
49
|
+
cur_version = ::Gem::Version.new(::Toys::VERSION)
|
27
50
|
if latest_version > cur_version
|
28
51
|
logger.warn("Updating toys from #{cur_version} to #{latest_version}...")
|
29
52
|
sh("gem install toys")
|
@@ -35,5 +58,4 @@ name "update" do
|
|
35
58
|
exit(1)
|
36
59
|
end
|
37
60
|
end
|
38
|
-
|
39
61
|
end
|
data/lib/toys/cli.rb
CHANGED
@@ -1,10 +1,68 @@
|
|
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 "logger"
|
31
|
+
|
1
32
|
module Toys
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
33
|
+
##
|
34
|
+
# A Toys-based CLI
|
35
|
+
#
|
36
|
+
class CLI
|
37
|
+
##
|
38
|
+
# Path to default builtins
|
39
|
+
# @return [String]
|
40
|
+
#
|
41
|
+
BUILTINS_PATH = ::File.join(__dir__, "builtins").freeze
|
42
|
+
|
43
|
+
##
|
44
|
+
# Default name of toys configuration directory
|
45
|
+
# @return [String]
|
46
|
+
#
|
47
|
+
DEFAULT_DIR_NAME = ".toys".freeze
|
48
|
+
|
49
|
+
##
|
50
|
+
# Default name of toys configuration file
|
51
|
+
# @return [String]
|
52
|
+
#
|
53
|
+
DEFAULT_FILE_NAME = ".toys.rb".freeze
|
54
|
+
|
55
|
+
##
|
56
|
+
# Default name of toys preload file
|
57
|
+
# @return [String]
|
58
|
+
#
|
59
|
+
DEFAULT_PRELOAD_NAME = ".preload.rb".freeze
|
60
|
+
|
61
|
+
##
|
62
|
+
# Default name of toys binary
|
63
|
+
# @return [String]
|
64
|
+
#
|
65
|
+
DEFAULT_BINARY_NAME = "toys".freeze
|
8
66
|
|
9
67
|
def initialize(
|
10
68
|
binary_name: nil,
|
@@ -14,15 +72,13 @@ module Toys
|
|
14
72
|
index_file_name: nil,
|
15
73
|
preload_file_name: nil
|
16
74
|
)
|
17
|
-
@lookup =
|
75
|
+
@lookup = Lookup.new(
|
18
76
|
config_dir_name: config_dir_name,
|
19
77
|
config_file_name: config_file_name,
|
20
78
|
index_file_name: index_file_name,
|
21
|
-
preload_file_name: preload_file_name
|
22
|
-
|
23
|
-
|
24
|
-
logger: logger || self.class.default_logger,
|
25
|
-
binary_name: binary_name)
|
79
|
+
preload_file_name: preload_file_name
|
80
|
+
)
|
81
|
+
@context_base = Context::Base.new(@lookup, binary_name, logger || self.class.default_logger)
|
26
82
|
end
|
27
83
|
|
28
84
|
def add_paths(paths)
|
@@ -35,13 +91,13 @@ module Toys
|
|
35
91
|
self
|
36
92
|
end
|
37
93
|
|
38
|
-
def add_config_path_hierarchy(path=nil, base="/")
|
39
|
-
path ||= Dir.pwd
|
94
|
+
def add_config_path_hierarchy(path = nil, base = "/")
|
95
|
+
path ||= ::Dir.pwd
|
40
96
|
paths = []
|
41
97
|
loop do
|
42
98
|
paths << path
|
43
99
|
break if !base || path == base
|
44
|
-
next_path = File.dirname(path)
|
100
|
+
next_path = ::File.dirname(path)
|
45
101
|
break if next_path == path
|
46
102
|
path = next_path
|
47
103
|
end
|
@@ -50,16 +106,16 @@ module Toys
|
|
50
106
|
end
|
51
107
|
|
52
108
|
def add_standard_config_paths
|
53
|
-
toys_path = ENV["TOYS_PATH"].to_s.split(File::PATH_SEPARATOR)
|
54
|
-
if toys_path.
|
55
|
-
toys_path << ENV["HOME"] if ENV["HOME"]
|
56
|
-
toys_path << "/etc" if File.directory?("/etc") && File.readable?("/etc")
|
109
|
+
toys_path = ::ENV["TOYS_PATH"].to_s.split(::File::PATH_SEPARATOR)
|
110
|
+
if toys_path.empty?
|
111
|
+
toys_path << ::ENV["HOME"] if ::ENV["HOME"]
|
112
|
+
toys_path << "/etc" if File.directory?("/etc") && ::File.readable?("/etc")
|
57
113
|
end
|
58
114
|
@lookup.add_config_paths(toys_path)
|
59
115
|
end
|
60
116
|
|
61
117
|
def run(*args)
|
62
|
-
@
|
118
|
+
@context_base.run(*args)
|
63
119
|
end
|
64
120
|
|
65
121
|
class << self
|
@@ -78,21 +134,21 @@ module Toys
|
|
78
134
|
end
|
79
135
|
|
80
136
|
def default_logger
|
81
|
-
logger = Logger.new(STDERR)
|
82
|
-
logger.formatter =
|
137
|
+
logger = ::Logger.new(::STDERR)
|
138
|
+
logger.formatter = proc do |severity, time, _progname, msg|
|
83
139
|
msg_str =
|
84
140
|
case msg
|
85
|
-
when String
|
141
|
+
when ::String
|
86
142
|
msg
|
87
|
-
when Exception
|
143
|
+
when ::Exception
|
88
144
|
"#{msg.message} (#{msg.class})\n" << (msg.backtrace || []).join("\n")
|
89
145
|
else
|
90
146
|
msg.inspect
|
91
147
|
end
|
92
148
|
timestr = time.strftime("%Y-%m-%d %H:%M:%S")
|
93
|
-
"[%s %5s] %s\n"
|
94
|
-
|
95
|
-
logger.level = Logger::WARN
|
149
|
+
format("[%s %5s] %s\n", timestr, severity, msg_str)
|
150
|
+
end
|
151
|
+
logger.level = ::Logger::WARN
|
96
152
|
logger
|
97
153
|
end
|
98
154
|
end
|