Skip to content

Implement "sync-recipes --force" option #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/build/
.php_cs.cache
composer.lock
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"require-dev": {
"composer/composer": "^1.0.2",
"symfony/phpunit-bridge": "^3.4.19|^4.1.8"
"symfony/phpunit-bridge": "^3.4.19|^4.1.8",
"symfony/process": "^2.7|^3.0|^4.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Symfony Flex Test Suite">
Expand Down
16 changes: 8 additions & 8 deletions src/Command/SyncRecipesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
use Composer\Command\BaseCommand;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\Factory;
use Composer\Script\Event;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Flex\Event\UpdateEvent;
use Symfony\Flex\Lock;

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

protected function execute(InputInterface $input, OutputInterface $output)
{
$force = $input->getOption('force');

$symfonyLock = new Lock(getenv('SYMFONY_LOCKFILE') ?: str_replace('composer.json', 'symfony.lock', Factory::getComposerFile()));
$composer = $this->getComposer();
$locker = $composer->getLocker();
$lockData = $locker->getLockData();

$packages = [];
foreach ($lockData['packages'] as $pkg) {
if (!$symfonyLock->has($pkg['name'])) {
if ($force || !$symfonyLock->has($pkg['name'])) {
$packages[] = $pkg['name'];
}
}
foreach ($lockData['packages-dev'] as $pkg) {
if (!$symfonyLock->has($pkg['name'])) {
if ($force || !$symfonyLock->has($pkg['name'])) {
$packages[] = $pkg['name'];
}
}
Expand All @@ -76,10 +80,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$operations[] = new InstallOperation($pkg);
}

$this->flex->update(new class() extends Event {
public function __construct()
{
}
}, $operations);
$this->flex->update(new UpdateEvent($force), $operations);
}
}
4 changes: 2 additions & 2 deletions src/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public function __construct(Composer $composer, IOInterface $io, Options $option
];
}

public function install(Recipe $recipe)
public function install(Recipe $recipe, array $options = [])
{
$manifest = $recipe->getManifest();
foreach (array_keys($this->configurators) as $key) {
if (isset($manifest[$key])) {
$this->get($key)->configure($recipe, $manifest[$key]);
$this->get($key)->configure($recipe, $manifest[$key], $options);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/AbstractConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(Composer $composer, IOInterface $io, Options $option
$this->path = new Path(getcwd());
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/BundlesConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class BundlesConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $bundles)
public function configure(Recipe $recipe, $bundles, array $options = [])
{
$this->write('Enabling the package as a Symfony bundle');
$file = $this->getConfFile();
Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/ComposerScriptsConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class ComposerScriptsConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $scripts)
public function configure(Recipe $recipe, $scripts, array $options = [])
{
$json = new JsonFile(Factory::getComposerFile());

Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/ContainerConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class ContainerConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $parameters)
public function configure(Recipe $recipe, $parameters, array $options = [])
{
$this->write('Setting parameters');
$this->addParameters($parameters);
Expand Down
20 changes: 9 additions & 11 deletions src/Configurator/CopyFromPackageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
*/
class CopyFromPackageConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $config)
public function configure(Recipe $recipe, $config, array $options = [])
{
$this->write('Setting configuration and copying files');
$packageDir = $this->composer->getInstallationManager()->getInstallPath($recipe->getPackage());
$this->copyFiles($config, $packageDir, getcwd());
$this->copyFiles($config, $packageDir, getcwd(), $options['force'] ?? false);
}

public function unconfigure(Recipe $recipe, $config)
Expand All @@ -33,22 +33,20 @@ public function unconfigure(Recipe $recipe, $config)
$this->removeFiles($config, $packageDir, getcwd());
}

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

if (!file_exists($targetPath)) {
$this->copyFile($this->path->concatenate([$from, $source]), $targetPath);
}
$this->copyFile($this->path->concatenate([$from, $source]), $targetPath, $overwrite);
}
}
}
Expand All @@ -69,7 +67,7 @@ private function removeFiles(array $manifest, string $from, string $to)
}
}

