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,9 @@
1
+ class File #:nodoc:
2
+
3
+ unless File.respond_to?(:binread)
4
+ def self.binread(file)
5
+ File.open(file, 'rb') { |f| f.read }
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,75 @@
1
+ class Thor
2
+ module CoreExt #:nodoc:
3
+
4
+ # A hash with indifferent access and magic predicates.
5
+ #
6
+ # hash = Thor::CoreExt::HashWithIndifferentAccess.new 'foo' => 'bar', 'baz' => 'bee', 'force' => true
7
+ #
8
+ # hash[:foo] #=> 'bar'
9
+ # hash['foo'] #=> 'bar'
10
+ # hash.foo? #=> true
11
+ #
12
+ class HashWithIndifferentAccess < ::Hash #:nodoc:
13
+
14
+ def initialize(hash={})
15
+ super()
16
+ hash.each do |key, value|
17
+ self[convert_key(key)] = value
18
+ end
19
+ end
20
+
21
+ def [](key)
22
+ super(convert_key(key))
23
+ end
24
+
25
+ def []=(key, value)
26
+ super(convert_key(key), value)
27
+ end
28
+
29
+ def delete(key)
30
+ super(convert_key(key))
31
+ end
32
+
33
+ def values_at(*indices)
34
+ indices.collect { |key| self[convert_key(key)] }
35
+ end
36
+
37
+ def merge(other)
38
+ dup.merge!(other)
39
+ end
40
+
41
+ def merge!(other)
42
+ other.each do |key, value|
43
+ self[convert_key(key)] = value
44
+ end
45
+ self
46
+ end
47
+
48
+ protected
49
+
50
+ def convert_key(key)
51
+ key.is_a?(Symbol) ? key.to_s : key
52
+ end
53
+
54
+ # Magic predicates. For instance:
55
+ #
56
+ # options.force? # => !!options['force']
57
+ # options.shebang # => "/usr/lib/local/ruby"
58
+ # options.test_framework?(:rspec) # => options[:test_framework] == :rspec
59
+ #
60
+ def method_missing(method, *args, &block)
61
+ method = method.to_s
62
+ if method =~ /^(\w+)\?$/
63
+ if args.empty?
64
+ !!self[$1]
65
+ else
66
+ self[$1] == args.first
67
+ end
68
+ else
69
+ self[method]
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,100 @@
1
+ class Thor
2
+ module CoreExt #:nodoc:
3
+
4
+ if RUBY_VERSION >= '1.9'
5
+ class OrderedHash < ::Hash
6
+ end
7
+ else
8
+ # This class is based on the Ruby 1.9 ordered hashes.
9
+ #
10
+ # It keeps the semantics and most of the efficiency of normal hashes
11
+ # while also keeping track of the order in which elements were set.
12
+ #
13
+ class OrderedHash #:nodoc:
14
+ include Enumerable
15
+
16
+ Node = Struct.new(:key, :value, :next, :prev)
17
+
18
+ def initialize
19
+ @hash = {}
20
+ end
21
+
22
+ def [](key)
23
+ @hash[key] && @hash[key].value
24
+ end
25
+
26
+ def []=(key, value)
27
+ if node = @hash[key]
28
+ node.value = value
29
+ else
30
+ node = Node.new(key, value)
31
+
32
+ if @first.nil?
33
+ @first = @last = node
34
+ else
35
+ node.prev = @last
36
+ @last.next = node
37
+ @last = node
38
+ end
39
+ end
40
+
41
+ @hash[key] = node
42
+ value
43
+ end
44
+
45
+ def delete(key)
46
+ if node = @hash[key]
47
+ prev_node = node.prev
48
+ next_node = node.next
49
+
50
+ next_node.prev = prev_node if next_node
51
+ prev_node.next = next_node if prev_node
52
+
53
+ @first = next_node if @first == node
54
+ @last = prev_node if @last == node
55
+
56
+ value = node.value
57
+ end
58
+
59
+ @hash.delete(key)
60
+ value
61
+ end
62
+
63
+ def keys
64
+ self.map { |k, v| k }
65
+ end
66
+
67
+ def values
68
+ self.map { |k, v| v }
69
+ end
70
+
71
+ def each
72
+ return unless @first
73
+ yield [@first.key, @first.value]
74
+ node = @first
75
+ yield [node.key, node.value] while node = node.next
76
+ self
77
+ end
78
+
79
+ def merge(other)
80
+ hash = self.class.new
81
+
82
+ self.each do |key, value|
83
+ hash[key] = value
84
+ end
85
+
86
+ other.each do |key, value|
87
+ hash[key] = value
88
+ end
89
+
90
+ hash
91
+ end
92
+
93
+ def empty?
94
+ @hash.empty?
95
+ end
96
+ end
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,30 @@
1
+ class Thor
2
+ # Thor::Error is raised when it's caused by wrong usage of thor classes. Those
3
+ # errors have their backtrace supressed and are nicely shown to the user.
4
+ #
5
+ # Errors that are caused by the developer, like declaring a method which
6
+ # overwrites a thor keyword, it SHOULD NOT raise a Thor::Error. This way, we
7
+ # ensure that developer errors are shown with full backtrace.
8
+ #
9
+ class Error < StandardError
10
+ end
11
+
12
+ # Raised when a task was not found.
13
+ #
14
+ class UndefinedTaskError < Error
15
+ end
16
+
17
+ # Raised when a task was found, but not invoked properly.
18
+ #
19
+ class InvocationError < Error
20
+ end
21
+
22
+ class UnknownArgumentError < Error
23
+ end
24
+
25
+ class RequiredArgumentMissingError < InvocationError
26
+ end
27
+
28
+ class MalformattedArgumentError < InvocationError
29
+ end
30
+ end
@@ -0,0 +1,271 @@
1
+ require 'thor/base'
2
+
3
+ # Thor has a special class called Thor::Group. The main difference to Thor class
4
+ # is that it invokes all tasks at once. It also include some methods that allows
5
+ # invocations to be done at the class method, which are not available to Thor
6
+ # tasks.
7
+ class Thor::Group
8
+ class << self
9
+ # The descrition for this Thor::Group. If none is provided, but a source root
10
+ # exists, tries to find the USAGE one folder above it, otherwise searches
11
+ # in the superclass.
12
+ #
13
+ # ==== Parameters
14
+ # description<String>:: The description for this Thor::Group.
15
+ #
16
+ def desc(description=nil)
17
+ case description
18
+ when nil
19
+ @desc ||= from_superclass(:desc, nil)
20
+ else
21
+ @desc = description
22
+ end
23
+ end
24
+
25
+ # Start works differently in Thor::Group, it simply invokes all tasks
26
+ # inside the class.
27
+ #
28
+ def start(original_args=ARGV, config={})
29
+ super do |given_args|
30
+ if Thor::HELP_MAPPINGS.include?(given_args.first)
31
+ help(config[:shell])
32
+ return
33
+ end
34
+
35
+ args, opts = Thor::Options.split(given_args)
36
+ new(args, opts, config).invoke
37
+ end
38
+ end
39
+
40
+ # Prints help information.
41
+ #
42
+ # ==== Options
43
+ # short:: When true, shows only usage.
44
+ #
45
+ def help(shell)
46
+ shell.say "Usage:"
47
+ shell.say " #{banner}\n"
48
+ shell.say
49
+ class_options_help(shell)
50
+ shell.say self.desc if self.desc
51
+ end
52
+
53
+ # Stores invocations for this class merging with superclass values.
54
+ #
55
+ def invocations #:nodoc:
56
+ @invocations ||= from_superclass(:invocations, {})
57
+ end
58
+
59
+ # Stores invocation blocks used on invoke_from_option.
60
+ #
61
+ def invocation_blocks #:nodoc:
62
+ @invocation_blocks ||= from_superclass(:invocation_blocks, {})
63
+ end
64
+
65
+ # Invoke the given namespace or class given. It adds an instance
66
+ # method that will invoke the klass and task. You can give a block to
67
+ # configure how it will be invoked.
68
+ #
69
+ # The namespace/class given will have its options showed on the help
70
+ # usage. Check invoke_from_option for more information.
71
+ #
72
+ def invoke(*names, &block)
73
+ options = names.last.is_a?(Hash) ? names.pop : {}
74
+ verbose = options.fetch(:verbose, true)
75
+
76
+ names.each do |name|
77
+ invocations[name] = false
78
+ invocation_blocks[name] = block if block_given?
79
+
80
+ class_eval <<-METHOD, __FILE__, __LINE__
81
+ def _invoke_#{name.to_s.gsub(/\W/, '_')}
82
+ klass, task = self.class.prepare_for_invocation(nil, #{name.inspect})
83
+
84
+ if klass
85
+ say_status :invoke, #{name.inspect}, #{verbose.inspect}
86
+ block = self.class.invocation_blocks[#{name.inspect}]
87
+ _invoke_for_class_method klass, task, &block
88
+ else
89
+ say_status :error, %(#{name.inspect} [not found]), :red
90
+ end
91
+ end
92
+ METHOD
93
+ end
94
+ end
95
+
96
+ # Invoke a thor class based on the value supplied by the user to the
97
+ # given option named "name". A class option must be created before this
98
+ # method is invoked for each name given.
99
+ #
100
+ # ==== Examples
101
+ #
102
+ # class GemGenerator < Thor::Group
103
+ # class_option :test_framework, :type => :string
104
+ # invoke_from_option :test_framework
105
+ # end
106
+ #
107
+ # ==== Boolean options
108
+ #
109
+ # In some cases, you want to invoke a thor class if some option is true or
110
+ # false. This is automatically handled by invoke_from_option. Then the
111
+ # option name is used to invoke the generator.
112
+ #
113
+ # ==== Preparing for invocation
114
+ #
115
+ # In some cases you want to customize how a specified hook is going to be
116
+ # invoked. You can do that by overwriting the class method
117
+ # prepare_for_invocation. The class method must necessarily return a klass
118
+ # and an optional task.
119
+ #
120
+ # ==== Custom invocations
121
+ #
122
+ # You can also supply a block to customize how the option is giong to be
123
+ # invoked. The block receives two parameters, an instance of the current
124
+ # class and the klass to be invoked.
125
+ #
126
+ def invoke_from_option(*names, &block)
127
+ options = names.last.is_a?(Hash) ? names.pop : {}
128
+ verbose = options.fetch(:verbose, :white)
129
+
130
+ names.each do |name|
131
+ unless class_options.key?(name)
132
+ raise ArgumentError, "You have to define the option #{name.inspect} " <<
133
+ "before setting invoke_from_option."
134
+ end
135
+
136
+ invocations[name] = true
137
+ invocation_blocks[name] = block if block_given?
138
+
139
+ class_eval <<-METHOD, __FILE__, __LINE__
140
+ def _invoke_from_option_#{name.to_s.gsub(/\W/, '_')}
141
+ return unless options[#{name.inspect}]
142
+
143
+ value = options[#{name.inspect}]
144
+ value = #{name.inspect} if TrueClass === value
145
+ klass, task = self.class.prepare_for_invocation(#{name.inspect}, value)
146
+
147
+ if klass
148
+ say_status :invoke, value, #{verbose.inspect}
149
+ block = self.class.invocation_blocks[#{name.inspect}]
150
+ _invoke_for_class_method klass, task, &block
151
+ else
152
+ say_status :error, %(\#{value} [not found]), :red
153
+ end
154
+ end
155
+ METHOD
156
+ end
157
+ end
158
+
159
+ # Remove a previously added invocation.
160
+ #
161
+ # ==== Examples
162
+ #
163
+ # remove_invocation :test_framework
164
+ #
165
+ def remove_invocation(*names)
166
+ names.each do |name|
167
+ remove_task(name)
168
+ remove_class_option(name)
169
+ invocations.delete(name)
170
+ invocation_blocks.delete(name)
171
+ end
172
+ end
173
+
174
+ # Overwrite class options help to allow invoked generators options to be
175
+ # shown recursively when invoking a generator.
176
+ #
177
+ def class_options_help(shell, groups={}) #:nodoc:
178
+ get_options_from_invocations(groups, class_options) do |klass|
179
+ klass.send(:get_options_from_invocations, groups, class_options)
180
+ end
181
+ super(shell, groups)
182
+ end
183
+
184
+ # Get invocations array and merge options from invocations. Those
185
+ # options are added to group_options hash. Options that already exists
186
+ # in base_options are not added twice.
187
+ #
188
+ def get_options_from_invocations(group_options, base_options) #:nodoc:
189
+ invocations.each do |name, from_option|
190
+ value = if from_option
191
+ option = class_options[name]
192
+ option.type == :boolean ? name : option.default
193
+ else
194
+ name
195
+ end
196
+ next unless value
197
+
198
+ klass, task = prepare_for_invocation(name, value)
199
+ next unless klass && klass.respond_to?(:class_options)
200
+
201
+ value = value.to_s
202
+ human_name = value.respond_to?(:classify) ? value.classify : value
203
+
204
+ group_options[human_name] ||= []
205
+ group_options[human_name] += klass.class_options.values.select do |option|
206
+ base_options[option.name.to_sym].nil? && option.group.nil? &&
207
+ !group_options.values.flatten.any? { |i| i.name == option.name }
208
+ end
209
+
210
+ yield klass if block_given?
211
+ end
212
+ end
213
+
214
+ # Returns tasks ready to be printed.
215
+ def printable_tasks(*)
216
+ item = []
217
+ item << banner
218
+ item << (desc ? "# #{desc.gsub(/\s+/m,' ')}" : "")
219
+ [item]
220
+ end
221
+
222
+ def handle_argument_error(task, error) #:nodoc:
223
+ raise error, "#{task.name.inspect} was called incorrectly. Are you sure it has arity equals to 0?"
224
+ end
225
+
226
+ protected
227
+
228
+ # The banner for this class. You can customize it if you are invoking the
229
+ # thor class by another ways which is not the Thor::Runner.
230
+ def banner
231
+ "#{banner_base} #{self_task.formatted_usage(self, false)}"
232
+ end
233
+
234
+ # Represents the whole class as a task.
235
+ def self_task #:nodoc:
236
+ Thor::Task::Dynamic.new(self.namespace, class_options)
237
+ end
238
+
239
+ def baseclass #:nodoc:
240
+ Thor::Group
241
+ end
242
+
243
+ def create_task(meth) #:nodoc:
244
+ tasks[meth.to_s] = Thor::Task.new(meth, nil, nil, nil)
245
+ true
246
+ end
247
+ end
248
+
249
+ include Thor::Base
250
+
251
+ protected
252
+
253
+ # Shortcut to invoke with padding and block handling. Use internally by
254
+ # invoke and invoke_from_option class methods.
255
+ def _invoke_for_class_method(klass, task=nil, *args, &block) #:nodoc:
256
+ with_padding do
257
+ if block
258
+ case block.arity
259
+ when 3
260
+ block.call(self, klass, task)
261
+ when 2
262
+ block.call(self, klass)
263
+ when 1
264
+ instance_exec(klass, &block)
265
+ end
266
+ else
267
+ invoke klass, task, *args
268
+ end
269
+ end
270
+ end
271
+ end