2016-11-19 15:13:32 +07:00
|
|
|
<?php
|
|
|
|
/* (c) Anton Medvedev <anton@medv.io>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Deployer;
|
|
|
|
|
2017-04-08 17:02:30 +07:00
|
|
|
use Deployer\Exception\Exception;
|
2017-01-18 13:27:08 +07:00
|
|
|
|
2016-11-19 15:13:32 +07:00
|
|
|
desc('Creating symlinks for shared files and dirs');
|
|
|
|
task('deploy:shared', function () {
|
|
|
|
$sharedPath = "{{deploy_path}}/shared";
|
|
|
|
|
2017-01-18 13:27:08 +07:00
|
|
|
// Validate shared_dir, find duplicates
|
|
|
|
foreach (get('shared_dirs') as $a) {
|
|
|
|
foreach (get('shared_dirs') as $b) {
|
2017-02-17 07:46:51 +03:00
|
|
|
if ($a !== $b && strpos(rtrim($a, '/') . '/', rtrim($b, '/') . '/') === 0) {
|
2017-04-08 17:02:30 +07:00
|
|
|
throw new Exception("Can not share same dirs `$a` and `$b`.");
|
2017-01-18 13:27:08 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 15:13:32 +07:00
|
|
|
foreach (get('shared_dirs') as $dir) {
|
2018-03-17 17:39:37 +00:00
|
|
|
// Check if shared dir does not exist.
|
2016-11-25 20:39:21 +07:00
|
|
|
if (!test("[ -d $sharedPath/$dir ]")) {
|
|
|
|
// Create shared dir if it does not exist.
|
|
|
|
run("mkdir -p $sharedPath/$dir");
|
2016-11-22 21:46:17 -02:00
|
|
|
|
2016-11-25 20:39:21 +07:00
|
|
|
// If release contains shared dir, copy that dir from release to shared.
|
|
|
|
if (test("[ -d $(echo {{release_path}}/$dir) ]")) {
|
2018-02-14 09:44:35 +01:00
|
|
|
run("cp -rv {{release_path}}/$dir $sharedPath/" . dirname(parse($dir)));
|
2016-11-25 20:39:21 +07:00
|
|
|
}
|
|
|
|
}
|
2016-11-19 15:13:32 +07:00
|
|
|
|
|
|
|
// Remove from source.
|
2016-11-25 20:39:21 +07:00
|
|
|
run("rm -rf {{release_path}}/$dir");
|
2016-11-19 15:13:32 +07:00
|
|
|
|
|
|
|
// Create path to shared dir in release dir if it does not exist.
|
2016-11-25 20:39:21 +07:00
|
|
|
// Symlink will not create the path and will fail otherwise.
|
2016-11-19 15:13:32 +07:00
|
|
|
run("mkdir -p `dirname {{release_path}}/$dir`");
|
|
|
|
|
|
|
|
// Symlink shared dir to release dir
|
|
|
|
run("{{bin/symlink}} $sharedPath/$dir {{release_path}}/$dir");
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (get('shared_files') as $file) {
|
2018-02-14 09:44:35 +01:00
|
|
|
$dirname = dirname(parse($file));
|
2017-04-04 19:48:16 +00:00
|
|
|
|
|
|
|
// Create dir of shared file
|
|
|
|
run("mkdir -p $sharedPath/" . $dirname);
|
|
|
|
|
2018-03-17 17:39:37 +00:00
|
|
|
// Check if shared file does not exist in shared.
|
2017-04-04 20:16:16 +00:00
|
|
|
// and file exist in release
|
|
|
|
if (!test("[ -f $sharedPath/$file ]") && test("[ -f {{release_path}}/$file ]")) {
|
2017-04-04 19:48:16 +00:00
|
|
|
// Copy file in shared dir if not present
|
2017-04-04 20:16:16 +00:00
|
|
|
run("cp -rv {{release_path}}/$file $sharedPath/$file");
|
2017-04-05 19:24:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-19 15:13:32 +07:00
|
|
|
// Remove from source.
|
|
|
|
run("if [ -f $(echo {{release_path}}/$file) ]; then rm -rf {{release_path}}/$file; fi");
|
|
|
|
|
|
|
|
// Ensure dir is available in release
|
|
|
|
run("if [ ! -d $(echo {{release_path}}/$dirname) ]; then mkdir -p {{release_path}}/$dirname;fi");
|
|
|
|
|
|
|
|
// Touch shared
|
|
|
|
run("touch $sharedPath/$file");
|
|
|
|
|
|
|
|
// Symlink shared dir to release dir
|
|
|
|
run("{{bin/symlink}} $sharedPath/$file {{release_path}}/$file");
|
|
|
|
}
|
|
|
|
});
|