php-censor/src/Plugin/CopyBuild.php

92 lines
2.4 KiB
PHP
Raw Normal View History

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
/**
* 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>
*/
2016-07-11 18:00:04 +02:00
class CopyBuild extends Plugin
2013-05-17 18:50:27 +02:00
{
protected $directory;
protected $ignore;
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';
}
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
2013-05-17 18:50:27 +02:00
{
parent::__construct($builder, $build, $options);
2016-07-11 18:00:04 +02:00
$path = $this->builder->buildPath;
$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
}
/**
* Copies files from the root of the build directory into the target folder
2013-05-17 18:50:27 +02:00
*/
public function execute()
{
$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();
$cmd = 'mkdir -p "%s" && cp -R "%s" "%s"';
$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;
}
/**
* 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*"';
$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) {
throw new \Exception(sprintf('Failed to wipe existing directory %s before copy', $this->directory));
2014-05-15 14:39:54 +02: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) {
foreach ($this->builder->ignore as $file) {
2013-05-17 18:50:27 +02:00
$cmd = 'rm -Rf "%s/%s"';
$this->builder->executeCommand($cmd, $this->directory, $file);
2013-05-17 18:50:27 +02:00
}
}
}
}