amqp-daemon-kit 0.1.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/.gitignore +5 -0
  2. data/Configuration.txt +110 -0
  3. data/Deployment.txt +113 -0
  4. data/History.txt +131 -0
  5. data/Logging.txt +96 -0
  6. data/PostInstall.txt +6 -0
  7. data/README.rdoc +132 -0
  8. data/Rakefile +29 -0
  9. data/RuoteParticipants.txt +113 -0
  10. data/TODO.txt +27 -0
  11. data/bin/daemon-kit +18 -0
  12. data/config/website.yml +2 -0
  13. data/daemon-kit.gemspec +265 -0
  14. data/lib/daemon_kit.rb +60 -0
  15. data/lib/daemon_kit/abstract_logger.rb +249 -0
  16. data/lib/daemon_kit/application.rb +230 -0
  17. data/lib/daemon_kit/arguments.rb +165 -0
  18. data/lib/daemon_kit/commands/console.rb +38 -0
  19. data/lib/daemon_kit/commands/destroy.rb +10 -0
  20. data/lib/daemon_kit/commands/generate.rb +10 -0
  21. data/lib/daemon_kit/config.rb +113 -0
  22. data/lib/daemon_kit/console_daemon.rb +2 -0
  23. data/lib/daemon_kit/core_ext.rb +1 -0
  24. data/lib/daemon_kit/core_ext/configurable.rb +96 -0
  25. data/lib/daemon_kit/core_ext/string.rb +22 -0
  26. data/lib/daemon_kit/cron.rb +67 -0
  27. data/lib/daemon_kit/cucumber/world.rb +38 -0
  28. data/lib/daemon_kit/deployment/capistrano.rb +516 -0
  29. data/lib/daemon_kit/dk_amqp.rb +39 -0
  30. data/lib/daemon_kit/em.rb +43 -0
  31. data/lib/daemon_kit/error_handlers/base.rb +32 -0
  32. data/lib/daemon_kit/error_handlers/hoptoad.rb +180 -0
  33. data/lib/daemon_kit/exceptions.rb +15 -0
  34. data/lib/daemon_kit/generators.rb +67 -0
  35. data/lib/daemon_kit/generators/base.rb +60 -0
  36. data/lib/daemon_kit/initializer.rb +449 -0
  37. data/lib/daemon_kit/jabber.rb +171 -0
  38. data/lib/daemon_kit/nanite.rb +7 -0
  39. data/lib/daemon_kit/nanite/agent.rb +77 -0
  40. data/lib/daemon_kit/pid_file.rb +61 -0
  41. data/lib/daemon_kit/ruote_participants.rb +125 -0
  42. data/lib/daemon_kit/ruote_pseudo_participant.rb +68 -0
  43. data/lib/daemon_kit/ruote_workitem.rb +187 -0
  44. data/lib/daemon_kit/safety.rb +84 -0
  45. data/lib/daemon_kit/tasks.rb +2 -0
  46. data/lib/daemon_kit/tasks/environment.rake +11 -0
  47. data/lib/daemon_kit/tasks/framework.rake +123 -0
  48. data/lib/daemon_kit/tasks/god.rake +62 -0
  49. data/lib/daemon_kit/tasks/log.rake +8 -0
  50. data/lib/daemon_kit/tasks/monit.rake +29 -0
  51. data/lib/daemon_kit/vendor/thor-0.13.6/CHANGELOG.rdoc +89 -0
  52. data/lib/daemon_kit/vendor/thor-0.13.6/LICENSE +20 -0
  53. data/lib/daemon_kit/vendor/thor-0.13.6/README.rdoc +297 -0
  54. data/lib/daemon_kit/vendor/thor-0.13.6/Thorfile +69 -0
  55. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor.rb +244 -0
  56. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions.rb +296 -0
  57. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/create_file.rb +103 -0
  58. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/directory.rb +91 -0
  59. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/empty_directory.rb +134 -0
  60. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/file_manipulation.rb +223 -0
  61. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/actions/inject_into_file.rb +104 -0
  62. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/base.rb +540 -0
  63. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/file_binary_read.rb +9 -0
  64. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  65. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/core_ext/ordered_hash.rb +100 -0
  66. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/error.rb +30 -0
  67. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/group.rb +271 -0
  68. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/invocation.rb +180 -0
  69. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser.rb +4 -0
  70. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/argument.rb +67 -0
  71. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/arguments.rb +150 -0
  72. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/option.rb +128 -0
  73. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/parser/options.rb +169 -0
  74. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/rake_compat.rb +66 -0
  75. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/runner.rb +314 -0
  76. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/shell.rb +83 -0
  77. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/shell/basic.rb +239 -0
  78. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/shell/color.rb +108 -0
  79. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/task.rb +102 -0
  80. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/util.rb +224 -0
  81. data/lib/daemon_kit/vendor/thor-0.13.6/lib/thor/version.rb +3 -0
  82. data/lib/daemon_kit/xmpp.rb +100 -0
  83. data/lib/generators/daemon_kit/amqp/USAGE +5 -0
  84. data/lib/generators/daemon_kit/amqp/amqp_generator.rb +24 -0
  85. data/lib/generators/daemon_kit/amqp/templates/config/amqp.yml +28 -0
  86. data/lib/generators/daemon_kit/amqp/templates/config/pre-daemonize/amqp.rb +7 -0
  87. data/lib/generators/daemon_kit/amqp/templates/libexec/%app_name%-daemon.rb +37 -0
  88. data/lib/generators/daemon_kit/app/USAGE +7 -0
  89. data/lib/generators/daemon_kit/app/app_generator.rb +140 -0
  90. data/lib/generators/daemon_kit/app/templates/Gemfile +12 -0
  91. data/lib/generators/daemon_kit/app/templates/README.tt +58 -0
  92. data/lib/generators/daemon_kit/app/templates/Rakefile +6 -0
  93. data/lib/generators/daemon_kit/app/templates/bin/daemon.tt +7 -0
  94. data/lib/generators/daemon_kit/app/templates/config/arguments.rb +12 -0
  95. data/lib/generators/daemon_kit/app/templates/config/boot.rb +70 -0
  96. data/lib/generators/daemon_kit/app/templates/config/environment.rb.tt +26 -0
  97. data/lib/generators/daemon_kit/app/templates/config/environments/development.rb +2 -0
  98. data/lib/generators/daemon_kit/app/templates/config/environments/production.rb +5 -0
  99. data/lib/generators/daemon_kit/app/templates/config/environments/test.rb +2 -0
  100. data/lib/generators/daemon_kit/app/templates/config/post-daemonize/readme +5 -0
  101. data/lib/generators/daemon_kit/app/templates/config/pre-daemonize/readme +12 -0
  102. data/lib/generators/daemon_kit/app/templates/lib/%app_name%.rb +2 -0
  103. data/lib/generators/daemon_kit/app/templates/libexec/%app_name%-daemon.rb +18 -0
  104. data/lib/generators/daemon_kit/app/templates/script/console +3 -0
  105. data/lib/generators/daemon_kit/app/templates/script/destroy +3 -0
  106. data/lib/generators/daemon_kit/app/templates/script/generate +3 -0
  107. data/lib/generators/daemon_kit/capistrano/capistrano_generator.rb +26 -0
  108. data/lib/generators/daemon_kit/capistrano/templates/Capfile +10 -0
  109. data/lib/generators/daemon_kit/capistrano/templates/USAGE +10 -0
  110. data/lib/generators/daemon_kit/capistrano/templates/config/deploy.rb.tt +67 -0
  111. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/logrotate.erb +13 -0
  112. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/production.rb.tt +6 -0
  113. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/staging.rb.tt +6 -0
  114. data/lib/generators/daemon_kit/capistrano/templates/config/environments/staging.rb +0 -0
  115. data/lib/generators/daemon_kit/cron/USAGE +5 -0
  116. data/lib/generators/daemon_kit/cron/cron_generator.rb +24 -0
  117. data/lib/generators/daemon_kit/cron/templates/config/pre-daemonize/cron.rb +11 -0
  118. data/lib/generators/daemon_kit/cron/templates/libexec/%app_name%-daemon.rb +48 -0
  119. data/lib/generators/daemon_kit/cucumber/USAGE +11 -0
  120. data/lib/generators/daemon_kit/cucumber/cucumber_generator.rb +45 -0
  121. data/lib/generators/daemon_kit/cucumber/templates/config/environments/cucumber.rb +2 -0
  122. data/lib/generators/daemon_kit/cucumber/templates/features/step_definitions/.empty_directory +0 -0
  123. data/lib/generators/daemon_kit/cucumber/templates/features/support/env.rb +7 -0
  124. data/lib/generators/daemon_kit/cucumber/templates/script/cucumber +7 -0
  125. data/lib/generators/daemon_kit/cucumber/templates/tasks/cucumber.rake +13 -0
  126. data/lib/generators/daemon_kit/nanite_agent/USAGE +5 -0
  127. data/lib/generators/daemon_kit/nanite_agent/nanite_agent_generator.rb +29 -0
  128. data/lib/generators/daemon_kit/nanite_agent/templates/config/nanite.yml +35 -0
  129. data/lib/generators/daemon_kit/nanite_agent/templates/config/pre-daemonize/nanite_agent.rb +6 -0
  130. data/lib/generators/daemon_kit/nanite_agent/templates/lib/actors/sample.rb +11 -0
  131. data/lib/generators/daemon_kit/nanite_agent/templates/libexec/%app_name%-daemon.rb +31 -0
  132. data/lib/generators/daemon_kit/rspec/USAGE +5 -0
  133. data/lib/generators/daemon_kit/rspec/rspec_generator.rb +20 -0
  134. data/lib/generators/daemon_kit/rspec/templates/spec/%app_name%_spec.rb +11 -0
  135. data/lib/generators/daemon_kit/rspec/templates/spec/spec.opts +1 -0
  136. data/lib/generators/daemon_kit/rspec/templates/spec/spec_helper.rb +23 -0
  137. data/lib/generators/daemon_kit/rspec/templates/tasks/rspec.rake +21 -0
  138. data/lib/generators/daemon_kit/ruote/USAGE +5 -0
  139. data/lib/generators/daemon_kit/ruote/ruote_generator.rb +29 -0
  140. data/lib/generators/daemon_kit/ruote/templates/config/amqp.yml +30 -0
  141. data/lib/generators/daemon_kit/ruote/templates/config/pre-daemonize/ruote.rb +13 -0
  142. data/lib/generators/daemon_kit/ruote/templates/config/ruote.yml +23 -0
  143. data/lib/generators/daemon_kit/ruote/templates/lib/%app_name%.rb +4 -0
  144. data/lib/generators/daemon_kit/ruote/templates/lib/sample.rb +26 -0
  145. data/lib/generators/daemon_kit/ruote/templates/libexec/%app_name%-daemon.rb +33 -0
  146. data/lib/generators/daemon_kit/test_unit/USAGE +5 -0
  147. data/lib/generators/daemon_kit/test_unit/templates/tasks/test_unit.rake +7 -0
  148. data/lib/generators/daemon_kit/test_unit/templates/test/%app_name%_test.rb.tt +9 -0
  149. data/lib/generators/daemon_kit/test_unit/templates/test/test_helper.rb +6 -0
  150. data/lib/generators/daemon_kit/test_unit/test_unit_generator.rb +20 -0
  151. data/lib/generators/daemon_kit/xmpp/templates/config/pre-daemonize/xmpp.rb +6 -0
  152. data/lib/generators/daemon_kit/xmpp/templates/config/xmpp.yml +29 -0
  153. data/lib/generators/daemon_kit/xmpp/templates/libexec/%app_name%-daemon.rb +27 -0
  154. data/lib/generators/daemon_kit/xmpp/xmpp_generator.rb +24 -0
  155. data/script/console +10 -0
  156. data/script/destroy +14 -0
  157. data/script/generate +14 -0
  158. data/script/txt2html +71 -0
  159. data/spec/abstract_logger_spec.rb +126 -0
  160. data/spec/argument_spec.rb +70 -0
  161. data/spec/config_spec.rb +83 -0
  162. data/spec/configurable_spec.rb +56 -0
  163. data/spec/daemon_kit_spec.rb +7 -0
  164. data/spec/error_handlers_spec.rb +23 -0
  165. data/spec/fixtures/env.yml +15 -0
  166. data/spec/fixtures/noenv.yml +4 -0
  167. data/spec/initializer_spec.rb +26 -0
  168. data/spec/spec.opts +1 -0
  169. data/spec/spec_helper.rb +27 -0
  170. data/tasks/cucumber.rake +13 -0
  171. data/tasks/rspec.rake +20 -0
  172. data/tasks/tests.rake +6 -0
  173. data/templates/god/god.erb +69 -0
  174. data/templates/monit/monit.erb +14 -0
  175. data/test/test_amqp_generator.rb +48 -0
  176. data/test/test_cron_generator.rb +45 -0
  177. data/test/test_daemon-kit_generator.rb +84 -0
  178. data/test/test_daemon_kit_config.rb +28 -0
  179. data/test/test_deploy_capistrano_generator.rb +48 -0
  180. data/test/test_generator_helper.rb +29 -0
  181. data/test/test_helper.rb +7 -0
  182. data/test/test_nanite_agent_generator.rb +49 -0
  183. data/test/test_ruote_generator.rb +51 -0
  184. data/test/test_test_unit_generator.rb +46 -0
  185. metadata +302 -0
