Skip to content

Instantly share code, notes, and snippets.

@vpassapera
Forked from shaneog/GruntBridge.php
Created August 21, 2013 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vpassapera/6292327 to your computer and use it in GitHub Desktop.
Save vpassapera/6292327 to your computer and use it in GitHub Desktop.
<?php
namespace SOG\Composer;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;
use RuntimeException;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Composer\IO\IOInterface;
use Icecave\Isolator\Isolator;
class GruntBridge
{
private $executableFinder;
private $isolator;
private $gruntPath;
/**
* @param GruntBridge|null $instance
*
* @return GruntBridge
*/
public static function get(GruntBridge $instance = null)
{
if (null === $instance) {
$instance = new GruntBridge;
}
return $instance;
}
/**
* @param Event $event
* @param GruntBridge|null $instance
*/
public static function handle(
Event $event,
GruntBridge $instance = null
) {
$instance = static::get($instance);
switch ($event->getName()) {
case ScriptEvents::POST_INSTALL_CMD:
$instance->postInstall($event);
break;
case ScriptEvents::POST_UPDATE_CMD:
$instance->postUpdate($event);
}
}
/**
* @param ExecutableFinder|null $executableFinder
* @param Isolator|null $isolator
*/
public function __construct(
ExecutableFinder $executableFinder = null,
Isolator $isolator = null
) {
if (null === $executableFinder) {
$executableFinder = new ExecutableFinder;
}
$this->executableFinder = $executableFinder;
$this->isolator = Isolator::get($isolator);
}
/**
* @return ExecutableFinder
*/
public function executableFinder()
{
return $this->executableFinder;
}
/**
* @param Event $event
*/
public function postInstall(Event $event)
{
$io = $event->getIO();
$io->write('<info>Running Grunt default tasks</info>');
$this->executeGrunt(array(), $io);
}
/**
* @param Event $event
*/
public function postUpdate(Event $event)
{
$io = $event->getIO();
$io->write('<info>Running Grunt default tasks</info>');
$this->executeGrunt(array(), $io);
}
/**
* @param array<string> $arguments
*
* @return Process
*/
protected function createGruntProcess(array $arguments)
{
array_unshift($arguments, $this->gruntPath());
$processBuilder = $this->createProcessBuilder($arguments);
return $processBuilder->getProcess();
}
/**
* @return string
*/
protected function gruntPath()
{
if (null === $this->gruntPath) {
$this->gruntPath = $this->executableFinder()->find('grunt');
if (null === $this->gruntPath) {
throw new RuntimeException('Unable to locate grunt executable.');
}
}
return $this->gruntPath;
}
/**
* @param array<string> $arguments
* @param IOInterface $io
*/
protected function executeGrunt(array $arguments, IOInterface $io)
{
$gruntProcess = $this->createGruntProcess($arguments);
$isolator = $this->isolator;
$gruntProcess->run(function ($type, $buffer) use ($isolator, $io) {
if (Process::ERR === $type) {
$isolator->fwrite(STDERR, $buffer);
} else {
$io->write($buffer, false);
}
});
}
/**
* @param array<string> $arguments
*
* @return ProcessBuilder
*/
protected function createProcessBuilder(array $arguments)
{
return new ProcessBuilder($arguments);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment