From fa1174d6e39d09e8173cc92f77c1e197a1c65014 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Tue, 13 Aug 2013 13:01:56 +0200 Subject: [PATCH] Update bower install command --- .../Command/Developer/BowerInstall.php | 50 ++++++++++--------- .../Phrasea/Command/Developer/InstallAll.php | 7 ++- .../Command/Developer/Utils/GruntDriver.php | 42 ++++++++++++++++ .../CLIProvider/CLIDriversServiceProvider.php | 13 ++++- .../Developper/Utils/GruntDriverTest.php | 26 ++++++++++ 5 files changed, 113 insertions(+), 25 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Command/Developer/Utils/GruntDriver.php create mode 100644 tests/Alchemy/Tests/Phrasea/Command/Developper/Utils/GruntDriverTest.php diff --git a/lib/Alchemy/Phrasea/Command/Developer/BowerInstall.php b/lib/Alchemy/Phrasea/Command/Developer/BowerInstall.php index 5d5e242543..46eebf761e 100644 --- a/lib/Alchemy/Phrasea/Command/Developer/BowerInstall.php +++ b/lib/Alchemy/Phrasea/Command/Developer/BowerInstall.php @@ -28,12 +28,20 @@ class BowerInstall extends Command $this ->setDescription('Installs bower dependencies') ->addOption('no-dev', 'd', InputOption::VALUE_NONE, 'Do not install dev dependencies') - ->addOption('attempts', 'a', InputOption::VALUE_REQUIRED, 'Number of attempts to install dependencies.', 4); + ->addOption( + 'prefer-dist', + null, + InputOption::VALUE_NONE, + 'If defined forces installation from dist package' + ); } protected function doExecute(InputInterface $input, OutputInterface $output) { + $grunt = $this->container['driver.grunt']; $bower = $this->container['driver.bower']; + + $output->writeln("Using ".$grunt->getProcessBuilderFactory()->getBinary()." for driver"); $output->writeln("Using ".$bower->getProcessBuilderFactory()->getBinary()." for driver"); $version = trim($bower->command('-v')); @@ -44,33 +52,29 @@ class BowerInstall extends Command )); } - $attempts = $input->getOption('attempts'); + $version = trim($grunt->command('--version')); - if (0 >= $attempts) { - throw new InvalidArgumentException('Attempts number should be a positive value.'); + if (!version_compare('0.4.0', substr($version, -5), '<=')) { + throw new RuntimeException(sprintf( + 'Grunt version >= 0.4.0 is required (version %s provided), please install grunt `http://gruntjs.com/getting-started`', $version + )); } - $output->write("Cleaning bower cache... "); - $bower->command(array('cache', 'clean')); - $output->writeln("OK"); - - $output->write("Removing assets... "); - $this->container['filesystem']->remove($this->container['root.path'] . '/www/assets'); - $output->writeln("OK"); + if ($input->getOption('prefer-dist')) { + $output->write("Cleaning bower cache... "); + $bower->command(array('cache', 'clean')); + $output->writeln("OK"); + } $success = false; - $n = 1; - while ($attempts > 0) { - try { - $output->write("\rInstalling assets (attempt #$n)..."); - $bower->command($input->getOption('no-dev') ? array('install', '--production') : 'install'); - $success = true; - $output->writeln("OK"); - break; - } catch (ExecutionFailureException $e) { - $attempts--; - $n++; - } + + try { + $output->write("\rInstalling assets..."); + $grunt->command('build-assets'); + $success = true; + $output->writeln("OK"); + } catch (ExecutionFailureException $e) { + } if (!$success) { diff --git a/lib/Alchemy/Phrasea/Command/Developer/InstallAll.php b/lib/Alchemy/Phrasea/Command/Developer/InstallAll.php index f5a7a6953b..16d6e2b28c 100644 --- a/lib/Alchemy/Phrasea/Command/Developer/InstallAll.php +++ b/lib/Alchemy/Phrasea/Command/Developer/InstallAll.php @@ -26,7 +26,12 @@ class InstallAll extends Command ->setDescription('Installs all dependencies') ->addOption('no-dev', 'd', InputOption::VALUE_NONE, 'Do not install dev dependencies') ->addOption('prefer-source', 'p', InputOption::VALUE_NONE, 'Use the --prefer-source composer option') - ->addOption('attempts', 'a', InputOption::VALUE_REQUIRED, 'Number of attempts to install bower dependencies.', 4); + ->addOption( + 'prefer-dist', + null, + InputOption::VALUE_NONE, + 'If defined forces installation from bower dist package' + ); } protected function doExecute(InputInterface $input, OutputInterface $output) diff --git a/lib/Alchemy/Phrasea/Command/Developer/Utils/GruntDriver.php b/lib/Alchemy/Phrasea/Command/Developer/Utils/GruntDriver.php new file mode 100644 index 0000000000..36c7c1179e --- /dev/null +++ b/lib/Alchemy/Phrasea/Command/Developer/Utils/GruntDriver.php @@ -0,0 +1,42 @@ +get('grunt.binaries', array('grunt')); + + return static::load($binaries, $logger, $conf); + } +} diff --git a/lib/Alchemy/Phrasea/Core/CLIProvider/CLIDriversServiceProvider.php b/lib/Alchemy/Phrasea/Core/CLIProvider/CLIDriversServiceProvider.php index ab7220a231..b8ebf542ad 100644 --- a/lib/Alchemy/Phrasea/Core/CLIProvider/CLIDriversServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/CLIProvider/CLIDriversServiceProvider.php @@ -12,9 +12,10 @@ namespace Alchemy\Phrasea\Core\CLIProvider; use Alchemy\Phrasea\Command\Developer\Utils\BowerDriver; -use Alchemy\Phrasea\Command\Developer\Utils\UglifyJsDriver; use Alchemy\Phrasea\Command\Developer\Utils\ComposerDriver; +use Alchemy\Phrasea\Command\Developer\Utils\GruntDriver; use Alchemy\Phrasea\Command\Developer\Utils\RecessDriver; +use Alchemy\Phrasea\Command\Developer\Utils\UglifyJsDriver; use Alchemy\Phrasea\Exception\RuntimeException; use Silex\Application; use Silex\ServiceProviderInterface; @@ -85,6 +86,16 @@ class CLIDriversServiceProvider implements ServiceProviderInterface return UglifyJsDriver::create(array('uglifyjs.binaries' => $uglifyJsBinary), $app['monolog']); }); + + $app['driver.grunt'] = $app->share(function (Application $app) { + $gruntBinary = $app['driver.binary-finder']('grunt', 'grunt_binary'); + + if (null === $gruntBinary) { + throw new RuntimeException('Unable to find grunt executable.'); + } + + return GruntDriver::create(array('grunt.binaries' => $gruntBinary), $app['monolog']); + }); } public function boot(Application $app) diff --git a/tests/Alchemy/Tests/Phrasea/Command/Developper/Utils/GruntDriverTest.php b/tests/Alchemy/Tests/Phrasea/Command/Developper/Utils/GruntDriverTest.php new file mode 100644 index 0000000000..ba1bfa01d5 --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Command/Developper/Utils/GruntDriverTest.php @@ -0,0 +1,26 @@ +assertInstanceOf('Alchemy\Phrasea\Command\Developer\Utils\GruntDriver', $driver); + $this->assertEquals('grunt', $driver->getName()); + } + + public function testCreateWithCustomBinary() + { + $finder = new PhpExecutableFinder(); + $php = $finder->find(); + + $driver = GruntDriver::create(array('grunt.binaries' => $php)); + $this->assertInstanceOf('Alchemy\Phrasea\Command\Developer\Utils\GruntDriver', $driver); + $this->assertEquals($php, $driver->getProcessBuilderFactory()->getBinary()); + } +}