engineyard-serverside 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. data/LICENSE +19 -0
  2. data/bin/engineyard-serverside +10 -0
  3. data/lib/engineyard-serverside.rb +49 -0
  4. data/lib/engineyard-serverside/bundle_installer.rb +4 -0
  5. data/lib/engineyard-serverside/cli.rb +146 -0
  6. data/lib/engineyard-serverside/configuration.rb +130 -0
  7. data/lib/engineyard-serverside/default_maintenance_page.html +29 -0
  8. data/lib/engineyard-serverside/deploy.rb +321 -0
  9. data/lib/engineyard-serverside/deploy_hook.rb +80 -0
  10. data/lib/engineyard-serverside/lockfile_parser.rb +55 -0
  11. data/lib/engineyard-serverside/logged_output.rb +78 -0
  12. data/lib/engineyard-serverside/server.rb +70 -0
  13. data/lib/engineyard-serverside/strategies/git.rb +136 -0
  14. data/lib/engineyard-serverside/task.rb +62 -0
  15. data/lib/engineyard-serverside/version.rb +3 -0
  16. data/lib/vendor/dataflow/HISTORY +52 -0
  17. data/lib/vendor/dataflow/LICENSE +19 -0
  18. data/lib/vendor/dataflow/README.textile +290 -0
  19. data/lib/vendor/dataflow/Rakefile +36 -0
  20. data/lib/vendor/dataflow/dataflow.rb +120 -0
  21. data/lib/vendor/dataflow/dataflow/actor.rb +22 -0
  22. data/lib/vendor/dataflow/dataflow/equality.rb +28 -0
  23. data/lib/vendor/dataflow/dataflow/future_queue.rb +24 -0
  24. data/lib/vendor/dataflow/dataflow/port.rb +54 -0
  25. data/lib/vendor/dataflow/examples/barrier.rb +9 -0
  26. data/lib/vendor/dataflow/examples/data_driven.rb +17 -0
  27. data/lib/vendor/dataflow/examples/dataflow_http_gets.rb +13 -0
  28. data/lib/vendor/dataflow/examples/flow.rb +20 -0
  29. data/lib/vendor/dataflow/examples/future_http_gets.rb +12 -0
  30. data/lib/vendor/dataflow/examples/future_queue.rb +11 -0
  31. data/lib/vendor/dataflow/examples/instance_variables.rb +15 -0
  32. data/lib/vendor/dataflow/examples/laziness.rb +9 -0
  33. data/lib/vendor/dataflow/examples/local_variables.rb +11 -0
  34. data/lib/vendor/dataflow/examples/messages.rb +26 -0
  35. data/lib/vendor/dataflow/examples/port_http_gets.rb +13 -0
  36. data/lib/vendor/dataflow/examples/port_send.rb +10 -0
  37. data/lib/vendor/dataflow/examples/ring.rb +21 -0
  38. data/lib/vendor/dataflow/spec/actor_spec.rb +28 -0
  39. data/lib/vendor/dataflow/spec/anonymous_variables_spec.rb +21 -0
  40. data/lib/vendor/dataflow/spec/barrier_spec.rb +25 -0
  41. data/lib/vendor/dataflow/spec/by_need_spec.rb +55 -0
  42. data/lib/vendor/dataflow/spec/dataflow_spec.rb +151 -0
  43. data/lib/vendor/dataflow/spec/equality_spec.rb +40 -0
  44. data/lib/vendor/dataflow/spec/flow_spec.rb +25 -0
  45. data/lib/vendor/dataflow/spec/forker_spec.rb +28 -0
  46. data/lib/vendor/dataflow/spec/future_queue_spec.rb +31 -0
  47. data/lib/vendor/dataflow/spec/inspect_spec.rb +19 -0
  48. data/lib/vendor/dataflow/spec/need_later_spec.rb +12 -0
  49. data/lib/vendor/dataflow/spec/port_spec.rb +26 -0
  50. data/lib/vendor/dataflow/spec/spec.opts +1 -0
  51. data/lib/vendor/dataflow/spec/spec_helper.rb +10 -0
  52. data/lib/vendor/escape/Readme +21 -0
  53. data/lib/vendor/escape/doc_include/template/qualitysmith.rb +631 -0
  54. data/lib/vendor/escape/lib/escape.rb +247 -0
  55. data/lib/vendor/json_pure/CHANGES +166 -0
  56. data/lib/vendor/json_pure/COPYING +58 -0
  57. data/lib/vendor/json_pure/GPL +340 -0
  58. data/lib/vendor/json_pure/README +358 -0
  59. data/lib/vendor/json_pure/Rakefile +292 -0
  60. data/lib/vendor/json_pure/TODO +1 -0
  61. data/lib/vendor/json_pure/VERSION +1 -0
  62. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +52 -0
  63. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +1000 -0
  64. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +1001 -0
  65. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +900 -0
  66. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +901 -0
  67. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +1000 -0
  68. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +1001 -0
  69. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +261 -0
  70. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +1000 -0
  71. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +1001 -0
  72. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +1000 -0
  73. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +1001 -0
  74. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +1000 -0
  75. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +1001 -0
  76. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +262 -0
  77. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +1000 -0
  78. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +1001 -0
  79. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +82 -0
  80. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +34 -0
  81. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +900 -0
  82. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +901 -0
  83. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +81 -0
  84. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +1000 -0
  85. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +1001 -0
  86. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +82 -0
  87. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +1000 -0
  88. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +1001 -0
  89. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +82 -0
  90. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +1000 -0
  91. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +1001 -0
  92. data/lib/vendor/json_pure/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +82 -0
  93. data/lib/vendor/json_pure/benchmarks/generator2_benchmark.rb +222 -0
  94. data/lib/vendor/json_pure/benchmarks/generator_benchmark.rb +224 -0
  95. data/lib/vendor/json_pure/benchmarks/ohai.json +1216 -0
  96. data/lib/vendor/json_pure/benchmarks/ohai.ruby +1 -0
  97. data/lib/vendor/json_pure/benchmarks/parser2_benchmark.rb +251 -0
  98. data/lib/vendor/json_pure/benchmarks/parser_benchmark.rb +259 -0
  99. data/lib/vendor/json_pure/bin/edit_json.rb +9 -0
  100. data/lib/vendor/json_pure/bin/prettify_json.rb +75 -0
  101. data/lib/vendor/json_pure/data/example.json +1 -0
  102. data/lib/vendor/json_pure/data/index.html +38 -0
  103. data/lib/vendor/json_pure/data/prototype.js +4184 -0
  104. data/lib/vendor/json_pure/ext/json/ext/generator/extconf.rb +16 -0
  105. data/lib/vendor/json_pure/ext/json/ext/generator/generator.c +1323 -0
  106. data/lib/vendor/json_pure/ext/json/ext/generator/generator.h +170 -0
  107. data/lib/vendor/json_pure/ext/json/ext/parser/extconf.rb +15 -0
  108. data/lib/vendor/json_pure/ext/json/ext/parser/parser.c +1935 -0
  109. data/lib/vendor/json_pure/ext/json/ext/parser/parser.h +71 -0
  110. data/lib/vendor/json_pure/ext/json/ext/parser/parser.rl +792 -0
  111. data/lib/vendor/json_pure/install.rb +26 -0
  112. data/lib/vendor/json_pure/lib/json.rb +10 -0
  113. data/lib/vendor/json_pure/lib/json/Array.xpm +21 -0
  114. data/lib/vendor/json_pure/lib/json/FalseClass.xpm +21 -0
  115. data/lib/vendor/json_pure/lib/json/Hash.xpm +21 -0
  116. data/lib/vendor/json_pure/lib/json/Key.xpm +73 -0
  117. data/lib/vendor/json_pure/lib/json/NilClass.xpm +21 -0
  118. data/lib/vendor/json_pure/lib/json/Numeric.xpm +28 -0
  119. data/lib/vendor/json_pure/lib/json/String.xpm +96 -0
  120. data/lib/vendor/json_pure/lib/json/TrueClass.xpm +21 -0
  121. data/lib/vendor/json_pure/lib/json/add/core.rb +148 -0
  122. data/lib/vendor/json_pure/lib/json/add/rails.rb +58 -0
  123. data/lib/vendor/json_pure/lib/json/common.rb +397 -0
  124. data/lib/vendor/json_pure/lib/json/editor.rb +1371 -0
  125. data/lib/vendor/json_pure/lib/json/ext.rb +15 -0
  126. data/lib/vendor/json_pure/lib/json/json.xpm +1499 -0
  127. data/lib/vendor/json_pure/lib/json/pure.rb +77 -0
  128. data/lib/vendor/json_pure/lib/json/pure/generator.rb +452 -0
  129. data/lib/vendor/json_pure/lib/json/pure/parser.rb +307 -0
  130. data/lib/vendor/json_pure/lib/json/version.rb +8 -0
  131. data/lib/vendor/json_pure/tests/fixtures/fail1.json +1 -0
  132. data/lib/vendor/json_pure/tests/fixtures/fail10.json +1 -0
  133. data/lib/vendor/json_pure/tests/fixtures/fail11.json +1 -0
  134. data/lib/vendor/json_pure/tests/fixtures/fail12.json +1 -0
  135. data/lib/vendor/json_pure/tests/fixtures/fail13.json +1 -0
  136. data/lib/vendor/json_pure/tests/fixtures/fail14.json +1 -0
  137. data/lib/vendor/json_pure/tests/fixtures/fail18.json +1 -0
  138. data/lib/vendor/json_pure/tests/fixtures/fail19.json +1 -0
  139. data/lib/vendor/json_pure/tests/fixtures/fail2.json +1 -0
  140. data/lib/vendor/json_pure/tests/fixtures/fail20.json +1 -0
  141. data/lib/vendor/json_pure/tests/fixtures/fail21.json +1 -0
  142. data/lib/vendor/json_pure/tests/fixtures/fail22.json +1 -0
  143. data/lib/vendor/json_pure/tests/fixtures/fail23.json +1 -0
  144. data/lib/vendor/json_pure/tests/fixtures/fail24.json +1 -0
  145. data/lib/vendor/json_pure/tests/fixtures/fail25.json +1 -0
  146. data/lib/vendor/json_pure/tests/fixtures/fail27.json +2 -0
  147. data/lib/vendor/json_pure/tests/fixtures/fail28.json +2 -0
  148. data/lib/vendor/json_pure/tests/fixtures/fail3.json +1 -0
  149. data/lib/vendor/json_pure/tests/fixtures/fail4.json +1 -0
  150. data/lib/vendor/json_pure/tests/fixtures/fail5.json +1 -0
  151. data/lib/vendor/json_pure/tests/fixtures/fail6.json +1 -0
  152. data/lib/vendor/json_pure/tests/fixtures/fail7.json +1 -0
  153. data/lib/vendor/json_pure/tests/fixtures/fail8.json +1 -0
  154. data/lib/vendor/json_pure/tests/fixtures/fail9.json +1 -0
  155. data/lib/vendor/json_pure/tests/fixtures/pass1.json +56 -0
  156. data/lib/vendor/json_pure/tests/fixtures/pass15.json +1 -0
  157. data/lib/vendor/json_pure/tests/fixtures/pass16.json +1 -0
  158. data/lib/vendor/json_pure/tests/fixtures/pass17.json +1 -0
  159. data/lib/vendor/json_pure/tests/fixtures/pass2.json +1 -0
  160. data/lib/vendor/json_pure/tests/fixtures/pass26.json +1 -0
  161. data/lib/vendor/json_pure/tests/fixtures/pass3.json +6 -0
  162. data/lib/vendor/json_pure/tests/test_json.rb +361 -0
  163. data/lib/vendor/json_pure/tests/test_json_addition.rb +162 -0
  164. data/lib/vendor/json_pure/tests/test_json_encoding.rb +68 -0
  165. data/lib/vendor/json_pure/tests/test_json_fixtures.rb +34 -0
  166. data/lib/vendor/json_pure/tests/test_json_generate.rb +122 -0
  167. data/lib/vendor/json_pure/tests/test_json_rails.rb +144 -0
  168. data/lib/vendor/json_pure/tests/test_json_unicode.rb +76 -0
  169. data/lib/vendor/json_pure/tools/fuzz.rb +139 -0
  170. data/lib/vendor/json_pure/tools/server.rb +61 -0
  171. data/lib/vendor/open4/lib/open4.rb +403 -0
  172. data/lib/vendor/thor/CHANGELOG.rdoc +89 -0
  173. data/lib/vendor/thor/LICENSE +20 -0
  174. data/lib/vendor/thor/README.rdoc +297 -0
  175. data/lib/vendor/thor/Thorfile +69 -0
  176. data/lib/vendor/thor/bin/rake2thor +86 -0
  177. data/lib/vendor/thor/bin/thor +6 -0
  178. data/lib/vendor/thor/lib/thor.rb +244 -0
  179. data/lib/vendor/thor/lib/thor/actions.rb +275 -0
  180. data/lib/vendor/thor/lib/thor/actions/create_file.rb +103 -0
  181. data/lib/vendor/thor/lib/thor/actions/directory.rb +91 -0
  182. data/lib/vendor/thor/lib/thor/actions/empty_directory.rb +134 -0
  183. data/lib/vendor/thor/lib/thor/actions/file_manipulation.rb +223 -0
  184. data/lib/vendor/thor/lib/thor/actions/inject_into_file.rb +104 -0
  185. data/lib/vendor/thor/lib/thor/base.rb +540 -0
  186. data/lib/vendor/thor/lib/thor/core_ext/file_binary_read.rb +9 -0
  187. data/lib/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  188. data/lib/vendor/thor/lib/thor/core_ext/ordered_hash.rb +100 -0
  189. data/lib/vendor/thor/lib/thor/error.rb +30 -0
  190. data/lib/vendor/thor/lib/thor/group.rb +271 -0
  191. data/lib/vendor/thor/lib/thor/invocation.rb +180 -0
  192. data/lib/vendor/thor/lib/thor/parser.rb +4 -0
  193. data/lib/vendor/thor/lib/thor/parser/argument.rb +67 -0
  194. data/lib/vendor/thor/lib/thor/parser/arguments.rb +150 -0
  195. data/lib/vendor/thor/lib/thor/parser/option.rb +128 -0
  196. data/lib/vendor/thor/lib/thor/parser/options.rb +169 -0
  197. data/lib/vendor/thor/lib/thor/rake_compat.rb +66 -0
  198. data/lib/vendor/thor/lib/thor/runner.rb +314 -0
  199. data/lib/vendor/thor/lib/thor/shell.rb +83 -0
  200. data/lib/vendor/thor/lib/thor/shell/basic.rb +239 -0
  201. data/lib/vendor/thor/lib/thor/shell/color.rb +108 -0
  202. data/lib/vendor/thor/lib/thor/task.rb +102 -0
  203. data/lib/vendor/thor/lib/thor/util.rb +230 -0
  204. data/lib/vendor/thor/lib/thor/version.rb +3 -0
  205. data/lib/vendor/thor/thor.gemspec +120 -0
  206. data/spec/custom_deploy_spec.rb +95 -0
  207. data/spec/deploy_hook_spec.rb +211 -0
  208. data/spec/fixtures/gitrepo.tar.gz +0 -0
  209. data/spec/fixtures/gitrepo/foo +0 -0
  210. data/spec/fixtures/invalid_hook.rb +1 -0
  211. data/spec/fixtures/valid_hook.rb +1 -0
  212. data/spec/git_strategy_spec.rb +22 -0
  213. data/spec/lockfile_parser_spec.rb +30 -0
  214. data/spec/spec_helper.rb +37 -0
  215. data/spec/support/lockfiles/0.9-no-bundler +111 -0
  216. data/spec/support/lockfiles/0.9-with-bundler +117 -0
  217. data/spec/support/lockfiles/1.0-no-bundler +54 -0
  218. data/spec/support/lockfiles/1.0.0.rc.1-with-bundler +162 -0
  219. data/spec/support/lockfiles/not-a-lockfile +10 -0
  220. metadata +279 -0
