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,103 @@
1
+ require 'thor/actions/empty_directory'
2
+
3
+ class Thor
4
+ module Actions
5
+
6
+ # Create a new file relative to the destination root with the given data,
7
+ # which is the return value of a block or a data string.
8
+ #
9
+ # ==== Parameters
10
+ # destination<String>:: the relative path to the destination root.
11
+ # data<String|NilClass>:: the data to append to the file.
12
+ # config<Hash>:: give :verbose => false to not log the status.
13
+ #
14
+ # ==== Examples
15
+ #
16
+ # create_file "lib/fun_party.rb" do
17
+ # hostname = ask("What is the virtual hostname I should use?")
18
+ # "vhost.name = #{hostname}"
19
+ # end
20
+ #
21
+ # create_file "config/apach.conf", "your apache config"
22
+ #
23
+ def create_file(destination, data=nil, config={}, &block)
24
+ action CreateFile.new(self, destination, block || data.to_s, config)
25
+ end
26
+ alias :add_file :create_file
27
+
28
+ # AddFile is a subset of Template, which instead of rendering a file with
29
+ # ERB, it gets the content from the user.
30
+ #
31
+ class CreateFile < EmptyDirectory #:nodoc:
32
+ attr_reader :data
33
+
34
+ def initialize(base, destination, data, config={})
35
+ @data = data
36
+ super(base, destination, config)
37
+ end
38
+
39
+ # Checks if the content of the file at the destination is identical to the rendered result.
40
+ #
41
+ # ==== Returns
42
+ # Boolean:: true if it is identical, false otherwise.
43
+ #
44
+ def identical?
45
+ exists? && File.binread(destination) == render
46
+ end
47
+
48
+ # Holds the content to be added to the file.
49
+ #
50
+ def render
51
+ @render ||= if data.is_a?(Proc)
52
+ data.call
53
+ else
54
+ data
55
+ end
56
+ end
57
+
58
+ def invoke!
59
+ invoke_with_conflict_check do
60
+ FileUtils.mkdir_p(File.dirname(destination))
61
+ File.open(destination, 'wb') { |f| f.write render }
62
+ end
63
+ given_destination
64
+ end
65
+
66
+ protected
67
+
68
+ # Now on conflict we check if the file is identical or not.
69
+ #
70
+ def on_conflict_behavior(&block)
71
+ if identical?
72
+ say_status :identical, :blue
73
+ else
74
+ options = base.options.merge(config)
75
+ force_or_skip_or_conflict(options[:force], options[:skip], &block)
76
+ end
77
+ end
78
+
79
+ # If force is true, run the action, otherwise check if it's not being
80
+ # skipped. If both are false, show the file_collision menu, if the menu
81
+ # returns true, force it, otherwise skip.
82
+ #
83
+ def force_or_skip_or_conflict(force, skip, &block)
84
+ if force
85
+ say_status :force, :yellow
86
+ block.call unless pretend?
87
+ elsif skip
88
+ say_status :skip, :yellow
89
+ else
90
+ say_status :conflict, :red
91
+ force_or_skip_or_conflict(force_on_collision?, true, &block)
92
+ end
93
+ end
94
+
95
+ # Shows the file collision menu to the user and gets the result.
96
+ #
97
+ def force_on_collision?
98
+ base.shell.file_collision(destination){ render }
99
+ end
100
+
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,91 @@
1
+ require 'thor/actions/empty_directory'
2
+
3
+ class Thor
4
+ module Actions
5
+
6
+ # Copies recursively the files from source directory to root directory.
7
+ # If any of the files finishes with .tt, it's considered to be a template
8
+ # and is placed in the destination without the extension .tt. If any
9
+ # empty directory is found, it's copied and all .empty_directory files are
10
+ # ignored. Remember that file paths can also be encoded, let's suppose a doc
11
+ # directory with the following files:
12
+ #
13
+ # doc/
14
+ # components/.empty_directory
15
+ # README
16
+ # rdoc.rb.tt
17
+ # %app_name%.rb
18
+ #
19
+ # When invoked as:
20
+ #
21
+ # directory "doc"
22
+ #
23
+ # It will create a doc directory in the destination with the following
24
+ # files (assuming that the app_name is "blog"):
25
+ #
26
+ # doc/
27
+ # components/
28
+ # README
29
+ # rdoc.rb
30
+ # blog.rb
31
+ #
32
+ # ==== Parameters
33
+ # source<String>:: the relative path to the source root.
34
+ # destination<String>:: the relative path to the destination root.
35
+ # config<Hash>:: give :verbose => false to not log the status.
36
+ # If :recursive => false, does not look for paths recursively.
37
+ #
38
+ # ==== Examples
39
+ #
40
+ # directory "doc"
41
+ # directory "doc", "docs", :recursive => false
42
+ #
43
+ def directory(source, destination=nil, config={}, &block)
44
+ action Directory.new(self, source, destination || source, config, &block)
45
+ end
46
+
47
+ class Directory < EmptyDirectory #:nodoc:
48
+ attr_reader :source
49
+
50
+ def initialize(base, source, destination=nil, config={}, &block)
51
+ @source = File.expand_path(base.find_in_source_paths(source.to_s))
52
+ @block = block
53
+ super(base, destination, { :recursive => true }.merge(config))
54
+ end
55
+
56
+ def invoke!
57
+ base.empty_directory given_destination, config
58
+ execute!
59
+ end
60
+
61
+ def revoke!
62
+ execute!
63
+ end
64
+
65
+ protected
66
+
67
+ def execute!
68
+ lookup = config[:recursive] ? File.join(source, '**') : source
69
+ lookup = File.join(lookup, '{*,.[a-z]*}')
70
+
71
+ Dir[lookup].each do |file_source|
72
+ next if File.directory?(file_source)
73
+ file_destination = File.join(given_destination, file_source.gsub(source, '.'))
74
+ file_destination.gsub!('/./', '/')
75
+
76
+ case file_source
77
+ when /\.empty_directory$/
78
+ dirname = File.dirname(file_destination).gsub(/\/\.$/, '')
79
+ next if dirname == given_destination
80
+ base.empty_directory(dirname, config)
81
+ when /\.tt$/
82
+ destination = base.template(file_source, file_destination[0..-4], config, &@block)
83
+ else
84
+ destination = base.copy_file(file_source, file_destination, config, &@block)
85
+ end
86
+ end
87
+ end
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,134 @@
1
+ class Thor
2
+ module Actions
3
+
4
+ # Creates an empty directory.
5
+ #
6
+ # ==== Parameters
7
+ # destination<String>:: the relative path to the destination root.
8
+ # config<Hash>:: give :verbose => false to not log the status.
9
+ #
10
+ # ==== Examples
11
+ #
12
+ # empty_directory "doc"
13
+ #
14
+ def empty_directory(destination, config={})
15
+ action EmptyDirectory.new(self, destination, config)
16
+ end
17
+
18
+ # Class which holds create directory logic. This is the base class for
19
+ # other actions like create_file and directory.
20
+ #
21
+ # This implementation is based in Templater actions, created by Jonas Nicklas
22
+ # and Michael S. Klishin under MIT LICENSE.
23
+ #
24
+ class EmptyDirectory #:nodoc:
25
+ attr_reader :base, :destination, :given_destination, :relative_destination, :config
26
+
27
+ # Initializes given the source and destination.
28
+ #
29
+ # ==== Parameters
30
+ # base<Thor::Base>:: A Thor::Base instance
31
+ # source<String>:: Relative path to the source of this file
32
+ # destination<String>:: Relative path to the destination of this file
33
+ # config<Hash>:: give :verbose => false to not log the status.
34
+ #
35
+ def initialize(base, destination, config={})
36
+ @base, @config = base, { :verbose => true }.merge(config)
37
+ self.destination = destination
38
+ end
39
+
40
+ # Checks if the destination file already exists.
41
+ #
42
+ # ==== Returns
43
+ # Boolean:: true if the file exists, false otherwise.
44
+ #
45
+ def exists?
46
+ ::File.exists?(destination)
47
+ end
48
+
49
+ def invoke!
50
+ invoke_with_conflict_check do
51
+ ::FileUtils.mkdir_p(destination)
52
+ end
53
+ end
54
+
55
+ def revoke!
56
+ say_status :remove, :red
57
+ ::FileUtils.rm_rf(destination) if !pretend? && exists?
58
+ given_destination
59
+ end
60
+
61
+ protected
62
+
63
+ # Shortcut for pretend.
64
+ #
65
+ def pretend?
66
+ base.options[:pretend]
67
+ end
68
+
69
+ # Sets the absolute destination value from a relative destination value.
70
+ # It also stores the given and relative destination. Let's suppose our
71
+ # script is being executed on "dest", it sets the destination root to
72
+ # "dest". The destination, given_destination and relative_destination
73
+ # are related in the following way:
74
+ #
75
+ # inside "bar" do
76
+ # empty_directory "baz"
77
+ # end
78
+ #
79
+ # destination #=> dest/bar/baz
80
+ # relative_destination #=> bar/baz
81
+ # given_destination #=> baz
82
+ #
83
+ def destination=(destination)
84
+ if destination
85
+ @given_destination = convert_encoded_instructions(destination.to_s)
86
+ @destination = ::File.expand_path(@given_destination, base.destination_root)
87
+ @relative_destination = base.relative_to_original_destination_root(@destination)
88
+ end
89
+ end
90
+
91
+ # Filenames in the encoded form are converted. If you have a file:
92
+ #
93
+ # %class_name%.rb
94
+ #
95
+ # It gets the class name from the base and replace it:
96
+ #
97
+ # user.rb
98
+ #
99
+ def convert_encoded_instructions(filename)
100
+ filename.gsub(/%(.*?)%/) do |string|
101
+ instruction = $1.strip
102
+ base.respond_to?(instruction) ? base.send(instruction) : string
103
+ end
104
+ end
105
+
106
+ # Receives a hash of options and just execute the block if some
107
+ # conditions are met.
108
+ #
109
+ def invoke_with_conflict_check(&block)
110
+ if exists?
111
+ on_conflict_behavior(&block)
112
+ else
113
+ say_status :create, :green
114
+ block.call unless pretend?
115
+ end
116
+
117
+ destination
118
+ end
119
+
120
+ # What to do when the destination file already exists.
121
+ #
122
+ def on_conflict_behavior(&block)
123
+ say_status :exist, :blue
124
+ end
125
+
126
+ # Shortcut to say_status shell method.
127
+ #
128
+ def say_status(status, color)
129
+ base.shell.say_status status, relative_destination, color if config[:verbose]
130
+ end
131
+
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,223 @@
1
+ require 'erb'
2
+ require 'open-uri'
3
+
4
+ class Thor
5
+ module Actions
6
+
7
+ # Copies the file from the relative source to the relative destination. If
8
+ # the destination is not given it's assumed to be equal to the source.
9
+ #
10
+ # ==== Parameters
11
+ # source<String>:: the relative path to the source root.
12
+ # destination<String>:: the relative path to the destination root.
13
+ # config<Hash>:: give :verbose => false to not log the status.
14
+ #
15
+ # ==== Examples
16
+ #
17
+ # copy_file "README", "doc/README"
18
+ #
19
+ # copy_file "doc/README"
20
+ #
21
+ def copy_file(source, destination=nil, config={}, &block)
22
+ destination ||= source
23
+ source = File.expand_path(find_in_source_paths(source.to_s))
24
+
25
+ create_file destination, nil, config do
26
+ content = File.binread(source)
27
+ content = block.call(content) if block
28
+ content
29
+ end
30
+ end
31
+
32
+ # Gets the content at the given address and places it at the given relative
33
+ # destination. If a block is given instead of destination, the content of
34
+ # the url is yielded and used as location.
35
+ #
36
+ # ==== Parameters
37
+ # source<String>:: the address of the given content.
38
+ # destination<String>:: the relative path to the destination root.
39
+ # config<Hash>:: give :verbose => false to not log the status.
40
+ #
41
+ # ==== Examples
42
+ #
43
+ # get "http://gist.github.com/103208", "doc/README"
44
+ #
45
+ # get "http://gist.github.com/103208" do |content|
46
+ # content.split("\n").first
47
+ # end
48
+ #
49
+ def get(source, destination=nil, config={}, &block)
50
+ source = File.expand_path(find_in_source_paths(source.to_s)) unless source =~ /^http\:\/\//
51
+ render = open(source) {|input| input.binmode.read }
52
+
53
+ destination ||= if block_given?
54
+ block.arity == 1 ? block.call(render) : block.call
55
+ else
56
+ File.basename(source)
57
+ end
58
+
59
+ create_file destination, render, config
60
+ end
61
+
62
+ # Gets an ERB template at the relative source, executes it and makes a copy
63
+ # at the relative destination. If the destination is not given it's assumed
64
+ # to be equal to the source removing .tt from the filename.
65
+ #
66
+ # ==== Parameters
67
+ # source<String>:: the relative path to the source root.
68
+ # destination<String>:: the relative path to the destination root.
69
+ # config<Hash>:: give :verbose => false to not log the status.
70
+ #
71
+ # ==== Examples
72
+ #
73
+ # template "README", "doc/README"
74
+ #
75
+ # template "doc/README"
76
+ #
77
+ def template(source, destination=nil, config={}, &block)
78
+ destination ||= source
79
+ source = File.expand_path(find_in_source_paths(source.to_s))
80
+ context = instance_eval('binding')
81
+
82
+ create_file destination, nil, config do
83
+ content = ERB.new(::File.binread(source), nil, '-').result(context)
84
+ content = block.call(content) if block
85
+ content
86
+ end
87
+ end
88
+
89
+ # Changes the mode of the given file or directory.
90
+ #
91
+ # ==== Parameters
92
+ # mode<Integer>:: the file mode
93
+ # path<String>:: the name of the file to change mode
94
+ # config<Hash>:: give :verbose => false to not log the status.
95
+ #
96
+ # ==== Example
97
+ #
98
+ # chmod "script/*", 0755
99
+ #
100
+ def chmod(path, mode, config={})
101
+ return unless behavior == :invoke
102
+ path = File.expand_path(path, destination_root)
103
+ say_status :chmod, relative_to_original_destination_root(path), config.fetch(:verbose, true)
104
+ FileUtils.chmod_R(mode, path) unless options[:pretend]
105
+ end
106
+
107
+ # Prepend text to a file. Since it depends on inject_into_file, it's reversible.
108
+ #
109
+ # ==== Parameters
110
+ # path<String>:: path of the file to be changed
111
+ # data<String>:: the data to prepend to the file, can be also given as a block.
112
+ # config<Hash>:: give :verbose => false to not log the status.
113
+ #
114
+ # ==== Example
115
+ #
116
+ # prepend_file 'config/environments/test.rb', 'config.gem "rspec"'
117
+ #
118
+ # prepend_file 'config/environments/test.rb' do
119
+ # 'config.gem "rspec"'
120
+ # end
121
+ #
122
+ def prepend_file(path, *args, &block)
123
+ config = args.last.is_a?(Hash) ? args.pop : {}
124
+ config.merge!(:after => /\A/)
125
+ inject_into_file(path, *(args << config), &block)
126
+ end
127
+
128
+ # Append text to a file. Since it depends on inject_into_file, it's reversible.
129
+ #
130
+ # ==== Parameters
131
+ # path<String>:: path of the file to be changed
132
+ # data<String>:: the data to append to the file, can be also given as a block.
133
+ # config<Hash>:: give :verbose => false to not log the status.
134
+ #
135
+ # ==== Example
136
+ #
137
+ # append_file 'config/environments/test.rb', 'config.gem "rspec"'
138
+ #
139
+ # append_file 'config/environments/test.rb' do
140
+ # 'config.gem "rspec"'
141
+ # end
142
+ #
143
+ def append_file(path, *args, &block)
144
+ config = args.last.is_a?(Hash) ? args.pop : {}
145
+ config.merge!(:before => /\z/)
146
+ inject_into_file(path, *(args << config), &block)
147
+ end
148
+
149
+ # Injects text right after the class definition. Since it depends on
150
+ # inject_into_file, it's reversible.
151
+ #
152
+ # ==== Parameters
153
+ # path<String>:: path of the file to be changed
154
+ # klass<String|Class>:: the class to be manipulated
155
+ # data<String>:: the data to append to the class, can be also given as a block.
156
+ # config<Hash>:: give :verbose => false to not log the status.
157
+ #
158
+ # ==== Examples
159
+ #
160
+ # inject_into_class "app/controllers/application_controller.rb", " filter_parameter :password\n"
161
+ #
162
+ # inject_into_class "app/controllers/application_controller.rb", ApplicationController do
163
+ # " filter_parameter :password\n"
164
+ # end
165
+ #
166
+ def inject_into_class(path, klass, *args, &block)
167
+ config = args.last.is_a?(Hash) ? args.pop : {}
168
+ config.merge!(:after => /class #{klass}\n|class #{klass} .*\n/)
169
+ inject_into_file(path, *(args << config), &block)
170
+ end
171
+
172
+ # Run a regular expression replacement on a file.
173
+ #
174
+ # ==== Parameters
175
+ # path<String>:: path of the file to be changed
176
+ # flag<Regexp|String>:: the regexp or string to be replaced
177
+ # replacement<String>:: the replacement, can be also given as a block
178
+ # config<Hash>:: give :verbose => false to not log the status.
179
+ #
180
+ # ==== Example
181
+ #
182
+ # gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'
183
+ #
184
+ # gsub_file 'README', /rake/, :green do |match|
185
+ # match << " no more. Use thor!"
186
+ # end
187
+ #
188
+ def gsub_file(path, flag, *args, &block)
189
+ return unless behavior == :invoke
190
+ config = args.last.is_a?(Hash) ? args.pop : {}
191
+
192
+ path = File.expand_path(path, destination_root)
193
+ say_status :gsub, relative_to_original_destination_root(path), config.fetch(:verbose, true)
194
+
195
+ unless options[:pretend]
196
+ content = File.binread(path)
197
+ content.gsub!(flag, *args, &block)
198
+ File.open(path, 'wb') { |file| file.write(content) }
199
+ end
200
+ end
201
+
202
+ # Removes a file at the given location.
203
+ #
204
+ # ==== Parameters
205
+ # path<String>:: path of the file to be changed
206
+ # config<Hash>:: give :verbose => false to not log the status.
207
+ #
208
+ # ==== Example
209
+ #
210
+ # remove_file 'README'
211
+ # remove_file 'app/controllers/application_controller.rb'
212
+ #
213
+ def remove_file(path, config={})
214
+ return unless behavior == :invoke
215
+ path = File.expand_path(path, destination_root)
216
+
217
+ say_status :remove, relative_to_original_destination_root(path), config.fetch(:verbose, true)
218
+ ::FileUtils.rm_rf(path) if !options[:pretend] && File.exists?(path)
219
+ end
220
+ alias :remove_dir :remove_file
221
+
222
+ end
223
+ end