Skip to content
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

[WIP] Issue 1135 create project in existing directory #1206

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
13 changes: 9 additions & 4 deletions src/Composer/Downloader/VcsDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public function download(PackageInterface $package, $path)
}

$this->io->write(" - Installing <info>" . $package->getName() . "</info> (<comment>" . VersionParser::formatVersion($package) . "</comment>)");
$this->filesystem->removeDirectory($path);

if (!$this->filesystem->directoryIsEmpty($path, array('composer.json'))) {
$this->filesystem->removeDirectory($path);
}

$this->doDownload($package, $path);
$this->io->write('');
}
Expand Down Expand Up @@ -135,9 +139,10 @@ public function remove(PackageInterface $package, $path)
/**
* Prompt the user to check if changes should be stashed/removed or the operation aborted
*
* @param string $path
* @param bool $stash if true (update) the changes can be stashed and reapplied after an update,
* if false (remove) the changes should be assumed to be lost if the operation is not aborted
* @param string $path
* @param Boolean $update if true (update) the changes can be stashed and reapplied after an update,
* if false (remove) the changes should be assumed to be lost if the operation is not aborted
*
* @throws \RuntimeException in case the operation must be aborted
*/
protected function cleanChanges($path, $update)
Expand Down
16 changes: 10 additions & 6 deletions src/Composer/Installer/ProjectInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Composer\Package\PackageInterface;
use Composer\Downloader\DownloadManager;
use Composer\Repository\InstalledRepositoryInterface;
use Composer\Util\Filesystem;

/**
* Project Installer is used to install a single package into a directory as
Expand Down Expand Up @@ -58,13 +59,16 @@ public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface
public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{
$installPath = $this->installPath;
if (file_exists($installPath)) {
throw new \InvalidArgumentException("Project directory $installPath already exists.");
}
if (!file_exists(dirname($installPath))) {
throw new \InvalidArgumentException("Project root " . dirname($installPath) . " does not exist.");
$fs = new FileSystem();

if ($fs->directoryExists($installPath)) {
if (!$fs->directoryIsEmpty($installPath, array('composer.json'))) {
throw new \InvalidArgumentException("Project directory \"$installPath\" must be empty.");
}
} else {
mkdir($installPath, 0777, true);
}
mkdir($installPath, 0777);

$this->downloadManager->download($package, $installPath);
}

Expand Down
27 changes: 27 additions & 0 deletions src/Composer/Util/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ public function ensureDirectoryExists($directory)
}
}

public function directoryExists($dir)
{
return is_dir($dir);
}

/**
* @param string $dir
* @param array $excluding
* @return bool
* @throws \RuntimeException
*/
public function directoryIsEmpty($dir, $excluding = array())
{
if (!$this->directoryExists($dir)) {
throw new \RuntimeException("Directory \"$dir\" does not exist");
}

$excludeList = array('.', '..');
if (!empty($excluding)) {
$excludeList = array_merge($excludeList, $excluding);
}

$dirContent = array_diff(scandir($dir), $excludeList);

return empty($dirContent);
}

public function rename($source, $target)
{
if (true === @rename($source, $target)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "vendor/package-name-sample"
}
33 changes: 33 additions & 0 deletions tests/Composer/Test/Util/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,37 @@ public function providePathCouples()
array('C:/Temp', 'c:\Temp\test', "test"),
);
}

public function testDirectoryExists()
{
$fs = new Filesystem();
$directory1 = '/does/not/exist';
$directory2 = __DIR__;

$this->assertFalse($fs->directoryExists($directory1));
$this->assertTrue($fs->directoryExists($directory2));
}

/**
* @expectedException \RuntimeException
*/
public function testDirectoryIsEmptyRuntimeException()
{
$fs = new Filesystem();
$dir = '/does/not/exist';

$fs->directoryIsEmpty($dir);
}

public function testDirectoryIsEmpty()
{
$fs = new Filesystem();
$dir = __DIR__;
$project = __DIR__.'/../Fixtures/projectTestDirectory';

$this->assertFalse($fs->directoryIsEmpty($dir));
$this->assertTrue($fs->directoryIsEmpty($project, array('composer.json')));
$this->assertFalse($fs->directoryIsEmpty($project));

}
}