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,104 @@
1
+ require 'thor/actions/empty_directory'
2
+
3
+ class Thor
4
+ module Actions
5
+
6
+ # Injects the given content into a file. Different from gsub_file, this
7
+ # method is reversible.
8
+ #
9
+ # ==== Parameters
10
+ # destination<String>:: Relative path to the destination root
11
+ # data<String>:: Data to add to the file. Can be given as a block.
12
+ # config<Hash>:: give :verbose => false to not log the status and the flag
13
+ # for injection (:after or :before) or :force => true for
14
+ # insert two or more times the same content.
15
+ #
16
+ # ==== Examples
17
+ #
18
+ # inject_into_file "config/environment.rb", "config.gem :thor", :after => "Rails::Initializer.run do |config|\n"
19
+ #
20
+ # inject_into_file "config/environment.rb", :after => "Rails::Initializer.run do |config|\n" do
21
+ # gems = ask "Which gems would you like to add?"
22
+ # gems.split(" ").map{ |gem| " config.gem :#{gem}" }.join("\n")
23
+ # end
24
+ #
25
+ def inject_into_file(destination, *args, &block)
26
+ if block_given?
27
+ data, config = block, args.shift
28
+ else
29
+ data, config = args.shift, args.shift
30
+ end
31
+ action InjectIntoFile.new(self, destination, data, config)
32
+ end
33
+
34
+ class InjectIntoFile < EmptyDirectory #:nodoc:
35
+ attr_reader :replacement, :flag, :behavior
36
+
37
+ def initialize(base, destination, data, config)
38
+ super(base, destination, { :verbose => true }.merge(config))
39
+
40
+ @behavior, @flag = if @config.key?(:after)
41
+ [:after, @config.delete(:after)]
42
+ else
43
+ [:before, @config.delete(:before)]
44
+ end
45
+
46
+ @replacement = data.is_a?(Proc) ? data.call : data
47
+ @flag = Regexp.escape(@flag) unless @flag.is_a?(Regexp)
48
+ end
49
+
50
+ def invoke!
51
+ say_status :invoke
52
+
53
+ content = if @behavior == :after
54
+ '\0' + replacement
55
+ else
56
+ replacement + '\0'
57
+ end
58
+
59
+ replace!(/#{flag}/, content, config[:force])
60
+ end
61
+
62
+ def revoke!
63
+ say_status :revoke
64
+
65
+ regexp = if @behavior == :after
66
+ content = '\1\2'
67
+ /(#{flag})(.*)(#{Regexp.escape(replacement)})/m
68
+ else
69
+ content = '\2\3'
70
+ /(#{Regexp.escape(replacement)})(.*)(#{flag})/m
71
+ end
72
+
73
+ replace!(regexp, content, true)
74
+ end
75
+
76
+ protected
77
+
78
+ def say_status(behavior)
79
+ status = if flag == /\A/
80
+ behavior == :invoke ? :prepend : :unprepend
81
+ elsif flag == /\z/
82
+ behavior == :invoke ? :append : :unappend
83
+ else
84
+ behavior == :invoke ? :inject : :deinject
85
+ end
86
+
87
+ super(status, config[:verbose])
88
+ end
89
+
90
+ # Adds the content to the file.
91
+ #
92
+ def replace!(regexp, string, force)
93
+ unless base.options[:pretend]
94
+ content = File.binread(destination)
95
+ if force || !content.include?(replacement)
96
+ content.gsub!(regexp, string)
97
+ File.open(destination, 'wb') { |file| file.write(content) }
98
+ end
99
+ end
100
+ end
101
+
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,540 @@
1
+ require 'thor/core_ext/hash_with_indifferent_access'
2
+ require 'thor/core_ext/ordered_hash'
3
+ require 'thor/error'
4
+ require 'thor/shell'
5
+ require 'thor/invocation'
6
+ require 'thor/parser'
7
+ require 'thor/task'
8
+ require 'thor/util'
9
+
10
+ class Thor
11
+ autoload :Actions, 'thor/actions'
12
+ autoload :RakeCompat, 'thor/rake_compat'
13
+
14
+ # Shortcuts for help.
15
+ HELP_MAPPINGS = %w(-h -? --help -D)
16
+
17
+ # Thor methods that should not be overwritten by the user.
18
+ THOR_RESERVED_WORDS = %w(invoke shell options behavior root destination_root relative_root
19
+ action add_file create_file in_root inside run run_ruby_script)
20
+
21
+ module Base
22
+ attr_accessor :options
23
+
24
+ # It receives arguments in an Array and two hashes, one for options and
25
+ # other for configuration.
26
+ #
27
+ # Notice that it does not check if all required arguments were supplied.
28
+ # It should be done by the parser.
29
+ #
30
+ # ==== Parameters
31
+ # args<Array[Object]>:: An array of objects. The objects are applied to their
32
+ # respective accessors declared with <tt>argument</tt>.
33
+ #
34
+ # options<Hash>:: An options hash that will be available as self.options.
35
+ # The hash given is converted to a hash with indifferent
36
+ # access, magic predicates (options.skip?) and then frozen.
37
+ #
38
+ # config<Hash>:: Configuration for this Thor class.
39
+ #
40
+ def initialize(args=[], options={}, config={})
41
+ args = Thor::Arguments.parse(self.class.arguments, args)
42
+ args.each { |key, value| send("#{key}=", value) }
43
+
44
+ parse_options = self.class.class_options
45
+
46
+ if options.is_a?(Array)
47
+ task_options = config.delete(:task_options) # hook for start
48
+ parse_options = parse_options.merge(task_options) if task_options
49
+ array_options, hash_options = options, {}
50
+ else
51
+ array_options, hash_options = [], options
52
+ end
53
+
54
+ opts = Thor::Options.new(parse_options, hash_options)
55
+ self.options = opts.parse(array_options)
56
+ opts.check_unknown! if self.class.check_unknown_options?
57
+ end
58
+
59
+ class << self
60
+ def included(base) #:nodoc:
61
+ base.send :extend, ClassMethods
62
+ base.send :include, Invocation
63
+ base.send :include, Shell
64
+ end
65
+
66
+ # Returns the classes that inherits from Thor or Thor::Group.
67
+ #
68
+ # ==== Returns
69
+ # Array[Class]
70
+ #
71
+ def subclasses
72
+ @subclasses ||= []
73
+ end
74
+
75
+ # Returns the files where the subclasses are kept.
76
+ #
77
+ # ==== Returns
78
+ # Hash[path<String> => Class]
79
+ #
80
+ def subclass_files
81
+ @subclass_files ||= Hash.new{ |h,k| h[k] = [] }
82
+ end
83
+
84
+ # Whenever a class inherits from Thor or Thor::Group, we should track the
85
+ # class and the file on Thor::Base. This is the method responsable for it.
86
+ #
87
+ def register_klass_file(klass) #:nodoc:
88
+ file = caller[1].match(/(.*):\d+/)[1]
89
+ Thor::Base.subclasses << klass unless Thor::Base.subclasses.include?(klass)
90
+
91
+ file_subclasses = Thor::Base.subclass_files[File.expand_path(file)]
92
+ file_subclasses << klass unless file_subclasses.include?(klass)
93
+ end
94
+ end
95
+
96
+ module ClassMethods
97
+ attr_accessor :debugging
98
+
99
+ def attr_reader(*) #:nodoc:
100
+ no_tasks { super }
101
+ end
102
+
103
+ def attr_writer(*) #:nodoc:
104
+ no_tasks { super }
105
+ end
106
+
107
+ def attr_accessor(*) #:nodoc:
108
+ no_tasks { super }
109
+ end
110
+
111
+ # If you want to raise an error for unknown options, call check_unknown_options!
112
+ # This is disabled by default to allow dynamic invocations.
113
+ def check_unknown_options!
114
+ @check_unknown_options = true
115
+ end
116
+
117
+ def check_unknown_options? #:nodoc:
118
+ @check_unknown_options || false
119
+ end
120
+
121
+ # Adds an argument to the class and creates an attr_accessor for it.
122
+ #
123
+ # Arguments are different from options in several aspects. The first one
124
+ # is how they are parsed from the command line, arguments are retrieved
125
+ # from position:
126
+ #
127
+ # thor task NAME
128
+ #
129
+ # Instead of:
130
+ #
131
+ # thor task --name=NAME
132
+ #
133
+ # Besides, arguments are used inside your code as an accessor (self.argument),
134
+ # while options are all kept in a hash (self.options).
135
+ #
136
+ # Finally, arguments cannot have type :default or :boolean but can be
137
+ # optional (supplying :optional => :true or :required => false), although
138
+ # you cannot have a required argument after a non-required argument. If you
139
+ # try it, an error is raised.
140
+ #
141
+ # ==== Parameters
142
+ # name<Symbol>:: The name of the argument.
143
+ # options<Hash>:: Described below.
144
+ #
145
+ # ==== Options
146
+ # :desc - Description for the argument.
147
+ # :required - If the argument is required or not.
148
+ # :optional - If the argument is optional or not.
149
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric.
150
+ # :default - Default value for this argument. It cannot be required and have default values.
151
+ # :banner - String to show on usage notes.
152
+ #
153
+ # ==== Errors
154
+ # ArgumentError:: Raised if you supply a required argument after a non required one.
155
+ #
156
+ def argument(name, options={})
157
+ is_thor_reserved_word?(name, :argument)
158
+ no_tasks { attr_accessor name }
159
+
160
+ required = if options.key?(:optional)
161
+ !options[:optional]
162
+ elsif options.key?(:required)
163
+ options[:required]
164
+ else
165
+ options[:default].nil?
166
+ end
167
+
168
+ remove_argument name
169
+
170
+ arguments.each do |argument|
171
+ next if argument.required?
172
+ raise ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " <<
173
+ "the non-required argument #{argument.human_name.inspect}."
174
+ end if required
175
+
176
+ arguments << Thor::Argument.new(name, options[:desc], required, options[:type],
177
+ options[:default], options[:banner])
178
+ end
179
+
180
+ # Returns this class arguments, looking up in the ancestors chain.
181
+ #
182
+ # ==== Returns
183
+ # Array[Thor::Argument]
184
+ #
185
+ def arguments
186
+ @arguments ||= from_superclass(:arguments, [])
187
+ end
188
+
189
+ # Adds a bunch of options to the set of class options.
190
+ #
191
+ # class_options :foo => false, :bar => :required, :baz => :string
192
+ #
193
+ # If you prefer more detailed declaration, check class_option.
194
+ #
195
+ # ==== Parameters
196
+ # Hash[Symbol => Object]
197
+ #
198
+ def class_options(options=nil)
199
+ @class_options ||= from_superclass(:class_options, {})
200
+ build_options(options, @class_options) if options
201
+ @class_options
202
+ end
203
+
204
+ # Adds an option to the set of class options
205
+ #
206
+ # ==== Parameters
207
+ # name<Symbol>:: The name of the argument.
208
+ # options<Hash>:: Described below.
209
+ #
210
+ # ==== Options
211
+ # :desc - Description for the argument.
212
+ # :required - If the argument is required or not.
213
+ # :default - Default value for this argument.
214
+ # :group - The group for this options. Use by class options to output options in different levels.
215
+ # :aliases - Aliases for this option.
216
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
217
+ # :banner - String to show on usage notes.
218
+ #
219
+ def class_option(name, options={})
220
+ build_option(name, options, class_options)
221
+ end
222
+
223
+ # Removes a previous defined argument. If :undefine is given, undefine
224
+ # accessors as well.
225
+ #
226
+ # ==== Paremeters
227
+ # names<Array>:: Arguments to be removed
228
+ #
229
+ # ==== Examples
230
+ #
231
+ # remove_argument :foo
232
+ # remove_argument :foo, :bar, :baz, :undefine => true
233
+ #
234
+ def remove_argument(*names)
235
+ options = names.last.is_a?(Hash) ? names.pop : {}
236
+
237
+ names.each do |name|
238
+ arguments.delete_if { |a| a.name == name.to_s }
239
+ undef_method name, "#{name}=" if options[:undefine]
240
+ end
241
+ end
242
+
243
+ # Removes a previous defined class option.
244
+ #
245
+ # ==== Paremeters
246
+ # names<Array>:: Class options to be removed
247
+ #
248
+ # ==== Examples
249
+ #
250
+ # remove_class_option :foo
251
+ # remove_class_option :foo, :bar, :baz
252
+ #
253
+ def remove_class_option(*names)
254
+ names.each do |name|
255
+ class_options.delete(name)
256
+ end
257
+ end
258
+
259
+ # Defines the group. This is used when thor list is invoked so you can specify
260
+ # that only tasks from a pre-defined group will be shown. Defaults to standard.
261
+ #
262
+ # ==== Parameters
263
+ # name<String|Symbol>
264
+ #
265
+ def group(name=nil)
266
+ case name
267
+ when nil
268
+ @group ||= from_superclass(:group, 'standard')
269
+ else
270
+ @group = name.to_s
271
+ end
272
+ end
273
+
274
+ # Returns the tasks for this Thor class.
275
+ #
276
+ # ==== Returns
277
+ # OrderedHash:: An ordered hash with tasks names as keys and Thor::Task
278
+ # objects as values.
279
+ #
280
+ def tasks
281
+ @tasks ||= Thor::CoreExt::OrderedHash.new
282
+ end
283
+
284
+ # Returns the tasks for this Thor class and all subclasses.
285
+ #
286
+ # ==== Returns
287
+ # OrderedHash:: An ordered hash with tasks names as keys and Thor::Task
288
+ # objects as values.
289
+ #
290
+ def all_tasks
291
+ @all_tasks ||= from_superclass(:all_tasks, Thor::CoreExt::OrderedHash.new)
292
+ @all_tasks.merge(tasks)
293
+ end
294
+
295
+ # Removes a given task from this Thor class. This is usually done if you
296
+ # are inheriting from another class and don't want it to be available
297
+ # anymore.
298
+ #
299
+ # By default it only remove the mapping to the task. But you can supply
300
+ # :undefine => true to undefine the method from the class as well.
301
+ #
302
+ # ==== Parameters
303
+ # name<Symbol|String>:: The name of the task to be removed
304
+ # options<Hash>:: You can give :undefine => true if you want tasks the method
305
+ # to be undefined from the class as well.
306
+ #
307
+ def remove_task(*names)
308
+ options = names.last.is_a?(Hash) ? names.pop : {}
309
+
310
+ names.each do |name|
311
+ tasks.delete(name.to_s)
312
+ all_tasks.delete(name.to_s)
313
+ undef_method name if options[:undefine]
314
+ end
315
+ end
316
+
317
+ # All methods defined inside the given block are not added as tasks.
318
+ #
319
+ # So you can do:
320
+ #
321
+ # class MyScript < Thor
322
+ # no_tasks do
323
+ # def this_is_not_a_task
324
+ # end
325
+ # end
326
+ # end
327
+ #
328
+ # You can also add the method and remove it from the task list:
329
+ #
330
+ # class MyScript < Thor
331
+ # def this_is_not_a_task
332
+ # end
333
+ # remove_task :this_is_not_a_task
334
+ # end
335
+ #
336
+ def no_tasks
337
+ @no_tasks = true
338
+ yield
339
+ @no_tasks = false
340
+ end
341
+
342
+ # Sets the namespace for the Thor or Thor::Group class. By default the
343
+ # namespace is retrieved from the class name. If your Thor class is named
344
+ # Scripts::MyScript, the help method, for example, will be called as:
345
+ #
346
+ # thor scripts:my_script -h
347
+ #
348
+ # If you change the namespace:
349
+ #
350
+ # namespace :my_scripts
351
+ #
352
+ # You change how your tasks are invoked:
353
+ #
354
+ # thor my_scripts -h
355
+ #
356
+ # Finally, if you change your namespace to default:
357
+ #
358
+ # namespace :default
359
+ #
360
+ # Your tasks can be invoked with a shortcut. Instead of:
361
+ #
362
+ # thor :my_task
363
+ #
364
+ def namespace(name=nil)
365
+ case name
366
+ when nil
367
+ @namespace ||= Thor::Util.namespace_from_thor_class(self)
368
+ else
369
+ @namespace = name.to_s
370
+ end
371
+ end
372
+
373
+ # Default way to start generators from the command line.
374
+ #
375
+ def start(given_args=ARGV, config={})
376
+ self.debugging = given_args.include?("--debug")
377
+ config[:shell] ||= Thor::Base.shell.new
378
+ yield(given_args.dup)
379
+ rescue Thor::Error => e
380
+ debugging ? (raise e) : config[:shell].error(e.message)
381
+ exit(1) if exit_on_failure?
382
+ end
383
+
384
+ def handle_no_task_error(task) #:nodoc:
385
+ if self.banner_base == "thor"
386
+ raise UndefinedTaskError, "Could not find task #{task.inspect} in #{namespace.inspect} namespace."
387
+ else
388
+ raise UndefinedTaskError, "Could not find task #{task.inspect}."
389
+ end
390
+ end
391
+
392
+ protected
393
+
394
+ # Prints the class options per group. If an option does not belong to
395
+ # any group, it's printed as Class option.
396
+ #
397
+ def class_options_help(shell, groups={}) #:nodoc:
398
+ # Group options by group
399
+ class_options.each do |_, value|
400
+ groups[value.group] ||= []
401
+ groups[value.group] << value
402
+ end
403
+
404
+ # Deal with default group
405
+ global_options = groups.delete(nil) || []
406
+ print_options(shell, global_options)
407
+
408
+ # Print all others
409
+ groups.each do |group_name, options|
410
+ print_options(shell, options, group_name)
411
+ end
412
+ end
413
+
414
+ # Receives a set of options and print them.
415
+ def print_options(shell, options, group_name=nil)
416
+ return if options.empty?
417
+
418
+ list = []
419
+ padding = options.collect{ |o| o.aliases.size }.max.to_i * 4
420
+
421
+ options.each do |option|
422
+ item = [ option.usage(padding) ]
423
+ item.push(option.description ? "# #{option.description}" : "")
424
+
425
+ list << item
426
+ list << [ "", "# Default: #{option.default}" ] if option.show_default?
427
+ end
428
+
429
+ shell.say(group_name ? "#{group_name} options:" : "Options:")
430
+ shell.print_table(list, :ident => 2)
431
+ shell.say ""
432
+ end
433
+
434
+ # Raises an error if the word given is a Thor reserved word.
435
+ def is_thor_reserved_word?(word, type) #:nodoc:
436
+ return false unless THOR_RESERVED_WORDS.include?(word.to_s)
437
+ raise "#{word.inspect} is a Thor reserved word and cannot be defined as #{type}"
438
+ end
439
+
440
+ # Build an option and adds it to the given scope.
441
+ #
442
+ # ==== Parameters
443
+ # name<Symbol>:: The name of the argument.
444
+ # options<Hash>:: Described in both class_option and method_option.
445
+ def build_option(name, options, scope) #:nodoc:
446
+ scope[name] = Thor::Option.new(name, options[:desc], options[:required],
447
+ options[:type], options[:default], options[:banner],
448
+ options[:group], options[:aliases])
449
+ end
450
+
451
+ # Receives a hash of options, parse them and add to the scope. This is a
452
+ # fast way to set a bunch of options:
453
+ #
454
+ # build_options :foo => true, :bar => :required, :baz => :string
455
+ #
456
+ # ==== Parameters
457
+ # Hash[Symbol => Object]
458
+ def build_options(options, scope) #:nodoc:
459
+ options.each do |key, value|
460
+ scope[key] = Thor::Option.parse(key, value)
461
+ end
462
+ end
463
+
464
+ # Finds a task with the given name. If the task belongs to the current
465
+ # class, just return it, otherwise dup it and add the fresh copy to the
466
+ # current task hash.
467
+ def find_and_refresh_task(name) #:nodoc:
468
+ task = if task = tasks[name.to_s]
469
+ task
470
+ elsif task = all_tasks[name.to_s]
471
+ tasks[name.to_s] = task.clone
472
+ else
473
+ raise ArgumentError, "You supplied :for => #{name.inspect}, but the task #{name.inspect} could not be found."
474
+ end
475
+ end
476
+
477
+ # Everytime someone inherits from a Thor class, register the klass
478
+ # and file into baseclass.
479
+ def inherited(klass)
480
+ Thor::Base.register_klass_file(klass)
481
+ end
482
+
483
+ # Fire this callback whenever a method is added. Added methods are
484
+ # tracked as tasks by invoking the create_task method.
485
+ def method_added(meth)
486
+ meth = meth.to_s
487
+
488
+ if meth == "initialize"
489
+ initialize_added
490
+ return
491
+ end
492
+
493
+ # Return if it's not a public instance method
494
+ return unless public_instance_methods.include?(meth) ||
495
+ public_instance_methods.include?(meth.to_sym)
496
+
497
+ return if @no_tasks || !create_task(meth)
498
+
499
+ is_thor_reserved_word?(meth, :task)
500
+ Thor::Base.register_klass_file(self)
501
+ end
502
+
503
+ # Retrieves a value from superclass. If it reaches the baseclass,
504
+ # returns default.
505
+ def from_superclass(method, default=nil)
506
+ if self == baseclass || !superclass.respond_to?(method, true)
507
+ default
508
+ else
509
+ value = superclass.send(method)
510
+ value.dup if value
511
+ end
512
+ end
513
+
514
+ # A flag that makes the process exit with status 1 if any error happens.
515
+ def exit_on_failure?
516
+ false
517
+ end
518
+
519
+ # Returns the base for banner.
520
+ def banner_base
521
+ @banner_base ||= $thor_runner ? "thor" : File.basename($0.split(" ").first)
522
+ end
523
+
524
+ # SIGNATURE: Sets the baseclass. This is where the superclass lookup
525
+ # finishes.
526
+ def baseclass #:nodoc:
527
+ end
528
+
529
+ # SIGNATURE: Creates a new task if valid_task? is true. This method is
530
+ # called when a new method is added to the class.
531
+ def create_task(meth) #:nodoc:
532
+ end
533
+
534
+ # SIGNATURE: Defines behavior when the initialize method is added to the
535
+ # class.
536
+ def initialize_added #:nodoc:
537
+ end
538
+ end
539
+ end
540
+ end