private function copyDir(string $source, string $target)
private function copyDir(string $source, string $target, bool $overwrite)
{
if (!is_dir($target)) {
mkdir($target, 0777, true);
Expand All @@ -84,14 +82,14 @@ private function copyDir(string $source, string $target)
$this->write(sprintf('Created <fg=green>"%s"</>', $this->path->relativize($targetPath)));
}
} elseif (!file_exists($targetPath)) {
$this->copyFile($item, $targetPath);
$this->copyFile($item, $targetPath, $overwrite);
}
}
}

public function copyFile(string $source, string $target)
public function copyFile(string $source, string $target, bool $overwrite = false)
{
if (file_exists($target)) {
if (!$this->options->shouldWriteFile($target, $overwrite)) {
return;
}

Expand Down
18 changes: 9 additions & 9 deletions src/Configurator/CopyFromRecipeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/
class CopyFromRecipeConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $config)
public function configure(Recipe $recipe, $config, array $options = [])
{
$this->write('Setting configuration and copying files');
$this->copyFiles($config, $recipe->getFiles(), getcwd());
$this->copyFiles($config, $recipe->getFiles(), getcwd(), $options['force'] ?? false);
}

public function unconfigure(Recipe $recipe, $config)
Expand All @@ -30,31 +30,31 @@ public function unconfigure(Recipe $recipe, $config)
$this->removeFiles($config, $recipe->getFiles(), getcwd());
}

private function copyFiles(array $manifest, array $files, string $to)
private function copyFiles(array $manifest, array $files, string $to, bool $overwrite = false)
{
foreach ($manifest as $source => $target) {
$target = $this->options->expandTargetDir($target);
if ('/' === substr($source, -1)) {
$this->copyDir($source, $this->path->concatenate([$to, $target]), $files);
$this->copyDir($source, $this->path->concatenate([$to, $target]), $files, $overwrite);
} else {
$this->copyFile($this->path->concatenate([$to, $target]), $files[$source]['contents'], $files[$source]['executable']);
$this->copyFile($this->path->concatenate([$to, $target]), $files[$source]['contents'], $files[$source]['executable'], $overwrite);
}
}
}

private function copyDir(string $source, string $target, array $files)
private function copyDir(string $source, string $target, array $files, bool $overwrite = false)
{
foreach ($files as $file => $data) {
if (0 === strpos($file, $source)) {
$file = $this->path->concatenate([$target, substr($file, \strlen($source))]);
$this->copyFile($file, $data['contents'], $data['executable']);
$this->copyFile($file, $data['contents'], $data['executable'], $overwrite);
}
}
}

private function copyFile(string $to, string $contents, bool $executable)
private function copyFile(string $to, string $contents, bool $executable, bool $overwrite = false)
{
if (file_exists($to)) {
if (!$this->options->shouldWriteFile($to, $overwrite)) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/EnvConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class EnvConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $vars)
public function configure(Recipe $recipe, $vars, array $options = [])
{
$this->write('Added environment variable defaults');

Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/GitignoreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class GitignoreConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $vars)
public function configure(Recipe $recipe, $vars, array $options = [])
{
$this->write('Added entries to .gitignore');

Expand Down
2 changes: 1 addition & 1 deletion src/Configurator/MakefileConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class MakefileConfigurator extends AbstractConfigurator
{
public function configure(Recipe $recipe, $definitions)
public function configure(Recipe $recipe, $definitions, array $options = [])
{
$this->write('Added Makefile entries');

Expand Down
29 changes: 29 additions & 0 deletions src/Event/UpdateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Flex\Event;

use Composer\Script\Event;

class UpdateEvent extends Event
{
private $force;

public function __construct(bool $force)
{
$this->force = $force;
}

public function force(): bool
{
return $this->force;
}
}
Loading