From 21929e9e936f93f7d8d8c3d7f69bf02e49d2bffd Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 26 Oct 2016 22:28:38 +0200 Subject: [PATCH 1/3] add options for composer and php binaries Added `:composer_bin` and `:composer_php` options for better control of which binaries to use. When not explicitly set, the existence of the `composer` and `php` executables is now validated before the tasks are run. Also Composer is now always invoked through php. For this the path to the `composer` executable is expanded by calling `which composer` on the remote host. When `:composer_bin` is set to `:local`, `composer:install_executable` is automatically invoked on deployment. So no more manual hooking of additional tasks in the deploy configuration. --- README.md | 14 +++------ lib/capistrano/tasks/composer.rake | 46 +++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 15c1638..18e5e54 100644 --- a/README.md +++ b/README.md @@ -56,20 +56,14 @@ set :composer_working_dir, -> { fetch(:release_path) } set :composer_dump_autoload_flags, '--optimize' set :composer_download_url, "https://getcomposer.org/installer" set :composer_version, '1.0.0-alpha8' #(default: not set) +set :composer_bin, 'bin/composer.phar' # (default: not set) +set :composer_php, 'php5' # (default: not set) ``` ### Installing composer as part of a deployment -Add the following to `deploy.rb` to manage the installation of composer during -deployment (composer.phar is install in the shared path). - -```ruby -SSHKit.config.command_map[:composer] = "php #{shared_path.join("composer.phar")}" - -namespace :deploy do - after :starting, 'composer:install_executable' -end -``` +To install Composer as part of the deployment, set `:composer_bin` to `:local`. +This ensures installation of a local `composer.phar` in the shared path. ### Accessing composer commands directly diff --git a/lib/capistrano/tasks/composer.rake b/lib/capistrano/tasks/composer.rake index 6bee32f..c6c1d2f 100644 --- a/lib/capistrano/tasks/composer.rake +++ b/lib/capistrano/tasks/composer.rake @@ -1,20 +1,39 @@ namespace :composer do - desc <<-DESC - Installs composer.phar to the shared directory - In order to use the .phar file, the composer command needs to be mapped: - SSHKit.config.command_map[:composer] = "\#{shared_path.join("composer.phar")}" - This is best used after deploy:starting: - namespace :deploy do - after :starting, 'composer:install_executable' + task :validate do + on release_roles(fetch(:composer_roles)) do + composer_php = fetch(:composer_php, capture(:which, 'php')) + if composer_php.nil? + error 'composer: php not found and composer_php is not set' + exit 1 + end + + composer_bin = fetch(:composer_bin, capture(:which, 'composer')) + if composer_bin.nil? + error 'composer: composer not found and composer_bin is not set' + exit 1 end + + if composer_bin === :local + composer_bin = shared_path.join('composer.phar') + end + + SSHKit.config.command_map[:composer] = "#{composer_php} #{composer_bin}" + end + end + + desc <<-DESC + Installs composer.phar to the shared directory. + + When `:composer_bin` is set to `:local`, this task is automatically invoked. DESC task :install_executable do on release_roles(fetch(:composer_roles)) do within shared_path do - unless test "[", "-e", "composer.phar", "]" + unless test "[ -f #{shared_path.join('composer.phar')} ]" composer_version = fetch(:composer_version, nil) - composer_version_option = composer_version ? "-- --version=#{composer_version}" : "" - execute :curl, "-s", fetch(:composer_download_url), "|", :php, composer_version_option + composer_version_option = composer_version ? "-- --version=#{composer_version}" : '' + execute :curl, '-s', fetch(:composer_download_url), + '|', fetch(:composer_php, 'php'), composer_version_option end end end @@ -61,6 +80,13 @@ namespace :composer do before 'deploy:reverted', 'composer:install' end +Capistrano::DSL.stages.each do |stage| + after stage, 'composer:validate' + after stage, 'composer:auto_install_executable' do + invoke 'composer:install_executable' if fetch(:composer_bin) === :local + end +end + namespace :load do task :defaults do set :composer_install_flags, '--no-dev --prefer-dist --no-interaction --quiet --optimize-autoloader' From 4c03a4b50e7886279575d3288d3229af3045d187 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 26 Oct 2016 22:29:40 +0200 Subject: [PATCH 2/3] quote consistency and capitalize product names --- README.md | 18 +++++++++--------- lib/capistrano/tasks/composer.rake | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 18e5e54..d53b530 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Capistrano's default deploy, or can be run in isolation with: $ cap production composer:install ``` -By default it is assumed that you have the composer executable installed and in your +By default it is assumed that you have the Composer executable installed and in your `$PATH` on all target hosts. ### Configuration @@ -54,33 +54,33 @@ set :composer_install_flags, '--no-dev --no-interaction --quiet --optimize-autol set :composer_roles, :all set :composer_working_dir, -> { fetch(:release_path) } set :composer_dump_autoload_flags, '--optimize' -set :composer_download_url, "https://getcomposer.org/installer" -set :composer_version, '1.0.0-alpha8' #(default: not set) +set :composer_download_url, 'https://getcomposer.org/installer' +set :composer_version, '1.0.0-alpha8' # (default: not set) set :composer_bin, 'bin/composer.phar' # (default: not set) set :composer_php, 'php5' # (default: not set) ``` -### Installing composer as part of a deployment +### Installing Composer as part of a deployment To install Composer as part of the deployment, set `:composer_bin` to `:local`. This ensures installation of a local `composer.phar` in the shared path. -### Accessing composer commands directly +### Accessing Composer commands directly This library also provides a `composer:run` task which allows access to any -composer command. +Composer command. From the command line you can run ```bash -$ cap production composer:run['status','--profile'] +$ cap production composer:run[status,--profile] ``` -Or from within a rake task using capistrano's `invoke` +Or from within a rake task using Capistrano's `invoke` ```ruby task :my_custom_composer_task do - invoke "composer:run", :update, "--dev --prefer-dist" + invoke 'composer:run', :update, '--dev --prefer-dist' end ``` diff --git a/lib/capistrano/tasks/composer.rake b/lib/capistrano/tasks/composer.rake index c6c1d2f..3d115b5 100644 --- a/lib/capistrano/tasks/composer.rake +++ b/lib/capistrano/tasks/composer.rake @@ -58,11 +58,11 @@ namespace :composer do set :composer_roles, :all DESC task :install do - invoke "composer:run", :install, fetch(:composer_install_flags) + invoke 'composer:run', :install, fetch(:composer_install_flags) end task :dump_autoload do - invoke "composer:run", :dumpautoload, fetch(:composer_dump_autoload_flags) + invoke 'composer:run', :dumpautoload, fetch(:composer_dump_autoload_flags) end desc <<-DESC @@ -73,7 +73,7 @@ namespace :composer do set :composer_version, '1.0.0-alpha8' DESC task :self_update do - invoke "composer:run", :selfupdate, fetch(:composer_version, '') + invoke 'composer:run', :selfupdate, fetch(:composer_version, '') end before 'deploy:updated', 'composer:install' @@ -93,6 +93,6 @@ namespace :load do set :composer_roles, :all set :composer_working_dir, -> { fetch(:release_path) } set :composer_dump_autoload_flags, '--optimize' - set :composer_download_url, "https://getcomposer.org/installer" + set :composer_download_url, 'https://getcomposer.org/installer' end end From 11efec0a8f091c3434f7cddd0f69b3d08a2b52ca Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 26 Oct 2016 22:36:17 +0200 Subject: [PATCH 3/3] consistent indentation --- lib/capistrano/tasks/composer.rake | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/capistrano/tasks/composer.rake b/lib/capistrano/tasks/composer.rake index 3d115b5..3e36d6e 100644 --- a/lib/capistrano/tasks/composer.rake +++ b/lib/capistrano/tasks/composer.rake @@ -49,14 +49,14 @@ namespace :composer do end desc <<-DESC - Install the project dependencies via Composer. By default, require-dev \ - dependencies will not be installed. + Install the project dependencies via Composer. By default, require-dev \ + dependencies will not be installed. - You can override any of the defaults by setting the variables shown below. + You can override any of the defaults by setting the variables shown below. - set :composer_install_flags, '--no-dev --no-interaction --quiet --optimize-autoloader' - set :composer_roles, :all - DESC + set :composer_install_flags, '--no-dev --no-interaction --quiet --optimize-autoloader' + set :composer_roles, :all + DESC task :install do invoke 'composer:run', :install, fetch(:composer_install_flags) end @@ -66,12 +66,12 @@ namespace :composer do end desc <<-DESC - Run the self-update command for composer.phar + Run the self-update command for composer.phar - You can update to a specific release by setting the variables shown below. + You can update to a specific release by setting the variables shown below. - set :composer_version, '1.0.0-alpha8' - DESC + set :composer_version, '1.0.0-alpha8' + DESC task :self_update do invoke 'composer:run', :selfupdate, fetch(:composer_version, '') end