acquia_toolbelt 2.3.0 → 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CONTRIBUTING.md +11 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +55 -0
- data/Guardfile +5 -0
- data/LICENSE +19 -0
- data/README.md +149 -0
- data/Rakefile +1 -0
- data/acquia_toolbelt.gemspec +34 -0
- data/bin/acquia +9 -0
- data/lib/acquia_toolbelt/cli/api.rb +164 -0
- data/lib/acquia_toolbelt/cli/auth.rb +31 -0
- data/lib/acquia_toolbelt/cli/database.rb +237 -0
- data/lib/acquia_toolbelt/cli/deploy.rb +36 -0
- data/lib/acquia_toolbelt/cli/domain.rb +177 -0
- data/lib/acquia_toolbelt/cli/environment.rb +71 -0
- data/lib/acquia_toolbelt/cli/file.rb +31 -0
- data/lib/acquia_toolbelt/cli/server.rb +67 -0
- data/lib/acquia_toolbelt/cli/site.rb +28 -0
- data/lib/acquia_toolbelt/cli/ssh.rb +78 -0
- data/lib/acquia_toolbelt/cli/svn.rb +73 -0
- data/lib/acquia_toolbelt/cli/task.rb +74 -0
- data/lib/acquia_toolbelt/cli/ui.rb +44 -0
- data/lib/acquia_toolbelt/cli.rb +103 -0
- data/lib/acquia_toolbelt/error.rb +4 -0
- data/lib/acquia_toolbelt/thor.rb +95 -0
- data/lib/acquia_toolbelt/version.rb +3 -0
- data/lib/acquia_toolbelt.rb +4 -0
- data/lib/vendor/thor/CHANGELOG.md +139 -0
- data/lib/vendor/thor/Gemfile +20 -0
- data/lib/vendor/thor/LICENSE.md +20 -0
- data/lib/vendor/thor/README.md +35 -0
- data/lib/vendor/thor/lib/thor/actions/create_file.rb +105 -0
- data/lib/vendor/thor/lib/thor/actions/create_link.rb +60 -0
- data/lib/vendor/thor/lib/thor/actions/directory.rb +119 -0
- data/lib/vendor/thor/lib/thor/actions/empty_directory.rb +137 -0
- data/lib/vendor/thor/lib/thor/actions/file_manipulation.rb +317 -0
- data/lib/vendor/thor/lib/thor/actions/inject_into_file.rb +109 -0
- data/lib/vendor/thor/lib/thor/actions.rb +318 -0
- data/lib/vendor/thor/lib/thor/base.rb +654 -0
- data/lib/vendor/thor/lib/thor/command.rb +136 -0
- data/lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +80 -0
- data/lib/vendor/thor/lib/thor/core_ext/io_binary_read.rb +12 -0
- data/lib/vendor/thor/lib/thor/core_ext/ordered_hash.rb +100 -0
- data/lib/vendor/thor/lib/thor/error.rb +32 -0
- data/lib/vendor/thor/lib/thor/group.rb +282 -0
- data/lib/vendor/thor/lib/thor/invocation.rb +172 -0
- data/lib/vendor/thor/lib/thor/parser/argument.rb +74 -0
- data/lib/vendor/thor/lib/thor/parser/arguments.rb +171 -0
- data/lib/vendor/thor/lib/thor/parser/option.rb +121 -0
- data/lib/vendor/thor/lib/thor/parser/options.rb +218 -0
- data/lib/vendor/thor/lib/thor/parser.rb +4 -0
- data/lib/vendor/thor/lib/thor/rake_compat.rb +72 -0
- data/lib/vendor/thor/lib/thor/runner.rb +322 -0
- data/lib/vendor/thor/lib/thor/shell/basic.rb +422 -0
- data/lib/vendor/thor/lib/thor/shell/color.rb +148 -0
- data/lib/vendor/thor/lib/thor/shell/html.rb +127 -0
- data/lib/vendor/thor/lib/thor/shell.rb +88 -0
- data/lib/vendor/thor/lib/thor/util.rb +270 -0
- data/lib/vendor/thor/lib/thor/version.rb +3 -0
- data/lib/vendor/thor/lib/thor.rb +474 -0
- data/lib/vendor/thor/spec/actions/create_file_spec.rb +170 -0
- data/lib/vendor/thor/spec/actions/create_link_spec.rb +95 -0
- data/lib/vendor/thor/spec/actions/directory_spec.rb +169 -0
- data/lib/vendor/thor/spec/actions/empty_directory_spec.rb +129 -0
- data/lib/vendor/thor/spec/actions/file_manipulation_spec.rb +382 -0
- data/lib/vendor/thor/spec/actions/inject_into_file_spec.rb +135 -0
- data/lib/vendor/thor/spec/actions_spec.rb +331 -0
- data/lib/vendor/thor/spec/base_spec.rb +291 -0
- data/lib/vendor/thor/spec/command_spec.rb +80 -0
- data/lib/vendor/thor/spec/core_ext/hash_with_indifferent_access_spec.rb +48 -0
- data/lib/vendor/thor/spec/core_ext/ordered_hash_spec.rb +115 -0
- data/lib/vendor/thor/spec/exit_condition_spec.rb +19 -0
- data/lib/vendor/thor/spec/fixtures/application.rb +2 -0
- data/lib/vendor/thor/spec/fixtures/app{1}/README +3 -0
- data/lib/vendor/thor/spec/fixtures/bundle/execute.rb +6 -0
- data/lib/vendor/thor/spec/fixtures/bundle/main.thor +1 -0
- data/lib/vendor/thor/spec/fixtures/command.thor +10 -0
- data/lib/vendor/thor/spec/fixtures/doc/%file_name%.rb.tt +1 -0
- data/lib/vendor/thor/spec/fixtures/doc/COMMENTER +11 -0
- data/lib/vendor/thor/spec/fixtures/doc/README +3 -0
- data/lib/vendor/thor/spec/fixtures/doc/block_helper.rb +3 -0
- data/lib/vendor/thor/spec/fixtures/doc/components/.empty_directory +0 -0
- data/lib/vendor/thor/spec/fixtures/doc/config.rb +1 -0
- data/lib/vendor/thor/spec/fixtures/doc/config.yaml.tt +1 -0
- data/lib/vendor/thor/spec/fixtures/doc/excluding/%file_name%.rb.tt +1 -0
- data/lib/vendor/thor/spec/fixtures/enum.thor +10 -0
- data/lib/vendor/thor/spec/fixtures/group.thor +128 -0
- data/lib/vendor/thor/spec/fixtures/invoke.thor +118 -0
- data/lib/vendor/thor/spec/fixtures/path with spaces +0 -0
- data/lib/vendor/thor/spec/fixtures/preserve/script.sh +3 -0
- data/lib/vendor/thor/spec/fixtures/script.thor +220 -0
- data/lib/vendor/thor/spec/fixtures/subcommand.thor +17 -0
- data/lib/vendor/thor/spec/group_spec.rb +222 -0
- data/lib/vendor/thor/spec/helper.rb +67 -0
- data/lib/vendor/thor/spec/invocation_spec.rb +108 -0
- data/lib/vendor/thor/spec/parser/argument_spec.rb +53 -0
- data/lib/vendor/thor/spec/parser/arguments_spec.rb +66 -0
- data/lib/vendor/thor/spec/parser/option_spec.rb +202 -0
- data/lib/vendor/thor/spec/parser/options_spec.rb +400 -0
- data/lib/vendor/thor/spec/rake_compat_spec.rb +72 -0
- data/lib/vendor/thor/spec/register_spec.rb +197 -0
- data/lib/vendor/thor/spec/runner_spec.rb +241 -0
- data/lib/vendor/thor/spec/shell/basic_spec.rb +330 -0
- data/lib/vendor/thor/spec/shell/color_spec.rb +95 -0
- data/lib/vendor/thor/spec/shell/html_spec.rb +31 -0
- data/lib/vendor/thor/spec/shell_spec.rb +47 -0
- data/lib/vendor/thor/spec/subcommand_spec.rb +30 -0
- data/lib/vendor/thor/spec/thor_spec.rb +499 -0
- data/lib/vendor/thor/spec/util_spec.rb +196 -0
- data/lib/vendor/thor/thor.gemspec +24 -0
- data/script/release +50 -0
- data/script/setup_build +6 -0
- data/script/test +23 -0
- data/spec/auth_spec.rb +15 -0
- data/spec/cassettes/databases/all_databases.json +1 -0
- data/spec/cassettes/databases/copy_database_from_dev_to_stage.json +1 -0
- data/spec/cassettes/databases/create_a_database_backup.json +1 -0
- data/spec/cassettes/databases/create_a_new_database.json +1 -0
- data/spec/cassettes/databases/delete_a_database.json +1 -0
- data/spec/cassettes/databases/get_all_existing_databases.json +1 -0
- data/spec/cassettes/databases/list_all_database_backups.json +1 -0
- data/spec/cassettes/databases/view_database_instance_details.json +1 -0
- data/spec/cassettes/deploy/release_vcs_branch.json +1 -0
- data/spec/cassettes/domains/create_new_domain.json +1 -0
- data/spec/cassettes/domains/delete_a_domain.json +1 -0
- data/spec/cassettes/domains/get_all_existing_domains.json +1 -0
- data/spec/cassettes/domains/list_all_dev_domains.json +1 -0
- data/spec/cassettes/domains/move_from_dev_to_stage.json +1 -0
- data/spec/cassettes/domains/purge_varnish_cache.json +1 -0
- data/spec/cassettes/environments/all_environments.json +1 -0
- data/spec/cassettes/environments/disable_live_development.json +1 -0
- data/spec/cassettes/environments/enable_live_development.json +1 -0
- data/spec/cassettes/files/copy_from_dev_to_stage.json +1 -0
- data/spec/cassettes/servers/all_dev_servers.json +1 -0
- data/spec/cassettes/servers/all_prod_servers.json +1 -0
- data/spec/cassettes/sites/all_sites.json +1 -0
- data/spec/cassettes/ssh/all_sshkeys.json +1 -0
- data/spec/cassettes/svn/all_svnusers.json +1 -0
- data/spec/cassettes/tasks/all_tasks.json +1 -0
- data/spec/databases_spec.rb +78 -0
- data/spec/deploy_spec.rb +12 -0
- data/spec/domains_spec.rb +59 -0
- data/spec/environments_spec.rb +35 -0
- data/spec/files_spec.rb +11 -0
- data/spec/helper.rb +104 -0
- data/spec/servers_spec.rb +59 -0
- data/spec/sites_spec.rb +19 -0
- data/spec/ssh_spec.rb +19 -0
- data/spec/svn_spec.rb +11 -0
- data/spec/tasks_spec.rb +11 -0
- metadata +158 -4
@@ -0,0 +1,474 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'thor/base'
|
3
|
+
|
4
|
+
class Thor
|
5
|
+
class << self
|
6
|
+
# Allows for custom "Command" package naming.
|
7
|
+
#
|
8
|
+
# === Parameters
|
9
|
+
# name<String>
|
10
|
+
# options<Hash>
|
11
|
+
#
|
12
|
+
def package_name(name, options={})
|
13
|
+
@package_name = name.nil? || name == '' ? nil : name
|
14
|
+
end
|
15
|
+
|
16
|
+
# Sets the default command when thor is executed without an explicit command to be called.
|
17
|
+
#
|
18
|
+
# ==== Parameters
|
19
|
+
# meth<Symbol>:: name of the default command
|
20
|
+
#
|
21
|
+
def default_command(meth=nil)
|
22
|
+
@default_command = case meth
|
23
|
+
when :none
|
24
|
+
'help'
|
25
|
+
when nil
|
26
|
+
@default_command || from_superclass(:default_command, 'help')
|
27
|
+
else
|
28
|
+
meth.to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
alias default_task default_command
|
32
|
+
|
33
|
+
# Registers another Thor subclass as a command.
|
34
|
+
#
|
35
|
+
# ==== Parameters
|
36
|
+
# klass<Class>:: Thor subclass to register
|
37
|
+
# command<String>:: Subcommand name to use
|
38
|
+
# usage<String>:: Short usage for the subcommand
|
39
|
+
# description<String>:: Description for the subcommand
|
40
|
+
def register(klass, subcommand_name, usage, description, options={})
|
41
|
+
if klass <= Thor::Group
|
42
|
+
desc usage, description, options
|
43
|
+
define_method(subcommand_name) { |*args| invoke(klass, args) }
|
44
|
+
else
|
45
|
+
desc usage, description, options
|
46
|
+
subcommand subcommand_name, klass
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Defines the usage and the description of the next command.
|
51
|
+
#
|
52
|
+
# ==== Parameters
|
53
|
+
# usage<String>
|
54
|
+
# description<String>
|
55
|
+
# options<String>
|
56
|
+
#
|
57
|
+
def desc(usage, description, options={})
|
58
|
+
if options[:for]
|
59
|
+
command = find_and_refresh_command(options[:for])
|
60
|
+
command.usage = usage if usage
|
61
|
+
command.description = description if description
|
62
|
+
else
|
63
|
+
@usage, @desc, @hide = usage, description, options[:hide] || false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Defines the long description of the next command.
|
68
|
+
#
|
69
|
+
# ==== Parameters
|
70
|
+
# long description<String>
|
71
|
+
#
|
72
|
+
def long_desc(long_description, options={})
|
73
|
+
if options[:for]
|
74
|
+
command = find_and_refresh_command(options[:for])
|
75
|
+
command.long_description = long_description if long_description
|
76
|
+
else
|
77
|
+
@long_desc = long_description
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Maps an input to a command. If you define:
|
82
|
+
#
|
83
|
+
# map "-T" => "list"
|
84
|
+
#
|
85
|
+
# Running:
|
86
|
+
#
|
87
|
+
# thor -T
|
88
|
+
#
|
89
|
+
# Will invoke the list command.
|
90
|
+
#
|
91
|
+
# ==== Parameters
|
92
|
+
# Hash[String|Array => Symbol]:: Maps the string or the strings in the array to the given command.
|
93
|
+
#
|
94
|
+
def map(mappings=nil)
|
95
|
+
@map ||= from_superclass(:map, {})
|
96
|
+
|
97
|
+
if mappings
|
98
|
+
mappings.each do |key, value|
|
99
|
+
if key.respond_to?(:each)
|
100
|
+
key.each {|subkey| @map[subkey] = value}
|
101
|
+
else
|
102
|
+
@map[key] = value
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
@map
|
108
|
+
end
|
109
|
+
|
110
|
+
# Declares the options for the next command to be declared.
|
111
|
+
#
|
112
|
+
# ==== Parameters
|
113
|
+
# Hash[Symbol => Object]:: The hash key is the name of the option and the value
|
114
|
+
# is the type of the option. Can be :string, :array, :hash, :boolean, :numeric
|
115
|
+
# or :required (string). If you give a value, the type of the value is used.
|
116
|
+
#
|
117
|
+
def method_options(options=nil)
|
118
|
+
@method_options ||= {}
|
119
|
+
build_options(options, @method_options) if options
|
120
|
+
@method_options
|
121
|
+
end
|
122
|
+
|
123
|
+
alias options method_options
|
124
|
+
|
125
|
+
# Adds an option to the set of method options. If :for is given as option,
|
126
|
+
# it allows you to change the options from a previous defined command.
|
127
|
+
#
|
128
|
+
# def previous_command
|
129
|
+
# # magic
|
130
|
+
# end
|
131
|
+
#
|
132
|
+
# method_option :foo => :bar, :for => :previous_command
|
133
|
+
#
|
134
|
+
# def next_command
|
135
|
+
# # magic
|
136
|
+
# end
|
137
|
+
#
|
138
|
+
# ==== Parameters
|
139
|
+
# name<Symbol>:: The name of the argument.
|
140
|
+
# options<Hash>:: Described below.
|
141
|
+
#
|
142
|
+
# ==== Options
|
143
|
+
# :desc - Description for the argument.
|
144
|
+
# :required - If the argument is required or not.
|
145
|
+
# :default - Default value for this argument. It cannot be required and have default values.
|
146
|
+
# :aliases - Aliases for this option.
|
147
|
+
# :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
|
148
|
+
# :banner - String to show on usage notes.
|
149
|
+
# :hide - If you want to hide this option from the help.
|
150
|
+
#
|
151
|
+
def method_option(name, options={})
|
152
|
+
scope = if options[:for]
|
153
|
+
find_and_refresh_command(options[:for]).options
|
154
|
+
else
|
155
|
+
method_options
|
156
|
+
end
|
157
|
+
|
158
|
+
build_option(name, options, scope)
|
159
|
+
end
|
160
|
+
alias option method_option
|
161
|
+
|
162
|
+
# Prints help information for the given command.
|
163
|
+
#
|
164
|
+
# ==== Parameters
|
165
|
+
# shell<Thor::Shell>
|
166
|
+
# command_name<String>
|
167
|
+
#
|
168
|
+
def command_help(shell, command_name)
|
169
|
+
meth = normalize_command_name(command_name)
|
170
|
+
command = all_commands[meth]
|
171
|
+
handle_no_command_error(meth) unless command
|
172
|
+
|
173
|
+
shell.say "Usage:"
|
174
|
+
shell.say " #{banner(command)}"
|
175
|
+
shell.say
|
176
|
+
class_options_help(shell, nil => command.options.map { |_, o| o })
|
177
|
+
if command.long_description
|
178
|
+
shell.say "Description:"
|
179
|
+
shell.print_wrapped(command.long_description, :indent => 2)
|
180
|
+
else
|
181
|
+
shell.say command.description
|
182
|
+
end
|
183
|
+
end
|
184
|
+
alias task_help command_help
|
185
|
+
|
186
|
+
# Prints help information for this class.
|
187
|
+
#
|
188
|
+
# ==== Parameters
|
189
|
+
# shell<Thor::Shell>
|
190
|
+
#
|
191
|
+
def help(shell, subcommand = false)
|
192
|
+
list = printable_commands(true, subcommand)
|
193
|
+
Thor::Util.thor_classes_in(self).each do |klass|
|
194
|
+
list += klass.printable_commands(false)
|
195
|
+
end
|
196
|
+
list.sort!{ |a,b| a[0] <=> b[0] }
|
197
|
+
|
198
|
+
if @package_name
|
199
|
+
shell.say "#{@package_name} commands:"
|
200
|
+
else
|
201
|
+
shell.say
|
202
|
+
shell.say "Commands:"
|
203
|
+
end
|
204
|
+
|
205
|
+
shell.print_table(list, :indent => 2, :truncate => true)
|
206
|
+
shell.say
|
207
|
+
class_options_help(shell)
|
208
|
+
end
|
209
|
+
|
210
|
+
# Returns commands ready to be printed.
|
211
|
+
def printable_commands(all = true, subcommand = false)
|
212
|
+
(all ? all_commands : commands).map do |_, command|
|
213
|
+
next if command.hidden?
|
214
|
+
item = []
|
215
|
+
item << banner(command, false, subcommand)
|
216
|
+
item << (command.description ? "# #{command.description.gsub(/\s+/m,' ')}" : "")
|
217
|
+
item
|
218
|
+
end.compact
|
219
|
+
end
|
220
|
+
alias printable_tasks printable_commands
|
221
|
+
|
222
|
+
def subcommands
|
223
|
+
@subcommands ||= from_superclass(:subcommands, [])
|
224
|
+
end
|
225
|
+
alias subtasks subcommands
|
226
|
+
|
227
|
+
def subcommand(subcommand, subcommand_class)
|
228
|
+
self.subcommands << subcommand.to_s
|
229
|
+
subcommand_class.subcommand_help subcommand
|
230
|
+
|
231
|
+
define_method(subcommand) do |*args|
|
232
|
+
args, opts = Thor::Arguments.split(args)
|
233
|
+
invoke subcommand_class, args, opts, :invoked_via_subcommand => true, :class_options => options
|
234
|
+
end
|
235
|
+
end
|
236
|
+
alias subtask subcommand
|
237
|
+
|
238
|
+
# Extend check unknown options to accept a hash of conditions.
|
239
|
+
#
|
240
|
+
# === Parameters
|
241
|
+
# options<Hash>: A hash containing :only and/or :except keys
|
242
|
+
def check_unknown_options!(options={})
|
243
|
+
@check_unknown_options ||= Hash.new
|
244
|
+
options.each do |key, value|
|
245
|
+
if value
|
246
|
+
@check_unknown_options[key] = Array(value)
|
247
|
+
else
|
248
|
+
@check_unknown_options.delete(key)
|
249
|
+
end
|
250
|
+
end
|
251
|
+
@check_unknown_options
|
252
|
+
end
|
253
|
+
|
254
|
+
# Overwrite check_unknown_options? to take subcommands and options into account.
|
255
|
+
def check_unknown_options?(config) #:nodoc:
|
256
|
+
options = check_unknown_options
|
257
|
+
return false unless options
|
258
|
+
|
259
|
+
command = config[:current_command]
|
260
|
+
return true unless command
|
261
|
+
|
262
|
+
name = command.name
|
263
|
+
|
264
|
+
if subcommands.include?(name)
|
265
|
+
false
|
266
|
+
elsif options[:except]
|
267
|
+
!options[:except].include?(name.to_sym)
|
268
|
+
elsif options[:only]
|
269
|
+
options[:only].include?(name.to_sym)
|
270
|
+
else
|
271
|
+
true
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
# Stop parsing of options as soon as an unknown option or a regular
|
276
|
+
# argument is encountered. All remaining arguments are passed to the command.
|
277
|
+
# This is useful if you have a command that can receive arbitrary additional
|
278
|
+
# options, and where those additional options should not be handled by
|
279
|
+
# Thor.
|
280
|
+
#
|
281
|
+
# ==== Example
|
282
|
+
#
|
283
|
+
# To better understand how this is useful, let's consider a command that calls
|
284
|
+
# an external command. A user may want to pass arbitrary options and
|
285
|
+
# arguments to that command. The command itself also accepts some options,
|
286
|
+
# which should be handled by Thor.
|
287
|
+
#
|
288
|
+
# class_option "verbose", :type => :boolean
|
289
|
+
# stop_on_unknown_option! :exec
|
290
|
+
# check_unknown_options! :except => :exec
|
291
|
+
#
|
292
|
+
# desc "exec", "Run a shell command"
|
293
|
+
# def exec(*args)
|
294
|
+
# puts "diagnostic output" if options[:verbose]
|
295
|
+
# Kernel.exec(*args)
|
296
|
+
# end
|
297
|
+
#
|
298
|
+
# Here +exec+ can be called with +--verbose+ to get diagnostic output,
|
299
|
+
# e.g.:
|
300
|
+
#
|
301
|
+
# $ thor exec --verbose echo foo
|
302
|
+
# diagnostic output
|
303
|
+
# foo
|
304
|
+
#
|
305
|
+
# But if +--verbose+ is given after +echo+, it is passed to +echo+ instead:
|
306
|
+
#
|
307
|
+
# $ thor exec echo --verbose foo
|
308
|
+
# --verbose foo
|
309
|
+
#
|
310
|
+
# ==== Parameters
|
311
|
+
# Symbol ...:: A list of commands that should be affected.
|
312
|
+
def stop_on_unknown_option!(*command_names)
|
313
|
+
@stop_on_unknown_option ||= Set.new
|
314
|
+
@stop_on_unknown_option.merge(command_names)
|
315
|
+
end
|
316
|
+
|
317
|
+
def stop_on_unknown_option?(command) #:nodoc:
|
318
|
+
command && !@stop_on_unknown_option.nil? && @stop_on_unknown_option.include?(command.name.to_sym)
|
319
|
+
end
|
320
|
+
|
321
|
+
protected
|
322
|
+
|
323
|
+
# The method responsible for dispatching given the args.
|
324
|
+
def dispatch(meth, given_args, given_opts, config) #:nodoc:
|
325
|
+
# There is an edge case when dispatching from a subcommand.
|
326
|
+
# A problem occurs invoking the default command. This case occurs
|
327
|
+
# when arguments are passed and a default command is defined, and
|
328
|
+
# the first given_args does not match the default command.
|
329
|
+
# Thor use "help" by default so we skip that case.
|
330
|
+
# Note the call to retrieve_command_name. It's called with
|
331
|
+
# given_args.dup since that method calls args.shift. Then lookup
|
332
|
+
# the command normally. If the first item in given_args is not
|
333
|
+
# a command then use the default command. The given_args will be
|
334
|
+
# intact later since dup was used.
|
335
|
+
if config[:invoked_via_subcommand] && given_args.size >= 1 && default_command != "help" && given_args.first != default_command
|
336
|
+
meth ||= retrieve_command_name(given_args.dup)
|
337
|
+
command = all_commands[normalize_command_name(meth)]
|
338
|
+
command ||= all_commands[normalize_command_name(default_command)]
|
339
|
+
else
|
340
|
+
meth ||= retrieve_command_name(given_args)
|
341
|
+
command = all_commands[normalize_command_name(meth)]
|
342
|
+
end
|
343
|
+
|
344
|
+
if command
|
345
|
+
args, opts = Thor::Options.split(given_args)
|
346
|
+
if stop_on_unknown_option?(command) && !args.empty?
|
347
|
+
# given_args starts with a non-option, so we treat everything as
|
348
|
+
# ordinary arguments
|
349
|
+
args.concat opts
|
350
|
+
opts.clear
|
351
|
+
end
|
352
|
+
else
|
353
|
+
args, opts = given_args, nil
|
354
|
+
command = Thor::DynamicCommand.new(meth)
|
355
|
+
end
|
356
|
+
|
357
|
+
opts = given_opts || opts || []
|
358
|
+
config.merge!(:current_command => command, :command_options => command.options)
|
359
|
+
|
360
|
+
instance = new(args, opts, config)
|
361
|
+
yield instance if block_given?
|
362
|
+
args = instance.args
|
363
|
+
trailing = args[Range.new(arguments.size, -1)]
|
364
|
+
instance.invoke_command(command, trailing || [])
|
365
|
+
end
|
366
|
+
|
367
|
+
# The banner for this class. You can customize it if you are invoking the
|
368
|
+
# thor class by another ways which is not the Thor::Runner. It receives
|
369
|
+
# the command that is going to be invoked and a boolean which indicates if
|
370
|
+
# the namespace should be displayed as arguments.
|
371
|
+
#
|
372
|
+
def banner(command, namespace = nil, subcommand = false)
|
373
|
+
"#{basename} #{command.formatted_usage(self, $thor_runner, subcommand)}"
|
374
|
+
end
|
375
|
+
|
376
|
+
def baseclass #:nodoc:
|
377
|
+
Thor
|
378
|
+
end
|
379
|
+
|
380
|
+
def create_command(meth) #:nodoc:
|
381
|
+
if @usage && @desc
|
382
|
+
base_class = @hide ? Thor::HiddenCommand : Thor::Command
|
383
|
+
commands[meth] = base_class.new(meth, @desc, @long_desc, @usage, method_options)
|
384
|
+
@usage, @desc, @long_desc, @method_options, @hide = nil
|
385
|
+
true
|
386
|
+
elsif self.all_commands[meth] || meth == "method_missing"
|
387
|
+
true
|
388
|
+
else
|
389
|
+
puts "[WARNING] Attempted to create command #{meth.inspect} without usage or description. " <<
|
390
|
+
"Call desc if you want this method to be available as command or declare it inside a " <<
|
391
|
+
"no_commands{} block. Invoked from #{caller[1].inspect}."
|
392
|
+
false
|
393
|
+
end
|
394
|
+
end
|
395
|
+
alias create_task create_command
|
396
|
+
|
397
|
+
def initialize_added #:nodoc:
|
398
|
+
class_options.merge!(method_options)
|
399
|
+
@method_options = nil
|
400
|
+
end
|
401
|
+
|
402
|
+
# Retrieve the command name from given args.
|
403
|
+
def retrieve_command_name(args) #:nodoc:
|
404
|
+
meth = args.first.to_s unless args.empty?
|
405
|
+
if meth && (map[meth] || meth !~ /^\-/)
|
406
|
+
args.shift
|
407
|
+
else
|
408
|
+
nil
|
409
|
+
end
|
410
|
+
end
|
411
|
+
alias retrieve_task_name retrieve_command_name
|
412
|
+
|
413
|
+
# receives a (possibly nil) command name and returns a name that is in
|
414
|
+
# the commands hash. In addition to normalizing aliases, this logic
|
415
|
+
# will determine if a shortened command is an unambiguous substring of
|
416
|
+
# a command or alias.
|
417
|
+
#
|
418
|
+
# +normalize_command_name+ also converts names like +animal-prison+
|
419
|
+
# into +animal_prison+.
|
420
|
+
def normalize_command_name(meth) #:nodoc:
|
421
|
+
return default_command.to_s.gsub('-', '_') unless meth
|
422
|
+
|
423
|
+
possibilities = find_command_possibilities(meth)
|
424
|
+
if possibilities.size > 1
|
425
|
+
raise AmbiguousTaskError, "Ambiguous command #{meth} matches [#{possibilities.join(', ')}]"
|
426
|
+
elsif possibilities.size < 1
|
427
|
+
meth = meth || default_command
|
428
|
+
elsif map[meth]
|
429
|
+
meth = map[meth]
|
430
|
+
else
|
431
|
+
meth = possibilities.first
|
432
|
+
end
|
433
|
+
|
434
|
+
meth.to_s.gsub('-','_') # treat foo-bar as foo_bar
|
435
|
+
end
|
436
|
+
alias normalize_task_name normalize_command_name
|
437
|
+
|
438
|
+
# this is the logic that takes the command name passed in by the user
|
439
|
+
# and determines whether it is an unambiguous substrings of a command or
|
440
|
+
# alias name.
|
441
|
+
def find_command_possibilities(meth)
|
442
|
+
len = meth.to_s.length
|
443
|
+
possibilities = all_commands.merge(map).keys.select { |n| meth == n[0, len] }.sort
|
444
|
+
unique_possibilities = possibilities.map { |k| map[k] || k }.uniq
|
445
|
+
|
446
|
+
if possibilities.include?(meth)
|
447
|
+
[meth]
|
448
|
+
elsif unique_possibilities.size == 1
|
449
|
+
unique_possibilities
|
450
|
+
else
|
451
|
+
possibilities
|
452
|
+
end
|
453
|
+
end
|
454
|
+
alias find_task_possibilities find_command_possibilities
|
455
|
+
|
456
|
+
def subcommand_help(cmd)
|
457
|
+
desc "help [COMMAND]", "Describe subcommands or one specific subcommand"
|
458
|
+
class_eval <<-RUBY
|
459
|
+
def help(command = nil, subcommand = true); super; end
|
460
|
+
RUBY
|
461
|
+
end
|
462
|
+
alias subtask_help subcommand_help
|
463
|
+
|
464
|
+
end
|
465
|
+
|
466
|
+
include Thor::Base
|
467
|
+
|
468
|
+
map HELP_MAPPINGS => :help
|
469
|
+
|
470
|
+
desc "help [COMMAND]", "Describe available commands or one specific command"
|
471
|
+
def help(command = nil, subcommand = false)
|
472
|
+
command ? self.class.command_help(shell, command) : self.class.help(shell, subcommand)
|
473
|
+
end
|
474
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'thor/actions'
|
3
|
+
|
4
|
+
describe Thor::Actions::CreateFile do
|
5
|
+
before do
|
6
|
+
::FileUtils.rm_rf(destination_root)
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_file(destination=nil, config={}, options={})
|
10
|
+
@base = MyCounter.new([1, 2], options, { :destination_root => destination_root })
|
11
|
+
allow(@base).to receive(:file_name).and_return('rdoc')
|
12
|
+
|
13
|
+
@action = Thor::Actions::CreateFile.new(@base, destination, "CONFIGURATION",
|
14
|
+
{ :verbose => !@silence }.merge(config))
|
15
|
+
end
|
16
|
+
|
17
|
+
def invoke!
|
18
|
+
capture(:stdout) { @action.invoke! }
|
19
|
+
end
|
20
|
+
|
21
|
+
def revoke!
|
22
|
+
capture(:stdout) { @action.revoke! }
|
23
|
+
end
|
24
|
+
|
25
|
+
def silence!
|
26
|
+
@silence = true
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#invoke!" do
|
30
|
+
it "creates a file" do
|
31
|
+
create_file("doc/config.rb")
|
32
|
+
invoke!
|
33
|
+
expect(File.exists?(File.join(destination_root, "doc/config.rb"))).to be_true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "does not create a file if pretending" do
|
37
|
+
create_file("doc/config.rb", {}, :pretend => true)
|
38
|
+
invoke!
|
39
|
+
expect(File.exists?(File.join(destination_root, "doc/config.rb"))).to be_false
|
40
|
+
end
|
41
|
+
|
42
|
+
it "shows created status to the user" do
|
43
|
+
create_file("doc/config.rb")
|
44
|
+
expect(invoke!).to eq(" create doc/config.rb\n")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "does not show any information if log status is false" do
|
48
|
+
silence!
|
49
|
+
create_file("doc/config.rb")
|
50
|
+
expect(invoke!).to be_empty
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns the given destination" do
|
54
|
+
capture(:stdout) do
|
55
|
+
expect(create_file("doc/config.rb").invoke!).to eq("doc/config.rb")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it "converts encoded instructions" do
|
60
|
+
create_file("doc/%file_name%.rb.tt")
|
61
|
+
invoke!
|
62
|
+
expect(File.exists?(File.join(destination_root, "doc/rdoc.rb.tt"))).to be_true
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "when file exists" do
|
66
|
+
before do
|
67
|
+
create_file("doc/config.rb")
|
68
|
+
invoke!
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "and is identical" do
|
72
|
+
it "shows identical status" do
|
73
|
+
create_file("doc/config.rb")
|
74
|
+
invoke!
|
75
|
+
expect(invoke!).to eq(" identical doc/config.rb\n")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "and is not identical" do
|
80
|
+
before do
|
81
|
+
File.open(File.join(destination_root, 'doc/config.rb'), 'w'){ |f| f.write("FOO = 3") }
|
82
|
+
end
|
83
|
+
|
84
|
+
it "shows forced status to the user if force is given" do
|
85
|
+
expect(create_file("doc/config.rb", {}, :force => true)).not_to be_identical
|
86
|
+
expect(invoke!).to eq(" force doc/config.rb\n")
|
87
|
+
end
|
88
|
+
|
89
|
+
it "shows skipped status to the user if skip is given" do
|
90
|
+
expect(create_file("doc/config.rb", {}, :skip => true)).not_to be_identical
|
91
|
+
expect(invoke!).to eq(" skip doc/config.rb\n")
|
92
|
+
end
|
93
|
+
|
94
|
+
it "shows forced status to the user if force is configured" do
|
95
|
+
expect(create_file("doc/config.rb", :force => true)).not_to be_identical
|
96
|
+
expect(invoke!).to eq(" force doc/config.rb\n")
|
97
|
+
end
|
98
|
+
|
99
|
+
it "shows skipped status to the user if skip is configured" do
|
100
|
+
expect(create_file("doc/config.rb", :skip => true)).not_to be_identical
|
101
|
+
expect(invoke!).to eq(" skip doc/config.rb\n")
|
102
|
+
end
|
103
|
+
|
104
|
+
it "shows conflict status to the user" do
|
105
|
+
expect(create_file("doc/config.rb")).not_to be_identical
|
106
|
+
expect($stdin).to receive(:gets).and_return('s')
|
107
|
+
file = File.join(destination_root, 'doc/config.rb')
|
108
|
+
|
109
|
+
content = invoke!
|
110
|
+
expect(content).to match(/conflict doc\/config\.rb/)
|
111
|
+
expect(content).to match(/Overwrite #{file}\? \(enter "h" for help\) \[Ynaqdh\]/)
|
112
|
+
expect(content).to match(/skip doc\/config\.rb/)
|
113
|
+
end
|
114
|
+
|
115
|
+
it "creates the file if the file collision menu returns true" do
|
116
|
+
create_file("doc/config.rb")
|
117
|
+
expect($stdin).to receive(:gets).and_return('y')
|
118
|
+
expect(invoke!).to match(/force doc\/config\.rb/)
|
119
|
+
end
|
120
|
+
|
121
|
+
it "skips the file if the file collision menu returns false" do
|
122
|
+
create_file("doc/config.rb")
|
123
|
+
expect($stdin).to receive(:gets).and_return('n')
|
124
|
+
expect(invoke!).to match(/skip doc\/config\.rb/)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "executes the block given to show file content" do
|
128
|
+
create_file("doc/config.rb")
|
129
|
+
expect($stdin).to receive(:gets).and_return('d')
|
130
|
+
expect($stdin).to receive(:gets).and_return('n')
|
131
|
+
expect(@base.shell).to receive(:system).with(/diff -u/)
|
132
|
+
invoke!
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "#revoke!" do
|
139
|
+
it "removes the destination file" do
|
140
|
+
create_file("doc/config.rb")
|
141
|
+
invoke!
|
142
|
+
revoke!
|
143
|
+
expect(File.exists?(@action.destination)).to be_false
|
144
|
+
end
|
145
|
+
|
146
|
+
it "does not raise an error if the file does not exist" do
|
147
|
+
create_file("doc/config.rb")
|
148
|
+
revoke!
|
149
|
+
expect(File.exists?(@action.destination)).to be_false
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe "#exists?" do
|
154
|
+
it "returns true if the destination file exists" do
|
155
|
+
create_file("doc/config.rb")
|
156
|
+
expect(@action.exists?).to be_false
|
157
|
+
invoke!
|
158
|
+
expect(@action.exists?).to be_true
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "#identical?" do
|
163
|
+
it "returns true if the destination file exists and is identical" do
|
164
|
+
create_file("doc/config.rb")
|
165
|
+
expect(@action.identical?).to be_false
|
166
|
+
invoke!
|
167
|
+
expect(@action.identical?).to be_true
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|