@@ -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,239 @@
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>:: Ident the first column by ident value.
85
+ #
86
+ def print_table(table, options={})
87
+ return if table.empty?
88
+
89
+ formats, ident = [], options[:ident].to_i
90
+ options[:truncate] = terminal_width if options[:truncate] == true
91
+
92
+ 0.upto(table.first.length - 2) do |i|
93
+ maxima = table.max{ |a,b| a[i].size <=> b[i].size }[i].size
94
+ formats << "%-#{maxima + 2}s"
95
+ end
96
+
97
+ formats[0] = formats[0].insert(0, " " * ident)
98
+ formats << "%s"
99
+
100
+ table.each do |row|
101
+ sentence = ""
102
+
103
+ row.each_with_index do |column, i|
104
+ sentence << formats[i] % column.to_s
105
+ end
106
+
107
+ sentence = truncate(sentence, options[:truncate]) if options[:truncate]
108
+ $stdout.puts sentence
109
+ end
110
+ end
111
+
112
+ # Deals with file collision and returns true if the file should be
113
+ # overwriten and false otherwise. If a block is given, it uses the block
114
+ # response as the content for the diff.
115
+ #
116
+ # ==== Parameters
117
+ # destination<String>:: the destination file to solve conflicts
118
+ # block<Proc>:: an optional block that returns the value to be used in diff
119
+ #
120
+ def file_collision(destination)
121
+ return true if @always_force
122
+ options = block_given? ? "[Ynaqdh]" : "[Ynaqh]"
123
+
124
+ while true
125
+ answer = ask %[Overwrite #{destination}? (enter "h" for help) #{options}]
126
+
127
+ case answer
128
+ when is?(:yes), is?(:force), ""
129
+ return true
130
+ when is?(:no), is?(:skip)
131
+ return false
132
+ when is?(:always)
133
+ return @always_force = true
134
+ when is?(:quit)
135
+ say 'Aborting...'
136
+ raise SystemExit
137
+ when is?(:diff)
138
+ show_diff(destination, yield) if block_given?
139
+ say 'Retrying...'
140
+ else
141
+ say file_collision_help
142
+ end
143
+ end
144
+ end
145
+
146
+ # Called if something goes wrong during the execution. This is used by Thor
147
+ # internally and should not be used inside your scripts. If someone went
148
+ # wrong, you can always raise an exception. If you raise a Thor::Error, it
149
+ # will be rescued and wrapped in the method below.
150
+ #
151
+ def error(statement)
152
+ $stderr.puts statement
153
+ end
154
+
155
+ # Apply color to the given string with optional bold. Disabled in the
156
+ # Thor::Shell::Basic class.
157
+ #
158
+ def set_color(string, color, bold=false) #:nodoc:
159
+ string
160
+ end
161
+
162
+ protected
163
+
164
+ def is?(value) #:nodoc:
165
+ value = value.to_s
166
+
167
+ if value.size == 1
168
+ /\A#{value}\z/i
169
+ else
170
+ /\A(#{value}|#{value[0,1]})\z/i
171
+ end
172
+ end
173
+
174
+ def file_collision_help #:nodoc:
175
+ <<HELP
176
+ Y - yes, overwrite
177
+ n - no, do not overwrite
178
+ a - all, overwrite this and all others
179
+ q - quit, abort
180
+ d - diff, show the differences between the old and the new
181
+ h - help, show this help
182
+ HELP
183
+ end
184
+
185
+ def show_diff(destination, content) #:nodoc:
186
+ diff_cmd = ENV['THOR_DIFF'] || ENV['RAILS_DIFF'] || 'diff -u'
187
+
188
+ Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
189
+ temp.write content
190
+ temp.rewind
191
+ system %(#{diff_cmd} "#{destination}" "#{temp.path}")
192
+ end
193
+ end
194
+
195
+ def quiet? #:nodoc:
196
+ base && base.options[:quiet]
197
+ end
198
+
199
+ # This code was copied from Rake, available under MIT-LICENSE
200
+ # Copyright (c) 2003, 2004 Jim Weirich
201
+ def terminal_width
202
+ if ENV['THOR_COLUMNS']
203
+ result = ENV['THOR_COLUMNS'].to_i
204
+ else
205
+ result = unix? ? dynamic_width : 80
206
+ end
207
+ (result < 10) ? 80 : result
208
+ rescue
209
+ 80
210
+ end
211
+
212
+ # Calculate the dynamic width of the terminal
213
+ def dynamic_width
214
+ @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
215
+ end
216
+
217
+ def dynamic_width_stty
218
+ %x{stty size 2>/dev/null}.split[1].to_i
219
+ end
220
+
221
+ def dynamic_width_tput
222
+ %x{tput cols 2>/dev/null}.to_i
223
+ end
224
+
225
+ def unix?
226
+ RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
227
+ end
228
+
229
+ def truncate(string, width)
230
+ if string.length <= width
231
+ string
232
+ else
233
+ ( string[0, width-3] || "" ) + "..."
234
+ end
235
+ end
236
+
237
+ end
238
+ end
239
+ 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
@@ -0,0 +1,102 @@
1
+ class Thor
2
+ class Task < Struct.new(:name, :description, :usage, :options)
3
+ FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/
4
+
5
+ # A dynamic task that handles method missing scenarios.
6
+ class Dynamic < Task
7
+ def initialize(name, options=nil)
8
+ super(name.to_s, "A dynamically-generated task", name.to_s, options)
9
+ end
10
+
11
+ def run(instance, args=[])
12
+ if (instance.methods & [name.to_s, name.to_sym]).empty?
13
+ super
14
+ else
15
+ instance.class.handle_no_task_error(name)
16
+ end
17
+ end
18
+ end
19
+
20
+ def initialize(name, description, usage, options=nil)
21
+ super(name.to_s, description, usage, options || {})
22
+ end
23
+
24
+ def initialize_copy(other) #:nodoc:
25
+ super(other)
26
+ self.options = other.options.dup if other.options
27
+ end
28
+
29
+ # By default, a task invokes a method in the thor class. You can change this
30
+ # implementation to create custom tasks.
31
+ def run(instance, args=[])
32
+ public_method?(instance) ?
33
+ instance.send(name, *args) : instance.class.handle_no_task_error(name)
34
+ rescue ArgumentError => e
35
+ handle_argument_error?(instance, e, caller) ?
36
+ instance.class.handle_argument_error(self, e) : (raise e)
37
+ rescue NoMethodError => e
38
+ handle_no_method_error?(instance, e, caller) ?
39
+ instance.class.handle_no_task_error(name) : (raise e)
40
+ end
41
+
42
+ # Returns the formatted usage by injecting given required arguments
43
+ # and required options into the given usage.
44
+ def formatted_usage(klass, namespace=true)
45
+ namespace = klass.namespace unless namespace == false
46
+
47
+ # Add namespace
48
+ formatted = if namespace
49
+ "#{namespace.gsub(/^(default|thor:runner:)/,'')}:"
50
+ else
51
+ ""
52
+ end
53
+
54
+ # Add usage with required arguments
55
+ formatted << if klass && !klass.arguments.empty?
56
+ usage.to_s.gsub(/^#{name}/) do |match|
57
+ match << " " << klass.arguments.map{ |a| a.usage }.compact.join(' ')
58
+ end
59
+ else
60
+ usage.to_s
61
+ end
62
+
63
+ # Add required options
64
+ formatted << " #{required_options}"
65
+
66
+ # Strip and go!
67
+ formatted.strip
68
+ end
69
+
70
+ protected
71
+
72
+ def not_debugging?(instance)
73
+ !(instance.class.respond_to?(:debugging) && instance.class.debugging)
74
+ end
75
+
76
+ def required_options
77
+ @required_options ||= options.map{ |_, o| o.usage if o.required? }.compact.sort.join(" ")
78
+ end
79
+
80
+ # Given a target, checks if this class name is not a private/protected method.
81
+ def public_method?(instance) #:nodoc:
82
+ collection = instance.private_methods + instance.protected_methods
83
+ (collection & [name.to_s, name.to_sym]).empty?
84
+ end
85
+
86
+ def sans_backtrace(backtrace, caller) #:nodoc:
87
+ saned = backtrace.reject { |frame| frame =~ FILE_REGEXP }
88
+ saned -= caller
89
+ end
90
+
91
+ def handle_argument_error?(instance, error, caller)
92
+ not_debugging?(instance) && error.message =~ /wrong number of arguments/ &&
93
+ sans_backtrace(error.backtrace, caller).empty?
94
+ end
95
+
96
+ def handle_no_method_error?(instance, error, caller)
97
+ not_debugging?(instance) &&
98
+ error.message =~ /^undefined method `#{name}' for #{Regexp.escape(instance.to_s)}$/
99
+ end
100
+
101
+ end
102
+ end