From 0a5a80afb9246789c4866d3551aa0dff5788b736 Mon Sep 17 00:00:00 2001 From: Alexandre Assouad Date: Mon, 28 Sep 2015 13:23:42 +0000 Subject: [PATCH 1/2] improved composer rake tasks --- lib/capistrano/tasks/composer.rake | 48 +++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/capistrano/tasks/composer.rake b/lib/capistrano/tasks/composer.rake index 6bee32f..23ee50e 100644 --- a/lib/capistrano/tasks/composer.rake +++ b/lib/capistrano/tasks/composer.rake @@ -1,8 +1,25 @@ namespace :composer do + +desc <<-DESC + Check wether composer is installed globally or not. + This test should be executed everytime. + DESC + task :check do + on release_roles(fetch(:composer_roles)) do + info "Testing if composer is installed globally" + if test "[[ -n `which composer` ]]" + invoke 'composer:install_executable' + end + end + end + 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")}" + Installs composer.phar to the composer_path directory + The advantage of using a variable is that we have better control on the path used in SSHKit. + + In order to use the .phar file, the composer command is mapped using the composer_path variable: + SSHKit.config.command_map[:composer] = "php \#{fetch(composer_path)}/composer.phar" + This is best used after deploy:starting: namespace :deploy do after :starting, 'composer:install_executable' @@ -10,11 +27,18 @@ namespace :composer do DESC task :install_executable do on release_roles(fetch(:composer_roles)) do - within shared_path do + + within fetch(:composer_path) do unless test "[", "-e", "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 + set :should_install_composer, ask('Do you want to install composer locally ?', 'y|n') + unless fetch(:should_install_composer) != 'y' + 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 + else + error("You need composer in order to continue") + abort() + end end end end @@ -23,6 +47,13 @@ namespace :composer do task :run, :command do |t, args| args.with_defaults(:command => :list) on release_roles(fetch(:composer_roles)) do + if fetch(:composer_use_global) + execute :echo, "Using globally installed composer at ", `which composer` + else + set :composer_exec_path, "php #{fetch(:composer_path)}/composer.phar" + execute :echo, fetch(:composer_exec_path) + SSHKit.config.command_map[:composer] = fetch(:composer_exec_path) + end within fetch(:composer_working_dir) do execute :composer, args[:command], *args.extras end @@ -57,6 +88,7 @@ namespace :composer do invoke "composer:run", :selfupdate, fetch(:composer_version, '') end + before 'composer:run', 'composer:check' before 'deploy:updated', 'composer:install' before 'deploy:reverted', 'composer:install' end @@ -68,5 +100,7 @@ namespace :load do set :composer_working_dir, -> { fetch(:release_path) } set :composer_dump_autoload_flags, '--optimize' set :composer_download_url, "https://getcomposer.org/installer" + set :composer_path, -> { "#{shared_path}" } + set :composer_use_global, false end end From f4c49be0583c91f975e8693a9c7f0c5657446230 Mon Sep 17 00:00:00 2001 From: Alexandre Assouad Date: Mon, 28 Sep 2015 13:49:42 +0000 Subject: [PATCH 2/2] minor change --- lib/capistrano/tasks/composer.rake | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/capistrano/tasks/composer.rake b/lib/capistrano/tasks/composer.rake index 23ee50e..aa2a95b 100644 --- a/lib/capistrano/tasks/composer.rake +++ b/lib/capistrano/tasks/composer.rake @@ -97,9 +97,10 @@ namespace :load do task :defaults do set :composer_install_flags, '--no-dev --prefer-dist --no-interaction --quiet --optimize-autoloader' 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" + # use ruby global variables in a lambda + set :composer_working_dir, -> { "#{release_path}" } set :composer_path, -> { "#{shared_path}" } set :composer_use_global, false end