wijet-thor 0.14.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/CHANGELOG.rdoc +103 -0
  2. data/LICENSE +20 -0
  3. data/README.md +307 -0
  4. data/Thorfile +24 -0
  5. data/bin/rake2thor +86 -0
  6. data/bin/thor +6 -0
  7. data/lib/thor.rb +334 -0
  8. data/lib/thor/actions.rb +314 -0
  9. data/lib/thor/actions/create_file.rb +105 -0
  10. data/lib/thor/actions/create_link.rb +57 -0
  11. data/lib/thor/actions/directory.rb +93 -0
  12. data/lib/thor/actions/empty_directory.rb +134 -0
  13. data/lib/thor/actions/file_manipulation.rb +270 -0
  14. data/lib/thor/actions/inject_into_file.rb +109 -0
  15. data/lib/thor/base.rb +579 -0
  16. data/lib/thor/core_ext/file_binary_read.rb +9 -0
  17. data/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  18. data/lib/thor/core_ext/ordered_hash.rb +100 -0
  19. data/lib/thor/error.rb +30 -0
  20. data/lib/thor/group.rb +273 -0
  21. data/lib/thor/invocation.rb +168 -0
  22. data/lib/thor/parser.rb +4 -0
  23. data/lib/thor/parser/argument.rb +67 -0
  24. data/lib/thor/parser/arguments.rb +161 -0
  25. data/lib/thor/parser/option.rb +120 -0
  26. data/lib/thor/parser/options.rb +173 -0
  27. data/lib/thor/rake_compat.rb +66 -0
  28. data/lib/thor/runner.rb +309 -0
  29. data/lib/thor/shell.rb +88 -0
  30. data/lib/thor/shell/basic.rb +290 -0
  31. data/lib/thor/shell/color.rb +108 -0
  32. data/lib/thor/shell/html.rb +121 -0
  33. data/lib/thor/task.rb +114 -0
  34. data/lib/thor/util.rb +229 -0
  35. data/lib/thor/version.rb +3 -0
  36. data/spec/actions/create_file_spec.rb +170 -0
  37. data/spec/actions/directory_spec.rb +136 -0
  38. data/spec/actions/empty_directory_spec.rb +98 -0
  39. data/spec/actions/file_manipulation_spec.rb +310 -0
  40. data/spec/actions/inject_into_file_spec.rb +135 -0
  41. data/spec/actions_spec.rb +322 -0
  42. data/spec/base_spec.rb +269 -0
  43. data/spec/core_ext/hash_with_indifferent_access_spec.rb +43 -0
  44. data/spec/core_ext/ordered_hash_spec.rb +115 -0
  45. data/spec/fixtures/application.rb +2 -0
  46. data/spec/fixtures/bundle/execute.rb +6 -0
  47. data/spec/fixtures/bundle/main.thor +1 -0
  48. data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
  49. data/spec/fixtures/doc/README +3 -0
  50. data/spec/fixtures/doc/block_helper.rb +3 -0
  51. data/spec/fixtures/doc/components/.empty_directory +0 -0
  52. data/spec/fixtures/doc/config.rb +1 -0
  53. data/spec/fixtures/group.thor +114 -0
  54. data/spec/fixtures/invoke.thor +112 -0
  55. data/spec/fixtures/path with spaces b/data/spec/fixtures/path with → spaces +0 -0
  56. data/spec/fixtures/script.thor +184 -0
  57. data/spec/fixtures/task.thor +10 -0
  58. data/spec/group_spec.rb +178 -0
  59. data/spec/invocation_spec.rb +100 -0
  60. data/spec/parser/argument_spec.rb +47 -0
  61. data/spec/parser/arguments_spec.rb +64 -0
  62. data/spec/parser/option_spec.rb +202 -0
  63. data/spec/parser/options_spec.rb +319 -0
  64. data/spec/rake_compat_spec.rb +68 -0
  65. data/spec/register_spec.rb +104 -0
  66. data/spec/runner_spec.rb +210 -0
  67. data/spec/shell/basic_spec.rb +223 -0
  68. data/spec/shell/color_spec.rb +41 -0
  69. data/spec/shell/html_spec.rb +27 -0
  70. data/spec/shell_spec.rb +47 -0
  71. data/spec/spec_helper.rb +54 -0
  72. data/spec/task_spec.rb +74 -0
  73. data/spec/thor_spec.rb +334 -0
  74. data/spec/util_spec.rb +163 -0
  75. metadata +193 -0
