Skip to content

Commit e04f680

Browse files
committed
Upgrade compatibility to bower >= 1.0
1 parent d39fa42 commit e04f680

File tree

9 files changed

+87
-19
lines changed

9 files changed

+87
-19
lines changed

Bower/Bower.php

+7-11
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
namespace Sp\BowerBundle\Bower;
1313

1414
use Doctrine\Common\Cache\Cache;
15+
use Sp\BowerBundle\Bower\Exception\CommandException;
16+
use Sp\BowerBundle\Bower\Exception\Error;
1517
use Sp\BowerBundle\Bower\Exception\FileNotFoundException;
1618
use Sp\BowerBundle\Bower\Exception\InvalidMappingException;
1719
use Sp\BowerBundle\Bower\Exception\MappingException;
1820
use Sp\BowerBundle\Bower\Exception\RuntimeException;
1921
use Sp\BowerBundle\Bower\Package\DependencyMapper;
2022
use Sp\BowerBundle\Bower\Package\DependencyMapperInterface;
2123
use Symfony\Component\EventDispatcher\EventDispatcher;
24+
use Symfony\Component\Process\Process;
2225
use Symfony\Component\Process\ProcessBuilder;
2326

2427
/**
@@ -76,7 +79,7 @@ public function __construct($bowerPath = '/usr/bin/bower', Cache $dependencyCach
7679
*/
7780
public function install(ConfigurationInterface $config, $callback = null)
7881
{
79-
$result = $this->execCommand($config, 'install', $callback);
82+
$result = $this->execCommand($config, array('install'), $callback);
8083

8184
return $result->getProcess()->getExitCode();
8285
}
@@ -92,11 +95,8 @@ public function install(ConfigurationInterface $config, $callback = null)
9295
*/
9396
public function createDependencyMappingCache(ConfigurationInterface $config)
9497
{
95-
$result = $this->execCommand($config, array('list', '--map'));
98+
$result = $this->execCommand($config, array('list', '--json'));
9699
$output = $result->getProcess()->getOutput();
97-
if (strpos($output, 'error')) {
98-
throw new MappingException(sprintf('An error occurred while creating dependency mapping. The error was %s.', $output));
99-
}
100100

101101
$mapping = json_decode($output, true);
102102
if (null === $mapping) {
@@ -187,7 +187,7 @@ private function createCacheKey(ConfigurationInterface $config)
187187
* @param string|array $commands
188188
* @param \Closure|string|array|null $callback
189189
*
190-
* @throws Exception\RuntimeException
190+
* @throws Exception\CommandException
191191
* @return BowerResult
192192
*/
193193
private function execCommand(ConfigurationInterface $config, $commands, $callback = null)
@@ -214,11 +214,7 @@ private function execCommand(ConfigurationInterface $config, $commands, $callbac
214214
$proc->run($callback);
215215

216216
if (!$proc->isSuccessful()) {
217-
throw new RuntimeException(sprintf(
218-
'An error occurred while executing the command %s. The error was: "%s"',
219-
$proc->getCommandLine(),
220-
trim($proc->getErrorOutput())
221-
));
217+
throw new CommandException($proc->getCommandLine(),trim($proc->getErrorOutput()));
222218
}
223219

224220
$this->eventDispatcher->dispatch(BowerEvents::POST_EXEC, new BowerEvent($config, $commands));

Bower/Exception/CommandException.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SpBowerBundle package.
5+
*
6+
* (c) Martin Parsiegla <martin.parsiegla@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Sp\BowerBundle\Bower\Exception;
13+
14+
/**
15+
* @author Martin Parsiegla <martin.parsiegla@gmail.com>
16+
*/
17+
class CommandException extends RuntimeException implements ExceptionInterface
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $commandError;
23+
24+
/**
25+
* @param string $commandName
26+
* @param string $commandError
27+
*/
28+
public function __construct($commandName, $commandError)
29+
{
30+
$message = "Something went wrong while executing the command %s\n %s";
31+
$message = sprintf($message, $commandName, $commandError);
32+
parent::__construct($message);
33+
$this->commandError = $commandError;
34+
}
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getCommandError()
40+
{
41+
return $this->commandError;
42+
}
43+
}

Bower/Package/DependencyMapper.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ public function map(array $packagesInfo, ConfigurationInterface $config)
8181
{
8282
$this->config = $config;
8383
$packagesInfo = $this->orderPackages($packagesInfo);
84-
85-
foreach ($packagesInfo as $packageName => $packageInfo) {
84+
foreach ($packagesInfo[self::DEPENDENCIES_KEY] as $packageName => $packageInfo) {
8685
$package = $this->createPackage($packageName, $packageInfo);
8786
$this->packages->set($packageName, $package);
8887
}
@@ -200,7 +199,7 @@ private function resolveDependencies(Package $package, array $packageInfo)
200199
private function orderPackages(array $packagesInfo)
201200
{
202201
$dependenciesKey = self::DEPENDENCIES_KEY;
203-
uasort($packagesInfo, function($first, $second) use($dependenciesKey) {
202+
uasort($packagesInfo[self::DEPENDENCIES_KEY], function($first, $second) use($dependenciesKey) {
204203
$firstCount = isset($first[$dependenciesKey]) ? count($first[$dependenciesKey]) : 0;
205204
$secondCount = isset($second[$dependenciesKey]) ? count($second[$dependenciesKey]) : 0;
206205

Command/InstallCommand.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111

1212
namespace Sp\BowerBundle\Command;
1313

14+
use Sp\BowerBundle\Bower\Exception\CommandException;
15+
use Sp\BowerBundle\Bower\Exception\RuntimeException;
1416
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
1517
use Symfony\Component\Console\Output\OutputInterface;
1618
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Process\Process;
1720

1821
/**
1922
* @author Martin Parsiegla <martin.parsiegla@gmail.com>
@@ -38,12 +41,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
3841
$bowerManager = $this->getBowerManager();
3942
$bower = $this->getBower();
4043
$callback = function($type, $data) use ($output) {
41-
$output->write($data);
44+
if (Process::ERR != $type) {
45+
$output->write($data);
46+
}
4247
};
4348

4449
foreach ($bowerManager->getBundles() as $bundle => $configuration) {
4550
$output->writeln(sprintf('Installing bower dependencies for <comment>"%s"</comment> into <comment>"%s"</comment>', $bundle, $configuration->getAssetDirectory()));
46-
$bower->install($configuration, $callback);
51+
try {
52+
$bower->install($configuration, $callback);
53+
} catch (CommandException $ex) {
54+
$output->writeln($ex->getCommandError());
55+
throw new RuntimeException("An error occured while installing dependencies");
56+
}
4757
}
4858
}
4959

DependencyInjection/Configuration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function getConfigTreeBuilder()
9494
->children()
9595
->scalarNode('config_dir')->defaultValue('Resources/config/bower')->end()
9696
->scalarNode('asset_dir')->defaultValue('../../public/components')->end()
97-
->scalarNode('json_file')->defaultValue('component.json')->end()
97+
->scalarNode('json_file')->defaultValue('bower.json')->end()
9898
->scalarNode('endpoint')->defaultValue('https://bower.herokuapp.com')->end()
9999
->end()
100100
->end()

Tests/Bower/BowerTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testCreateDependencyMappingCache()
118118
$this->processBuilder->expects($this->once())->method('setTimeout')->with($this->equalTo(600));
119119
$this->processBuilder->expects($this->at(2))->method('add')->with($this->equalTo($this->bin));
120120
$this->processBuilder->expects($this->at(3))->method('add')->with($this->equalTo('list'));
121-
$this->processBuilder->expects($this->at(4))->method('add')->with($this->equalTo('--map'));
121+
$this->processBuilder->expects($this->at(4))->method('add')->with($this->equalTo('--json'));
122122
$this->processBuilder->expects($this->once())->method('getProcess')->will($this->returnValue($this->process));
123123
$this->process->expects($this->once())->method('isSuccessful')->will($this->returnValue(true));
124124
$this->bower->expects($this->once())->method('dumpBowerConfig');
@@ -167,10 +167,12 @@ public function testGetDependencyMapping()
167167
*/
168168
public function testUnsuccessfulInstallThrowsRuntimeException()
169169
{
170+
$jsonString = file_get_contents(self::$fixturesDirectory .'/error.json');
170171
$configDir = "/config_dir";
171172
$config = new Configuration($configDir);
172173
$this->processBuilder->expects($this->once())->method('getProcess')->will($this->returnValue($this->process));
173174
$this->process->expects($this->once())->method('isSuccessful')->will($this->returnValue(false));
175+
$this->process->expects($this->once())->method('getErrorOutput')->will($this->returnValue($jsonString));
174176

175177
$this->bower->install($config);
176178
}

Tests/Bower/Fixtures/config/component.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "test-fixture",
23
"dependencies": {
34
"jquery": "~1.8.2"
45
}

Tests/Bower/Fixtures/error.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[{
2+
"level": "warn",
3+
"id": "deprecated",
4+
"message": "You are using the deprecated component.json file",
5+
"data": {
6+
"json": "foo"
7+
}
8+
}, {
9+
"code": "EINVALID",
10+
"details": "No name property set",
11+
"data": {
12+
"filename": "component.json"
13+
},
14+
"id": "EINVALID",
15+
"level": "error",
16+
"message": "Something went wrong while reading component.json"
17+
}]

Tests/DependencyInjection/SpBowerExtensionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function testLoadDefaults()
9696
$configDefinition = $calls[0][1][1];
9797
$configCalls = $configDefinition->getMethodCalls();
9898
$this->assertEquals($this->demoBundlePath .'/Resources/config/bower/../../public/components', $configCalls[0][1][0]);
99-
$this->assertEquals('component.json', $configCalls[1][1][0]);
99+
$this->assertEquals('bower.json', $configCalls[1][1][0]);
100100
$this->assertEquals('https://bower.herokuapp.com', $configCalls[2][1][0]);
101101
}
102102

0 commit comments

Comments
 (0)