From b783577f1b314f8188e28784524f378adf0004f0 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Thu, 13 Jun 2013 13:04:45 +0200 Subject: [PATCH] Add javascript builder --- .travis.yml | 5 +- bin/setup | 6 +- builder.php | 9 +- .../Command/Setup/JavascriptBuilder.php | 99 +++++++++++++++++++ .../{LessCompile.php => LessCompiler.php} | 16 +-- vendors.php | 17 +++- 6 files changed, 137 insertions(+), 15 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Command/Setup/JavascriptBuilder.php rename lib/Alchemy/Phrasea/Command/Setup/{LessCompile.php => LessCompiler.php} (73%) diff --git a/.travis.yml b/.travis.yml index e2b98af757..e5f637514d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,14 +10,15 @@ before_script: - cp -f hudson/_GV.php config/ - node --version - npm --version - - npm install bower mocha-phantomjs karma recess@">=1.1.0 <1.1.7" -g + - npm install bower mocha-phantomjs karma recess@">=1.1.0 <1.1.7" uglify-js -g - echo '' > ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini - sh -c 'if [ $(php -r "echo PHP_MINOR_VERSION;") -le 4 ]; then echo "extension = apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;' - sh -c 'if [ $(php -r "echo PHP_MINOR_VERSION;") -le 4 ]; then pecl install redis; fi;' - bower install - composer self-update - composer install --dev --prefer-source - - bin/setup less:compile + - bin/setup assets:compile-less + - bin/setup assets:build-javascript - sudo apt-get update - sudo apt-get install -y -qq xpdf ffmpeg libavcodec-extra-53 openoffice.org-writer unoconv ghostscript gsfonts dcraw ufraw graphicsmagick libgraphicsmagick1-dev libfreetype6-dev libjpeg-dev libgif-dev libtiff-dev libdjvulibre-dev libwmf-dev libmagickcore-dev libmagickwand-dev libpng-dev gettext zlib1g-dev build-essential libfaac-dev libmp3lame0 libx264-dev libvpx-dev libtheora-dev libvorbis-dev gpac - sudo apt-get clean diff --git a/bin/setup b/bin/setup index ae325c99bd..8ed77ab5b8 100755 --- a/bin/setup +++ b/bin/setup @@ -19,7 +19,8 @@ namespace KonsoleKommander; use Alchemy\Phrasea\Core\Version; use Alchemy\Phrasea\Command\UpgradeDBDatas; use Alchemy\Phrasea\Command\Setup\Install; -use Alchemy\Phrasea\Command\Setup\LessCompile; +use Alchemy\Phrasea\Command\Setup\LessCompiler; +use Alchemy\Phrasea\Command\Setup\JavascriptBuilder; use Alchemy\Phrasea\CLI; use Alchemy\Phrasea\Command\Setup\CheckEnvironment; @@ -63,7 +64,8 @@ try { $app->command(new CheckEnvironment('check:system')); $app->command(new Install('system:install')); - $app->command(new LessCompile('less:compile')); + $app->command(new LessCompiler()); + $app->command(new JavascriptBuilder()); $result_code = is_int($app->run()) ? : 1; } catch (\Exception $e) { diff --git a/builder.php b/builder.php index 95932e3340..59ba897505 100755 --- a/builder.php +++ b/builder.php @@ -15,13 +15,20 @@ require __DIR__ . '/vendor/autoload.php'; chdir(__DIR__); -system('bin/setup less:compile', $code); +system('bin/setup assets:compile-less', $code); if (0 !== $code) { echo "Failed to build less files\n"; exit(1); } +system('bin/setup assets:build-javascript', $code); + +if (0 !== $code) { + echo "Failed to build javascript files\n"; + exit(1); +} + set_time_limit(0); printf('Remove files ...' . PHP_EOL); diff --git a/lib/Alchemy/Phrasea/Command/Setup/JavascriptBuilder.php b/lib/Alchemy/Phrasea/Command/Setup/JavascriptBuilder.php new file mode 100644 index 0000000000..6dc02b03b2 --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/Setup/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/Setup/LessCompile.php b/lib/Alchemy/Phrasea/Command/Setup/LessCompiler.php similarity index 73% rename from lib/Alchemy/Phrasea/Command/Setup/LessCompile.php rename to lib/Alchemy/Phrasea/Command/Setup/LessCompiler.php index b5b87f48a9..5fe91e1d91 100644 --- a/lib/Alchemy/Phrasea/Command/Setup/LessCompile.php +++ b/lib/Alchemy/Phrasea/Command/Setup/LessCompiler.php @@ -19,11 +19,11 @@ use Symfony\Component\Process\ProcessBuilder; /** * This command builds less file */ -class LessCompile extends Command +class LessCompiler extends Command { - public function __construct($name = null) + public function __construct() { - parent::__construct($name); + parent::__construct('assets:compile-less'); $this->setDescription('Compile less files'); } @@ -34,17 +34,17 @@ class LessCompile extends Command protected function doExecute(InputInterface $input, OutputInterface $output) { $files = array( - $this->container['root.path'] . '/www/skins/build/login.css' => realpath($this->container['root.path'] . '/www/skins/login/less/login.less'), - $this->container['root.path'] . '/www/skins/build/account.css' => realpath($this->container['root.path'] . '/www/skins/account/account.less'), - $this->container['root.path'] . '/www/skins/build/bootstrap/css/bootstrap.css' => realpath($this->container['root.path'] . '/www/assets/bootstrap/less/bootstrap.less'), - $this->container['root.path'] . '/www/skins/build/bootstrap/css/bootstrap-responsive.css' => realpath($this->container['root.path'] . '/www/assets/bootstrap/less/responsive.less'), + $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 $buildFile => $lessFile) { + foreach ($files as $lessFile => $buildFile) { $this->container['filesystem']->mkdir(dirname($buildFile)); $output->writeln(sprintf('Building %s', basename($lessFile))); $builder = ProcessBuilder::create(array( diff --git a/vendors.php b/vendors.php index 42a732f1fa..be952f7c64 100755 --- a/vendors.php +++ b/vendors.php @@ -20,6 +20,7 @@ set_time_limit(0); $bower = 'bower'; $node = 'node'; $recess = 'recess'; +$uglifyjs = 'uglifyjs'; $npm = 'npm'; // Test if node exists @@ -62,6 +63,18 @@ if (0 !== $code) { } } +// Tests if recess exists else install it +exec('uglifyjs --version', $output, $code); + +if (0 !== $code) { + exec(sprintf('sudo %s install uglify-js -g', $npm), $output, $code); + + if (0 !== $code) { + echo 'Failed to install uglifyjs'; + exit(1); + } +} + // Install asset dependencies with bower system(sprintf('%s install', $bower), $code); @@ -100,5 +113,5 @@ if (isset($argv[1]) && $argv[1] == '--no-dev') { system($composer . ' install --dev --optimize-autoloader'); } -system('bin/setup less:compile'); - +system('bin/setup assets:compile-less'); +system('bin/setup assets:build-javascript');