sneakin-capistrano 2.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. data/CHANGELOG.rdoc +761 -0
  2. data/Manifest +104 -0
  3. data/README.rdoc +66 -0
  4. data/Rakefile +35 -0
  5. data/bin/cap +4 -0
  6. data/bin/capify +78 -0
  7. data/capistrano.gemspec +48 -0
  8. data/examples/sample.rb +14 -0
  9. data/lib/capistrano/callback.rb +45 -0
  10. data/lib/capistrano/cli/execute.rb +84 -0
  11. data/lib/capistrano/cli/help.rb +125 -0
  12. data/lib/capistrano/cli/help.txt +75 -0
  13. data/lib/capistrano/cli/options.rb +224 -0
  14. data/lib/capistrano/cli/ui.rb +40 -0
  15. data/lib/capistrano/cli.rb +47 -0
  16. data/lib/capistrano/command.rb +283 -0
  17. data/lib/capistrano/configuration/actions/file_transfer.rb +47 -0
  18. data/lib/capistrano/configuration/actions/inspect.rb +46 -0
  19. data/lib/capistrano/configuration/actions/invocation.rb +293 -0
  20. data/lib/capistrano/configuration/callbacks.rb +148 -0
  21. data/lib/capistrano/configuration/connections.rb +200 -0
  22. data/lib/capistrano/configuration/execution.rb +132 -0
  23. data/lib/capistrano/configuration/loading.rb +197 -0
  24. data/lib/capistrano/configuration/namespaces.rb +197 -0
  25. data/lib/capistrano/configuration/roles.rb +73 -0
  26. data/lib/capistrano/configuration/servers.rb +85 -0
  27. data/lib/capistrano/configuration/variables.rb +127 -0
  28. data/lib/capistrano/configuration.rb +43 -0
  29. data/lib/capistrano/errors.rb +15 -0
  30. data/lib/capistrano/extensions.rb +57 -0
  31. data/lib/capistrano/logger.rb +59 -0
  32. data/lib/capistrano/processable.rb +53 -0
  33. data/lib/capistrano/recipes/compat.rb +32 -0
  34. data/lib/capistrano/recipes/deploy/dependencies.rb +44 -0
  35. data/lib/capistrano/recipes/deploy/local_dependency.rb +54 -0
  36. data/lib/capistrano/recipes/deploy/remote_dependency.rb +105 -0
  37. data/lib/capistrano/recipes/deploy/scm/accurev.rb +169 -0
  38. data/lib/capistrano/recipes/deploy/scm/base.rb +196 -0
  39. data/lib/capistrano/recipes/deploy/scm/bzr.rb +83 -0
  40. data/lib/capistrano/recipes/deploy/scm/cvs.rb +152 -0
  41. data/lib/capistrano/recipes/deploy/scm/darcs.rb +85 -0
  42. data/lib/capistrano/recipes/deploy/scm/git.rb +274 -0
  43. data/lib/capistrano/recipes/deploy/scm/mercurial.rb +137 -0
  44. data/lib/capistrano/recipes/deploy/scm/none.rb +44 -0
  45. data/lib/capistrano/recipes/deploy/scm/perforce.rb +133 -0
  46. data/lib/capistrano/recipes/deploy/scm/subversion.rb +121 -0
  47. data/lib/capistrano/recipes/deploy/scm.rb +19 -0
  48. data/lib/capistrano/recipes/deploy/strategy/base.rb +79 -0
  49. data/lib/capistrano/recipes/deploy/strategy/checkout.rb +20 -0
  50. data/lib/capistrano/recipes/deploy/strategy/copy.rb +210 -0
  51. data/lib/capistrano/recipes/deploy/strategy/export.rb +20 -0
  52. data/lib/capistrano/recipes/deploy/strategy/remote.rb +52 -0
  53. data/lib/capistrano/recipes/deploy/strategy/remote_cache.rb +56 -0
  54. data/lib/capistrano/recipes/deploy/strategy.rb +19 -0
  55. data/lib/capistrano/recipes/deploy/templates/maintenance.rhtml +53 -0
  56. data/lib/capistrano/recipes/deploy.rb +562 -0
  57. data/lib/capistrano/recipes/standard.rb +37 -0
  58. data/lib/capistrano/recipes/templates/maintenance.rhtml +53 -0
  59. data/lib/capistrano/recipes/upgrade.rb +33 -0
  60. data/lib/capistrano/role.rb +102 -0
  61. data/lib/capistrano/server_definition.rb +56 -0
  62. data/lib/capistrano/shell.rb +260 -0
  63. data/lib/capistrano/ssh.rb +99 -0
  64. data/lib/capistrano/task_definition.rb +70 -0
  65. data/lib/capistrano/transfer.rb +216 -0
  66. data/lib/capistrano/version.rb +18 -0
  67. data/lib/capistrano.rb +2 -0
  68. data/setup.rb +1346 -0
  69. data/test/cli/execute_test.rb +132 -0
  70. data/test/cli/help_test.rb +165 -0
  71. data/test/cli/options_test.rb +317 -0
  72. data/test/cli/ui_test.rb +28 -0
  73. data/test/cli_test.rb +17 -0
  74. data/test/command_test.rb +286 -0
  75. data/test/configuration/actions/file_transfer_test.rb +61 -0
  76. data/test/configuration/actions/inspect_test.rb +65 -0
  77. data/test/configuration/actions/invocation_test.rb +224 -0
  78. data/test/configuration/callbacks_test.rb +220 -0
  79. data/test/configuration/connections_test.rb +349 -0
  80. data/test/configuration/execution_test.rb +175 -0
  81. data/test/configuration/loading_test.rb +132 -0
  82. data/test/configuration/namespace_dsl_test.rb +311 -0
  83. data/test/configuration/roles_test.rb +144 -0
  84. data/test/configuration/servers_test.rb +121 -0
  85. data/test/configuration/variables_test.rb +184 -0
  86. data/test/configuration_test.rb +88 -0
  87. data/test/deploy/local_dependency_test.rb +76 -0
  88. data/test/deploy/remote_dependency_test.rb +114 -0
  89. data/test/deploy/scm/accurev_test.rb +23 -0
  90. data/test/deploy/scm/base_test.rb +55 -0
  91. data/test/deploy/scm/git_test.rb +184 -0
  92. data/test/deploy/scm/mercurial_test.rb +129 -0
  93. data/test/deploy/scm/none_test.rb +35 -0
  94. data/test/deploy/strategy/copy_test.rb +258 -0
  95. data/test/extensions_test.rb +69 -0
  96. data/test/fixtures/cli_integration.rb +5 -0
  97. data/test/fixtures/config.rb +5 -0
  98. data/test/fixtures/custom.rb +3 -0
  99. data/test/logger_test.rb +123 -0
  100. data/test/role_test.rb +11 -0
  101. data/test/server_definition_test.rb +121 -0
  102. data/test/shell_test.rb +90 -0
  103. data/test/ssh_test.rb +104 -0
  104. data/test/task_definition_test.rb +101 -0
  105. data/test/transfer_test.rb +160 -0
  106. data/test/utils.rb +38 -0
  107. metadata +306 -0
