diff --git a/bin/developer b/bin/developer index 5e33b84c54..496de563be 100755 --- a/bin/developer +++ b/bin/developer @@ -10,11 +10,13 @@ */ use Alchemy\Phrasea\CLI; -use Alchemy\Phrasea\Core\Version; -use Alchemy\Phrasea\Command\Developer\RegenerateSqliteDb; -use Alchemy\Phrasea\Command\Developer\RoutesDumper; use Alchemy\Phrasea\Command\Developer\APIRoutesDumper; use Alchemy\Phrasea\Command\Developer\Behat; +use Alchemy\Phrasea\Command\Developer\JavascriptBuilder; +use Alchemy\Phrasea\Command\Developer\LessCompiler; +use Alchemy\Phrasea\Command\Developer\RegenerateSqliteDb; +use Alchemy\Phrasea\Command\Developer\RoutesDumper; +use Alchemy\Phrasea\Core\Version; use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper; use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper; @@ -66,6 +68,8 @@ try { $cli->command(new APIRoutesDumper()); $cli->command(new RoutesDumper()); $cli->command(new Behat()); + $cli->command(new LessCompiler()); + $cli->command(new JavascriptBuilder()); $cli['console']->addCommands(array( // DBAL Commands diff --git a/bin/setup b/bin/setup index 8ed77ab5b8..0bb44c9508 100755 --- a/bin/setup +++ b/bin/setup @@ -19,8 +19,6 @@ namespace KonsoleKommander; use Alchemy\Phrasea\Core\Version; use Alchemy\Phrasea\Command\UpgradeDBDatas; use Alchemy\Phrasea\Command\Setup\Install; -use Alchemy\Phrasea\Command\Setup\LessCompiler; -use Alchemy\Phrasea\Command\Setup\JavascriptBuilder; use Alchemy\Phrasea\CLI; use Alchemy\Phrasea\Command\Setup\CheckEnvironment; @@ -64,8 +62,6 @@ try { $app->command(new CheckEnvironment('check:system')); $app->command(new Install('system:install')); - $app->command(new LessCompiler()); - $app->command(new JavascriptBuilder()); $result_code = is_int($app->run()) ? : 1; } catch (\Exception $e) { diff --git a/lib/Alchemy/Phrasea/Command/Developer/JavascriptBuilder.php b/lib/Alchemy/Phrasea/Command/Developer/JavascriptBuilder.php new file mode 100644 index 0000000000..a51dd62197 --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/Developer/JavascriptBuilder.php @@ -0,0 +1,99 @@ +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 ".basename($target).""); + $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 ".basename($target).""); + $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()); + } +} diff --git a/lib/Alchemy/Phrasea/Command/Developer/LessCompiler.php b/lib/Alchemy/Phrasea/Command/Developer/LessCompiler.php new file mode 100644 index 0000000000..eb85155413 --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/Developer/LessCompiler.php @@ -0,0 +1,95 @@ +setDescription('Compile less files'); + } + + /** + * {@inheritdoc} + */ + protected function doExecute(InputInterface $input, OutputInterface $output) + { + $files = array( + $this->container['root.path'] . '/www/skins/login/less/login.less' => $this->container['root.path'] . '/www/skins/build/login.css', + $this->container['root.path'] . '/www/skins/account/account.less' => $this->container['root.path'] . '/www/skins/build/account.css', + $this->container['root.path'] . '/www/assets/bootstrap/less/bootstrap.less' => $this->container['root.path'] . '/www/skins/build/bootstrap/css/bootstrap.css', + $this->container['root.path'] . '/www/assets/bootstrap/less/responsive.less' => $this->container['root.path'] . '/www/skins/build/bootstrap/css/bootstrap-responsive.css', + ); + + $output->writeln('Building Assets...'); + + $failures = 0; + $errors = array(); + foreach ($files as $lessFile => $buildFile) { + $this->container['filesystem']->mkdir(dirname($buildFile)); + $output->writeln(sprintf('Building %s', basename($lessFile))); + + if (!is_file($lessFile)) { + throw new RuntimeException(realpath($lessFile) . ' does not exists.'); + } + + if (!is_writable(dirname($buildFile))) { + throw new RuntimeException(realpath(dirname($buildFile)) . ' is not writable.'); + } + + $builder = ProcessBuilder::create(array( + 'recess', + '--compile', + $lessFile, + )); + $process = $builder->getProcess(); + $process->run(); + + if (!$process->isSuccessful()) { + $failures++; + $errors[] = $process->getErrorOutput(); + } + file_put_contents($buildFile, $process->getOutput()); + } + + $copies = array( + $this->container['root.path'] . '/www/assets/bootstrap/img/glyphicons-halflings-white.png' => $this->container['root.path'] . '/www/skins/build/bootstrap/img/glyphicons-halflings-white.png', + $this->container['root.path'] . '/www/assets/bootstrap/img/glyphicons-halflings.png' => $this->container['root.path'] . '/www/skins/build/bootstrap/img/glyphicons-halflings.png', + ); + + foreach ($copies as $source => $target) { + $this->container['filesystem']->mkdir(dirname($target)); + $this->container['filesystem']->copy($source, $target); + } + + if (0 === $failures) { + $output->writeln('Build done !'); + + return 0; + } + + $output->writeln(sprintf('%d errors occured during the build %s', $failures, implode(', ', $errors))); + + return 1; + } +}