From 950bbcbed90c7872e837b1702ee10d46a7fd2f49 Mon Sep 17 00:00:00 2001 From: Justin Rainbow Date: Sun, 8 Jan 2012 15:27:19 -0700 Subject: [PATCH] Init now asks user if they want to ignore the vendor dir --- src/Composer/Command/InitCommand.php | 67 ++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index 2acaa2d47..e5cbaa34e 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -103,6 +103,22 @@ EOT } $file->write($options); + + if ($input->isInteractive()) { + $ignoreFile = realpath('.gitignore'); + + if (false === $ignoreFile) { + $ignoreFile = realpath('.') . '/.gitignore'; + } + + if (!$this->hasVendorIgnore($ignoreFile)) { + $question = 'Would you like the vendor directory added to your .gitignore [yes]?'; + + if ($dialog->askConfirmation($output, $question, true)) { + $this->addVendorIgnore($ignoreFile); + } + } + } } protected function interact(InputInterface $input, OutputInterface $output) @@ -291,4 +307,55 @@ EOT return $this->gitConfig = array(); } + + /** + * Checks the local .gitignore file for the Composer vendor directory. + * + * Tested patterns include: + * "/$vendor" + * "$vendor" + * "$vendor/" + * "/$vendor/" + * "/$vendor/*" + * "$vendor/*" + * + * @param string $ignoreFile + * @param string $vendor + * + * @return Boolean + */ + protected function hasVendorIgnore($ignoreFile, $vendor = 'vendor') + { + if (!file_exists($ignoreFile)) { + return false; + } + + $pattern = sprintf( + '~^/?%s(/|/\*)?$~', + preg_quote($vendor, '~') + ); + + $lines = file($ignoreFile, FILE_IGNORE_NEW_LINES); + foreach ($lines as $line) { + if (preg_match($pattern, $line)) { + return true; + } + } + + return false; + } + + protected function addVendorIgnore($ignoreFile, $vendor = 'vendor') + { + $contents = ""; + if (file_exists($ignoreFile)) { + $contents = file_get_contents($ignoreFile); + + if ("\n" !== substr($contents, 0, -1)) { + $contents .= "\n"; + } + } + + file_put_contents($ignoreFile, $contents . $vendor); + } } \ No newline at end of file