@@ -0,0 +1,103 @@
1
+ == 0.14, released 2010-07-25
2
+
3
+ * Added CreateLink class and #link_file method
4
+ * Made Thor::Actions#run use system as default method for system calls
5
+ * Allow use of private methods from superclass as tasks
6
+ * Added mute(&block) method which allows to run block without any output
7
+ * Removed config[:pretend]
8
+ * Enabled underscores for command line switches
9
+ * Added Thor::Base.basename which is used by both Thor.banner and Thor::Group.banner
10
+ * Deprecated invoke() without arguments
11
+ * Added :only and :except to check_unknown_options
12
+
13
+ == 0.13, released 2010-02-03
14
+
15
+ * Added :lazy_default which is only triggered if a switch is given
16
+ * Added Thor::Shell::HTML
17
+ * Added subcommands
18
+ * Decoupled Thor::Group and Thor, so it's easier to vendor
19
+ * Added check_unknown_options! in case you want error messages to be raised in valid switches
20
+ * run(command) should return the results of command
21
+
22
+ == 0.12, released 2010-01-02
23
+
24
+ * Methods generated by attr_* are automatically not marked as tasks
25
+ * inject_into_file does not add the same content twice, unless :force is set
26
+ * Removed rr in favor to rspec mock framework
27
+ * Improved output for thor -T
28
+ * [#7] Do not force white color on status
29
+ * [#8] Yield a block with the filename on directory
30
+
31
+ == 0.11, released 2009-07-01
32
+
33
+ * Added a rake compatibility layer. It allows you to use spec and rdoc tasks on
34
+ Thor classes.
35
+
36
+ * BACKWARDS INCOMPATIBLE: aliases are not generated automatically anymore
37
+ since it may cause wrong behavior in the invocation system.
38
+
39
+ * thor help now show information about any class/task. All those calls are
40
+ possible:
41
+
42
+ thor help describe
43
+ thor help describe:amazing
44
+
45
+ Or even with default namespaces:
46
+
47
+ thor help :spec
48
+
49
+ * Thor::Runner now invokes the default task if none is supplied:
50
+
51
+ thor describe # invokes the default task, usually help
52
+
53
+ * Thor::Runner now works with mappings:
54
+
55
+ thor describe -h
56
+
57
+ * Added some documentation and code refactoring.
58
+
59
+ == 0.9.8, released 2008-10-20
60
+
61
+ * Fixed some tiny issues that were introduced lately.
62
+
63
+ == 0.9.7, released 2008-10-13
64
+
65
+ * Setting global method options on the initialize method works as expected:
66
+ All other tasks will accept these global options in addition to their own.
67
+ * Added 'group' notion to Thor task sets (class Thor); by default all tasks
68
+ are in the 'standard' group. Running 'thor -T' will only show the standard
69
+ tasks - adding --all will show all tasks. You can also filter on a specific
70
+ group using the --group option: thor -T --group advanced
71
+
72
+ == 0.9.6, released 2008-09-13
73
+
74
+ * Generic improvements
75
+
76
+ == 0.9.5, released 2008-08-27
77
+
78
+ * Improve Windows compatibility
79
+ * Update (incorrect) README and task.thor sample file
80
+ * Options hash is now frozen (once returned)
81
+ * Allow magic predicates on options object. For instance: `options.force?`
82
+ * Add support for :numeric type
83
+ * BACKWARDS INCOMPATIBLE: Refactor Thor::Options. You cannot access shorthand forms in options hash anymore (for instance, options[:f])
84
+ * Allow specifying optional args with default values: method_options(:user => "mislav")
85
+ * Don't write options for nil or false values. This allows, for example, turning color off when running specs.
86
+ * Exit with the status of the spec command to help CI stuff out some.
87
+
88
+ == 0.9.4, released 2008-08-13
89
+
90
+ * Try to add Windows compatibility.
91
+ * BACKWARDS INCOMPATIBLE: options hash is now accessed as a property in your class and is not passed as last argument anymore
92
+ * Allow options at the beginning of the argument list as well as the end.
93
+ * Make options available with symbol keys in addition to string keys.
94
+ * Allow true to be passed to Thor#method_options to denote a boolean option.
95
+ * If loading a thor file fails, don't give up, just print a warning and keep going.
96
+ * Make sure that we re-raise errors if they happened further down the pipe than we care about.
97
+ * Only delete the old file on updating when the installation of the new one is a success
98
+ * Make it Ruby 1.8.5 compatible.
99
+ * Don't raise an error if a boolean switch is defined multiple times.
100
+ * Thor::Options now doesn't parse through things that look like options but aren't.
101
+ * Add URI detection to install task, and make sure we don't append ".thor" to URIs
102
+ * Add rake2thor to the gem binfiles.
103
+ * Make sure local Thorfiles override system-wide ones.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Yehuda Katz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
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.
@@ -0,0 +1,307 @@
1
+ # Thor
2
+
3
+ ## Description
4
+
5
+ Thor is a simple and efficient tool for building self-documenting command line utilities. It removes the pain of parsing command line options, writing "USAGE:" banners, and can also be used as an alternative to the [Rake](http://github.com/jimweirich/rake) build tool. The syntax is Rake-like, so it should be familiar to most Rake users.
6
+
7
+ ## Installation
8
+
9
+ $ gem install thor
10
+
11
+ or
12
+
13
+ $ gem install wycats-thor -s http://gems.github.com
14
+
15
+ ## Usage
16
+
17
+ Map options to a class. Simply create a class with the appropriate annotations
18
+ and have options automatically map to functions and parameters.
19
+
20
+ Example:
21
+
22
+ class App < Thor # [1]
23
+ map "-L" => :list # [2]
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
40
+
41
+ Thor automatically maps commands as such:
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.
@@ -0,0 +1,24 @@
1
+ # enconding: utf-8
2
+ require 'thor/rake_compat'
3
+
4
+ class Default < Thor
5
+ include Thor::RakeCompat
6
+
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ require 'bundler'
11
+ Bundler::GemHelper.install_tasks
12
+
13
+ require 'rdoc/task'
14
+ if defined?(RDoc)
15
+ RDoc::Task.new do |rdoc|
16
+ rdoc.main = 'README.md'
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'thor'
19
+ rdoc.rdoc_files.include('README.md', 'LICENSE', 'CHANGELOG.rdoc', 'Thorfile')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ end
23
+ end
24
+ end