# Load default values the capistrano 3.x way. # See github.com/capistrano/capistrano/pull/605 namespace :load do

task :defaults do
  set :composer_download_url, "https://getcomposer.org/installer"

  set :install_composer_exec, true
  set :install_composer, true
  set :install_drush, true

  set :drupal_path, -> { "web" }

  set :db_backup_path, -> { "db_backups" }

  set :linked_files, fetch(:linked_files, []).push(
    "#{fetch(:drupal_path)}/sites/default/settings.php",
    "#{fetch(:drupal_path)}/sites/default/default.services.yml"
  )

  set :linked_dirs, fetch(:linked_dirs, []).push(
    "#{fetch(:drupal_path)}/sites/default/files"
  )

  # if fetch(:install_drush)
  #   set :drush, -> { "#{shared_path.join("vendor/bin/drush") }" }
  # end
end

end

namespace :deploy do

desc 'Deploy your project and do an updatedb, feature revert, cache clear...'
task :drupal do
  if fetch(:install_composer_exec)
    invoke "composer:install_executable"
  end

  if fetch(:install_drush)
    invoke "drush:install"
  end

  if fetch(:install_composer)
    invoke "drupal:composer:install"
  end

  invoke "drupal:update:updatedb"

  invoke "drupal:config:import"

  #invoke "drupal:features:import"

  invoke "drupal:cache:clear"
end

namespace :check do
  task :backup_directories do
    on release_roles :all do
      within deploy_path do
        execute :mkdir, "-p", fetch(:db_backup_path)
      end
    end
  end
end

end

# Specific Drupal tasks namespace :drupal do

namespace :composer do
  desc 'Test task'
  task :install do
    on roles(:app) do
      within release_path do
        execute :composer, 'install'
      end
    end
  end
end

desc 'Run any drush command'
task :drush do
  ask(:drush_command, "Drush command you want to run (eg. 'cache-rebuild'). Type 'help' to have a list of available command.")
  on roles(:app) do
    within release_path.join(fetch(:drupal_path)) do
      execute :drush, fetch(:drush_command)
    end
  end
end

desc 'Show logs'
task :logs do
  on roles(:app) do
    within release_path.join(fetch(:drupal_path)) do
      execute :drush, 'watchdog-show  --tail'
    end
  end
end

desc 'Provides information about things that may be wrong in your Drupal installation, if any.'
task :requirements do
  on roles(:app) do
    within release_path.join(fetch(:drupal_path)) do
      execute :drush, 'core-requirements'
    end
  end
end

desc 'Open an interactive shell on a Drupal site.'
task :cli do
  on roles(:app) do
    within release_path.join(fetch(:drupal_path)) do
      execute :drush, 'core-cli'
    end
  end
end

namespace :features do
  desc 'Revert feature'
  task :import do
    on roles(:app) do
      within release_path.join(fetch(:drupal_path)) do
        execute :drush, 'features-import-all -y'
      end
    end
  end
end

namespace :config do
  desc 'List any pending database updates.'
  task :import do
    on roles(:app) do
      within release_path.join(fetch(:drupal_path)) do
        execute :drush, 'config-import -y'
      end
    end
  end
end

namespace :update do
  desc 'List any pending database updates.'
  task :updatedb_status do
    on roles(:app) do
      within release_path.join(fetch(:drupal_path)) do
        execute :drush, 'updatedb-status'
      end
    end
  end

  desc 'Apply any database updates required (as with running update.php).'
  task :updatedb do
    on roles(:app) do
      within release_path.join(fetch(:drupal_path)) do
        execute :drush, 'updatedb -y'
      end
    end
  end

  desc 'Show a report of available minor updates to Drupal core and contrib projects.'
  task :updatedb_status do
    on roles(:app) do
      within release_path.join(fetch(:drupal_path)) do
        execute :drush, 'pm-refresh'
        execute :drush, 'pm-updatestatus'
      end
    end
  end
end

namespace :cache do
  desc 'Clear all caches'
  task :clear do
    on roles(:app) do
      within release_path.join(fetch(:drupal_path)) do
        execute :drush, 'cache-rebuild'
      end
    end
  end
end

namespace :site do
  desc 'Execute site actions'

  task :enable do
    on roles(:app) do
      within release_path.join(fetch(:drupal_path)) do
        execute :drush, 'state-set system.maintenance_mode 0'
      end
    end
  end

  task :disable do
    on roles(:app) do
      within release_path.join(fetch(:drupal_path)) do
        execute :drush, 'state-set system.maintenance_mode 1'
      end
    end
  end

  task :backup_db do
    on roles(:app) do
      within release_path.join(fetch(:drupal_path)) do
        execute :drush, "sql-dump --result-file=#{db_backup_path}/backup_db.sql"
      end
    end
  end

end

end

# Install drush namespace :drush do

desc "Install Drush"
task :install do
  on roles(:app) do
    within shared_path do
      execute :composer, "require drush/drush:~8"
    end
  end
end

end

after “deploy:check:directories”, “deploy:check:backup_directories” after “deploy:finished”, “deploy:drupal”

before “deploy:starting”, “drupal:site:backup_db” after “drupal:site:enable”, “drupal:cache:clear” after “drupal:site:disable”, “drupal:cache:clear”