Skip to content

Add a dedicated file deletion for unconfigure recipes #706

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
28 changes: 1 addition & 27 deletions src/Configurator/CopyFromRecipeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function configure(Recipe $recipe, $config, Lock $lock, array $options =
public function unconfigure(Recipe $recipe, $config, Lock $lock)
{
$this->write('Removing files from recipe');
$this->removeFiles($config, $this->getRemovableFilesFromRecipeAndLock($recipe, $lock), $this->options->get('root-dir'));
$this->removeFiles($config, $this->options->getRemovableFilesFromRecipeAndLock($recipe), $this->options->get('root-dir'));
}

public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
Expand Down Expand Up @@ -66,32 +66,6 @@ private function resolveTargetFolder(string $path, array $config): string
return $path;
}

private function getRemovableFilesFromRecipeAndLock(Recipe $recipe, Lock $lock): array
{
$lockedFiles = array_unique(
array_reduce(
array_column($lock->all(), 'files'),
function (array $carry, array $package) {
return array_merge($carry, $package);
},
[]
)
);

$removableFiles = $recipe->getFiles();

$lockedFiles = array_map('realpath', $lockedFiles);

// Compare file paths by their real path to abstract OS differences
foreach (array_keys($removableFiles) as $file) {
if (\in_array(realpath($file), $lockedFiles)) {
unset($removableFiles[$file]);
}
}

return $removableFiles;
}

