2013-05-17 18:50:27 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2013-05-17 18:50:27 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Builder;
|
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Plugin;
|
2013-10-10 02:12:30 +02:00
|
|
|
|
2013-05-17 18:50:27 +02:00
|
|
|
/**
|
2017-01-10 18:46:19 +01:00
|
|
|
* Copy Build Plugin - Copies the entire build to another directory.
|
|
|
|
*
|
2017-03-04 16:39:56 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
2017-01-10 18:46:19 +01:00
|
|
|
*/
|
2016-07-11 18:00:04 +02:00
|
|
|
class CopyBuild extends Plugin
|
2013-05-17 18:50:27 +02:00
|
|
|
{
|
|
|
|
protected $directory;
|
2014-04-12 13:50:10 +02:00
|
|
|
protected $ignore;
|
2014-04-12 13:48:26 +02:00
|
|
|
protected $wipe;
|
2013-05-17 18:50:27 +02:00
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'copy_build';
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
2016-07-11 18:00:04 +02:00
|
|
|
* {@inheritdoc}
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2016-07-22 09:05:34 +02:00
|
|
|
public function __construct(Builder $builder, Build $build, array $options = [])
|
2013-05-17 18:50:27 +02:00
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
parent::__construct($builder, $build, $options);
|
2016-07-11 18:00:04 +02:00
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$path = $this->builder->buildPath;
|
2015-10-23 05:18:22 +02:00
|
|
|
$this->directory = isset($options['directory']) ? $options['directory'] : $path;
|
|
|
|
$this->wipe = isset($options['wipe']) ? (bool)$options['wipe'] : false;
|
|
|
|
$this->ignore = isset($options['respect_ignore']) ? (bool)$options['respect_ignore'] : false;
|
2013-05-17 18:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-12 02:44:27 +02:00
|
|
|
* Copies files from the root of the build directory into the target folder
|
2013-05-17 18:50:27 +02:00
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
$build = $this->builder->buildPath;
|
2013-05-17 18:50:27 +02:00
|
|
|
|
|
|
|
if ($this->directory == $build) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 14:39:54 +02:00
|
|
|
$this->wipeExistingDirectory();
|
2014-04-12 13:48:26 +02:00
|
|
|
|
2014-04-12 13:52:26 +02:00
|
|
|
$cmd = 'mkdir -p "%s" && cp -R "%s" "%s"';
|
2014-04-12 13:48:26 +02:00
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$success = $this->builder->executeCommand($cmd, $this->directory, $build, $this->directory);
|
2013-05-17 18:50:27 +02:00
|
|
|
|
2014-05-15 14:39:54 +02:00
|
|
|
$this->deleteIgnoredFiles();
|
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Wipe the destination directory if it already exists.
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2014-05-15 14:39:54 +02:00
|
|
|
protected function wipeExistingDirectory()
|
|
|
|
{
|
2015-02-12 15:11:58 +01:00
|
|
|
if ($this->wipe === true && $this->directory != '/' && is_dir($this->directory)) {
|
2014-05-15 14:39:54 +02:00
|
|
|
$cmd = 'rm -Rf "%s*"';
|
2016-07-22 09:05:34 +02:00
|
|
|
$success = $this->builder->executeCommand($cmd, $this->directory);
|
2014-12-04 16:48:52 +01:00
|
|
|
|
2014-05-15 14:39:54 +02:00
|
|
|
if (!$success) {
|
2017-02-07 13:05:44 +01:00
|
|
|
throw new \Exception(sprintf('Failed to wipe existing directory %s before copy', $this->directory));
|
2014-05-15 14:39:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Delete any ignored files from the build prior to copying.
|
|
|
|
*/
|
2014-05-15 14:39:54 +02:00
|
|
|
protected function deleteIgnoredFiles()
|
|
|
|
{
|
2013-05-17 18:50:27 +02:00
|
|
|
if ($this->ignore) {
|
2016-07-22 09:05:34 +02:00
|
|
|
foreach ($this->builder->ignore as $file) {
|
2013-05-17 18:50:27 +02:00
|
|
|
$cmd = 'rm -Rf "%s/%s"';
|
2016-07-22 09:05:34 +02:00
|
|
|
$this->builder->executeCommand($cmd, $this->directory, $file);
|
2013-05-17 18:50:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|