engineyard 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/lib/engineyard/cli.rb +71 -44
  2. data/lib/engineyard/cli/recipes.rb +17 -17
  3. data/lib/engineyard/cli/ui.rb +4 -8
  4. data/lib/engineyard/cli/web.rb +14 -15
  5. data/lib/engineyard/model/environment.rb +1 -1
  6. data/lib/engineyard/model/instance.rb +16 -4
  7. data/lib/engineyard/thor.rb +24 -18
  8. data/lib/engineyard/vendor/thor.rb +270 -0
  9. data/lib/engineyard/vendor/thor/actions.rb +297 -0
  10. data/lib/engineyard/vendor/thor/actions/create_file.rb +105 -0
  11. data/lib/engineyard/vendor/thor/actions/directory.rb +93 -0
  12. data/lib/engineyard/vendor/thor/actions/empty_directory.rb +134 -0
  13. data/lib/engineyard/vendor/thor/actions/file_manipulation.rb +229 -0
  14. data/lib/engineyard/vendor/thor/actions/inject_into_file.rb +104 -0
  15. data/lib/engineyard/vendor/thor/base.rb +540 -0
  16. data/lib/engineyard/vendor/thor/core_ext/file_binary_read.rb +9 -0
  17. data/lib/engineyard/vendor/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  18. data/lib/engineyard/vendor/thor/core_ext/ordered_hash.rb +100 -0
  19. data/lib/engineyard/vendor/thor/error.rb +30 -0
  20. data/lib/engineyard/vendor/thor/group.rb +271 -0
  21. data/lib/engineyard/vendor/thor/invocation.rb +180 -0
  22. data/lib/engineyard/vendor/thor/parser.rb +4 -0
  23. data/lib/engineyard/vendor/thor/parser/argument.rb +67 -0
  24. data/lib/engineyard/vendor/thor/parser/arguments.rb +161 -0
  25. data/lib/engineyard/vendor/thor/parser/option.rb +128 -0
  26. data/lib/engineyard/vendor/thor/parser/options.rb +164 -0
  27. data/lib/engineyard/vendor/thor/rake_compat.rb +66 -0
  28. data/lib/engineyard/vendor/thor/runner.rb +314 -0
  29. data/lib/engineyard/vendor/thor/shell.rb +83 -0
  30. data/lib/engineyard/vendor/thor/shell/basic.rb +268 -0
  31. data/lib/engineyard/vendor/thor/shell/color.rb +108 -0
  32. data/lib/engineyard/vendor/thor/task.rb +102 -0
  33. data/lib/engineyard/vendor/thor/util.rb +229 -0
  34. data/lib/engineyard/vendor/thor/version.rb +3 -0
  35. data/lib/engineyard/version.rb +1 -1
  36. data/spec/ey/deploy_spec.rb +24 -5
  37. data/spec/ey/ey_spec.rb +1 -1
  38. data/spec/ey/rollback_spec.rb +7 -0
  39. data/spec/spec_helper.rb +13 -0
  40. data/spec/support/git_repo.rb +10 -7
  41. data/spec/support/helpers.rb +2 -2
  42. metadata +30 -4
  43. data/lib/engineyard/cli/thor_fixes.rb +0 -26