private function copyFiles(array $manifest, array $files, array $options): array
{
$copiedFiles = [];
Expand Down
106 changes: 106 additions & 0 deletions src/FilesManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

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

namespace Symfony\Flex;

use Composer\IO\IOInterface;
use Composer\Util\ProcessExecutor;

/**
* @author Maxime Hélias <[email protected]>
*/
class FilesManager
{
private $io;
protected $path;

private $writtenFiles = [];
private $files;

public function __construct(IOInterface $io, Lock $lock, string $rootDir)
{
$this->io = $io;

$this->path = new Path($rootDir);
$this->files = array_count_values(
array_map(
function (string $file) {
return realpath($file) ?: '';
}, array_reduce(
array_column($lock->all(), 'files'),
function (array $carry, array $package) {
return array_merge($carry, $package);
},
[]
)
)
);
}

public function shouldWriteFile(string $file, bool $overwrite, bool $skipQuestion): bool
{
if (isset($this->writtenFiles[$file])) {
return false;
}
$this->writtenFiles[$file] = true;

if (!file_exists($file)) {
return true;
}

if (!$overwrite) {
return false;
}

if (!filesize($file)) {
return true;
}

if ($skipQuestion) {
return true;
}

exec('git status --short --ignored --untracked-files=all -- '.ProcessExecutor::escape($file).' 2>&1', $output, $status);

if (0 !== $status) {
return $this->io->askConfirmation(\sprintf('Cannot determine the state of the "%s" file, overwrite anyway? [y/N] ', $file), false);
}

if (empty($output[0]) || preg_match('/^[ AMDRCU][ D][ \t]/', $output[0])) {
return true;
}

$name = basename($file);
$name = \strlen($output[0]) - \strlen($name) === strrpos($output[0], $name) ? substr($output[0], 3) : $name;

return $this->io->askConfirmation(\sprintf('File "%s" has uncommitted changes, overwrite? [y/N] ', $name), false);
}

public function getRemovableFilesFromRecipeAndLock(Recipe $recipe): array
{
$removableFiles = $recipe->getFiles();
// Compare file paths by their real path to abstract OS differences
foreach (array_keys($removableFiles) as $file) {
$file = realpath($file);
if (!isset($this->files[$file])) {
continue;
}

--$this->files[$file];

if ($this->files[$file] <= 0) {
unset($removableFiles[$file]);
}
}

return $removableFiles;
}
}
17 changes: 9 additions & 8 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@ class_exists(__NAMESPACE__.str_replace('/', '\\', substr($file, \strlen(__DIR__)
$this->composer = $composer;
$this->io = $io;
$this->config = $composer->getConfig();
$this->options = $this->initOptions();

$composerFile = Factory::getComposerFile();
$composerLock = 'json' === pathinfo($composerFile, \PATHINFO_EXTENSION) ? substr($composerFile, 0, -4).'lock' : $composerFile.'.lock';
$symfonyLock = str_replace('composer', 'symfony', basename($composerLock));
$this->lock = new Lock(getenv('SYMFONY_LOCKFILE') ?: \dirname($composerLock).'/'.(basename($composerLock) !== $symfonyLock ? $symfonyLock : 'symfony.lock'));

$this->options = $this->initOptions($this->io, $this->lock);

// if Flex is being upgraded, the original operations from the original Flex
// instance are stored in the static property, so we can reuse them now.
Expand All @@ -130,12 +136,7 @@ class_exists(__NAMESPACE__.str_replace('/', '\\', substr($file, \strlen(__DIR__)
$this->filter = new PackageFilter($io, $symfonyRequire, $this->downloader);
}

$composerFile = Factory::getComposerFile();
$composerLock = 'json' === pathinfo($composerFile, \PATHINFO_EXTENSION) ? substr($composerFile, 0, -4).'lock' : $composerFile.'.lock';
$symfonyLock = str_replace('composer', 'symfony', basename($composerLock));

$this->configurator = new Configurator($composer, $io, $this->options);
$this->lock = new Lock(getenv('SYMFONY_LOCKFILE') ?: \dirname($composerLock).'/'.(basename($composerLock) !== $symfonyLock ? $symfonyLock : 'symfony.lock'));

$disable = true;
foreach (array_merge($composer->getPackage()->getRequires() ?? [], $composer->getPackage()->getDevRequires() ?? []) as $link) {
Expand Down Expand Up @@ -701,7 +702,7 @@ public function getLock(): Lock
return $this->lock;
}

private function initOptions(): Options
private function initOptions(IOInterface $io, Lock $lock): Options
{
$extra = $this->composer->getPackage()->getExtra();

Expand All @@ -716,7 +717,7 @@ private function initOptions(): Options
'runtime' => $extra['runtime'] ?? [],
], $extra);

return new Options($options, $this->io);
return new Options($options, new FilesManager($io, $lock, $options['root-dir']));
}

private function formatOrigin(Recipe $recipe): string
Expand Down
47 changes: 11 additions & 36 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@

namespace Symfony\Flex;

use Composer\IO\IOInterface;
use Composer\Util\ProcessExecutor;

/**
* @author Fabien Potencier <[email protected]>
*/
class Options
{
private $options;
private $writtenFiles = [];
private $io;
private $filesManager;

public function __construct(array $options = [], ?IOInterface $io = null)
public function __construct(array $options = [], ?FilesManager $filesManager = null)
{
$this->options = $options;
$this->io = $io;
$this->filesManager = $filesManager;
}

public function get(string $name)
Expand Down Expand Up @@ -64,41 +60,20 @@ public function expandTargetDir(string $target): string

public function shouldWriteFile(string $file, bool $overwrite, bool $skipQuestion): bool
{
if (isset($this->writtenFiles[$file])) {
return false;
}
$this->writtenFiles[$file] = true;

if (!file_exists($file)) {
return true;
}

if (!$overwrite) {
if (null === $this->filesManager) {
return false;
}

if (!filesize($file)) {
return true;
}

if ($skipQuestion) {
return true;
}

exec('git status --short --ignored --untracked-files=all -- '.ProcessExecutor::escape($file).' 2>&1', $output, $status);

if (0 !== $status) {
return $this->io && $this->io->askConfirmation(\sprintf('Cannot determine the state of the "%s" file, overwrite anyway? [y/N] ', $file), false);
}
return $this->filesManager->shouldWriteFile($file, $overwrite, $skipQuestion);
}

if (empty($output[0]) || preg_match('/^[ AMDRCU][ D][ \t]/', $output[0])) {
return true;
public function getRemovableFilesFromRecipeAndLock(Recipe $recipe): array
{
if (null === $this->filesManager) {
return [];
}

$name = basename($file);
$name = \strlen($output[0]) - \strlen($name) === strrpos($output[0], $name) ? substr($output[0], 3) : $name;

return $this->io && $this->io->askConfirmation(\sprintf('File "%s" has uncommitted changes, overwrite? [y/N] ', $name), false);
return $this->filesManager->getRemovableFilesFromRecipeAndLock($recipe);
}

public function toArray(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected function tearDown(): void

private function createConfigurator(): CopyFromPackageConfigurator
{
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR], $this->io));
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR]));
}

private function cleanUpTargetFiles()
Expand Down
2 changes: 1 addition & 1 deletion tests/Configurator/CopyFromPackageConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected function tearDown(): void

private function createConfigurator(): CopyFromPackageConfigurator
{
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR], $this->io));
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR]));
}

