Skip to content

Remove files when updating a recipe #615

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

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 26 additions & 1 deletion src/Configurator/CopyFromRecipeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,32 @@ public function configure(Recipe $recipe, $config, Lock $lock, array $options =
$this->write('Copying files from recipe');
$options = array_merge($this->options->toArray(), $options);

$lock->add($recipe->getName(), ['files' => $this->copyFiles($config, $recipe->getFiles(), $options)]);
$files = $this->copyFiles($config, $recipe->getFiles(), $options);
$lock->add($recipe->getName(), ['files' => $files]);

// Remove extra file only on force
if (!($options['force'] ?? false)) {
return;
}

// Recipe not installed yet
if (null === $recipeLock = $options['beforeState']) {
return;
}

$lockFiles = $recipeLock['files'] ?? null;
if (null === $lockFiles) {
return;
}

$lockFiles = array_flip($lockFiles);
foreach ($files as $file) {
if (isset($lockFiles[$file])) {
unset($lockFiles[$file]);
}
}

$this->removeFiles($config, $lockFiles, $this->options->get('root-dir'));
}

public function unconfigure(Recipe $recipe, $config, Lock $lock)
Expand Down
3 changes: 3 additions & 0 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Flex implements PluginInterface, EventSubscriberInterface
private $postInstallOutput = [''];
private $operations = [];
private $lock;
private $frozenLock;
private $cacheDirPopulated = false;
private $displayThanksReminder = 0;
private $rfs;
Expand Down Expand Up @@ -133,6 +134,7 @@ class_exists(__NAMESPACE__.str_replace('/', '\\', substr($file, \strlen(__DIR__)
$composer->setRepositoryManager($manager);
$this->configurator = new Configurator($composer, $io, $this->options);
$this->lock = new Lock(getenv('SYMFONY_LOCKFILE') ?: str_replace('composer.json', 'symfony.lock', Factory::getComposerFile()));
$this->frozenLock = $this->lock->all();

$disable = true;
foreach (array_merge($composer->getPackage()->getRequires() ?? [], $composer->getPackage()->getDevRequires() ?? []) as $link) {
Expand Down Expand Up @@ -475,6 +477,7 @@ function ($value) {
$this->io->writeError(sprintf(' - Configuring %s', $this->formatOrigin($recipe->getOrigin())));
$this->configurator->install($recipe, $this->lock, [
'force' => $event instanceof UpdateEvent && $event->force(),
'beforeState' => $this->frozenLock[$recipe->getName()] ?? null,
]);
$manifest = $recipe->getManifest();
if (isset($manifest['post-install-output'])) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Configurator/CopyFromRecipeConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testConfigureAndOverwriteFiles()
$this->recipe,
[$this->sourceFileRelativePath => $this->targetFileRelativePath],
$lock,
['force' => true]
['force' => true, 'beforeState' => null]
);
$this->assertFileExists($this->targetFile);
$this->assertSame('somecontent', file_get_contents($this->targetFile));
Expand Down