@@ -0,0 +1,244 @@
1
+ require 'thor/base'
2
+
3
+ # TODO: Update thor to allow for git-style CLI (git bisect run)
4
+ class Thor
5
+ class << self
6
+ # Sets the default task when thor is executed without an explicit task to be called.
7
+ #
8
+ # ==== Parameters
9
+ # meth<Symbol>:: name of the defaut task
10
+ #
11
+ def default_task(meth=nil)
12
+ case meth
13
+ when :none
14
+ @default_task = 'help'
15
+ when nil
16
+ @default_task ||= from_superclass(:default_task, 'help')
17
+ else
18
+ @default_task = meth.to_s
19
+ end
20
+ end
21
+
22
+ # Defines the usage and the description of the next task.
23
+ #
24
+ # ==== Parameters
25
+ # usage<String>
26
+ # description<String>
27
+ #
28
+ def desc(usage, description, options={})
29
+ if options[:for]
30
+ task = find_and_refresh_task(options[:for])
31
+ task.usage = usage if usage
32
+ task.description = description if description
33
+ else
34
+ @usage, @desc = usage, description
35
+ end
36
+ end
37
+
38
+ # Maps an input to a task. If you define:
39
+ #
40
+ # map "-T" => "list"
41
+ #
42
+ # Running:
43
+ #
44
+ # thor -T
45
+ #
46
+ # Will invoke the list task.
47
+ #
48
+ # ==== Parameters
49
+ # Hash[String|Array => Symbol]:: Maps the string or the strings in the array to the given task.
50
+ #
51
+ def map(mappings=nil)
52
+ @map ||= from_superclass(:map, {})
53
+
54
+ if mappings
55
+ mappings.each do |key, value|
56
+ if key.respond_to?(:each)
57
+ key.each {|subkey| @map[subkey] = value}
58
+ else
59
+ @map[key] = value
60
+ end
61
+ end
62
+ end
63
+
64
+ @map
65
+ end
66
+
67
+ # Declares the options for the next task to be declared.
68
+ #
69
+ # ==== Parameters
70
+ # Hash[Symbol => Object]:: The hash key is the name of the option and the value
71
+ # is the type of the option. Can be :string, :array, :hash, :boolean, :numeric
72
+ # or :required (string). If you give a value, the type of the value is used.
73
+ #
74
+ def method_options(options=nil)
75
+ @method_options ||= {}
76
+ build_options(options, @method_options) if options
77
+ @method_options
78
+ end
79
+
80
+ # Adds an option to the set of method options. If :for is given as option,
81
+ # it allows you to change the options from a previous defined task.
82
+ #
83
+ # def previous_task
84
+ # # magic
85
+ # end
86
+ #
87
+ # method_option :foo => :bar, :for => :previous_task
88
+ #
89
+ # def next_task
90
+ # # magic
91
+ # end
92
+ #
93
+ # ==== Parameters
94
+ # name<Symbol>:: The name of the argument.
95
+ # options<Hash>:: Described below.
96
+ #
97
+ # ==== Options
98
+ # :desc - Description for the argument.
99
+ # :required - If the argument is required or not.
100
+ # :default - Default value for this argument. It cannot be required and have default values.
101
+ # :aliases - Aliases for this option.
102
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
103
+ # :banner - String to show on usage notes.
104
+ #
105
+ def method_option(name, options={})
106
+ scope = if options[:for]
107
+ find_and_refresh_task(options[:for]).options
108
+ else
109
+ method_options
110
+ end
111
+
112
+ build_option(name, options, scope)
113
+ end
114
+
115
+ # Parses the task and options from the given args, instantiate the class
116
+ # and invoke the task. This method is used when the arguments must be parsed
117
+ # from an array. If you are inside Ruby and want to use a Thor class, you
118
+ # can simply initialize it:
119
+ #
120
+ # script = MyScript.new(args, options, config)
121
+ # script.invoke(:task, first_arg, second_arg, third_arg)
122
+ #
123
+ def start(original_args=ARGV, config={})
124
+ super do |given_args|
125
+ meth = normalize_task_name(given_args.shift)
126
+ task = all_tasks[meth]
127
+
128
+ if task
129
+ args, opts = Thor::Options.split(given_args)
130
+ config.merge!(:task_options => task.options)
131
+ else
132
+ args, opts = given_args, {}
133
+ end
134
+
135
+ task ||= Thor::Task::Dynamic.new(meth)
136
+ trailing = args[Range.new(arguments.size, -1)]
137
+ new(args, opts, config).invoke(task, trailing || [])
138
+ end
139
+ end
140
+
141
+ # Prints help information for the given task.
142
+ #
143
+ # ==== Parameters
144
+ # shell<Thor::Shell>
145
+ # task_name<String>
146
+ #
147
+ def task_help(shell, task_name)
148
+ meth = normalize_task_name(task_name)
149
+ task = all_tasks[meth]
150
+ handle_no_task_error(meth) unless task
151
+
152
+ shell.say "Usage:"
153
+ shell.say " #{banner(task)}"
154
+ shell.say
155
+ class_options_help(shell, nil => task.options.map { |_, o| o })
156
+ shell.say task.description
157
+ end
158
+
159
+ # Prints help information for this class.
160
+ #
161
+ # ==== Parameters
162
+ # shell<Thor::Shell>
163
+ #
164
+ def help(shell)
165
+ list = printable_tasks
166
+ Thor::Util.thor_classes_in(self).each do |klass|
167
+ list += klass.printable_tasks(false)
168
+ end
169
+ list.sort!{ |a,b| a[0] <=> b[0] }
170
+
171
+ shell.say "Tasks:"
172
+ shell.print_table(list, :ident => 2, :truncate => true)
173
+ shell.say
174
+ class_options_help(shell)
175
+ end
176
+
177
+ # Returns tasks ready to be printed.
178
+ def printable_tasks(all=true)
179
+ (all ? all_tasks : tasks).map do |_, task|
180
+ item = []
181
+ item << banner(task)
182
+ item << (task.description ? "# #{task.description.gsub(/\s+/m,' ')}" : "")
183
+ item
184
+ end
185
+ end
186
+
187
+ def handle_argument_error(task, error) #:nodoc:
188
+ raise InvocationError, "#{task.name.inspect} was called incorrectly. Call as #{task.formatted_usage(self, banner_base == "thor").inspect}."
189
+ end
190
+
191
+ protected
192
+
193
+ # The banner for this class. You can customize it if you are invoking the
194
+ # thor class by another ways which is not the Thor::Runner. It receives
195
+ # the task that is going to be invoked and a boolean which indicates if
196
+ # the namespace should be displayed as arguments.
197
+ #
198
+ def banner(task)
199
+ "#{banner_base} #{task.formatted_usage(self, banner_base == "thor")}"
200
+ end
201
+
202
+ def baseclass #:nodoc:
203
+ Thor
204
+ end
205
+
206
+ def create_task(meth) #:nodoc:
207
+ if @usage && @desc
208
+ tasks[meth.to_s] = Thor::Task.new(meth, @desc, @usage, method_options)
209
+ @usage, @desc, @method_options = nil
210
+ true
211
+ elsif self.all_tasks[meth.to_s] || meth.to_sym == :method_missing
212
+ true
213
+ else
214
+ puts "[WARNING] Attempted to create task #{meth.inspect} without usage or description. " <<
215
+ "Call desc if you want this method to be available as task or declare it inside a " <<
216
+ "no_tasks{} block. Invoked from #{caller[1].inspect}."
217
+ false
218
+ end
219
+ end
220
+
221
+ def initialize_added #:nodoc:
222
+ class_options.merge!(method_options)
223
+ @method_options = nil
224
+ end
225
+
226
+ # Receives a task name (can be nil), and try to get a map from it.
227
+ # If a map can't be found use the sent name or the default task.
228
+ #
229
+ def normalize_task_name(meth) #:nodoc:
230
+ mapping = map[meth.to_s]
231
+ meth = mapping || meth || default_task
232
+ meth.to_s.gsub('-','_') # treat foo-bar > foo_bar
233
+ end
234
+ end
235
+
236
+ include Thor::Base
237
+
238
+ map HELP_MAPPINGS => :help
239
+
240
+ desc "help [TASK]", "Describe available tasks or one specific task"
241
+ def help(task=nil)
242
+ task ? self.class.task_help(shell, task) : self.class.help(shell)
243
+ end
244
+ end
@@ -0,0 +1,296 @@
1
+ require 'fileutils'
2
+ require 'uri'
3
+ require 'thor/core_ext/file_binary_read'
4
+
5
+ Dir[File.join(File.dirname(__FILE__), "actions", "*.rb")].each do |action|
6
+ require action
7
+ end
8
+
9
+ class Thor
10
+ module Actions
11
+ attr_accessor :behavior
12
+
13
+ def self.included(base) #:nodoc:
14
+ base.extend ClassMethods
15
+ end
16
+
17
+ module ClassMethods
18
+ # Hold source paths for one Thor instance. source_paths_for_search is the
19
+ # method responsible to gather source_paths from this current class,
20
+ # inherited paths and the source root.
21
+ #
22
+ def source_paths
23
+ @_source_paths ||= []
24
+ end
25
+
26
+ # Stores and return the source root for this class
27
+ def source_root(path=nil)
28
+ @_source_root = path if path
29
+ @_source_root
30
+ end
31
+
32
+ # Returns the source paths in the following order:
33
+ #
34
+ # 1) This class source paths
35
+ # 2) Source root
36
+ # 3) Parents source paths
37
+ #
38
+ def source_paths_for_search
39
+ paths = []
40
+ paths += self.source_paths
41
+ paths << self.source_root if self.source_root
42
+ paths += from_superclass(:source_paths, [])
43
+ paths
44
+ end
45
+
46
+ # Add runtime options that help actions execution.
47
+ #
48
+ def add_runtime_options!
49
+ class_option :force, :type => :boolean, :aliases => "-f", :group => :runtime,
50
+ :desc => "Overwrite files that already exist"
51
+
52
+ class_option :pretend, :type => :boolean, :aliases => "-p", :group => :runtime,
53
+ :desc => "Run but do not make any changes"
54
+
55
+ class_option :quiet, :type => :boolean, :aliases => "-q", :group => :runtime,
56
+ :desc => "Supress status output"
57
+
58
+ class_option :skip, :type => :boolean, :aliases => "-s", :group => :runtime,
59
+ :desc => "Skip files that already exist"
60
+ end
61
+ end
62
+
63
+ # Extends initializer to add more configuration options.
64
+ #
65
+ # ==== Configuration
66
+ # behavior<Symbol>:: The actions default behavior. Can be :invoke or :revoke.
67
+ # It also accepts :force, :skip and :pretend to set the behavior
68
+ # and the respective option.
69
+ #
70
+ # destination_root<String>:: The root directory needed for some actions.
71
+ #
72
+ def initialize(args=[], options={}, config={})
73
+ self.behavior = case config[:behavior].to_s
74
+ when "force", "skip"
75
+ _cleanup_options_and_set(options, config[:behavior])
76
+ :invoke
77
+ when "revoke"
78
+ :revoke
79
+ else
80
+ :invoke
81
+ end
82
+
83
+ super
84
+ self.destination_root = config[:destination_root]
85
+ end
86
+
87
+ # Wraps an action object and call it accordingly to the thor class behavior.
88
+ #
89
+ def action(instance) #:nodoc:
90
+ if behavior == :revoke
91
+ instance.revoke!
92
+ else
93
+ instance.invoke!
94
+ end
95
+ end
96
+
97
+ # Returns the root for this thor class (also aliased as destination root).
98
+ #
99
+ def destination_root
100
+ @destination_stack.last
101
+ end
102
+
103
+ # Sets the root for this thor class. Relatives path are added to the
104
+ # directory where the script was invoked and expanded.
105
+ #
106
+ def destination_root=(root)
107
+ @destination_stack ||= []
108
+ @destination_stack[0] = File.expand_path(root || '')
109
+ end
110
+
111
+ # Returns the given path relative to the absolute root (ie, root where
112
+ # the script started).
113
+ #
114
+ def relative_to_original_destination_root(path, remove_dot=true)
115
+ path = path.gsub(@destination_stack[0], '.')
116
+ remove_dot ? (path[2..-1] || '') : path
117
+ end
118
+
119
+ # Holds source paths in instance so they can be manipulated.
120
+ #
121
+ def source_paths
122
+ @source_paths ||= self.class.source_paths_for_search
123
+ end
124
+
125
+ # Receives a file or directory and search for it in the source paths.
126
+ #
127
+ def find_in_source_paths(file)
128
+ relative_root = relative_to_original_destination_root(destination_root, false)
129
+
130
+ source_paths.each do |source|
131
+ source_file = File.expand_path(file, File.join(source, relative_root))
132
+ return source_file if File.exists?(source_file)
133
+ end
134
+
135
+ message = "Could not find #{file.inspect} in any of your source paths. "
136
+
137
+ unless self.class.source_root
138
+ message << "Please invoke #{self.class.name}.source_root(PATH) with the PATH containing your templates. "
139
+ end
140
+
141
+ if source_paths.empty?
142
+ message << "Currently you have no source paths."
143
+ else
144
+ message << "Your current source paths are: \n#{source_paths.join("\n")}"
145
+ end
146
+
147
+ raise Error, message
148
+ end
149
+
150
+ # Do something in the root or on a provided subfolder. If a relative path
151
+ # is given it's referenced from the current root. The full path is yielded
152
+ # to the block you provide. The path is set back to the previous path when
153
+ # the method exits.
154
+ #
155
+ # ==== Parameters
156
+ # dir<String>:: the directory to move to.
157
+ # config<Hash>:: give :verbose => true to log and use padding.
158
+ #
159
+ def inside(dir='', config={}, &block)
160
+ verbose = config.fetch(:verbose, false)
161
+
162
+ say_status :inside, dir, verbose
163
+ shell.padding += 1 if verbose
164
+ @destination_stack.push File.expand_path(dir, destination_root)
165
+
166
+ FileUtils.mkdir_p(destination_root) unless File.exist?(destination_root)
167
+ FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
168
+
169
+ @destination_stack.pop
170
+ shell.padding -= 1 if verbose
171
+ end
172
+
173
+ # Goes to the root and execute the given block.
174
+ #
175
+ def in_root
176
+ inside(@destination_stack.first) { yield }
177
+ end
178
+
179
+ # Loads an external file and execute it in the instance binding.
180
+ #
181
+ # ==== Parameters
182
+ # path<String>:: The path to the file to execute. Can be a web address or
183
+ # a relative path from the source root.
184
+ #
185
+ # ==== Examples
186
+ #
187
+ # apply "http://gist.github.com/103208"
188
+ #
189
+ # apply "recipes/jquery.rb"
190
+ #
191
+ def apply(path, config={})
192
+ verbose = config.fetch(:verbose, true)
193
+ path = find_in_source_paths(path) unless path =~ /^http\:\/\//
194
+
195
+ say_status :apply, path, verbose
196
+ shell.padding += 1 if verbose
197
+
198
+ if URI(path).is_a?(URI::HTTP)
199
+ contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read }
200
+ else
201
+ contents = open(path) {|io| io.read }
202
+ end
203
+
204
+ instance_eval(contents, path)
205
+ shell.padding -= 1 if verbose
206
+ end
207
+
208
+ # Executes a command returning the contents of the command.
209
+ #
210
+ # ==== Parameters
211
+ # command<String>:: the command to be executed.
212
+ # config<Hash>:: give :verbose => false to not log the status. Specify :with
213
+ # to append an executable to command executation.
214
+ #
215
+ # ==== Example
216
+ #
217
+ # inside('vendor') do
218
+ # run('ln -s ~/edge rails')
219
+ # end
220
+ #
221
+ def run(command, config={})
222
+ return unless behavior == :invoke
223
+
224
+ destination = relative_to_original_destination_root(destination_root, false)
225
+ desc = "#{command} from #{destination.inspect}"
226
+
227
+ if config[:with]
228
+ desc = "#{File.basename(config[:with].to_s)} #{desc}"
229
+ command = "#{config[:with]} #{command}"
230
+ end
231
+
232
+ say_status :run, desc, config.fetch(:verbose, true)
233
+ `#{command}` unless options[:pretend]
234
+ end
235
+
236
+ # Executes a ruby script (taking into account WIN32 platform quirks).
237
+ #
238
+ # ==== Parameters
239
+ # command<String>:: the command to be executed.
240
+ # config<Hash>:: give :verbose => false to not log the status.
241
+ #
242
+ def run_ruby_script(command, config={})
243
+ return unless behavior == :invoke
244
+ run command, config.merge(:with => Thor::Util.ruby_command)
245
+ end
246
+
247
+ # Run a thor command. A hash of options can be given and it's converted to
248
+ # switches.
249
+ #
250
+ # ==== Parameters
251
+ # task<String>:: the task to be invoked
252
+ # args<Array>:: arguments to the task
253
+ # config<Hash>:: give :verbose => false to not log the status. Other options
254
+ # are given as parameter to Thor.
255
+ #
256
+ # ==== Examples
257
+ #
258
+ # thor :install, "http://gist.github.com/103208"
259
+ # #=> thor install http://gist.github.com/103208
260
+ #
261
+ # thor :list, :all => true, :substring => 'rails'
262
+ # #=> thor list --all --substring=rails
263
+ #
264
+ def thor(task, *args)
265
+ config = args.last.is_a?(Hash) ? args.pop : {}
266
+ verbose = config.key?(:verbose) ? config.delete(:verbose) : true
267
+ pretend = config.key?(:pretend) ? config.delete(:pretend) : false
268
+
269
+ args.unshift task
270
+ args.push Thor::Options.to_switches(config)
271
+ command = args.join(' ').strip
272
+
273
+ run command, :with => :thor, :verbose => verbose, :pretend => pretend
274
+ end
275
+
276
+ protected
277
+
278
+ # Allow current root to be shared between invocations.
279
+ #
280
+ def _shared_configuration #:nodoc:
281
+ super.merge!(:destination_root => self.destination_root)
282
+ end
283
+
284
+ def _cleanup_options_and_set(options, key) #:nodoc:
285
+ case options
286
+ when Array
287
+ %w(--force -f --skip -s).each { |i| options.delete(i) }
288
+ options << "--#{key}"
289
+ when Hash
290
+ [:force, :skip, "force", "skip"].each { |i| options.delete(i) }
291
+ options.merge!(key => true)
292
+ end
293
+ end
294
+
295
+ end
296
+ end