2013-05-18 00:19:38 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2013-05-18 00:19:38 +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-18 00:19:38 +02:00
|
|
|
/**
|
2017-01-10 18:46:19 +01:00
|
|
|
* Create a ZIP or TAR.GZ archive of the entire build.
|
2018-02-04 08:22:07 +01:00
|
|
|
*
|
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 PackageBuild extends Plugin
|
2013-05-18 00:19:38 +02:00
|
|
|
{
|
|
|
|
protected $directory;
|
|
|
|
protected $filename;
|
|
|
|
protected $format;
|
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'package_build';
|
|
|
|
}
|
2018-02-04 08:22:07 +01:00
|
|
|
|
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-18 00:19:38 +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->filename = isset($options['filename']) ? $options['filename'] : 'build';
|
|
|
|
$this->format = isset($options['format']) ? $options['format'] : 'zip';
|
2013-05-18 00:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes Composer and runs a specified command (e.g. install / update)
|
|
|
|
*/
|
|
|
|
public function execute()
|
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
$path = $this->builder->buildPath;
|
2013-10-10 02:01:06 +02:00
|
|
|
$build = $this->build;
|
2013-05-18 00:19:38 +02:00
|
|
|
|
|
|
|
if ($this->directory == $path) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filename = str_replace('%build.commit%', $build->getCommitId(), $this->filename);
|
|
|
|
$filename = str_replace('%build.id%', $build->getId(), $filename);
|
|
|
|
$filename = str_replace('%build.branch%', $build->getBranch(), $filename);
|
|
|
|
$filename = str_replace('%project.title%', $build->getProject()->getTitle(), $filename);
|
|
|
|
$filename = str_replace('%date%', date('Y-m-d'), $filename);
|
|
|
|
$filename = str_replace('%time%', date('Hi'), $filename);
|
|
|
|
$filename = preg_replace('/([^a-zA-Z0-9_-]+)/', '', $filename);
|
|
|
|
|
2018-02-04 08:22:07 +01:00
|
|
|
$currentDir = getcwd();
|
2016-07-22 09:05:34 +02:00
|
|
|
chdir($this->builder->buildPath);
|
2013-05-18 00:19:38 +02:00
|
|
|
|
|
|
|
if (!is_array($this->format)) {
|
2016-04-21 06:58:09 +02:00
|
|
|
$this->format = [$this->format];
|
2013-05-18 00:19:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->format as $format) {
|
2015-10-05 15:17:13 +02:00
|
|
|
switch ($format) {
|
2013-05-18 00:19:38 +02:00
|
|
|
case 'tar':
|
|
|
|
$cmd = 'tar cfz "%s/%s.tar.gz" ./*';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case 'zip':
|
|
|
|
$cmd = 'zip -rq "%s/%s.zip" ./*';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:05:34 +02:00
|
|
|
$success = $this->builder->executeCommand($cmd, $this->directory, $filename);
|
2013-05-18 00:19:38 +02:00
|
|
|
}
|
|
|
|
|
2018-02-04 08:22:07 +01:00
|
|
|
chdir($currentDir);
|
2013-05-18 00:19:38 +02:00
|
|
|
|
|
|
|
return $success;
|
|
|
|
}
|
|
|
|
}
|