Files
Phraseanet/lib/Alchemy/Phrasea/Command/Setup/JavascriptBuilder.php
2013-06-13 13:52:03 +02:00

100 lines
3.9 KiB
PHP

<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Command\Setup;
use Alchemy\Phrasea\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\ProcessBuilder;
use Alchemy\Phrasea\Exception\RuntimeException;
/**
* This command builds less file
*/
class JavascriptBuilder extends Command
{
public function __construct()
{
parent::__construct('assets:build-javascript');
$this->setDescription('Compile less files');
}
/**
* {@inheritdoc}
*/
protected function doExecute(InputInterface $input, OutputInterface $output)
{
$files = array(
$this->container['root.path'] . '/www/skins/build/bootstrap/js/bootstrap.js' => array(
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-transition.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-alert.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-button.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-carousel.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-collapse.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-dropdown.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-modal.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-tooltip.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-popover.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-scrollspy.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-tab.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-typeahead.js',
$this->container['root.path'] . '/www/assets/bootstrap/js/bootstrap-affix.js',
)
);
foreach ($files as $target => $sources) {
$this->buildJavascript($input, $output, $target, $sources);
$minifiedTarget = substr($target, 0, -3) . '.min.js';
$this->buildMinifiedJavascript($input, $output, $minifiedTarget, $target);
}
}
private function buildJavascript(InputInterface $input, OutputInterface $output, $target, $sources)
{
$output->writeln("Building <info>".basename($target)."</info>");
$this->container['filesystem']->remove($target);
$process = ProcessBuilder::create(array_merge(array('cat'), $sources))->getProcess();
if ($input->getOption('verbose')) {
$output->writeln("Executing ".$process->getCommandLine()."\n");
}
$process->run();
if (!$process->isSuccessFul()) {
throw new RuntimeException(sprintf('Failed to generate %s', $target));
}
$this->container['filesystem']->mkdir(dirname($target));
file_put_contents($target, $process->getOutput());
}
private function buildMinifiedJavascript(InputInterface $input, OutputInterface $output, $target, $source)
{
$output->writeln("Building <info>".basename($target)."</info>");
$this->container['filesystem']->remove($target);
$process = ProcessBuilder::create(array('uglifyjs', $source, '-nc'))->getProcess();
if ($input->getOption('verbose')) {
$output->writeln("Executing ".$process->getCommandLine()."\n");
}
$process->run();
if (!$process->isSuccessFul()) {
throw new RuntimeException(sprintf('Failed to generate %s', $target));
}
file_put_contents($target, $process->getOutput());
}
}