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,422 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'io/console'
|
3
|
+
|
4
|
+
class Thor
|
5
|
+
module Shell
|
6
|
+
class Basic
|
7
|
+
attr_accessor :base
|
8
|
+
attr_reader :padding
|
9
|
+
|
10
|
+
# Initialize base, mute and padding to nil.
|
11
|
+
#
|
12
|
+
def initialize #:nodoc:
|
13
|
+
@base, @mute, @padding = nil, false, 0
|
14
|
+
end
|
15
|
+
|
16
|
+
# Mute everything that's inside given block
|
17
|
+
#
|
18
|
+
def mute
|
19
|
+
@mute = true
|
20
|
+
yield
|
21
|
+
ensure
|
22
|
+
@mute = false
|
23
|
+
end
|
24
|
+
|
25
|
+
# Check if base is muted
|
26
|
+
#
|
27
|
+
def mute?
|
28
|
+
@mute
|
29
|
+
end
|
30
|
+
|
31
|
+
# Sets the output padding, not allowing less than zero values.
|
32
|
+
#
|
33
|
+
def padding=(value)
|
34
|
+
@padding = [0, value].max
|
35
|
+
end
|
36
|
+
|
37
|
+
# Asks something to the user and receives a response.
|
38
|
+
#
|
39
|
+
# If asked to limit the correct responses, you can pass in an
|
40
|
+
# array of acceptable answers. If one of those is not supplied,
|
41
|
+
# they will be shown a message stating that one of those answers
|
42
|
+
# must be given and re-asked the question.
|
43
|
+
#
|
44
|
+
# If asking for sensitive information, the :echo option can be set
|
45
|
+
# to false to mask user input from $stdin.
|
46
|
+
#
|
47
|
+
# ==== Example
|
48
|
+
# ask("What is your name?")
|
49
|
+
#
|
50
|
+
# ask("What is your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"])
|
51
|
+
#
|
52
|
+
# ask("What is your password?", :echo => false)
|
53
|
+
#
|
54
|
+
def ask(statement, *args)
|
55
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
56
|
+
color = args.first
|
57
|
+
|
58
|
+
if options[:limited_to]
|
59
|
+
ask_filtered(statement, color, options)
|
60
|
+
else
|
61
|
+
ask_simply(statement, color, options)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Say (print) something to the user. If the sentence ends with a whitespace
|
66
|
+
# or tab character, a new line is not appended (print + flush). Otherwise
|
67
|
+
# are passed straight to puts (behavior got from Highline).
|
68
|
+
#
|
69
|
+
# ==== Example
|
70
|
+
# say("I know you knew that.")
|
71
|
+
#
|
72
|
+
def say(message="", color=nil, force_new_line=(message.to_s !~ /( |\t)\Z/))
|
73
|
+
message = message.to_s
|
74
|
+
message = set_color(message, *color) if color && can_display_colors?
|
75
|
+
|
76
|
+
buffer = " " * padding
|
77
|
+
buffer << message
|
78
|
+
buffer << "\n" if force_new_line && !message.end_with?("\n")
|
79
|
+
|
80
|
+
stdout.print(buffer)
|
81
|
+
stdout.flush
|
82
|
+
end
|
83
|
+
|
84
|
+
# Say a status with the given color and appends the message. Since this
|
85
|
+
# method is used frequently by actions, it allows nil or false to be given
|
86
|
+
# in log_status, avoiding the message from being shown. If a Symbol is
|
87
|
+
# given in log_status, it's used as the color.
|
88
|
+
#
|
89
|
+
def say_status(status, message, log_status=true)
|
90
|
+
return if quiet? || log_status == false
|
91
|
+
spaces = " " * (padding + 1)
|
92
|
+
color = log_status.is_a?(Symbol) ? log_status : :green
|
93
|
+
|
94
|
+
status = status.to_s.rjust(12)
|
95
|
+
status = set_color status, color, true if color
|
96
|
+
|
97
|
+
buffer = "#{status}#{spaces}#{message}"
|
98
|
+
buffer << "\n" unless buffer.end_with?("\n")
|
99
|
+
|
100
|
+
stdout.print(buffer)
|
101
|
+
stdout.flush
|
102
|
+
end
|
103
|
+
|
104
|
+
# Make a question the to user and returns true if the user replies "y" or
|
105
|
+
# "yes".
|
106
|
+
#
|
107
|
+
def yes?(statement, color=nil)
|
108
|
+
!!(ask(statement, color) =~ is?(:yes))
|
109
|
+
end
|
110
|
+
|
111
|
+
# Make a question the to user and returns true if the user replies "n" or
|
112
|
+
# "no".
|
113
|
+
#
|
114
|
+
def no?(statement, color=nil)
|
115
|
+
!yes?(statement, color)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Prints values in columns
|
119
|
+
#
|
120
|
+
# ==== Parameters
|
121
|
+
# Array[String, String, ...]
|
122
|
+
#
|
123
|
+
def print_in_columns(array)
|
124
|
+
return if array.empty?
|
125
|
+
colwidth = (array.map{|el| el.to_s.size}.max || 0) + 2
|
126
|
+
array.each_with_index do |value, index|
|
127
|
+
# Don't output trailing spaces when printing the last column
|
128
|
+
if ((((index + 1) % (terminal_width / colwidth))).zero? && !index.zero?) || index + 1 == array.length
|
129
|
+
stdout.puts value
|
130
|
+
else
|
131
|
+
stdout.printf("%-#{colwidth}s", value)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Prints a table.
|
137
|
+
#
|
138
|
+
# ==== Parameters
|
139
|
+
# Array[Array[String, String, ...]]
|
140
|
+
#
|
141
|
+
# ==== Options
|
142
|
+
# indent<Integer>:: Indent the first column by indent value.
|
143
|
+
# colwidth<Integer>:: Force the first column to colwidth spaces wide.
|
144
|
+
#
|
145
|
+
def print_table(array, options={})
|
146
|
+
return if array.empty?
|
147
|
+
|
148
|
+
formats, indent, colwidth = [], options[:indent].to_i, options[:colwidth]
|
149
|
+
options[:truncate] = terminal_width if options[:truncate] == true
|
150
|
+
|
151
|
+
formats << "%-#{colwidth + 2}s" if colwidth
|
152
|
+
start = colwidth ? 1 : 0
|
153
|
+
|
154
|
+
colcount = array.max{|a,b| a.size <=> b.size }.size
|
155
|
+
|
156
|
+
maximas = []
|
157
|
+
|
158
|
+
start.upto(colcount - 1) do |index|
|
159
|
+
maxima = array.map {|row| row[index] ? row[index].to_s.size : 0 }.max
|
160
|
+
maximas << maxima
|
161
|
+
if index == colcount - 1
|
162
|
+
# Don't output 2 trailing spaces when printing the last column
|
163
|
+
formats << "%-s"
|
164
|
+
else
|
165
|
+
formats << "%-#{maxima + 2}s"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
formats[0] = formats[0].insert(0, " " * indent)
|
170
|
+
formats << "%s"
|
171
|
+
|
172
|
+
array.each do |row|
|
173
|
+
sentence = ""
|
174
|
+
|
175
|
+
row.each_with_index do |column, index|
|
176
|
+
maxima = maximas[index]
|
177
|
+
|
178
|
+
if column.is_a?(Numeric)
|
179
|
+
if index == row.size - 1
|
180
|
+
# Don't output 2 trailing spaces when printing the last column
|
181
|
+
f = "%#{maxima}s"
|
182
|
+
else
|
183
|
+
f = "%#{maxima}s "
|
184
|
+
end
|
185
|
+
else
|
186
|
+
f = formats[index]
|
187
|
+
end
|
188
|
+
sentence << f % column.to_s
|
189
|
+
end
|
190
|
+
|
191
|
+
sentence = truncate(sentence, options[:truncate]) if options[:truncate]
|
192
|
+
stdout.puts sentence
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
# Prints a long string, word-wrapping the text to the current width of the
|
197
|
+
# terminal display. Ideal for printing heredocs.
|
198
|
+
#
|
199
|
+
# ==== Parameters
|
200
|
+
# String
|
201
|
+
#
|
202
|
+
# ==== Options
|
203
|
+
# indent<Integer>:: Indent each line of the printed paragraph by indent value.
|
204
|
+
#
|
205
|
+
def print_wrapped(message, options={})
|
206
|
+
indent = options[:indent] || 0
|
207
|
+
width = terminal_width - indent
|
208
|
+
paras = message.split("\n\n")
|
209
|
+
|
210
|
+
paras.map! do |unwrapped|
|
211
|
+
unwrapped.strip.gsub(/\n/, " ").squeeze(" ").
|
212
|
+
gsub(/.{1,#{width}}(?:\s|\Z)/){($& + 5.chr).
|
213
|
+
gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
|
214
|
+
end
|
215
|
+
|
216
|
+
paras.each do |para|
|
217
|
+
para.split("\n").each do |line|
|
218
|
+
stdout.puts line.insert(0, " " * indent)
|
219
|
+
end
|
220
|
+
stdout.puts unless para == paras.last
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
# Deals with file collision and returns true if the file should be
|
225
|
+
# overwritten and false otherwise. If a block is given, it uses the block
|
226
|
+
# response as the content for the diff.
|
227
|
+
#
|
228
|
+
# ==== Parameters
|
229
|
+
# destination<String>:: the destination file to solve conflicts
|
230
|
+
# block<Proc>:: an optional block that returns the value to be used in diff
|
231
|
+
#
|
232
|
+
def file_collision(destination)
|
233
|
+
return true if @always_force
|
234
|
+
options = block_given? ? "[Ynaqdh]" : "[Ynaqh]"
|
235
|
+
|
236
|
+
while true
|
237
|
+
answer = ask %[Overwrite #{destination}? (enter "h" for help) #{options}]
|
238
|
+
|
239
|
+
case answer
|
240
|
+
when is?(:yes), is?(:force), ""
|
241
|
+
return true
|
242
|
+
when is?(:no), is?(:skip)
|
243
|
+
return false
|
244
|
+
when is?(:always)
|
245
|
+
return @always_force = true
|
246
|
+
when is?(:quit)
|
247
|
+
say 'Aborting...'
|
248
|
+
raise SystemExit
|
249
|
+
when is?(:diff)
|
250
|
+
show_diff(destination, yield) if block_given?
|
251
|
+
say 'Retrying...'
|
252
|
+
else
|
253
|
+
say file_collision_help
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
# This code was copied from Rake, available under MIT-LICENSE
|
259
|
+
# Copyright (c) 2003, 2004 Jim Weirich
|
260
|
+
def terminal_width
|
261
|
+
if ENV['THOR_COLUMNS']
|
262
|
+
result = ENV['THOR_COLUMNS'].to_i
|
263
|
+
else
|
264
|
+
result = unix? ? dynamic_width : 80
|
265
|
+
end
|
266
|
+
(result < 10) ? 80 : result
|
267
|
+
rescue
|
268
|
+
80
|
269
|
+
end
|
270
|
+
|
271
|
+
# Called if something goes wrong during the execution. This is used by Thor
|
272
|
+
# internally and should not be used inside your scripts. If something went
|
273
|
+
# wrong, you can always raise an exception. If you raise a Thor::Error, it
|
274
|
+
# will be rescued and wrapped in the method below.
|
275
|
+
#
|
276
|
+
def error(statement)
|
277
|
+
stderr.puts statement
|
278
|
+
end
|
279
|
+
|
280
|
+
# Apply color to the given string with optional bold. Disabled in the
|
281
|
+
# Thor::Shell::Basic class.
|
282
|
+
#
|
283
|
+
def set_color(string, *args) #:nodoc:
|
284
|
+
string
|
285
|
+
end
|
286
|
+
|
287
|
+
protected
|
288
|
+
|
289
|
+
def can_display_colors?
|
290
|
+
false
|
291
|
+
end
|
292
|
+
|
293
|
+
def lookup_color(color)
|
294
|
+
return color unless color.is_a?(Symbol)
|
295
|
+
self.class.const_get(color.to_s.upcase)
|
296
|
+
end
|
297
|
+
|
298
|
+
def stdout
|
299
|
+
$stdout
|
300
|
+
end
|
301
|
+
|
302
|
+
def stdin
|
303
|
+
$stdin
|
304
|
+
end
|
305
|
+
|
306
|
+
def stderr
|
307
|
+
$stderr
|
308
|
+
end
|
309
|
+
|
310
|
+
def is?(value) #:nodoc:
|
311
|
+
value = value.to_s
|
312
|
+
|
313
|
+
if value.size == 1
|
314
|
+
/\A#{value}\z/i
|
315
|
+
else
|
316
|
+
/\A(#{value}|#{value[0,1]})\z/i
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
def file_collision_help #:nodoc:
|
321
|
+
<<HELP
|
322
|
+
Y - yes, overwrite
|
323
|
+
n - no, do not overwrite
|
324
|
+
a - all, overwrite this and all others
|
325
|
+
q - quit, abort
|
326
|
+
d - diff, show the differences between the old and the new
|
327
|
+
h - help, show this help
|
328
|
+
HELP
|
329
|
+
end
|
330
|
+
|
331
|
+
def show_diff(destination, content) #:nodoc:
|
332
|
+
diff_cmd = ENV['THOR_DIFF'] || ENV['RAILS_DIFF'] || 'diff -u'
|
333
|
+
|
334
|
+
Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
|
335
|
+
temp.write content
|
336
|
+
temp.rewind
|
337
|
+
system %(#{diff_cmd} "#{destination}" "#{temp.path}")
|
338
|
+
end
|
339
|
+
end
|
340
|
+
|
341
|
+
def quiet? #:nodoc:
|
342
|
+
mute? || (base && base.options[:quiet])
|
343
|
+
end
|
344
|
+
|
345
|
+
# Calculate the dynamic width of the terminal
|
346
|
+
def dynamic_width
|
347
|
+
@dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
|
348
|
+
end
|
349
|
+
|
350
|
+
def dynamic_width_stty
|
351
|
+
%x{stty size 2>/dev/null}.split[1].to_i
|
352
|
+
end
|
353
|
+
|
354
|
+
def dynamic_width_tput
|
355
|
+
%x{tput cols 2>/dev/null}.to_i
|
356
|
+
end
|
357
|
+
|
358
|
+
def unix?
|
359
|
+
RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
|
360
|
+
end
|
361
|
+
|
362
|
+
def truncate(string, width)
|
363
|
+
as_unicode do
|
364
|
+
chars = string.chars.to_a
|
365
|
+
if chars.length <= width
|
366
|
+
chars.join
|
367
|
+
else
|
368
|
+
( chars[0, width-3].join ) + "..."
|
369
|
+
end
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
if "".respond_to?(:encode)
|
374
|
+
def as_unicode
|
375
|
+
yield
|
376
|
+
end
|
377
|
+
else
|
378
|
+
def as_unicode
|
379
|
+
old, $KCODE = $KCODE, "U"
|
380
|
+
yield
|
381
|
+
ensure
|
382
|
+
$KCODE = old
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
def ask_simply(statement, color, options)
|
387
|
+
default = options[:default]
|
388
|
+
message = [statement, ("(#{default})" if default), nil].uniq.join(" ")
|
389
|
+
say(message, color)
|
390
|
+
|
391
|
+
result = if options[:echo] == false
|
392
|
+
stdin.noecho(&:gets)
|
393
|
+
else
|
394
|
+
stdin.gets
|
395
|
+
end
|
396
|
+
|
397
|
+
return unless result
|
398
|
+
|
399
|
+
result.strip!
|
400
|
+
|
401
|
+
if default && result == ""
|
402
|
+
default
|
403
|
+
else
|
404
|
+
result
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
def ask_filtered(statement, color, options)
|
409
|
+
answer_set = options[:limited_to]
|
410
|
+
correct_answer = nil
|
411
|
+
until correct_answer
|
412
|
+
answers = answer_set.join(", ")
|
413
|
+
answer = ask_simply("#{statement} [#{answers}]", color, options)
|
414
|
+
correct_answer = answer_set.include?(answer) ? answer : nil
|
415
|
+
say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer
|
416
|
+
end
|
417
|
+
correct_answer
|
418
|
+
end
|
419
|
+
|
420
|
+
end
|
421
|
+
end
|
422
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
require 'thor/shell/basic'
|
2
|
+
|
3
|
+
class Thor
|
4
|
+
module Shell
|
5
|
+
# Inherit from Thor::Shell::Basic and add set_color behavior. Check
|
6
|
+
# Thor::Shell::Basic to see all available methods.
|
7
|
+
#
|
8
|
+
class Color < Basic
|
9
|
+
# Embed in a String to clear all previous ANSI sequences.
|
10
|
+
CLEAR = "\e[0m"
|
11
|
+
# The start of an ANSI bold sequence.
|
12
|
+
BOLD = "\e[1m"
|
13
|
+
|
14
|
+
# Set the terminal's foreground ANSI color to black.
|
15
|
+
BLACK = "\e[30m"
|
16
|
+
# Set the terminal's foreground ANSI color to red.
|
17
|
+
RED = "\e[31m"
|
18
|
+
# Set the terminal's foreground ANSI color to green.
|
19
|
+
GREEN = "\e[32m"
|
20
|
+
# Set the terminal's foreground ANSI color to yellow.
|
21
|
+
YELLOW = "\e[33m"
|
22
|
+
# Set the terminal's foreground ANSI color to blue.
|
23
|
+
BLUE = "\e[34m"
|
24
|
+
# Set the terminal's foreground ANSI color to magenta.
|
25
|
+
MAGENTA = "\e[35m"
|
26
|
+
# Set the terminal's foreground ANSI color to cyan.
|
27
|
+
CYAN = "\e[36m"
|
28
|
+
# Set the terminal's foreground ANSI color to white.
|
29
|
+
WHITE = "\e[37m"
|
30
|
+
|
31
|
+
# Set the terminal's background ANSI color to black.
|
32
|
+
ON_BLACK = "\e[40m"
|
33
|
+
# Set the terminal's background ANSI color to red.
|
34
|
+
ON_RED = "\e[41m"
|
35
|
+
# Set the terminal's background ANSI color to green.
|
36
|
+
ON_GREEN = "\e[42m"
|
37
|
+
# Set the terminal's background ANSI color to yellow.
|
38
|
+
ON_YELLOW = "\e[43m"
|
39
|
+
# Set the terminal's background ANSI color to blue.
|
40
|
+
ON_BLUE = "\e[44m"
|
41
|
+
# Set the terminal's background ANSI color to magenta.
|
42
|
+
ON_MAGENTA = "\e[45m"
|
43
|
+
# Set the terminal's background ANSI color to cyan.
|
44
|
+
ON_CYAN = "\e[46m"
|
45
|
+
# Set the terminal's background ANSI color to white.
|
46
|
+
ON_WHITE = "\e[47m"
|
47
|
+
|
48
|
+
# Set color by using a string or one of the defined constants. If a third
|
49
|
+
# option is set to true, it also adds bold to the string. This is based
|
50
|
+
# on Highline implementation and it automatically appends CLEAR to the end
|
51
|
+
# of the returned String.
|
52
|
+
#
|
53
|
+
# Pass foreground, background and bold options to this method as
|
54
|
+
# symbols.
|
55
|
+
#
|
56
|
+
# Example:
|
57
|
+
#
|
58
|
+
# set_color "Hi!", :red, :on_white, :bold
|
59
|
+
#
|
60
|
+
# The available colors are:
|
61
|
+
#
|
62
|
+
# :bold
|
63
|
+
# :black
|
64
|
+
# :red
|
65
|
+
# :green
|
66
|
+
# :yellow
|
67
|
+
# :blue
|
68
|
+
# :magenta
|
69
|
+
# :cyan
|
70
|
+
# :white
|
71
|
+
# :on_black
|
72
|
+
# :on_red
|
73
|
+
# :on_green
|
74
|
+
# :on_yellow
|
75
|
+
# :on_blue
|
76
|
+
# :on_magenta
|
77
|
+
# :on_cyan
|
78
|
+
# :on_white
|
79
|
+
def set_color(string, *colors)
|
80
|
+
if colors.all? { |color| color.is_a?(Symbol) || color.is_a?(String) }
|
81
|
+
ansi_colors = colors.map { |color| lookup_color(color) }
|
82
|
+
"#{ansi_colors.join}#{string}#{CLEAR}"
|
83
|
+
else
|
84
|
+
# The old API was `set_color(color, bold=boolean)`. We
|
85
|
+
# continue to support the old API because you should never
|
86
|
+
# break old APIs unnecessarily :P
|
87
|
+
foreground, bold = colors
|
88
|
+
foreground = self.class.const_get(foreground.to_s.upcase) if foreground.is_a?(Symbol)
|
89
|
+
|
90
|
+
bold = bold ? BOLD : ""
|
91
|
+
"#{bold}#{foreground}#{string}#{CLEAR}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
protected
|
96
|
+
|
97
|
+
def can_display_colors?
|
98
|
+
stdout.tty?
|
99
|
+
end
|
100
|
+
|
101
|
+
# Overwrite show_diff to show diff with colors if Diff::LCS is
|
102
|
+
# available.
|
103
|
+
#
|
104
|
+
def show_diff(destination, content) #:nodoc:
|
105
|
+
if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
|
106
|
+
actual = File.binread(destination).to_s.split("\n")
|
107
|
+
content = content.to_s.split("\n")
|
108
|
+
|
109
|
+
Diff::LCS.sdiff(actual, content).each do |diff|
|
110
|
+
output_diff_line(diff)
|
111
|
+
end
|
112
|
+
else
|
113
|
+
super
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def output_diff_line(diff) #:nodoc:
|
118
|
+
case diff.action
|
119
|
+
when '-'
|
120
|
+
say "- #{diff.old_element.chomp}", :red, true
|
121
|
+
when '+'
|
122
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
123
|
+
when '!'
|
124
|
+
say "- #{diff.old_element.chomp}", :red, true
|
125
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
126
|
+
else
|
127
|
+
say " #{diff.old_element.chomp}", nil, true
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
|
132
|
+
# for diff.
|
133
|
+
#
|
134
|
+
def diff_lcs_loaded? #:nodoc:
|
135
|
+
return true if defined?(Diff::LCS)
|
136
|
+
return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
|
137
|
+
|
138
|
+
@diff_lcs_loaded = begin
|
139
|
+
require 'diff/lcs'
|
140
|
+
true
|
141
|
+
rescue LoadError
|
142
|
+
false
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
require 'thor/shell/basic'
|
2
|
+
|
3
|
+
class Thor
|
4
|
+
module Shell
|
5
|
+
# Inherit from Thor::Shell::Basic and add set_color behavior. Check
|
6
|
+
# Thor::Shell::Basic to see all available methods.
|
7
|
+
#
|
8
|
+
class HTML < Basic
|
9
|
+
# The start of an HTML bold sequence.
|
10
|
+
BOLD = "font-weight: bold"
|
11
|
+
|
12
|
+
# Set the terminal's foreground HTML color to black.
|
13
|
+
BLACK = 'color: black'
|
14
|
+
# Set the terminal's foreground HTML color to red.
|
15
|
+
RED = 'color: red'
|
16
|
+
# Set the terminal's foreground HTML color to green.
|
17
|
+
GREEN = 'color: green'
|
18
|
+
# Set the terminal's foreground HTML color to yellow.
|
19
|
+
YELLOW = 'color: yellow'
|
20
|
+
# Set the terminal's foreground HTML color to blue.
|
21
|
+
BLUE = 'color: blue'
|
22
|
+
# Set the terminal's foreground HTML color to magenta.
|
23
|
+
MAGENTA = 'color: magenta'
|
24
|
+
# Set the terminal's foreground HTML color to cyan.
|
25
|
+
CYAN = 'color: cyan'
|
26
|
+
# Set the terminal's foreground HTML color to white.
|
27
|
+
WHITE = 'color: white'
|
28
|
+
|
29
|
+
# Set the terminal's background HTML color to black.
|
30
|
+
ON_BLACK = 'background-color: black'
|
31
|
+
# Set the terminal's background HTML color to red.
|
32
|
+
ON_RED = 'background-color: red'
|
33
|
+
# Set the terminal's background HTML color to green.
|
34
|
+
ON_GREEN = 'background-color: green'
|
35
|
+
# Set the terminal's background HTML color to yellow.
|
36
|
+
ON_YELLOW = 'background-color: yellow'
|
37
|
+
# Set the terminal's background HTML color to blue.
|
38
|
+
ON_BLUE = 'background-color: blue'
|
39
|
+
# Set the terminal's background HTML color to magenta.
|
40
|
+
ON_MAGENTA = 'background-color: magenta'
|
41
|
+
# Set the terminal's background HTML color to cyan.
|
42
|
+
ON_CYAN = 'background-color: cyan'
|
43
|
+
# Set the terminal's background HTML color to white.
|
44
|
+
ON_WHITE = 'background-color: white'
|
45
|
+
|
46
|
+
# Set color by using a string or one of the defined constants. If a third
|
47
|
+
# option is set to true, it also adds bold to the string. This is based
|
48
|
+
# on Highline implementation and it automatically appends CLEAR to the end
|
49
|
+
# of the returned String.
|
50
|
+
#
|
51
|
+
def set_color(string, *colors)
|
52
|
+
if colors.all? { |color| color.is_a?(Symbol) || color.is_a?(String) }
|
53
|
+
html_colors = colors.map { |color| lookup_color(color) }
|
54
|
+
"<span style=\"#{html_colors.join("; ")};\">#{string}</span>"
|
55
|
+
else
|
56
|
+
color, bold = colors
|
57
|
+
html_color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
|
58
|
+
styles = [html_color]
|
59
|
+
styles << BOLD if bold
|
60
|
+
"<span style=\"#{styles.join("; ")};\">#{string}</span>"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Ask something to the user and receives a response.
|
65
|
+
#
|
66
|
+
# ==== Example
|
67
|
+
# ask("What is your name?")
|
68
|
+
#
|
69
|
+
# TODO: Implement #ask for Thor::Shell::HTML
|
70
|
+
def ask(statement, color=nil)
|
71
|
+
raise NotImplementedError, "Implement #ask for Thor::Shell::HTML"
|
72
|
+
end
|
73
|
+
|
74
|
+
protected
|
75
|
+
|
76
|
+
def can_display_colors?
|
77
|
+
true
|
78
|
+
end
|
79
|
+
|
80
|
+
# Overwrite show_diff to show diff with colors if Diff::LCS is
|
81
|
+
# available.
|
82
|
+
#
|
83
|
+
def show_diff(destination, content) #:nodoc:
|
84
|
+
if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
|
85
|
+
actual = File.binread(destination).to_s.split("\n")
|
86
|
+
content = content.to_s.split("\n")
|
87
|
+
|
88
|
+
Diff::LCS.sdiff(actual, content).each do |diff|
|
89
|
+
output_diff_line(diff)
|
90
|
+
end
|
91
|
+
else
|
92
|
+
super
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def output_diff_line(diff) #:nodoc:
|
97
|
+
case diff.action
|
98
|
+
when '-'
|
99
|
+
say "- #{diff.old_element.chomp}", :red, true
|
100
|
+
when '+'
|
101
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
102
|
+
when '!'
|
103
|
+
say "- #{diff.old_element.chomp}", :red, true
|
104
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
105
|
+
else
|
106
|
+
say " #{diff.old_element.chomp}", nil, true
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
|
111
|
+
# for diff.
|
112
|
+
#
|
113
|
+
def diff_lcs_loaded? #:nodoc:
|
114
|
+
return true if defined?(Diff::LCS)
|
115
|
+
return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
|
116
|
+
|
117
|
+
@diff_lcs_loaded = begin
|
118
|
+
require 'diff/lcs'
|
119
|
+
true
|
120
|
+
rescue LoadError
|
121
|
+
false
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|