private function cleanUpTargetFiles()
Expand Down
2 changes: 1 addition & 1 deletion tests/Configurator/CopyFromRecipeConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ protected function tearDown(): void

private function createConfigurator(): CopyFromRecipeConfigurator
{
return new CopyFromRecipeConfigurator($this->getMockBuilder(Composer::class)->getMock(), $this->io, new Options(['root-dir' => FLEX_TEST_DIR, 'config-dir' => 'config'], $this->io));
return new CopyFromRecipeConfigurator($this->getMockBuilder(Composer::class)->getMock(), $this->io, new Options(['root-dir' => FLEX_TEST_DIR, 'config-dir' => 'config']));
}

private function cleanUpTargetFiles()
Expand Down
65 changes: 65 additions & 0 deletions tests/FilesManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

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

namespace Symfony\Flex\Tests;

use Composer\IO\IOInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
use Symfony\Flex\FilesManager;
use Symfony\Flex\Lock;

class FilesManagerTest extends TestCase
{
public function testShouldWrite()
{
@mkdir(FLEX_TEST_DIR);
(new Process(['git', 'init'], FLEX_TEST_DIR))->mustRun();
(new Process(['git', 'config', 'user.name', 'Unit test'], FLEX_TEST_DIR))->mustRun();
(new Process(['git', 'config', 'user.email', ''], FLEX_TEST_DIR))->mustRun();

$filePath = FLEX_TEST_DIR.'/a.txt';
file_put_contents($filePath, 'a');
(new Process(['git', 'add', '-A'], FLEX_TEST_DIR))->mustRun();
(new Process(['git', 'commit', '-m', 'setup of original files'], FLEX_TEST_DIR))->mustRun();

file_put_contents($filePath, 'b');

$io = $this->getMockBuilder(IOInterface::class)->getMock();
$io->method('askConfirmation')->willReturn(true);

$lock = $this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock();
$lock->method('all')->willReturn([
'symfony/my-package' => [
'files' => [
$filePath,
],
],
]);

$filesManager = new FilesManager($io, $lock, FLEX_TEST_DIR);

// We need to set the writtenFiles property to reset the state
$reflection = new \ReflectionProperty(FilesManager::class, 'writtenFiles');
$reflection->setAccessible(true);

$this->assertTrue($filesManager->shouldWriteFile('non-existing-file.txt', false, false));
$this->assertFalse($filesManager->shouldWriteFile($filePath, false, false));

// It allowed to write the file
$reflection->setValue($filesManager, []);
$this->assertTrue($filesManager->shouldWriteFile($filePath, true, false));

// We skip all questions, so we're able to write
$reflection->setValue($filesManager, []);
$this->assertTrue($filesManager->shouldWriteFile($filePath, true, true));
}
}
Loading
Loading