@@ -0,0 +1,200 @@
1
+ require 'enumerator'
2
+ require 'net/ssh/gateway'
3
+ require 'capistrano/ssh'
4
+ require 'capistrano/errors'
5
+
6
+ module Capistrano
7
+ class Configuration
8
+ module Connections
9
+ def self.included(base) #:nodoc:
10
+ base.send :alias_method, :initialize_without_connections, :initialize
11
+ base.send :alias_method, :initialize, :initialize_with_connections
12
+ end
13
+
14
+ class DefaultConnectionFactory #:nodoc:
15
+ def initialize(options)
16
+ @options = options
17
+ end
18
+
19
+ def connect_to(server)
20
+ SSH.connect(server, @options)
21
+ end
22
+ end
23
+
24
+ class GatewayConnectionFactory #:nodoc:
25
+ def initialize(gateway, options)
26
+ @options = options
27
+ @options[:logger].debug "Creating gateway using #{[*gateway].join(', ')}" if @options[:logger]
28
+ Thread.abort_on_exception = true
29
+ @gateways = [*gateway].collect { |g| ServerDefinition.new(g) }
30
+ tunnel = SSH.connection_strategy(@gateways[0], @options) do |host, user, connect_options|
31
+ Net::SSH::Gateway.new(host, user, connect_options)
32
+ end
33
+ @gateway = (@gateways[1..-1]).inject(tunnel) do |tunnel, destination|
34
+ @options[:logger].debug "Creating tunnel to #{destination}" if @options[:logger]
35
+ local_host = ServerDefinition.new("127.0.0.1", :user => destination.user, :port => tunnel.open(destination.host, (destination.port || 22)))
36
+ SSH.connection_strategy(local_host, @options) do |host, user, connect_options|
37
+ Net::SSH::Gateway.new(host, user, connect_options)
38
+ end
39
+ end
40
+ end
41
+
42
+ def connect_to(server)
43
+ @options[:logger].debug "establishing connection to `#{server}' via gateway" if @options[:logger]
44
+ local_host = ServerDefinition.new("127.0.0.1", :user => server.user, :port => @gateway.open(server.host, server.port || 22))
45
+ session = SSH.connect(local_host, @options)
46
+ session.xserver = server
47
+ session
48
+ end
49
+ end
50
+
51
+ # A hash of the SSH sessions that are currently open and available.
52
+ # Because sessions are constructed lazily, this will only contain
53
+ # connections to those servers that have been the targets of one or more
54
+ # executed tasks.
55
+ attr_reader :sessions
56
+
57
+ def initialize_with_connections(*args) #:nodoc:
58
+ initialize_without_connections(*args)
59
+ @sessions = {}
60
+ @failed_sessions = []
61
+ end
62
+
63
+ # Indicate that the given server could not be connected to.
64
+ def failed!(server)
65
+ @failed_sessions << server
66
+ end
67
+
68
+ # Query whether previous connection attempts to the given server have
69
+ # failed.
70
+ def has_failed?(server)
71
+ @failed_sessions.include?(server)
72
+ end
73
+
74
+ # Used to force connections to be made to the current task's servers.
75
+ # Connections are normally made lazily in Capistrano--you can use this
76
+ # to force them open before performing some operation that might be
77
+ # time-sensitive.
78
+ def connect!(options={})
79
+ execute_on_servers(options) { }
80
+ end
81
+
82
+ # Returns the object responsible for establishing new SSH connections.
83
+ # The factory will respond to #connect_to, which can be used to
84
+ # establish connections to servers defined via ServerDefinition objects.
85
+ def connection_factory
86
+ @connection_factory ||= begin
87
+ if exists?(:gateway)
88
+ logger.debug "establishing connection to gateway `#{fetch(:gateway)}'"
89
+ GatewayConnectionFactory.new(fetch(:gateway), self)
90
+ else
91
+ DefaultConnectionFactory.new(self)
92
+ end
93
+ end
94
+ end
95
+
96
+ # Ensures that there are active sessions for each server in the list.
97
+ def establish_connections_to(servers)
98
+ failed_servers = []
99
+
100
+ # force the connection factory to be instantiated synchronously,
101
+ # otherwise we wind up with multiple gateway instances, because
102
+ # each connection is done in parallel.
103
+ connection_factory
104
+
105
+ threads = Array(servers).map { |server| establish_connection_to(server, failed_servers) }
106
+ threads.each { |t| t.join }
107
+
108
+ if failed_servers.any?
109
+ errors = failed_servers.map { |h| "#{h[:server]} (#{h[:error].class}: #{h[:error].message})" }
110
+ error = ConnectionError.new("connection failed for: #{errors.join(', ')}")
111
+ error.hosts = failed_servers.map { |h| h[:server] }
112
+ raise error
113
+ end
114
+ end
115
+
116
+ # Destroys sessions for each server in the list.
117
+ def teardown_connections_to(servers)
118
+ servers.each do |server|
119
+ @sessions[server].close
120
+ @sessions.delete(server)
121
+ end
122
+ end
123
+
124
+ # Determines the set of servers within the current task's scope and
125
+ # establishes connections to them, and then yields that list of
126
+ # servers.
127
+ def execute_on_servers(options={})
128
+ raise ArgumentError, "expected a block" unless block_given?
129
+
130
+ if task = current_task
131
+ servers = find_servers_for_task(task, options)
132
+
133
+ if servers.empty?
134
+ if ENV['HOSTFILTER']
135
+ logger.info "skipping `#{task.fully_qualified_name}' because no servers matched"
136
+ return
137
+ else
138
+ raise Capistrano::NoMatchingServersError, "`#{task.fully_qualified_name}' is only run for servers matching #{task.options.inspect}, but no servers matched"
139
+ end
140
+ end
141
+
142
+ if task.continue_on_error?
143
+ servers.delete_if { |s| has_failed?(s) }
144
+ return if servers.empty?
145
+ end
146
+ else
147
+ servers = find_servers(options)
148
+ raise Capistrano::NoMatchingServersError, "no servers found to match #{options.inspect}" if servers.empty?
149
+ end
150
+
151
+ servers = [servers.first] if options[:once]
152
+ logger.trace "servers: #{servers.map { |s| s.host }.inspect}"
153
+
154
+ max_hosts = (options[:max_hosts] || (task && task.max_hosts) || servers.size).to_i
155
+ is_subset = max_hosts < servers.size
156
+
157
+ # establish connections to those servers in groups of max_hosts, as necessary
158
+ servers.each_slice(max_hosts) do |servers_slice|
159
+ begin
160
+ establish_connections_to(servers_slice)
161
+ rescue ConnectionError => error
162
+ raise error unless task && task.continue_on_error?
163
+ error.hosts.each do |h|
164
+ servers_slice.delete(h)
165
+ failed!(h)
166
+ end
167
+ end
168
+
169
+ begin
170
+ yield servers_slice
171
+ rescue RemoteError => error
172
+ raise error unless task && task.continue_on_error?
173
+ error.hosts.each { |h| failed!(h) }
174
+ end
175
+
176
+ # if dealing with a subset (e.g., :max_hosts is less than the
177
+ # number of servers available) teardown the subset of connections
178
+ # that were just made, so that we can make room for the next subset.
179
+ teardown_connections_to(servers_slice) if is_subset
180
+ end
181
+ end
182
+
183
+ private
184
+
185
+ # We establish the connection by creating a thread in a new method--this
186
+ # prevents problems with the thread's scope seeing the wrong 'server'
187
+ # variable if the thread just happens to take too long to start up.
188
+ def establish_connection_to(server, failures=nil)
189
+ Thread.new { safely_establish_connection_to(server, failures) }
190
+ end
191
+
192
+ def safely_establish_connection_to(server, failures=nil)
193
+ sessions[server] ||= connection_factory.connect_to(server)
194
+ rescue Exception => err
195
+ raise unless failures
196
+ failures << { :server => server, :error => err }
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,132 @@
1
+ require 'capistrano/errors'
2
+
3
+ module Capistrano
4
+ class Configuration
5
+ module Execution
6
+ def self.included(base) #:nodoc:
7
+ base.send :alias_method, :initialize_without_execution, :initialize
8
+ base.send :alias_method, :initialize, :initialize_with_execution
9
+ end
10
+
11
+ # The call stack of the tasks. The currently executing task may inspect
12
+ # this to see who its caller was. The current task is always the last
13
+ # element of this stack.
14
+ attr_reader :task_call_frames
15
+
16
+ # The stack of tasks that have registered rollback handlers within the
17
+ # current transaction. If this is nil, then there is no transaction
18
+ # that is currently active.
19
+ attr_reader :rollback_requests
20
+
21
+ # A struct for representing a single instance of an invoked task.
22
+ TaskCallFrame = Struct.new(:task, :rollback)
23
+
24
+ def initialize_with_execution(*args) #:nodoc:
25
+ initialize_without_execution(*args)
26
+ @task_call_frames = []
27
+ end
28
+ private :initialize_with_execution
29
+
30
+ # Returns true if there is a transaction currently active.
31
+ def transaction?
32
+ !rollback_requests.nil?
33
+ end
34
+
35
+ # Invoke a set of tasks in a transaction. If any task fails (raises an
36
+ # exception), all tasks executed within the transaction are inspected to
37
+ # see if they have an associated on_rollback hook, and if so, that hook
38
+ # is called.
39
+ def transaction
40
+ raise ArgumentError, "expected a block" unless block_given?
41
+ raise ScriptError, "transaction must be called from within a task" if task_call_frames.empty?
42
+
43
+ return yield if transaction?
44
+
45
+ logger.info "transaction: start"
46
+ begin
47
+ @rollback_requests = []
48
+ yield
49
+ logger.info "transaction: commit"
50
+ rescue Object => e
51
+ rollback!
52
+ raise
53
+ ensure
54
+ @rollback_requests = nil
55
+ end
56
+ end
57
+
58
+ # Specifies an on_rollback hook for the currently executing task. If this
59
+ # or any subsequent task then fails, and a transaction is active, this
60
+ # hook will be executed.
61
+ def on_rollback(&block)
62
+ if transaction?
63
+ # don't note a new rollback request if one has already been set
64
+ rollback_requests << task_call_frames.last unless task_call_frames.last.rollback
65
+ task_call_frames.last.rollback = block
66
+ end
67
+ end
68
+
69
+ # Returns the TaskDefinition object for the currently executing task.
70
+ # It returns nil if there is no task being executed.
71
+ def current_task
72
+ return nil if task_call_frames.empty?
73
+ task_call_frames.last.task
74
+ end
75
+
76
+ # Executes the task with the given name, without invoking any associated
77
+ # callbacks.
78
+ def execute_task(task)
79
+ logger.debug "executing `#{task.fully_qualified_name}'"
80
+ push_task_call_frame(task)
81
+ invoke_task_directly(task)
82
+ ensure
83
+ pop_task_call_frame
84
+ end
85
+
86
+ # Attempts to locate the task at the given fully-qualified path, and
87
+ # execute it. If no such task exists, a Capistrano::NoSuchTaskError will
88
+ # be raised.
89
+ def find_and_execute_task(path, hooks={})
90
+ task = find_task(path) or raise NoSuchTaskError, "the task `#{path}' does not exist"
91
+
92
+ trigger(hooks[:before], task) if hooks[:before]
93
+ result = execute_task(task)
94
+ trigger(hooks[:after], task) if hooks[:after]
95
+
96
+ result
97
+ end
98
+
99
+ protected
100
+
101
+ def rollback!
102
+ # throw the task back on the stack so that roles are properly
103
+ # interpreted in the scope of the task in question.
104
+ rollback_requests.reverse.each do |frame|
105
+ begin
106
+ push_task_call_frame(frame.task)
107
+ logger.important "rolling back", frame.task.fully_qualified_name
108
+ frame.rollback.call
109
+ rescue Object => e
110
+ logger.info "exception while rolling back: #{e.class}, #{e.message}", frame.task.fully_qualified_name
111
+ ensure
112
+ pop_task_call_frame
113
+ end
114
+ end
115
+ end
116
+
117
+ def push_task_call_frame(task)
118
+ frame = TaskCallFrame.new(task)
119
+ task_call_frames.push frame
120
+ end
121
+
122
+ def pop_task_call_frame
123
+ task_call_frames.pop
124
+ end
125
+
126
+ # Invokes the task's body directly, without setting up the call frame.
127
+ def invoke_task_directly(task)
128
+ task.namespace.instance_eval(&task.body)
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,197 @@
1
+ module Capistrano
2
+ class Configuration
3
+ module Loading
4
+ def self.included(base) #:nodoc:
5
+ base.send :alias_method, :initialize_without_loading, :initialize
6
+ base.send :alias_method, :initialize, :initialize_with_loading
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ # Used by third-party task bundles to identify the capistrano
12
+ # configuration that is loading them. Its return value is not reliable
13
+ # in other contexts. If +require_config+ is not false, an exception
14
+ # will be raised if the current configuration is not set.
15
+ def instance(require_config=false)
16
+ config = Thread.current[:capistrano_configuration]
17
+ if require_config && config.nil?
18
+ raise LoadError, "Please require this file from within a Capistrano recipe"
19
+ end
20
+ config
21
+ end
22
+
23
+ # Used internally by Capistrano to specify the current configuration
24
+ # before loading a third-party task bundle.
25
+ def instance=(config)
26
+ Thread.current[:capistrano_configuration] = config
27
+ end
28
+
29
+ # Used internally by Capistrano to track which recipes have been loaded
30
+ # via require, so that they may be successfully reloaded when require
31
+ # is called again.
32
+ def recipes_per_feature
33
+ @recipes_per_feature ||= {}
34
+ end
35
+
36
+ # Used internally to determine what the current "feature" being
37
+ # required is. This is used to track which files load which recipes
38
+ # via require.
39
+ def current_feature
40
+ Thread.current[:capistrano_current_feature]
41
+ end
42
+
43
+ # Used internally to specify the current file being required, so that
44
+ # any recipes loaded by that file can be remembered. This allows
45
+ # recipes loaded via require to be correctly reloaded in different
46
+ # Configuration instances in the same Ruby instance.
47
+ def current_feature=(feature)
48
+ Thread.current[:capistrano_current_feature] = feature
49
+ end
50
+ end
51
+
52
+ # The load paths used for locating recipe files.
53
+ attr_reader :load_paths
54
+
55
+ def initialize_with_loading(*args) #:nodoc:
56
+ initialize_without_loading(*args)
57
+ @load_paths = [".", File.expand_path(File.join(File.dirname(__FILE__), "../recipes"))]
58
+ @loaded_features = []
59
+ end
60
+ private :initialize_with_loading
61
+
62
+ # Load a configuration file or string into this configuration.
63
+ #
64
+ # Usage:
65
+ #
66
+ # load("recipe"):
67
+ # Look for and load the contents of 'recipe.rb' into this
68
+ # configuration.
69
+ #
70
+ # load(:file => "recipe"):
71
+ # same as above
72
+ #
73
+ # load(:string => "set :scm, :subversion"):
74
+ # Load the given string as a configuration specification.
75
+ #
76
+ # load { ... }
77
+ # Load the block in the context of the configuration.
78
+ def load(*args, &block)
79
+ options = args.last.is_a?(Hash) ? args.pop : {}
80
+
81
+ if block
82
+ raise ArgumentError, "loading a block requires 0 arguments" unless options.empty? && args.empty?
83
+ load(:proc => block)
84
+
85
+ elsif args.any?
86
+ args.each { |arg| load options.merge(:file => arg) }
87
+
88
+ elsif options[:file]
89
+ load_from_file(options[:file], options[:name])
90
+
91
+ elsif options[:string]
92
+ remember_load(options) unless options[:reloading]
93
+ instance_eval(options[:string], options[:name] || "<eval>")
94
+
95
+ elsif options[:proc]
96
+ remember_load(options) unless options[:reloading]
97
+ instance_eval(&options[:proc])
98
+
99
+ else
100
+ raise ArgumentError, "don't know how to load #{options.inspect}"
101
+ end
102
+ end
103
+
104
+ # Require another file. This is identical to the standard require method,
105
+ # with the exception that it sets the receiver as the "current" configuration
106
+ # so that third-party task bundles can include themselves relative to
107
+ # that configuration.
108
+ #
109
+ # This is a bit more complicated than an initial review would seem to
110
+ # necessitate, but the use case that complicates things is this: An
111
+ # advanced user wants to embed capistrano, and needs to instantiate
112
+ # more than one capistrano configuration at a time. They also want each
113
+ # configuration to require a third-party capistrano extension. Using a
114
+ # naive require implementation, this would allow the first configuration
115
+ # to successfully load the third-party extension, but the require would
116
+ # fail for the second configuration because the extension has already
117
+ # been loaded.
118
+ #
119
+ # To work around this, we do a few things:
120
+ #
121
+ # 1. Each time a 'require' is invoked inside of a capistrano recipe,
122
+ # we remember the arguments (see "current_feature").
123
+ # 2. Each time a 'load' is invoked inside of a capistrano recipe, and
124
+ # "current_feature" is not nil (meaning we are inside of a pending
125
+ # require) we remember the options (see "remember_load" and
126
+ # "recipes_per_feature").
127
+ # 3. Each time a 'require' is invoked inside of a capistrano recipe,
128
+ # we check to see if this particular configuration has ever seen these
129
+ # arguments to require (see @loaded_features), and if not, we proceed
130
+ # as if the file had never been required. If the superclass' require
131
+ # returns false (meaning, potentially, that the file has already been
132
+ # required), then we look in the recipes_per_feature collection and
133
+ # load any remembered recipes from there.
134
+ #
135
+ # It's kind of a bear, but it works, and works transparently. Note that
136
+ # a simpler implementation would just muck with $", allowing files to be
137
+ # required multiple times, but that will cause warnings (and possibly
138
+ # errors) if the file to be required contains constant definitions and
139
+ # such, alongside (or instead of) capistrano recipe definitions.
140
+ def require(*args) #:nodoc:
141
+ # look to see if this specific configuration instance has ever seen
142
+ # these arguments to require before
143
+ if @loaded_features.include?(args)
144
+ return false
145
+ end
146
+
147
+ @loaded_features << args
148
+ begin
149
+ original_instance, self.class.instance = self.class.instance, self
150
+ original_feature, self.class.current_feature = self.class.current_feature, args
151
+
152
+ result = super
153
+ if !result # file has been required previously, load up the remembered recipes
154
+ list = self.class.recipes_per_feature[args] || []
155
+ list.each { |options| load(options.merge(:reloading => true)) }
156
+ end
157
+
158
+ return result
159
+ ensure
160
+ # restore the original, so that require's can be nested
161
+ self.class.instance = original_instance
162
+ self.class.current_feature = original_feature
163
+ end
164
+ end
165
+
166
+ private
167
+
168
+ # Load a recipe from the named file. If +name+ is given, the file will
169
+ # be reported using that name.
170
+ def load_from_file(file, name=nil)
171
+ file = find_file_in_load_path(file) unless File.file?(file)
172
+ load :string => File.read(file), :name => name || file
173
+ end
174
+
175
+ def find_file_in_load_path(file)
176
+ load_paths.each do |path|
177
+ ["", ".rb"].each do |ext|
178
+ name = File.join(path, "#{file}#{ext}")
179
+ return name if File.file?(name)
180
+ end
181
+ end
182
+
183
+ raise LoadError, "no such file to load -- #{file}"
184
+ end
185
+
186
+ # If a file is being required, the options associated with loading a
187
+ # recipe are remembered in the recipes_per_feature archive under the
188
+ # name of the file currently being required.
189
+ def remember_load(options)
190
+ if self.class.current_feature
191
+ list = (self.class.recipes_per_feature[self.class.current_feature] ||= [])
192
+ list << options
193
+ end
194
+ end
195
+ end
196
+ end
197
+ end