Skip to content

Add --yes flag to recipes:install command to overwrite all files without asking #963

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
May 20, 2025
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
3 changes: 2 additions & 1 deletion src/Command/InstallRecipesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected function configure()
->addArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Recipes that should be installed.')
->addOption('force', null, InputOption::VALUE_NONE, 'Overwrite existing files when a new version of a recipe is available')
->addOption('reset', null, InputOption::VALUE_NONE, 'Reset all recipes back to their initial state (should be combined with --force)')
->addOption('yes', null, InputOption::VALUE_NONE, "Answer prompt questions with 'yes' for all questions.")
;
}

Expand Down Expand Up @@ -135,7 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$this->flex->update(new UpdateEvent($force, (bool) $input->getOption('reset')), $operations);
$this->flex->update(new UpdateEvent($force, (bool) $input->getOption('reset'), (bool) $input->getOption('yes')), $operations);

if ($force) {
$output = [
Expand Down
3 changes: 1 addition & 2 deletions src/Configurator/CopyFromPackageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ public function copyFile(string $source, string $target, array $options)
return;
}

$overwrite = $options['force'] ?? false;
if (!$this->options->shouldWriteFile($target, $overwrite)) {
if (!$this->options->shouldWriteFile($target, $options['force'] ?? false, $options['assumeYesForPrompts'] ?? false)) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Configurator/CopyFromRecipeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ private function copyDir(string $source, string $target, array $files, array $op

private function copyFile(string $to, string $contents, bool $executable, array $options): string
{
$overwrite = $options['force'] ?? false;
$basePath = $options['root-dir'] ?? '.';
$copiedFile = $this->getLocalFilePath($basePath, $to);

if (!$this->options->shouldWriteFile($to, $overwrite)) {
if (!$this->options->shouldWriteFile($to, $options['force'] ?? false, $options['assumeYesForPrompts'] ?? false)) {
return $copiedFile;
}

Expand Down
9 changes: 8 additions & 1 deletion src/Event/UpdateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ class UpdateEvent extends Event
{
private $force;
private $reset;
private $assumeYesForPrompts;

public function __construct(bool $force, bool $reset)
public function __construct(bool $force, bool $reset, bool $assumeYesForPrompts)
{
$this->name = ScriptEvents::POST_UPDATE_CMD;
$this->force = $force;
$this->reset = $reset;
$this->assumeYesForPrompts = $assumeYesForPrompts;
}

public function force(): bool
Expand All @@ -35,4 +37,9 @@ public function reset(): bool
{
return $this->reset;
}

public function assumeYesForPrompts(): bool
{
return $this->assumeYesForPrompts;
}
}
2 changes: 2 additions & 0 deletions src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ function ($value) {
$this->io->writeError(\sprintf(' - Configuring %s', $this->formatOrigin($recipe)));
$this->configurator->install($recipe, $this->lock, [
'force' => $event instanceof UpdateEvent && $event->force(),
'assumeYesForPrompts' => $event instanceof UpdateEvent && $event->assumeYesForPrompts(),
]);
$manifest = $recipe->getManifest();
if (isset($manifest['post-install-output'])) {
Expand All @@ -471,6 +472,7 @@ function ($value) {
foreach ($postInstallRecipes as $recipe) {
$this->configurator->postInstall($recipe, $this->lock, [
'force' => $event instanceof UpdateEvent && $event->force(),
'assumeYesForPrompts' => $event instanceof UpdateEvent && $event->assumeYesForPrompts(),
]);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function expandTargetDir(string $target): string
return file_exists($rootDir.'/'.$otherPhpunitDistFile) ? $otherPhpunitDistFile : $result;
}

public function shouldWriteFile(string $file, bool $overwrite): bool
public function shouldWriteFile(string $file, bool $overwrite, bool $skipQuestion): bool
{
if (isset($this->writtenFiles[$file])) {
return false;
Expand All @@ -81,6 +81,10 @@ public function shouldWriteFile(string $file, bool $overwrite): bool
return true;
}

if ($skipQuestion) {
return true;
}

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

if (0 !== $status) {
Expand Down
42 changes: 42 additions & 0 deletions tests/Command/InstallRecipesCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Tests\Command;

use Composer\Console\Application;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Flex\Command\InstallRecipesCommand;
use Symfony\Flex\Event\UpdateEvent;
use Symfony\Flex\Flex;

class InstallRecipesCommandTest extends TestCase
{
public function testCommandFlagsPassedDown()
{
$flex = $this->createMock(Flex::class);
$flex->method('update')->willReturnCallback(function (UpdateEvent $event) {
$this->assertTrue($event->reset());
$this->assertTrue($event->assumeYesForPrompts());
});

$command = new InstallRecipesCommand($flex, __DIR__);
$application = new Application();
$application->add($command);
$command = $application->find('symfony:recipes:install');

$tester = new CommandTester($command);
$tester->execute([
'--reset' => true,
'--yes' => true,
]);
}
}
49 changes: 49 additions & 0 deletions tests/OptionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\Tests;

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

class OptionsTest 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');

$this->assertTrue((new Options([], null))->shouldWriteFile('non-existing-file.txt', false, false));
$this->assertFalse((new Options([], null))->shouldWriteFile($filePath, false, false));

// We don't have an IO, so we don't write the file
$this->assertFalse((new Options([], null))->shouldWriteFile($filePath, true, false));

// We have an IO, and it allowed to write the file
$io = $this->createMock(IOInterface::class);
$io->expects($this->once())->method('askConfirmation')->willReturn(true);
$this->assertTrue((new Options([], $io))->shouldWriteFile($filePath, true, false));

// We skip all questions, so we're able to write
$this->assertTrue((new Options([], null))->shouldWriteFile($filePath, true, true));
}
}