Skip to content

Commit 4a7cd6d

Browse files
feature #439 Implement "sync-recipes --force" option (Pierstoval)
This PR was merged into the 1.1-dev branch. Discussion ---------- Implement "sync-recipes --force" option Closes #179 Commits ------- 939de95 Implement "sync-recipes --force" option
2 parents 9bddfd8 + 939de95 commit 4a7cd6d

28 files changed

+242
-167
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/vendor/
2+
/build/
23
.php_cs.cache
34
composer.lock

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"require-dev": {
1717
"composer/composer": "^1.0.2",
18-
"symfony/phpunit-bridge": "^3.4.19|^4.1.8"
18+
"symfony/phpunit-bridge": "^3.4.19|^4.1.8",
19+
"symfony/process": "^2.7|^3.0|^4.0"
1920
},
2021
"autoload": {
2122
"psr-4": {

phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
processIsolation="false"
1010
stopOnFailure="false"
1111
syntaxCheck="false"
12-
bootstrap="vendor/autoload.php"
12+
bootstrap="tests/bootstrap.php"
1313
>
1414
<testsuites>
1515
<testsuite name="Symfony Flex Test Suite">

src/Command/SyncRecipesCommand.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
use Composer\Command\BaseCommand;
1515
use Composer\DependencyResolver\Operation\InstallOperation;
1616
use Composer\Factory;
17-
use Composer\Script\Event;
1817
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
1919
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Flex\Event\UpdateEvent;
2021
use Symfony\Flex\Lock;
2122

2223
class SyncRecipesCommand extends BaseCommand
@@ -35,24 +36,27 @@ protected function configure()
3536
$this->setName('symfony:sync-recipes')
3637
->setAliases(['sync-recipes', 'fix-recipes'])
3738
->setDescription('Installs or reinstalls recipes for already installed packages.')
39+
->addOption('force', null, InputOption::VALUE_NONE, 'Ignore the "symfony.lock" file and overwrite existing files')
3840
;
3941
}
4042

4143
protected function execute(InputInterface $input, OutputInterface $output)
4244
{
45+
$force = $input->getOption('force');
46+
4347
$symfonyLock = new Lock(getenv('SYMFONY_LOCKFILE') ?: str_replace('composer.json', 'symfony.lock', Factory::getComposerFile()));
4448
$composer = $this->getComposer();
4549
$locker = $composer->getLocker();
4650
$lockData = $locker->getLockData();
4751

4852
$packages = [];
4953
foreach ($lockData['packages'] as $pkg) {
50-
if (!$symfonyLock->has($pkg['name'])) {
54+
if ($force || !$symfonyLock->has($pkg['name'])) {
5155
$packages[] = $pkg['name'];
5256
}
5357
}
5458
foreach ($lockData['packages-dev'] as $pkg) {
55-
if (!$symfonyLock->has($pkg['name'])) {
59+
if ($force || !$symfonyLock->has($pkg['name'])) {
5660
$packages[] = $pkg['name'];
5761
}
5862
}
@@ -76,10 +80,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
7680
$operations[] = new InstallOperation($pkg);
7781
}
7882

79-
$this->flex->update(new class() extends Event {
80-
public function __construct()
81-
{
82-
}
83-
}, $operations);
83+
$this->flex->update(new UpdateEvent($force), $operations);
8484
}
8585
}

src/Configurator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public function __construct(Composer $composer, IOInterface $io, Options $option
4444
];
4545
}
4646

47-
public function install(Recipe $recipe)
47+
public function install(Recipe $recipe, array $options = [])
4848
{
4949
$manifest = $recipe->getManifest();
5050
foreach (array_keys($this->configurators) as $key) {
5151
if (isset($manifest[$key])) {
52-
$this->get($key)->configure($recipe, $manifest[$key]);
52+
$this->get($key)->configure($recipe, $manifest[$key], $options);
5353
}
5454
}
5555
}

src/Configurator/AbstractConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(Composer $composer, IOInterface $io, Options $option
3535
$this->path = new Path(getcwd());
3636
}
3737