@@ -0,0 +1,83 @@
1
+ require 'rbconfig'
2
+ require 'thor/shell/color'
3
+
4
+ class Thor
5
+ module Base
6
+ # Returns the shell used in all Thor classes. If you are in a Unix platform
7
+ # it will use a colored log, otherwise it will use a basic one without color.
8
+ #
9
+ def self.shell
10
+ @shell ||= if Config::CONFIG['host_os'] =~ /mswin|mingw/
11
+ Thor::Shell::Basic
12
+ else
13
+ Thor::Shell::Color
14
+ end
15
+ end
16
+
17
+ # Sets the shell used in all Thor classes.
18
+ #
19
+ def self.shell=(klass)
20
+ @shell = klass
21
+ end
22
+ end
23
+
24
+ module Shell
25
+ SHELL_DELEGATED_METHODS = [:ask, :yes?, :no?, :say, :say_status, :print_table]
26
+
27
+ # Add shell to initialize config values.
28
+ #
29
+ # ==== Configuration
30
+ # shell<Object>:: An instance of the shell to be used.
31
+ #
32
+ # ==== Examples
33
+ #
34
+ # class MyScript < Thor
35
+ # argument :first, :type => :numeric
36
+ # end
37
+ #
38
+ # MyScript.new [1.0], { :foo => :bar }, :shell => Thor::Shell::Basic.new
39
+ #
40
+ def initialize(args=[], options={}, config={})
41
+ super
42
+ self.shell = config[:shell]
43
+ self.shell.base ||= self if self.shell.respond_to?(:base)
44
+ end
45
+
46
+ # Holds the shell for the given Thor instance. If no shell is given,
47
+ # it gets a default shell from Thor::Base.shell.
48
+ def shell
49
+ @shell ||= Thor::Base.shell.new
50
+ end
51
+
52
+ # Sets the shell for this thor class.
53
+ def shell=(shell)
54
+ @shell = shell
55
+ end
56
+
57
+ # Common methods that are delegated to the shell.
58
+ SHELL_DELEGATED_METHODS.each do |method|
59
+ module_eval <<-METHOD, __FILE__, __LINE__
60
+ def #{method}(*args)
61
+ shell.#{method}(*args)
62
+ end
63
+ METHOD
64
+ end
65
+
66
+ # Yields the given block with padding.
67
+ def with_padding
68
+ shell.padding += 1
69
+ yield
70
+ ensure
71
+ shell.padding -= 1
72
+ end
73
+
74
+ protected
75
+
76
+ # Allow shell to be shared between invocations.
77
+ #
78
+ def _shared_configuration #:nodoc:
79
+ super.merge!(:shell => self.shell)
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,268 @@
1
+ require 'tempfile'
2
+
3
+ class Thor
4
+ module Shell
5
+ class Basic
6
+ attr_accessor :base, :padding
7
+
8
+ # Initialize base and padding to nil.
9
+ #
10
+ def initialize #:nodoc:
11
+ @base, @padding = nil, 0
12
+ end
13
+
14
+ # Sets the output padding, not allowing less than zero values.
15
+ #
16
+ def padding=(value)
17
+ @padding = [0, value].max
18
+ end
19
+
20
+ # Ask something to the user and receives a response.
21
+ #
22
+ # ==== Example
23
+ # ask("What is your name?")
24
+ #
25
+ def ask(statement, color=nil)
26
+ say("#{statement} ", color)
27
+ $stdin.gets.strip
28
+ end
29
+
30
+ # Say (print) something to the user. If the sentence ends with a whitespace
31
+ # or tab character, a new line is not appended (print + flush). Otherwise
32
+ # are passed straight to puts (behavior got from Highline).
33
+ #
34
+ # ==== Example
35
+ # say("I know you knew that.")
36
+ #
37
+ def say(message="", color=nil, force_new_line=(message.to_s !~ /( |\t)$/))
38
+ message = message.to_s
39
+ message = set_color(message, color) if color
40
+
41
+ if force_new_line
42
+ $stdout.puts(message)
43
+ else
44
+ $stdout.print(message)
45
+ $stdout.flush
46
+ end
47
+ end
48
+
49
+ # Say a status with the given color and appends the message. Since this
50
+ # method is used frequently by actions, it allows nil or false to be given
51
+ # in log_status, avoiding the message from being shown. If a Symbol is
52
+ # given in log_status, it's used as the color.
53
+ #
54
+ def say_status(status, message, log_status=true)
55
+ return if quiet? || log_status == false
56
+ spaces = " " * (padding + 1)
57
+ color = log_status.is_a?(Symbol) ? log_status : :green
58
+
59
+ status = status.to_s.rjust(12)
60
+ status = set_color status, color, true if color
61
+ say "#{status}#{spaces}#{message}", nil, true
62
+ end
63
+
64
+ # Make a question the to user and returns true if the user replies "y" or
65
+ # "yes".
66
+ #
67
+ def yes?(statement, color=nil)
68
+ ask(statement, color) =~ is?(:yes)
69
+ end
70
+
71
+ # Make a question the to user and returns true if the user replies "n" or
72
+ # "no".
73
+ #
74
+ def no?(statement, color=nil)
75
+ !yes?(statement, color)
76
+ end
77
+
78
+ # Prints a table.
79
+ #
80
+ # ==== Parameters
81
+ # Array[Array[String, String, ...]]
82
+ #
83
+ # ==== Options
84
+ # ident<Integer>:: Indent the first column by ident value.
85
+ # colwidth<Integer>:: Force the first column to colwidth spaces wide.
86
+ #
87
+ def print_table(table, options={})
88
+ return if table.empty?
89
+
90
+ formats, ident, colwidth = [], options[:ident].to_i, options[:colwidth].to_i
91
+ options[:truncate] = terminal_width if options[:truncate] == true
92
+
93
+ 0.upto(table.first.length - 2) do |i|
94
+ colwidth ||= table.max{ |a,b| a[i].size <=> b[i].size }[i].size
95
+ formats << "%-#{colwidth + 2}s"
96
+ end
97
+
98
+ formats[0] = formats[0].insert(0, " " * ident)
99
+ formats << "%s"
100
+
101
+ table.each do |row|
102
+ sentence = ""
103
+
104
+ row.each_with_index do |column, i|
105
+ sentence << formats[i] % column.to_s
106
+ end
107
+
108
+ sentence = truncate(sentence, options[:truncate]) if options[:truncate]
109
+ $stdout.puts sentence
110
+ end
111
+ end
112
+
113
+ # Prints a long string, word-wrapping the text to the current width of the
114
+ # terminal display. Ideal for printing heredocs.
115
+ #
116
+ # ==== Parameters
117
+ # String
118
+ #
119
+ # ==== Options
120
+ # ident<Integer>:: Indent each line of the printed paragraph by ident value.
121
+ #
122
+ def print_wrapped(message, options={})
123
+ ident = options[:ident] || 0
124
+ width = terminal_width - ident
125
+ paras = message.split("\n\n")
126
+
127
+ paras.map! do |unwrapped|
128
+ unwrapped.strip.gsub(/\n/, " ").squeeze(" ").
129
+ gsub(/.{1,#{width}}(?:\s|\Z)/){($& + 5.chr).
130
+ gsub(/\n\005/,"\n").gsub(/\005/,"\n")}
131
+ end
132
+
133
+ paras.each do |para|
134
+ para.split("\n").each do |line|
135
+ $stdout.puts line.insert(0, " " * ident)
136
+ end
137
+ $stdout.puts unless para == paras.last
138
+ end
139
+ end
140
+
141
+ # Deals with file collision and returns true if the file should be
142
+ # overwriten and false otherwise. If a block is given, it uses the block
143
+ # response as the content for the diff.
144
+ #
145
+ # ==== Parameters
146
+ # destination<String>:: the destination file to solve conflicts
147
+ # block<Proc>:: an optional block that returns the value to be used in diff
148
+ #
149
+ def file_collision(destination)
150
+ return true if @always_force
151
+ options = block_given? ? "[Ynaqdh]" : "[Ynaqh]"
152
+
153
+ while true
154
+ answer = ask %[Overwrite #{destination}? (enter "h" for help) #{options}]
155
+
156
+ case answer
157
+ when is?(:yes), is?(:force), ""
158
+ return true
159
+ when is?(:no), is?(:skip)
160
+ return false
161
+ when is?(:always)
162
+ return @always_force = true
163
+ when is?(:quit)
164
+ say 'Aborting...'
165
+ raise SystemExit
166
+ when is?(:diff)
167
+ show_diff(destination, yield) if block_given?
168
+ say 'Retrying...'
169
+ else
170
+ say file_collision_help
171
+ end
172
+ end
173
+ end
174
+
175
+ # Called if something goes wrong during the execution. This is used by Thor
176
+ # internally and should not be used inside your scripts. If someone went
177
+ # wrong, you can always raise an exception. If you raise a Thor::Error, it
178
+ # will be rescued and wrapped in the method below.
179
+ #
180
+ def error(statement)
181
+ $stderr.puts statement
182
+ end
183
+
184
+ # Apply color to the given string with optional bold. Disabled in the
185
+ # Thor::Shell::Basic class.
186
+ #
187
+ def set_color(string, color, bold=false) #:nodoc:
188
+ string
189
+ end
190
+
191
+ protected
192
+
193
+ def is?(value) #:nodoc:
194
+ value = value.to_s
195
+
196
+ if value.size == 1
197
+ /\A#{value}\z/i
198
+ else
199
+ /\A(#{value}|#{value[0,1]})\z/i
200
+ end
201
+ end
202
+
203
+ def file_collision_help #:nodoc:
204
+ <<HELP
205
+ Y - yes, overwrite
206
+ n - no, do not overwrite
207
+ a - all, overwrite this and all others
208
+ q - quit, abort
209
+ d - diff, show the differences between the old and the new
210
+ h - help, show this help
211
+ HELP
212
+ end
213
+
214
+ def show_diff(destination, content) #:nodoc:
215
+ diff_cmd = ENV['THOR_DIFF'] || ENV['RAILS_DIFF'] || 'diff -u'
216
+
217
+ Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
218
+ temp.write content
219
+ temp.rewind
220
+ system %(#{diff_cmd} "#{destination}" "#{temp.path}")
221
+ end
222
+ end
223
+
224
+ def quiet? #:nodoc:
225
+ base && base.options[:quiet]
226
+ end
227
+
228
+ # This code was copied from Rake, available under MIT-LICENSE
229
+ # Copyright (c) 2003, 2004 Jim Weirich
230
+ def terminal_width
231
+ if ENV['THOR_COLUMNS']
232
+ result = ENV['THOR_COLUMNS'].to_i
233
+ else
234
+ result = unix? ? dynamic_width : 80
235
+ end
236
+ (result < 10) ? 80 : result
237
+ rescue
238
+ 80
239
+ end
240
+
241
+ # Calculate the dynamic width of the terminal
242
+ def dynamic_width
243
+ @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
244
+ end
245
+
246
+ def dynamic_width_stty
247
+ %x{stty size 2>/dev/null}.split[1].to_i
248
+ end
249
+
250
+ def dynamic_width_tput
251
+ %x{tput cols 2>/dev/null}.to_i
252
+ end
253
+
254
+ def unix?
255
+ RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
256
+ end
257
+
258
+ def truncate(string, width)
259
+ if string.length <= width
260
+ string
261
+ else
262
+ ( string[0, width-3] || "" ) + "..."
263
+ end
264
+ end
265
+
266
+ end
267
+ end
268
+ end
@@ -0,0 +1,108 @@
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
+ def set_color(string, color, bold=false)
54
+ color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
55
+ bold = bold ? BOLD : ""
56
+ "#{bold}#{color}#{string}#{CLEAR}"
57
+ end
58
+
59
+ protected
60
+
61
+ # Overwrite show_diff to show diff with colors if Diff::LCS is
62
+ # available.
63
+ #
64
+ def show_diff(destination, content) #:nodoc:
65
+ if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
66
+ actual = File.binread(destination).to_s.split("\n")
67
+ content = content.to_s.split("\n")
68
+
69
+ Diff::LCS.sdiff(actual, content).each do |diff|
70
+ output_diff_line(diff)
71
+ end
72
+ else
73
+ super
74
+ end
75
+ end
76
+
77
+ def output_diff_line(diff) #:nodoc:
78
+ case diff.action
79
+ when '-'
80
+ say "- #{diff.old_element.chomp}", :red, true
81
+ when '+'
82
+ say "+ #{diff.new_element.chomp}", :green, true
83
+ when '!'
84
+ say "- #{diff.old_element.chomp}", :red, true
85
+ say "+ #{diff.new_element.chomp}", :green, true
86
+ else
87
+ say " #{diff.old_element.chomp}", nil, true
88
+ end
89
+ end
90
+
91
+ # Check if Diff::LCS is loaded. If it is, use it to create pretty output
92
+ # for diff.
93
+ #
94
+ def diff_lcs_loaded? #:nodoc:
95
+ return true if defined?(Diff::LCS)
96
+ return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
97
+
98
+ @diff_lcs_loaded = begin
99
+ require 'diff/lcs'
100
+ true
101
+ rescue LoadError
102
+ false
103
+ end
104
+ end
105
+
106
+ end
107
+ end
108
+ end