From 2e1373b339def68273267d7cbeb3292626005f3a Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 4 Oct 2014 17:23:04 +0100 Subject: [PATCH] Update require message and delete empty file at the end in case of failure, fixes #3260 --- doc/03-cli.md | 2 +- src/Composer/Command/RequireCommand.php | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/doc/03-cli.md b/doc/03-cli.md index 7145132c8..2dd0a6c36 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -139,7 +139,7 @@ php composer.phar update vendor/* ## require The `require` command adds new packages to the `composer.json` file from -the current directory. +the current directory. If no file exists one will be created on the fly. ```sh php composer.phar require diff --git a/src/Composer/Command/RequireCommand.php b/src/Composer/Command/RequireCommand.php index 9b4b50df1..682a44e66 100644 --- a/src/Composer/Command/RequireCommand.php +++ b/src/Composer/Command/RequireCommand.php @@ -59,6 +59,7 @@ EOT { $file = Factory::getComposerFile(); + $newlyCreated = !file_exists($file); if (!file_exists($file) && !file_put_contents($file, "{\n}\n")) { $output->writeln(''.$file.' could not be created.'); @@ -105,7 +106,7 @@ EOT $json->write($composer); } - $output->writeln(''.$file.' has been updated'); + $output->writeln(''.$file.' has been '.($newlyCreated ? 'created' : 'updated').''); if ($input->getOption('no-update')) { return 0; @@ -134,8 +135,13 @@ EOT $status = $install->run(); if ($status !== 0) { - $output->writeln("\n".'Installation failed, reverting '.$file.' to its original content.'); - file_put_contents($json->getPath(), $composerBackup); + if ($newlyCreated) { + $output->writeln("\n".'Installation failed, deleting '.$file.'.'); + unlink($json->getPath()); + } else { + $output->writeln("\n".'Installation failed, reverting '.$file.' to its original content.'); + file_put_contents($json->getPath(), $composerBackup); + } } return $status;