php-censor/src/Plugin/Grunt.php

82 lines
2 KiB
PHP
Raw 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:01:06 +02:00
/**
* Grunt Plugin - Provides access to grunt functionality.
*
2017-03-04 16:39:56 +01:00
* @author Tobias Tom <t.tom@succont.de>
*/
2016-07-11 18:00:04 +02:00
class Grunt extends Plugin
{
protected $directory;
protected $task;
protected $preferDist;
protected $grunt;
protected $gruntfile;
2017-01-11 16:15:54 +01:00
/**
* @return string
*/
public static function pluginName()
{
return 'grunt';
}
/**
2016-07-11 18:00:04 +02:00
* {@inheritdoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$path = $this->builder->buildPath;
2013-10-10 02:01:06 +02:00
$this->directory = $path;
2016-07-11 18:00:04 +02:00
$this->task = null;
$this->grunt = $this->findBinary('grunt');
2013-10-10 02:01:06 +02:00
$this->gruntfile = 'Gruntfile.js';
// Handle options:
if (isset($options['directory'])) {
$this->directory = $path . '/' . $options['directory'];
2013-10-10 02:01:06 +02:00
}
if (isset($options['task'])) {
$this->task = $options['task'];
}
if (isset($options['grunt'])) {
$this->grunt = $options['grunt'];
}
if (isset($options['gruntfile'])) {
$this->gruntfile = $options['gruntfile'];
}
}
/**
* Executes grunt and runs a specified command (e.g. install / update)
*/
public function execute()
{
// if npm does not work, we cannot use grunt, so we return false
2014-03-12 18:37:57 +01:00
$cmd = 'cd %s && npm install';
if (!$this->builder->executeCommand($cmd, $this->directory)) {
return false;
}
// build the grunt command
$cmd = 'cd %s && ' . $this->grunt;
$cmd .= ' --no-color';
$cmd .= ' --gruntfile %s';
$cmd .= ' %s'; // the task that will be executed
// and execute it
return $this->builder->executeCommand($cmd, $this->directory, $this->gruntfile, $this->task);
}
}