38-
abstract public function configure(Recipe $recipe, $config);
38+
abstract public function configure(Recipe $recipe, $config, array $options = []);
3939

4040
abstract public function unconfigure(Recipe $recipe, $config);
4141

src/Configurator/BundlesConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class BundlesConfigurator extends AbstractConfigurator
2020
{
21-
public function configure(Recipe $recipe, $bundles)
21+
public function configure(Recipe $recipe, $bundles, array $options = [])
2222
{
2323
$this->write('Enabling the package as a Symfony bundle');
2424
$file = $this->getConfFile();

src/Configurator/ComposerScriptsConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
class ComposerScriptsConfigurator extends AbstractConfigurator
2323
{
24-
public function configure(Recipe $recipe, $scripts)
24+
public function configure(Recipe $recipe, $scripts, array $options = [])
2525
{
2626
$json = new JsonFile(Factory::getComposerFile());
2727

src/Configurator/ContainerConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class ContainerConfigurator extends AbstractConfigurator
2020
{
21-
public function configure(Recipe $recipe, $parameters)
21+
public function configure(Recipe $recipe, $parameters, array $options = [])
2222
{
2323
$this->write('Setting parameters');
2424
$this->addParameters($parameters);

src/Configurator/CopyFromPackageConfigurator.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
*/
2020
class CopyFromPackageConfigurator extends AbstractConfigurator
2121
{
22-
public function configure(Recipe $recipe, $config)
22+
public function configure(Recipe $recipe, $config, array $options = [])
2323
{
2424
$this->write('Setting configuration and copying files');
2525
$packageDir = $this->composer->getInstallationManager()->getInstallPath($recipe->getPackage());
26-
$this->copyFiles($config, $packageDir, getcwd());
26+
$this->copyFiles($config, $packageDir, getcwd(), $options['force'] ?? false);
2727
}
2828

2929
public function unconfigure(Recipe $recipe, $config)
@@ -33,22 +33,20 @@ public function unconfigure(Recipe $recipe, $config)
3333
$this->removeFiles($config, $packageDir, getcwd());
3434
}
3535

36-
private function copyFiles(array $manifest, string $from, string $to)
36+
private function copyFiles(array $manifest, string $from, string $to, bool $overwrite = false)
3737
{
3838
foreach ($manifest as $source => $target) {
3939
$target = $this->options->expandTargetDir($target);
4040
if ('/' === substr($source, -1)) {
41-
$this->copyDir($this->path->concatenate([$from, $source]), $this->path->concatenate([$to, $target]));
41+
$this->copyDir($this->path->concatenate([$from, $source]), $this->path->concatenate([$to, $target]), $overwrite);
4242
} else {
4343
$targetPath = $this->path->concatenate([$to, $target]);
4444
if (!is_dir(\dirname($targetPath))) {
4545
mkdir(\dirname($targetPath), 0777, true);
4646
$this->write(sprintf('Created <fg=green>"%s"</>', $this->path->relativize(\dirname($targetPath))));
4747
}
4848

49-
if (!file_exists($targetPath)) {
50-
$this->copyFile($this->path->concatenate([$from, $source]), $targetPath);
51-
}
49+
$this->copyFile($this->path->concatenate([$from, $source]), $targetPath, $overwrite);
5250
}
5351
}
5452
}
@@ -69,7 +67,7 @@ private function removeFiles(array $manifest, string $from, string $to)
6967
}
7068
}
7169

72-
private function copyDir(string $source, string $target)
70+
private function copyDir(string $source, string $target, bool $overwrite)
7371
{
7472
if (!is_dir($target)) {
7573
mkdir($target, 0777, true);
@@ -84,14 +82,14 @@ private function copyDir(string $source, string $target)
8482
$this->write(sprintf('Created <fg=green>"%s"</>', $this->path->relativize($targetPath)));
8583
}
8684
} elseif (!file_exists($targetPath)) {
87-
$this->copyFile($item, $targetPath);
85+
$this->copyFile($item, $targetPath, $overwrite);
8886
}
8987
}
9088
}
9189

92-
public function copyFile(string $source, string $target)
90+
public function copyFile(string $source, string $target, bool $overwrite = false)
9391
{
94-
if (file_exists($target)) {
92+
if (!$this->options->shouldWriteFile($target, $overwrite)) {
9593
return;
9694
}
9795

src/Configurator/CopyFromRecipeConfigurator.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*/
1919
class CopyFromRecipeConfigurator extends AbstractConfigurator
2020
{
21-
public function configure(Recipe $recipe, $config)
21+
public function configure(Recipe $recipe, $config, array $options = [])
2222
{
2323
$this->write('Setting configuration and copying files');
24-
$this->copyFiles($config, $recipe->getFiles(), getcwd());
24+
$this->copyFiles($config, $recipe->getFiles(), getcwd(), $options['force'] ?? false);
2525
}
2626

2727
public function unconfigure(Recipe $recipe, $config)
@@ -30,31 +30,31 @@ public function unconfigure(Recipe $recipe, $config)
3030
$this->removeFiles($config, $recipe->getFiles(), getcwd());
3131
}
3232

33-
private function copyFiles(array $manifest, array $files, string $to)
33+
private function copyFiles(array $manifest, array $files, string $to, bool $overwrite = false)
3434
{
3535
foreach ($manifest as $source => $target) {
3636
$target = $this->options->expandTargetDir($target);
3737
if ('/' === substr($source, -1)) {
38-
$this->copyDir($source, $this->path->concatenate([$to, $target]), $files);
38+
$this->copyDir($source, $this->path->concatenate([$to, $target]), $files, $overwrite);
3939
} else {
40-
$this->copyFile($this->path->concatenate([$to, $target]), $files[$source]['contents'], $files[$source]['executable']);
40+
$this->copyFile($this->path->concatenate([$to, $target]), $files[$source]['contents'], $files[$source]['executable'], $overwrite);
4141
}
4242
}
4343
}
4444

45-
private function copyDir(string $source, string $target, array $files)
45+
private function copyDir(string $source, string $target, array $files, bool $overwrite = false)
4646
{
4747
foreach ($files as $file => $data) {
4848
if (0 === strpos($file, $source)) {
4949
$file = $this->path->concatenate([$target, substr($file, \strlen($source))]);
50-
$this->copyFile($file, $data['contents'], $data['executable']);
50+
$this->copyFile($file, $data['contents'], $data['executable'], $overwrite);
5151
}
5252
}
5353
}
5454

55-
private function copyFile(string $to, string $contents, bool $executable)
55+
private function copyFile(string $to, string $contents, bool $executable, bool $overwrite = false)
5656
{
57-
if (file_exists($to)) {
57+
if (!$this->options->shouldWriteFile($to, $overwrite)) {
5858
return;
5959
}
6060

src/Configurator/EnvConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class EnvConfigurator extends AbstractConfigurator
2020
{
21-
public function configure(Recipe $recipe, $vars)
21+
public function configure(Recipe $recipe, $vars, array $options = [])
2222
{
2323
$this->write('Added environment variable defaults');
2424

src/Configurator/GitignoreConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class GitignoreConfigurator extends AbstractConfigurator
2020
{
21-
public function configure(Recipe $recipe, $vars)
21+
public function configure(Recipe $recipe, $vars, array $options = [])
2222
{
2323
$this->write('Added entries to .gitignore');
2424

src/Configurator/MakefileConfigurator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class MakefileConfigurator extends AbstractConfigurator
2020
{
21-
public function configure(Recipe $recipe, $definitions)
21+
public function configure(Recipe $recipe, $definitions, array $options = [])
2222
{
2323
$this->write('Added Makefile entries');
2424

src/Event/UpdateEvent.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Flex\Event;
13+
14+
use Composer\Script\Event;
15+
16+
class UpdateEvent extends Event
17+
{
18+
private $force;
19+
20+
public function __construct(bool $force)
21+
{
22+
$this->force = $force;
23+
}
24+
25+
public function force(): bool
26+
{
27+
return $this->force;
28+
}
29+
}

0 commit comments

Comments
 (0)