thor 0.14.6 → 0.19.4
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 +7 -0
- data/.document +5 -0
- data/{CHANGELOG.rdoc → CHANGELOG.md} +91 -31
- data/CONTRIBUTING.md +15 -0
- data/{LICENSE → LICENSE.md} +2 -2
- data/README.md +36 -296
- data/bin/thor +1 -1
- data/lib/thor/actions/create_file.rb +33 -35
- data/lib/thor/actions/create_link.rb +7 -5
- data/lib/thor/actions/directory.rb +49 -24
- data/lib/thor/actions/empty_directory.rb +68 -67
- data/lib/thor/actions/file_manipulation.rb +85 -28
- data/lib/thor/actions/inject_into_file.rb +26 -32
- data/lib/thor/actions.rb +70 -66
- data/lib/thor/base.rb +314 -237
- data/lib/thor/command.rb +133 -0
- data/lib/thor/core_ext/hash_with_indifferent_access.rb +34 -24
- data/lib/thor/core_ext/io_binary_read.rb +12 -0
- data/lib/thor/core_ext/ordered_hash.rb +94 -65
- data/lib/thor/error.rb +10 -8
- data/lib/thor/group.rb +74 -66
- data/lib/thor/invocation.rb +65 -56
- data/lib/thor/line_editor/basic.rb +35 -0
- data/lib/thor/line_editor/readline.rb +88 -0
- data/lib/thor/line_editor.rb +17 -0
- data/lib/thor/parser/argument.rb +32 -29
- data/lib/thor/parser/arguments.rb +107 -93
- data/lib/thor/parser/option.rb +39 -13
- data/lib/thor/parser/options.rb +144 -97
- data/lib/thor/parser.rb +4 -4
- data/lib/thor/rake_compat.rb +21 -16
- data/lib/thor/runner.rb +166 -153
- data/lib/thor/shell/basic.rb +268 -122
- data/lib/thor/shell/color.rb +84 -43
- data/lib/thor/shell/html.rb +71 -66
- data/lib/thor/shell.rb +30 -37
- data/lib/thor/util.rb +227 -188
- data/lib/thor/version.rb +1 -1
- data/lib/thor.rb +289 -131
- data/thor.gemspec +21 -0
- metadata +50 -189
- data/Thorfile +0 -24
- data/bin/rake2thor +0 -86
- data/lib/thor/core_ext/file_binary_read.rb +0 -9
- data/lib/thor/task.rb +0 -114
- data/spec/actions/create_file_spec.rb +0 -170
- data/spec/actions/directory_spec.rb +0 -136
- data/spec/actions/empty_directory_spec.rb +0 -98
- data/spec/actions/file_manipulation_spec.rb +0 -310
- data/spec/actions/inject_into_file_spec.rb +0 -135
- data/spec/actions_spec.rb +0 -322
- data/spec/base_spec.rb +0 -269
- data/spec/core_ext/hash_with_indifferent_access_spec.rb +0 -43
- data/spec/core_ext/ordered_hash_spec.rb +0 -115
- data/spec/fixtures/application.rb +0 -2
- data/spec/fixtures/bundle/execute.rb +0 -6
- data/spec/fixtures/bundle/main.thor +0 -1
- data/spec/fixtures/doc/%file_name%.rb.tt +0 -1
- data/spec/fixtures/doc/README +0 -3
- data/spec/fixtures/doc/block_helper.rb +0 -3
- data/spec/fixtures/doc/components/.empty_directory +0 -0
- data/spec/fixtures/doc/config.rb +0 -1
- data/spec/fixtures/group.thor +0 -114
- data/spec/fixtures/invoke.thor +0 -112
- data/spec/fixtures/path with spaces +0 -0
- data/spec/fixtures/script.thor +0 -184
- data/spec/fixtures/task.thor +0 -10
- data/spec/group_spec.rb +0 -178
- data/spec/invocation_spec.rb +0 -100
- data/spec/parser/argument_spec.rb +0 -47
- data/spec/parser/arguments_spec.rb +0 -64
- data/spec/parser/option_spec.rb +0 -202
- data/spec/parser/options_spec.rb +0 -319
- data/spec/rake_compat_spec.rb +0 -68
- data/spec/register_spec.rb +0 -92
- data/spec/runner_spec.rb +0 -210
- data/spec/shell/basic_spec.rb +0 -223
- data/spec/shell/color_spec.rb +0 -41
- data/spec/shell/html_spec.rb +0 -27
- data/spec/shell_spec.rb +0 -47
- data/spec/spec_helper.rb +0 -54
- data/spec/task_spec.rb +0 -69
- data/spec/thor_spec.rb +0 -334
- data/spec/util_spec.rb +0 -163
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 5fd71663b46487af27e6f0b6a0206f5140d0d196
|
|
4
|
+
data.tar.gz: d67e482d506417552648630f8a7bc4b1886fc06c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f15702a93adea15d623fd708962193b42bc38bf2b86dff66fe43f1078971fc38f5dedff0d6a78bd1bf9241315e83ede0c216ffedea8c452c55b57aff17d0d051
|
|
7
|
+
data.tar.gz: 1e7093648f0913e9c7e32c796ea795e819ca59331588885059bfcae9867172ea50144033c342d22c4d5c8ab628f135821bb9a0ab8645f7784c396482370b98c0
|
data/.document
ADDED
|
@@ -1,8 +1,82 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
## 0.19.1, release 2014-03-24
|
|
2
|
+
* Fix `say` non-String break regression
|
|
3
|
+
|
|
4
|
+
## 0.19.0, release 2014-03-22
|
|
5
|
+
* Add support for a default to #ask
|
|
6
|
+
* Avoid @namespace not initialized warning
|
|
7
|
+
* Avoid private attribute? warning
|
|
8
|
+
* Fix initializing with unknown options
|
|
9
|
+
* Loosen required_rubygems_version for compatibility with Ubuntu 10.04
|
|
10
|
+
* Shell#ask: support a noecho option for stdin
|
|
11
|
+
* Shell#ask: change API to be :echo => false
|
|
12
|
+
* Display a message without a stack trace for ambiguous commands
|
|
13
|
+
* Make say and say_status thread safe
|
|
14
|
+
* Dependency for console io version check
|
|
15
|
+
* Alias --help to help on subcommands
|
|
16
|
+
* Use mime-types 1.x for Ruby 1.8.7 compatibility for Ruby 1.8 only
|
|
17
|
+
* Accept .tt files as templates
|
|
18
|
+
* Check if numeric value is in enum
|
|
19
|
+
* Use Readline for user input
|
|
20
|
+
* Fix dispatching of subcommands (concerning :help and *args)
|
|
21
|
+
* Fix warnings when running specs with `$VERBOSE = true`
|
|
22
|
+
* Make subcommand help more consistent
|
|
23
|
+
* Make the current command chain accessible in command
|
|
24
|
+
|
|
25
|
+
## 0.18.1, release 2013-03-30
|
|
26
|
+
* Revert regressions found in 0.18.0
|
|
27
|
+
|
|
28
|
+
## 0.18.0, release 2013-03-26
|
|
29
|
+
* Remove rake2thor
|
|
30
|
+
* Only display colors if output medium supports colors
|
|
31
|
+
* Pass parent_options to subcommands
|
|
32
|
+
* Fix non-dash-prefixed aliases
|
|
33
|
+
* Make error messages more helpful
|
|
34
|
+
* Rename "task" to "command"
|
|
35
|
+
* Add the method to allow for custom package name
|
|
36
|
+
|
|
37
|
+
## 0.17.0, release 2013-01-24
|
|
38
|
+
* Add better support for tasks that accept arbitrary additional arguments (e.g. things like `bundle exec`)
|
|
39
|
+
* Add #stop_on_unknown_option!
|
|
40
|
+
* Only strip from stdin.gets if it wasn't ended with EOF
|
|
41
|
+
* Allow "send" as a task name
|
|
42
|
+
* Allow passing options as arguments after "--"
|
|
43
|
+
* Autoload Thor::Group
|
|
44
|
+
|
|
45
|
+
## 0.16.0, release 2012-08-14
|
|
46
|
+
* Add enum to string arguments
|
|
47
|
+
|
|
48
|
+
## 0.15.4, release 2012-06-29
|
|
49
|
+
* Fix regression when destination root contains reserved regexp characters
|
|
50
|
+
|
|
51
|
+
## 0.15.3, release 2012-06-18
|
|
52
|
+
* Support strict_args_position! for backwards compatibility
|
|
53
|
+
* Escape Dir glob characters in paths
|
|
54
|
+
|
|
55
|
+
## 0.15.2, released 2012-05-07
|
|
56
|
+
* Added print_in_columns
|
|
57
|
+
* Exposed terminal_width as a public API
|
|
58
|
+
|
|
59
|
+
## 0.15.1, release 2012-05-06
|
|
60
|
+
* Fix Ruby 1.8 truncation bug with unicode chars
|
|
61
|
+
* Fix shell delegate methods to pass their block
|
|
62
|
+
* Don't output trailing spaces when printing the last column in a table
|
|
63
|
+
|
|
64
|
+
## 0.15, released 2012-04-29
|
|
65
|
+
* Alias method_options to options
|
|
66
|
+
* Refactor say to allow multiple colors
|
|
67
|
+
* Exposed error as a public API
|
|
68
|
+
* Exposed file_collision as a public API
|
|
69
|
+
* Exposed print_wrapped as a public API
|
|
70
|
+
* Exposed set_color as a public API
|
|
71
|
+
* Fix number-formatting bugs in print_table
|
|
72
|
+
* Fix "indent" typo in print_table
|
|
73
|
+
* Fix Errno::EPIPE when piping tasks to `head`
|
|
74
|
+
* More friendly error messages
|
|
75
|
+
|
|
76
|
+
## 0.14, released 2010-07-25
|
|
77
|
+
* Added CreateLink class and #link_file method
|
|
4
78
|
* Made Thor::Actions#run use system as default method for system calls
|
|
5
|
-
* Allow use of private methods from superclass as tasks
|
|
79
|
+
* Allow use of private methods from superclass as tasks
|
|
6
80
|
* Added mute(&block) method which allows to run block without any output
|
|
7
81
|
* Removed config[:pretend]
|
|
8
82
|
* Enabled underscores for command line switches
|
|
@@ -10,8 +84,7 @@
|
|
|
10
84
|
* Deprecated invoke() without arguments
|
|
11
85
|
* Added :only and :except to check_unknown_options
|
|
12
86
|
|
|
13
|
-
|
|
14
|
-
|
|
87
|
+
## 0.13, released 2010-02-03
|
|
15
88
|
* Added :lazy_default which is only triggered if a switch is given
|
|
16
89
|
* Added Thor::Shell::HTML
|
|
17
90
|
* Added subcommands
|
|
@@ -19,8 +92,7 @@
|
|
|
19
92
|
* Added check_unknown_options! in case you want error messages to be raised in valid switches
|
|
20
93
|
* run(command) should return the results of command
|
|
21
94
|
|
|
22
|
-
|
|
23
|
-
|
|
95
|
+
## 0.12, released 2010-01-02
|
|
24
96
|
* Methods generated by attr_* are automatically not marked as tasks
|
|
25
97
|
* inject_into_file does not add the same content twice, unless :force is set
|
|
26
98
|
* Removed rr in favor to rspec mock framework
|
|
@@ -28,53 +100,42 @@
|
|
|
28
100
|
* [#7] Do not force white color on status
|
|
29
101
|
* [#8] Yield a block with the filename on directory
|
|
30
102
|
|
|
31
|
-
|
|
32
|
-
|
|
103
|
+
## 0.11, released 2009-07-01
|
|
33
104
|
* Added a rake compatibility layer. It allows you to use spec and rdoc tasks on
|
|
34
105
|
Thor classes.
|
|
35
|
-
|
|
36
106
|
* BACKWARDS INCOMPATIBLE: aliases are not generated automatically anymore
|
|
37
107
|
since it may cause wrong behavior in the invocation system.
|
|
38
|
-
|
|
39
108
|
* thor help now show information about any class/task. All those calls are
|
|
40
109
|
possible:
|
|
41
|
-
|
|
42
|
-
thor help describe
|
|
43
|
-
thor help describe:amazing
|
|
44
110
|
|
|
111
|
+
thor help describe
|
|
112
|
+
thor help describe:amazing
|
|
45
113
|
Or even with default namespaces:
|
|
46
114
|
|
|
47
|
-
|
|
48
|
-
|
|
115
|
+
thor help :spec
|
|
49
116
|
* Thor::Runner now invokes the default task if none is supplied:
|
|
50
117
|
|
|
51
|
-
|
|
52
|
-
|
|
118
|
+
thor describe # invokes the default task, usually help
|
|
53
119
|
* Thor::Runner now works with mappings:
|
|
54
120
|
|
|
55
|
-
|
|
56
|
-
|
|
121
|
+
thor describe -h
|
|
57
122
|
* Added some documentation and code refactoring.
|
|
58
123
|
|
|
59
|
-
|
|
60
|
-
|
|
124
|
+
## 0.9.8, released 2008-10-20
|
|
61
125
|
* Fixed some tiny issues that were introduced lately.
|
|
62
126
|
|
|
63
|
-
|
|
64
|
-
|
|
127
|
+
## 0.9.7, released 2008-10-13
|
|
65
128
|
* Setting global method options on the initialize method works as expected:
|
|
66
129
|
All other tasks will accept these global options in addition to their own.
|
|
67
130
|
* Added 'group' notion to Thor task sets (class Thor); by default all tasks
|
|
68
131
|
are in the 'standard' group. Running 'thor -T' will only show the standard
|
|
69
132
|
tasks - adding --all will show all tasks. You can also filter on a specific
|
|
70
133
|
group using the --group option: thor -T --group advanced
|
|
71
|
-
|
|
72
|
-
== 0.9.6, released 2008-09-13
|
|
73
134
|
|
|
135
|
+
## 0.9.6, released 2008-09-13
|
|
74
136
|
* Generic improvements
|
|
75
137
|
|
|
76
|
-
|
|
77
|
-
|
|
138
|
+
## 0.9.5, released 2008-08-27
|
|
78
139
|
* Improve Windows compatibility
|
|
79
140
|
* Update (incorrect) README and task.thor sample file
|
|
80
141
|
* Options hash is now frozen (once returned)
|
|
@@ -85,8 +146,7 @@
|
|
|
85
146
|
* Don't write options for nil or false values. This allows, for example, turning color off when running specs.
|
|
86
147
|
* Exit with the status of the spec command to help CI stuff out some.
|
|
87
148
|
|
|
88
|
-
|
|
89
|
-
|
|
149
|
+
## 0.9.4, released 2008-08-13
|
|
90
150
|
* Try to add Windows compatibility.
|
|
91
151
|
* BACKWARDS INCOMPATIBLE: options hash is now accessed as a property in your class and is not passed as last argument anymore
|
|
92
152
|
* Allow options at the beginning of the argument list as well as the end.
|
data/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Pull Requests
|
|
2
|
+
-------------
|
|
3
|
+
Here are some reasons why a pull request may not be merged:
|
|
4
|
+
|
|
5
|
+
1. It hasn’t been reviewed.
|
|
6
|
+
2. It doesn’t include specs for new functionality.
|
|
7
|
+
3. It doesn’t include documentation for new functionality.
|
|
8
|
+
4. It changes behavior without changing the relevant documentation, comments, or specs.
|
|
9
|
+
5. It changes behavior of an existing public API, breaking backward compatibility.
|
|
10
|
+
6. It breaks the tests on a supported platform.
|
|
11
|
+
7. It doesn’t merge cleanly (requiring Git rebasing and conflict resolution).
|
|
12
|
+
|
|
13
|
+
If you would like to help in this process, you can start by evaluating open pull requests against the criteria above. For example, if a pull request does not include specs for new functionality, you can add a comment like: “If you would like this feature to be added to Thor, please add specs to ensure that it does not break in the future.” This will help move a pull request closer to being merged.
|
|
14
|
+
|
|
15
|
+
Include this emoji in the top of your ticket to signal to us that you read this file: 🌈
|
data/{LICENSE → LICENSE.md}
RENAMED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright (c) 2008 Yehuda Katz
|
|
1
|
+
Copyright (c) 2008 Yehuda Katz, Eric Hodel, et al.
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
4
|
a copy of this software and associated documentation files (the
|
|
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
17
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
19
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,307 +1,47 @@
|
|
|
1
|
-
|
|
1
|
+
Thor
|
|
2
|
+
====
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
[][gem]
|
|
5
|
+
[][travis]
|
|
6
|
+
[][gemnasium]
|
|
7
|
+
[][codeclimate]
|
|
8
|
+
[][coveralls]
|
|
4
9
|
|
|
5
|
-
|
|
10
|
+
[gem]: https://rubygems.org/gems/thor
|
|
11
|
+
[travis]: http://travis-ci.org/erikhuda/thor
|
|
12
|
+
[gemnasium]: https://gemnasium.com/erikhuda/thor
|
|
13
|
+
[codeclimate]: https://codeclimate.com/github/erikhuda/thor
|
|
14
|
+
[coveralls]: https://coveralls.io/r/erikhuda/thor
|
|
6
15
|
|
|
7
|
-
|
|
16
|
+
Description
|
|
17
|
+
-----------
|
|
18
|
+
Thor is a simple and efficient tool for building self-documenting command line
|
|
19
|
+
utilities. It removes the pain of parsing command line options, writing
|
|
20
|
+
"USAGE:" banners, and can also be used as an alternative to the [Rake][rake]
|
|
21
|
+
build tool. The syntax is Rake-like, so it should be familiar to most Rake
|
|
22
|
+
users.
|
|
8
23
|
|
|
9
|
-
|
|
24
|
+
[rake]: https://github.com/ruby/rake
|
|
10
25
|
|
|
11
|
-
|
|
26
|
+
Installation
|
|
27
|
+
------------
|
|
28
|
+
gem install thor
|
|
12
29
|
|
|
13
|
-
|
|
30
|
+
Usage and documentation
|
|
31
|
+
-----------------------
|
|
32
|
+
Please see the [wiki][] for basic usage and other documentation on using Thor. You can also checkout the [official homepage][homepage].
|
|
14
33
|
|
|
15
|
-
|
|
34
|
+
[wiki]: https://github.com/erikhuda/thor/wiki
|
|
35
|
+
[homepage]: http://whatisthor.com/
|
|
16
36
|
|
|
17
|
-
|
|
18
|
-
|
|
37
|
+
Contributing
|
|
38
|
+
------------
|
|
39
|
+
If you would like to help, please read the [CONTRIBUTING][] file for suggestions.
|
|
19
40
|
|
|
20
|
-
|
|
41
|
+
[contributing]: CONTRIBUTING.md
|
|
21
42
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
desc "install APP_NAME", "install one of the available apps" # [3]
|
|
26
|
-
method_options :force => :boolean, :alias => :string # [4]
|
|
27
|
-
def install(name)
|
|
28
|
-
user_alias = options[:alias]
|
|
29
|
-
if options.force?
|
|
30
|
-
# do something
|
|
31
|
-
end
|
|
32
|
-
# other code
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
desc "list [SEARCH]", "list all of the available apps, limited by SEARCH"
|
|
36
|
-
def list(search="")
|
|
37
|
-
# list everything
|
|
38
|
-
end
|
|
39
|
-
end
|
|
43
|
+
License
|
|
44
|
+
-------
|
|
45
|
+
Released under the MIT License. See the [LICENSE][] file for further details.
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
thor app:install myname --force
|
|
44
|
-
|
|
45
|
-
That gets converted to:
|
|
46
|
-
|
|
47
|
-
App.new.install("myname")
|
|
48
|
-
# with {'force' => true} as options hash
|
|
49
|
-
|
|
50
|
-
1. Inherit from Thor to turn a class into an option mapper.
|
|
51
|
-
2. Map additional non-valid identifiers to specific methods. In this case, convert -L to :list
|
|
52
|
-
3. Describe the method immediately below. The first parameter is the usage information, and the second parameter is the description.
|
|
53
|
-
4. Provide any additional options that will be available the instance method options.
|
|
54
|
-
|
|
55
|
-
## Types for <tt>method_options</tt>
|
|
56
|
-
|
|
57
|
-
* :boolean - is parsed as <tt>--option</tt> or <tt>--option=true</tt>
|
|
58
|
-
* :string - is parsed as <tt>--option=VALUE</tt>
|
|
59
|
-
* :numeric - is parsed as <tt>--option=N</tt>
|
|
60
|
-
* :array - is parsed as <tt>--option=one two three</tt>
|
|
61
|
-
* :hash - is parsed as <tt>--option=name:string age:integer</tt>
|
|
62
|
-
|
|
63
|
-
Besides, method_option allows a default value to be given. Examples:
|
|
64
|
-
|
|
65
|
-
method_options :force => false
|
|
66
|
-
#=> Creates a boolean option with default value false
|
|
67
|
-
|
|
68
|
-
method_options :alias => "bar"
|
|
69
|
-
#=> Creates a string option with default value "bar"
|
|
70
|
-
|
|
71
|
-
method_options :threshold => 3.0
|
|
72
|
-
#=> Creates a numeric option with default value 3.0
|
|
73
|
-
|
|
74
|
-
You can also supply <tt>:option => :required</tt> to mark an option as required. The
|
|
75
|
-
type is assumed to be string. If you want a required hash with default values
|
|
76
|
-
as option, you can use <tt>method_option</tt> which uses a more declarative style:
|
|
77
|
-
|
|
78
|
-
method_option :attributes, :type => :hash, :default => {}, :required => true
|
|
79
|
-
|
|
80
|
-
All arguments can be set to nil (except required arguments), by suppling a no or
|
|
81
|
-
skip variant. For example:
|
|
82
|
-
|
|
83
|
-
thor app name --no-attributes
|
|
84
|
-
|
|
85
|
-
In previous versions, aliases for options were created automatically, but now
|
|
86
|
-
they should be explicit. You can supply aliases in both short and declarative
|
|
87
|
-
styles:
|
|
88
|
-
|
|
89
|
-
method_options %w( force -f ) => :boolean
|
|
90
|
-
|
|
91
|
-
Or:
|
|
92
|
-
|
|
93
|
-
method_option :force, :type => :boolean, :aliases => "-f"
|
|
94
|
-
|
|
95
|
-
You can supply as many aliases as you want.
|
|
96
|
-
|
|
97
|
-
NOTE: Type :optional available in Thor 0.9.0 was deprecated. Use :string or :boolean instead.
|
|
98
|
-
|
|
99
|
-
## Namespaces
|
|
100
|
-
|
|
101
|
-
By default, your Thor tasks are invoked using Ruby namespace. In the example
|
|
102
|
-
above, tasks are invoked as:
|
|
103
|
-
|
|
104
|
-
thor app:install name --force
|
|
105
|
-
|
|
106
|
-
However, you could namespace your class as:
|
|
107
|
-
|
|
108
|
-
module Sinatra
|
|
109
|
-
class App < Thor
|
|
110
|
-
# tasks
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
And then you should invoke your tasks as:
|
|
115
|
-
|
|
116
|
-
thor sinatra:app:install name --force
|
|
117
|
-
|
|
118
|
-
If desired, you can change the namespace:
|
|
119
|
-
|
|
120
|
-
module Sinatra
|
|
121
|
-
class App < Thor
|
|
122
|
-
namespace :myapp
|
|
123
|
-
# tasks
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
And then your tasks should be invoked as:
|
|
128
|
-
|
|
129
|
-
thor myapp:install name --force
|
|
130
|
-
|
|
131
|
-
## Invocations
|
|
132
|
-
|
|
133
|
-
Thor comes with a invocation-dependency system as well, which allows a task to be invoked only once. For example:
|
|
134
|
-
|
|
135
|
-
class Counter < Thor
|
|
136
|
-
desc "one", "Prints 1, 2, 3"
|
|
137
|
-
def one
|
|
138
|
-
puts 1
|
|
139
|
-
invoke :two
|
|
140
|
-
invoke :three
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
desc "two", "Prints 2, 3"
|
|
144
|
-
def two
|
|
145
|
-
puts 2
|
|
146
|
-
invoke :three
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
desc "three", "Prints 3"
|
|
150
|
-
def three
|
|
151
|
-
puts 3
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
When invoking the task one:
|
|
156
|
-
|
|
157
|
-
thor counter:one
|
|
158
|
-
|
|
159
|
-
The output is "1 2 3", which means that the three task was invoked only once.
|
|
160
|
-
You can even invoke tasks from another class, so be sure to check the
|
|
161
|
-
[documentation](http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor.html).
|
|
162
|
-
|
|
163
|
-
Notice invocations do not share the same object. I.e, Thor will instantiate Counter once to invoke the task one, then, it instantiates another to invoke the task two and another for task three. This happens to allow options and arguments to parsed again. For example, if two and three have different options and both of them were given to the command line, calling invoke makes them be parsed each time and used accordingly by each task.
|
|
164
|
-
|
|
165
|
-
## Thor::Group
|
|
166
|
-
|
|
167
|
-
Thor has a special class called Thor::Group. The main difference to Thor class
|
|
168
|
-
is that it invokes all tasks at once. The example above could be rewritten in
|
|
169
|
-
Thor::Group as this:
|
|
170
|
-
|
|
171
|
-
class Counter < Thor::Group
|
|
172
|
-
desc "Prints 1, 2, 3"
|
|
173
|
-
|
|
174
|
-
def one
|
|
175
|
-
puts 1
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
def two
|
|
179
|
-
puts 2
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
def three
|
|
183
|
-
puts 3
|
|
184
|
-
end
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
When invoked:
|
|
188
|
-
|
|
189
|
-
thor counter
|
|
190
|
-
|
|
191
|
-
It prints "1 2 3" as well. Notice you should describe (using the method <tt>desc</tt>)
|
|
192
|
-
only the class and not each task anymore. Thor::Group is a great tool to create
|
|
193
|
-
generators, since you can define several steps which are invoked in the order they
|
|
194
|
-
are defined (Thor::Group is the tool use in generators in Rails 3.0).
|
|
195
|
-
|
|
196
|
-
Besides, Thor::Group can parse arguments and options as Thor tasks:
|
|
197
|
-
|
|
198
|
-
class Counter < Thor::Group
|
|
199
|
-
# number will be available as attr_accessor
|
|
200
|
-
argument :number, :type => :numeric, :desc => "The number to start counting"
|
|
201
|
-
desc "Prints the 'number' given upto 'number+2'"
|
|
202
|
-
|
|
203
|
-
def one
|
|
204
|
-
puts number + 0
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
def two
|
|
208
|
-
puts number + 1
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
def three
|
|
212
|
-
puts number + 2
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
The counter above expects one parameter and has the folling outputs:
|
|
217
|
-
|
|
218
|
-
thor counter 5
|
|
219
|
-
# Prints "5 6 7"
|
|
220
|
-
|
|
221
|
-
thor counter 11
|
|
222
|
-
# Prints "11 12 13"
|
|
223
|
-
|
|
224
|
-
You can also give options to Thor::Group, but instead of using <tt>method_option</tt>
|
|
225
|
-
and <tt>method_options</tt>, you should use <tt>class_option</tt> and <tt>class_options</tt>.
|
|
226
|
-
Both argument and class_options methods are available to Thor class as well.
|
|
227
|
-
|
|
228
|
-
## Actions
|
|
229
|
-
|
|
230
|
-
Thor comes with several actions which helps with script and generator tasks. You
|
|
231
|
-
might be familiar with them since some came from Rails Templates. They are:
|
|
232
|
-
<tt>say</tt>, <tt>ask</tt>, <tt>yes?</tt>, <tt>no?</tt>, <tt>add_file</tt>,
|
|
233
|
-
<tt>remove_file</tt>, <tt>copy_file</tt>, <tt>template</tt>, <tt>directory</tt>,
|
|
234
|
-
<tt>inside</tt>, <tt>run</tt>, <tt>inject_into_file</tt> and a couple more.
|
|
235
|
-
|
|
236
|
-
To use them, you just need to include Thor::Actions in your Thor classes:
|
|
237
|
-
|
|
238
|
-
class App < Thor
|
|
239
|
-
include Thor::Actions
|
|
240
|
-
# tasks
|
|
241
|
-
end
|
|
242
|
-
|
|
243
|
-
Some actions like copy file requires that a class method called source_root is
|
|
244
|
-
defined in your class. This is the directory where your templates should be
|
|
245
|
-
placed. Be sure to check the documentation on [actions](http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor/Actions.html).
|
|
246
|
-
|
|
247
|
-
## Generators
|
|
248
|
-
|
|
249
|
-
A great use for Thor is creating custom generators. Combining Thor::Group,
|
|
250
|
-
Thor::Actions and ERB templates makes this very easy. Here is an example:
|
|
251
|
-
|
|
252
|
-
class Newgem < Thor::Group
|
|
253
|
-
include Thor::Actions
|
|
254
|
-
|
|
255
|
-
# Define arguments and options
|
|
256
|
-
argument :name
|
|
257
|
-
class_option :test_framework, :default => :test_unit
|
|
258
|
-
|
|
259
|
-
def self.source_root
|
|
260
|
-
File.dirname(__FILE__)
|
|
261
|
-
end
|
|
262
|
-
|
|
263
|
-
def create_lib_file
|
|
264
|
-
template('templates/newgem.tt', "#{name}/lib/#{name}.rb")
|
|
265
|
-
end
|
|
266
|
-
|
|
267
|
-
def create_test_file
|
|
268
|
-
test = options[:test_framework] == "rspec" ? :spec : :test
|
|
269
|
-
create_file "#{name}/#{test}/#{name}_#{test}.rb"
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
def copy_licence
|
|
273
|
-
if yes?("Use MIT license?")
|
|
274
|
-
# Make a copy of the MITLICENSE file at the source root
|
|
275
|
-
copy_file "MITLICENSE", "#{name}/MITLICENSE"
|
|
276
|
-
else
|
|
277
|
-
say "Shame on you…", :red
|
|
278
|
-
end
|
|
279
|
-
end
|
|
280
|
-
end
|
|
281
|
-
|
|
282
|
-
Doing a <tt>thor -T</tt> will show how to run our generator. It should read:
|
|
283
|
-
<tt>thor newgem NAME</tt>. This shows that we have to supply a NAME
|
|
284
|
-
argument for our generator to run.
|
|
285
|
-
|
|
286
|
-
The <tt>create_lib_file</tt> uses an ERB template. This is what it looks like:
|
|
287
|
-
|
|
288
|
-
class <%= name.capitalize %>
|
|
289
|
-
end
|
|
290
|
-
|
|
291
|
-
The arguments that you set in your generator will automatically be passed in
|
|
292
|
-
when <tt>template</tt> gets called. Be sure to read the [documentation](http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor/Actions.html) for
|
|
293
|
-
more options.
|
|
294
|
-
|
|
295
|
-
Running the generator with <tt>thor newgem devise</tt> will
|
|
296
|
-
create two files: "devise/lib/devise.rb", and "devise/test/devise_test.rb". The user will then be asked (via a prompt by the <tt>yes?</tt> method) whether or not they would like to copy the MIT License. If you want to change the test framework, you can add the option: <tt>thor newgem devise --test-framework=rspec</tt>
|
|
297
|
-
|
|
298
|
-
This will generate two files - "devise/lib/devise.rb" and "devise/spec/devise_spec.rb".
|
|
299
|
-
|
|
300
|
-
## Further Reading
|
|
301
|
-
|
|
302
|
-
Thor offers many scripting possibilities beyond these examples. Be sure to read
|
|
303
|
-
through the [documentation](http://rdoc.info/rdoc/wycats/thor/blob/f939a3e8a854616784cac1dcff04ef4f3ee5f7ff/Thor.html) and [specs](http://github.com/wycats/thor/tree/master/spec/) to get a better understanding of the options available.
|
|
304
|
-
|
|
305
|
-
## License
|
|
306
|
-
|
|
307
|
-
Released under the MIT License. See the LICENSE file for further details.
|
|
47
|
+
[license]: LICENSE.md
|
data/bin/thor
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "thor/actions/empty_directory"
|
|
2
2
|
|
|
3
3
|
class Thor
|
|
4
4
|
module Actions
|
|
5
|
-
|
|
6
5
|
# Create a new file relative to the destination root with the given data,
|
|
7
6
|
# which is the return value of a block or a data string.
|
|
8
7
|
#
|
|
@@ -25,7 +24,7 @@ class Thor
|
|
|
25
24
|
data = args.first
|
|
26
25
|
action CreateFile.new(self, destination, block || data.to_s, config)
|
|
27
26
|
end
|
|
28
|
-
|
|
27
|
+
alias_method :add_file, :create_file
|
|
29
28
|
|
|
30
29
|
# CreateFile is a subset of Template, which instead of rendering a file with
|
|
31
30
|
# ERB, it gets the content from the user.
|
|
@@ -33,7 +32,7 @@ class Thor
|
|
|
33
32
|
class CreateFile < EmptyDirectory #:nodoc:
|
|
34
33
|
attr_reader :data
|
|
35
34
|
|
|
36
|
-
def initialize(base, destination, data, config={})
|
|
35
|
+
def initialize(base, destination, data, config = {})
|
|
37
36
|
@data = data
|
|
38
37
|
super(base, destination, config)
|
|
39
38
|
end
|
|
@@ -60,46 +59,45 @@ class Thor
|
|
|
60
59
|
def invoke!
|
|
61
60
|
invoke_with_conflict_check do
|
|
62
61
|
FileUtils.mkdir_p(File.dirname(destination))
|
|
63
|
-
File.open(destination,
|
|
62
|
+
File.open(destination, "wb") { |f| f.write render }
|
|
64
63
|
end
|
|
65
64
|
given_destination
|
|
66
65
|
end
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
# Now on conflict we check if the file is identical or not.
|
|
71
|
-
#
|
|
72
|
-
def on_conflict_behavior(&block)
|
|
73
|
-
if identical?
|
|
74
|
-
say_status :identical, :blue
|
|
75
|
-
else
|
|
76
|
-
options = base.options.merge(config)
|
|
77
|
-
force_or_skip_or_conflict(options[:force], options[:skip], &block)
|
|
78
|
-
end
|
|
79
|
-
end
|
|
67
|
+
protected
|
|
80
68
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
elsif skip
|
|
90
|
-
say_status :skip, :yellow
|
|
91
|
-
else
|
|
92
|
-
say_status :conflict, :red
|
|
93
|
-
force_or_skip_or_conflict(force_on_collision?, true, &block)
|
|
94
|
-
end
|
|
69
|
+
# Now on conflict we check if the file is identical or not.
|
|
70
|
+
#
|
|
71
|
+
def on_conflict_behavior(&block)
|
|
72
|
+
if identical?
|
|
73
|
+
say_status :identical, :blue
|
|
74
|
+
else
|
|
75
|
+
options = base.options.merge(config)
|
|
76
|
+
force_or_skip_or_conflict(options[:force], options[:skip], &block)
|
|
95
77
|
end
|
|
78
|
+
end
|
|
96
79
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
80
|
+
# If force is true, run the action, otherwise check if it's not being
|
|
81
|
+
# skipped. If both are false, show the file_collision menu, if the menu
|
|
82
|
+
# returns true, force it, otherwise skip.
|
|
83
|
+
#
|
|
84
|
+
def force_or_skip_or_conflict(force, skip, &block)
|
|
85
|
+
if force
|
|
86
|
+
say_status :force, :yellow
|
|
87
|
+
yield unless pretend?
|
|
88
|
+
elsif skip
|
|
89
|
+
say_status :skip, :yellow
|
|
90
|
+
else
|
|
91
|
+
say_status :conflict, :red
|
|
92
|
+
force_or_skip_or_conflict(force_on_collision?, true, &block)
|
|
101
93
|
end
|
|
94
|
+
end
|
|
102
95
|
|
|
96
|
+
# Shows the file collision menu to the user and gets the result.
|
|
97
|
+
#
|
|
98
|
+
def force_on_collision?
|
|
99
|
+
base.shell.file_collision(destination) { render }
|
|
100
|
+
end
|
|
103
101
|
end
|
|
104
102
|
end
|
|
105
103
|
end
|