php-censor/src/Plugin/PackageBuild.php

87 lines
2.4 KiB
PHP
Raw Permalink Normal View History

<?php
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Plugin;
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
/**
* 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>
*/
2016-07-11 18:00:04 +02:00
class PackageBuild extends Plugin
{
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
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
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->filename = isset($options['filename']) ? $options['filename'] : 'build';
$this->format = isset($options['format']) ? $options['format'] : 'zip';
}
/**
* Executes Composer and runs a specified command (e.g. install / update)
*/
public function execute()
{
$path = $this->builder->buildPath;
2013-10-10 02:01:06 +02:00
$build = $this->build;
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();
chdir($this->builder->buildPath);
if (!is_array($this->format)) {
2016-04-21 06:58:09 +02:00
$this->format = [$this->format];
}
foreach ($this->format as $format) {
2015-10-05 15:17:13 +02:00
switch ($format) {
case 'tar':
$cmd = 'tar cfz "%s/%s.tar.gz" ./*';
break;
default:
case 'zip':
$cmd = 'zip -rq "%s/%s.zip" ./*';
break;
}
$success = $this->builder->executeCommand($cmd, $this->directory, $filename);
}
2018-02-04 08:22:07 +01:00
chdir($currentDir);
return $success;
}
}