magallanes/src/Task/BuiltIn/Deploy/Tar/PrepareTask.php

71 lines
2 KiB
PHP
Raw Normal View History

2016-12-31 07:52:25 +01:00
<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Deploy\Tar;
2016-12-31 07:52:25 +01:00
2017-01-07 05:53:57 +01:00
use Mage\Task\Exception\ErrorException;
2016-12-31 07:52:25 +01:00
use Symfony\Component\Process\Process;
use Mage\Task\AbstractTask;
/**
* Tar Task - Create temporal Tar
2016-12-31 07:52:25 +01:00
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class PrepareTask extends AbstractTask
{
public function getName()
{
return 'deploy/tar/prepare';
2016-12-31 07:52:25 +01:00
}
public function getDescription()
{
return '[Deploy] Preparing Tar file';
2016-12-31 07:52:25 +01:00
}
public function execute()
{
2017-01-12 02:22:21 +01:00
if (!$this->runtime->getEnvOption('releases', false)) {
2017-01-06 02:14:35 +01:00
throw new ErrorException('This task is only available with releases enabled', 40);
2016-12-31 07:52:25 +01:00
}
$tarLocal = $this->runtime->getTempFile();
$this->runtime->setVar('tar_local', $tarLocal);
2016-12-31 07:52:25 +01:00
$excludes = $this->getExcludes();
2017-07-22 22:14:36 +02:00
$tarPath = $this->runtime->getEnvOption('tar_create_path', 'tar');
2021-02-20 21:16:24 +01:00
$flags = $this->runtime->getEnvOption('tar_create', $this->runtime->isWindows() ? '--force-local -c -z -p -f' : 'cfzp');
$from = $this->runtime->getEnvOption('from', './');
2021-02-19 02:18:45 +01:00
2021-02-20 21:16:24 +01:00
if ($this->runtime->getEnvOption('copyDirectory', false)) {
$from = sprintf('-C %s ./', $from);
2021-02-19 02:18:45 +01:00
}
2017-07-22 22:14:36 +02:00
$cmdTar = sprintf('%s %s %s %s %s', $tarPath, $flags, $tarLocal, $excludes, $from);
2016-12-31 07:52:25 +01:00
/** @var Process $process */
$process = $this->runtime->runLocalCommand($cmdTar, 300);
2016-12-31 07:52:25 +01:00
return $process->isSuccessful();
}
protected function getExcludes()
{
2017-04-14 21:29:22 +02:00
$excludes = $this->runtime->getMergedOption('exclude', []);
$excludes = array_merge(['.git'], array_filter($excludes));
2016-12-31 07:52:25 +01:00
foreach ($excludes as &$exclude) {
2017-01-03 03:56:54 +01:00
$exclude = '--exclude="' . $exclude . '"';
2016-12-31 07:52:25 +01:00
}
return implode(' ', $excludes);
}
}