<?php
namespace Deployer;

require 'recipe/symfony4.php';
require './vendor/deployer/recipes/recipe/cachetool.php';
require './vendor/deployer/recipes/recipe/yarn.php';

// Project name
set('application', 'keylighter.kadet.net');

// Project repository
set('repository', 'git@git.kadet.net:kadet.net/keylighter.git');

// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true);
set('keep_releases', 3);
set('default_stage', 'production');

// Shared files/dirs between deploys 
add('shared_files', []);
add('shared_dirs', ['var/keylighter']);

// Writable dirs by web server 
add('writable_dirs', []);

// Hosts
host('kadet.net')
    ->stage('production')
    ->set('deploy_path', '~/www/{{application}}')
;

localhost()
    ->roles('test', 'build')
    ->set('deploy_path', '/tmp/build/{{application}}')
    ->set('keep_releases', 1);

// Tasks
task('assets:build', function () {
    run('yarn install');
    run('yarn build');
})->onRoles('build');

task('assets:upload', function () {
    upload('public/build/', '{{release_path}}/public');
    upload('public/img/', '{{release_path}}/public');
});

task('assets', ['assets:build', 'assets:upload']);
task('restart:consumer', 'systemctl --user restart keylighter-updater');

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');
after('deploy:symlink', 'cachetool:clear:opcache');
after('deploy:symlink', 'restart:consumer');

// Migrate database before symlink new release.
//before('deploy:symlink', 'database:migrate');
before('deploy:symlink', 'assets');

