Permalink
Browse files

Automatically detect and use Bundler when running Rake

Also make specs for this less brittle.
  • Loading branch information...
njonsson committed Oct 5, 2012
1 parent d671cee commit 3d500db1789aec5fdceb493b1b8d26208b02b63b
View
@@ -46,7 +46,7 @@ Then
* **Mirror Rake tasks** as Capistrano recipes, optionally filtered by namespace or name
* **Embed Rake tasks** in Capistrano namespaces
* **Pass arguments** to Rake tasks by setting environment variables with the same names
-* **Override the default executables** for local and remote Rake installations (`/usr/bin/env rake` is the default)
+* **Specify the Rake executable** for local and remote Rake installations
* **Enumerate Rake tasks** for your own purposes
See what’s changed lately by reading the [project history](http://github.com/njonsson/cape/blob/master/History.markdown).
@@ -237,15 +237,22 @@ Cape lets you enumerate Rake tasks, optionally filtering them by task name or na
### Configure Rake execution
-Cape lets you specify how Rake should be executed on the local computer and on remote computers. Note that Cape statements must be executed within a `Cape` block.
+Cape lets you specify how Rake should be executed on the local computer and on remote computers. But the default behavior is most likely just right for your needs:
+
+* It detects whether Bundler is installed on the computer
+* It detects whether the project uses Bundler to manage its dependencies
+* It runs Rake via Bundler if the above conditions are true; otherwise, it runs Rake directly
+
+Note that Cape statements must be executed within a `Cape` block.
# config/deploy.rb
require 'cape'
- # Configure Cape to execute Rake via Bundler, both locally and remotely.
- Cape.local_rake_executable = '/usr/bin/env bundle exec rake'
- Cape.remote_rake_executable = '/usr/bin/env bundle exec rake'
+ # Configure Cape never to execute Rake via Bundler, neither locally nor
+ # remotely.
+ Cape.local_rake_executable = '/usr/bin/env rake'
+ Cape.remote_rake_executable = '/usr/bin/env rake'
Cape do
# Create Capistrano recipes for all Rake tasks.
@@ -90,7 +90,7 @@ Feature: The #mirror_rake_tasks DSL method, inside a Capistrano namespace, witho
Then the output should contain:
"""
* executing `ns:with_period'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_period"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_period"
"""
Scenario: mirror Rake task 'without_period' with its description
@@ -197,7 +197,7 @@ Feature: The #mirror_rake_tasks DSL method, inside a Capistrano namespace, witho
Then the output should contain:
"""
* executing `ns:my_namespace'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake my_namespace"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake my_namespace"
"""
Scenario: mirror Rake task 'my_namespace:in_a_namespace' with its description
@@ -91,7 +91,7 @@ Feature: The #mirror_rake_tasks DSL method with arguments of a defined task and
Then the output should contain:
"""
* executing `with_period'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_period RAILS_ENV=\"production\""
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_period RAILS_ENV=\"production\""
`with_period' is only run for servers matching {:roles=>:app}, but no servers matched
"""
@@ -112,7 +112,7 @@ Feature: The #mirror_rake_tasks DSL method with arguments of a defined task and
Then the output should contain:
"""
* executing `with_period'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_period"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_period"
`with_period' is only run for servers matching {:roles=>:app}, but no servers matched
"""
@@ -133,7 +133,7 @@ Feature: The #mirror_rake_tasks DSL method with arguments of a defined task and
Then the output should contain:
"""
* executing `with_period'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_period"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_period"
`with_period' is only run for servers matching {:roles=>:app}, but no servers matched
"""
@@ -198,7 +198,7 @@ Feature: The #mirror_rake_tasks DSL method with arguments of a defined task and
Then the output should contain:
"""
* executing `with_three_args'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_three_args[\"a value for an_arg1\",\"a value for an_arg2\",\"a value for an_arg3\"] RAILS_ENV=\"production\""
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_three_args[\"a value for an_arg1\",\"a value for an_arg2\",\"a value for an_arg3\"] RAILS_ENV=\"production\""
`with_three_args' is only run for servers matching {:roles=>:app}, but no servers matched
"""
@@ -219,7 +219,7 @@ Feature: The #mirror_rake_tasks DSL method with arguments of a defined task and
Then the output should contain:
"""
* executing `with_three_args'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_three_args[,\"a value for an_arg2\",] RAILS_ENV=\"production\""
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_three_args[,\"a value for an_arg2\",] RAILS_ENV=\"production\""
`with_three_args' is only run for servers matching {:roles=>:app}, but no servers matched
"""
@@ -85,6 +85,6 @@ Feature: The #mirror_rake_tasks DSL method with arguments of a defined task and
Then the output should contain:
"""
* executing `with_period'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_period"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_period"
`with_period' is only run for servers matching {:roles=>:app}, but no servers matched
"""
@@ -84,7 +84,7 @@ Feature: The #mirror_rake_tasks DSL method without arguments
Then the output should contain:
"""
* executing `with_period'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_period"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_period"
"""
Scenario: mirror Rake task 'without_period' with its description
@@ -181,7 +181,7 @@ Feature: The #mirror_rake_tasks DSL method without arguments
Then the output should contain:
"""
* executing `my_namespace'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake my_namespace"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake my_namespace"
"""
Scenario: mirror Rake task 'my_namespace:in_a_namespace' with its description
@@ -236,7 +236,7 @@ Feature: The #mirror_rake_tasks DSL method without arguments
Then the output should contain:
"""
* executing `my_namespace:my_nested_namespace:in_a_nested_namespace'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake my_namespace:my_nested_namespace:in_a_nested_namespace"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake my_namespace:my_nested_namespace:in_a_nested_namespace"
"""
Scenario: mirror Rake task 'with_two_args' with its description
@@ -297,7 +297,7 @@ Feature: The #mirror_rake_tasks DSL method without arguments
Then the output should contain:
"""
* executing `with_three_args'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_three_args[\"a value for an_arg1\",\"a value for an_arg2\",\"a value for an_arg3\"]"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_three_args[\"a value for an_arg1\",\"a value for an_arg2\",\"a value for an_arg3\"]"
"""
@@ -315,7 +315,7 @@ Feature: The #mirror_rake_tasks DSL method without arguments
Then the output should contain:
"""
* executing `with_three_args'
- * executing "cd /path/to/current/deployed/application && /usr/bin/env rake with_three_args[,\"a value for an_arg2\",]"
+ * executing "cd /path/to/current/deployed/application && /usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake with_three_args[,\"a value for an_arg2\",]"
"""
@@ -31,7 +31,7 @@ Feature: The #local_rake_executable and #remote_rake_executable DSL attributes
"""
And the output should contain:
"""
- We left the remote Rake executable as "/usr/bin/env rake"
+ We left the remote Rake executable as "/usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake"
"""
And the output should contain:
"""
@@ -61,7 +61,7 @@ Feature: The #local_rake_executable and #remote_rake_executable DSL attributes
"""
And the output should contain:
"""
- We left the local Rake executable as "/usr/bin/env rake"
+ We left the local Rake executable as "/usr/bin/env `/usr/bin/env bundle check >/dev/null 2>&1; case $? in 0|1 ) echo bundle exec ;; esac` rake"
"""
And the output should contain:
"""
View
@@ -3,8 +3,23 @@ module Cape
# An abstraction of the Rake installation and available tasks.
class Rake
- # The default command used to run Rake.
- DEFAULT_EXECUTABLE = '/usr/bin/env rake'.freeze
+ # The default command used to run Rake. We use `bundle check` to detect the
+ # presence of Bundler and a Bundler configuration. If Bundler is installed
+ # on the computer and configured, we prepend `rake` with `bundle exec`.
+ DEFAULT_EXECUTABLE = (
+ '/usr/bin/env ' +
+ '`' +
+ '/usr/bin/env bundle check >/dev/null 2>&1; ' +
+ 'case $? in ' +
+ # Exit code 0: bundle is defined and installed
+ # Exit code 1: bundle is defined but not installed
+ '0|1 ) ' +
+ 'echo bundle exec ' +
+ ';; ' +
+ 'esac' +
+ '` ' +
+ 'rake'
+ ).freeze
# Sets the command used to run Rake on remote computers.
#
@@ -128,7 +143,7 @@ def each_output_line(&block)
end
def fetch_output
- `#{local_executable} --tasks 2> /dev/null`
+ `#{local_executable} --tasks 2>/dev/null`
end
end
View
@@ -2,9 +2,7 @@
describe Cape::Rake do
describe '::DEFAULT_EXECUTABLE' do
- subject { Cape::Rake::DEFAULT_EXECUTABLE }
-
- it { should == '/usr/bin/env rake' }
+ subject { described_class::DEFAULT_EXECUTABLE }
it { should be_frozen }
end
@@ -24,9 +22,9 @@
end
describe '-- without specified attributes --' do
- its(:local_executable) { should == '/usr/bin/env rake' }
+ its(:local_executable) { should == described_class::DEFAULT_EXECUTABLE }
- its(:remote_executable) { should == '/usr/bin/env rake' }
+ its(:remote_executable) { should == described_class::DEFAULT_EXECUTABLE }
end
describe '-- with specified attributes --' do

0 comments on commit 3d500db

Please sign in to comment.