1
0
Fork 0

Reuse operation formatting logic in downloaders

pull/8955/head
Jordi Boggiano 2020-06-17 12:26:32 +02:00
parent aaef3ff5ff
commit 83c64a9d19
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
10 changed files with 56 additions and 61 deletions

View File

@ -61,7 +61,12 @@ class InstallOperation extends SolverOperation
*/ */
public function show($lock) public function show($lock)
{ {
return ($lock ? 'Locking ' : 'Installing ').'<info>'.$this->package->getPrettyName().'</info> (<comment>'.$this->package->getFullPrettyVersion().'</comment>)'; return self::format($this->package, $lock);
}
public static function format(PackageInterface $package, $lock = false)
{
return ($lock ? 'Locking ' : 'Installing ').'<info>'.$package->getPrettyName().'</info> (<comment>'.$package->getFullPrettyVersion().'</comment>)';
} }
/** /**

View File

@ -61,7 +61,12 @@ class UninstallOperation extends SolverOperation
*/ */
public function show($lock) public function show($lock)
{ {
return 'Removing <info>'.$this->package->getPrettyName().'</info> (<comment>'.$this->package->getFullPrettyVersion().'</comment>)'; return self::format($this->package, $lock);
}
public static function format(PackageInterface $package, $lock = false)
{
return 'Removing <info>'.$package->getPrettyName().'</info> (<comment>'.$package->getFullPrettyVersion().'</comment>)';
} }
/** /**

View File

@ -75,20 +75,25 @@ class UpdateOperation extends SolverOperation
*/ */
public function show($lock) public function show($lock)
{ {
$fromVersion = $this->initialPackage->getFullPrettyVersion(); return self::format($this->initialPackage, $this->targetPackage, $lock);
$toVersion = $this->targetPackage->getFullPrettyVersion(); }
if ($fromVersion === $toVersion && $this->initialPackage->getSourceReference() !== $this->targetPackage->getSourceReference()) { public static function format(PackageInterface $initialPackage, PackageInterface $targetPackage, $lock = false)
$fromVersion = $this->initialPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_SOURCE_REF); {
$toVersion = $this->targetPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_SOURCE_REF); $fromVersion = $initialPackage->getFullPrettyVersion();
} elseif ($fromVersion === $toVersion && $this->initialPackage->getDistReference() !== $this->targetPackage->getDistReference()) { $toVersion = $targetPackage->getFullPrettyVersion();
$fromVersion = $this->initialPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_DIST_REF);
$toVersion = $this->targetPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_DIST_REF); if ($fromVersion === $toVersion && $initialPackage->getSourceReference() !== $targetPackage->getSourceReference()) {
$fromVersion = $initialPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_SOURCE_REF);
$toVersion = $targetPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_SOURCE_REF);
} elseif ($fromVersion === $toVersion && $initialPackage->getDistReference() !== $targetPackage->getDistReference()) {
$fromVersion = $initialPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_DIST_REF);
$toVersion = $targetPackage->getFullPrettyVersion(true, PackageInterface::DISPLAY_DIST_REF);
} }
$actionName = VersionParser::isUpgrade($this->initialPackage->getVersion(), $this->targetPackage->getVersion()) ? 'Upgrading' : 'Downgrading'; $actionName = VersionParser::isUpgrade($initialPackage->getVersion(), $targetPackage->getVersion()) ? 'Upgrading' : 'Downgrading';
return $actionName.' <info>'.$this->initialPackage->getPrettyName().'</info> (<comment>'.$fromVersion.'</comment> => <comment>'.$toVersion.'</comment>)'; return $actionName.' <info>'.$initialPackage->getPrettyName().'</info> (<comment>'.$fromVersion.'</comment> => <comment>'.$toVersion.'</comment>)';
} }
/** /**

View File

@ -17,6 +17,7 @@ use Symfony\Component\Finder\Finder;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Exception\IrrecoverableDownloadException; use Composer\Exception\IrrecoverableDownloadException;
use React\Promise\PromiseInterface; use React\Promise\PromiseInterface;
use Composer\DependencyResolver\Operation\InstallOperation;
/** /**
* Base downloader for archives * Base downloader for archives
@ -40,7 +41,7 @@ abstract class ArchiveDownloader extends FileDownloader
public function install(PackageInterface $package, $path, $output = true) public function install(PackageInterface $package, $path, $output = true)
{ {
if ($output) { if ($output) {
$this->io->writeError(" - Installing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>): Extracting archive"); $this->io->writeError(" - " . InstallOperation::format($package).": Extracting archive");
} else { } else {
$this->io->writeError('Extracting archive', false); $this->io->writeError('Extracting archive', false);
} }

View File

@ -18,6 +18,9 @@ use Composer\Factory;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\IO\NullIO; use Composer\IO\NullIO;
use Composer\Package\Comparer\Comparer; use Composer\Package\Comparer\Comparer;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
use Composer\Plugin\PluginEvents; use Composer\Plugin\PluginEvents;
@ -284,7 +287,7 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface
public function install(PackageInterface $package, $path, $output = true) public function install(PackageInterface $package, $path, $output = true)
{ {
if ($output) { if ($output) {
$this->io->writeError(" - Installing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)"); $this->io->writeError(" - " . InstallOperation::format($package));
} }
$this->filesystem->emptyDirectory($path); $this->filesystem->emptyDirectory($path);
@ -332,12 +335,7 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface
*/ */
public function update(PackageInterface $initial, PackageInterface $target, $path) public function update(PackageInterface $initial, PackageInterface $target, $path)
{ {
$name = $target->getName(); $this->io->writeError(" - " . UpdateOperation::format($initial, $target) . ": ", false);
$from = $initial->getFullPrettyVersion();
$to = $target->getFullPrettyVersion();
$actionName = VersionParser::isUpgrade($initial->getVersion(), $target->getVersion()) ? 'Upgrading' : 'Downgrading';
$this->io->writeError(" - " . $actionName . " <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>): ", false);
$promise = $this->remove($initial, $path, false); $promise = $this->remove($initial, $path, false);
if (!$promise instanceof PromiseInterface) { if (!$promise instanceof PromiseInterface) {
@ -360,7 +358,7 @@ class FileDownloader implements DownloaderInterface, ChangeReportInterface
public function remove(PackageInterface $package, $path, $output = true) public function remove(PackageInterface $package, $path, $output = true)
{ {
if ($output) { if ($output) {
$this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)"); $this->io->writeError(" - " . UninstallOperation::format($package));
} }
if (!$this->filesystem->removeDirectory($path)) { if (!$this->filesystem->removeDirectory($path)) {
throw new \RuntimeException('Could not completely delete '.$path.', aborting.'); throw new \RuntimeException('Could not completely delete '.$path.', aborting.');

View File

@ -27,6 +27,9 @@ use Composer\Util\Filesystem;
use Composer\EventDispatcher\EventDispatcher; use Composer\EventDispatcher\EventDispatcher;
use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem; use Symfony\Component\Filesystem\Filesystem as SymfonyFilesystem;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
/** /**
* Download a package from a local path. * Download a package from a local path.
@ -82,11 +85,7 @@ class PathDownloader extends FileDownloader implements VcsCapableDownloaderInter
if (realpath($path) === $realUrl) { if (realpath($path) === $realUrl) {
if ($output) { if ($output) {
$this->io->writeError(sprintf( $this->io->writeError(" - " . InstallOperation::format($package).': Source already present');
' - Installing <info>%s</info> (<comment>%s</comment>): Source already present',
$package->getName(),
$package->getFullPrettyVersion()
));
} else { } else {
$this->io->writeError('Source already present', false); $this->io->writeError('Source already present', false);
} }
@ -124,11 +123,7 @@ class PathDownloader extends FileDownloader implements VcsCapableDownloaderInter
$this->filesystem->removeDirectory($path); $this->filesystem->removeDirectory($path);
if ($output) { if ($output) {
$this->io->writeError(sprintf( $this->io->writeError(" - " . InstallOperation::format($package).': ', false);
' - Installing <info>%s</info> (<comment>%s</comment>): ',
$package->getName(),
$package->getFullPrettyVersion()
), false);
} }
$isFallback = false; $isFallback = false;
@ -187,7 +182,7 @@ class PathDownloader extends FileDownloader implements VcsCapableDownloaderInter
if ($path === $realUrl) { if ($path === $realUrl) {
if ($output) { if ($output) {
$this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>), source is still present in $path"); $this->io->writeError(" - " . UninstallOperation::format($package).", source is still present in $path");
} }
return; return;
@ -200,7 +195,7 @@ class PathDownloader extends FileDownloader implements VcsCapableDownloaderInter
*/ */
if (Platform::isWindows() && $this->filesystem->isJunction($path)) { if (Platform::isWindows() && $this->filesystem->isJunction($path)) {
if ($output) { if ($output) {
$this->io->writeError(" - Removing junction for <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)"); $this->io->writeError(" - " . UninstallOperation::format($package).", source is still present in $path");
} }
if (!$this->filesystem->removeJunction($path)) { if (!$this->filesystem->removeJunction($path)) {
$this->io->writeError(" <warning>Could not remove junction at " . $path . " - is another process locking it?</warning>"); $this->io->writeError(" <warning>Could not remove junction at " . $path . " - is another process locking it?</warning>");

View File

@ -21,6 +21,9 @@ use Composer\Util\ProcessExecutor;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use React\Promise\PromiseInterface; use React\Promise\PromiseInterface;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -120,7 +123,7 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa
throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information'); throw new \InvalidArgumentException('Package '.$package->getPrettyName().' is missing reference information');
} }
$this->io->writeError(" - Installing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>): ", false); $this->io->writeError(" - " . InstallOperation::format($package).': ', false);
$urls = $this->prepareUrls($package->getSourceUrls()); $urls = $this->prepareUrls($package->getSourceUrls());
while ($url = array_shift($urls)) { while ($url = array_shift($urls)) {
@ -153,23 +156,7 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa
throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information'); throw new \InvalidArgumentException('Package '.$target->getPrettyName().' is missing reference information');
} }
$name = $target->getName(); $this->io->writeError(" - " . UpdateOperation::format($initial, $target).': ', false);
if ($initial->getPrettyVersion() == $target->getPrettyVersion()) {
if ($target->getSourceType() === 'svn') {
$from = $initial->getSourceReference();
$to = $target->getSourceReference();
} else {
$from = substr($initial->getSourceReference(), 0, 7);
$to = substr($target->getSourceReference(), 0, 7);
}
$name .= ' '.$initial->getPrettyVersion();
} else {
$from = $initial->getFullPrettyVersion();
$to = $target->getFullPrettyVersion();
}
$actionName = VersionParser::isUpgrade($initial->getVersion(), $target->getVersion()) ? 'Upgrading' : 'Downgrading';
$this->io->writeError(" - " . $actionName . " <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>): ", false);
$urls = $this->prepareUrls($target->getSourceUrls()); $urls = $this->prepareUrls($target->getSourceUrls());
@ -227,7 +214,7 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa
*/ */
public function remove(PackageInterface $package, $path) public function remove(PackageInterface $package, $path)
{ {
$this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getPrettyVersion() . "</comment>)"); $this->io->writeError(" - " . UninstallOperation::format($package));
if (!$this->filesystem->removeDirectory($path)) { if (!$this->filesystem->removeDirectory($path)) {
throw new \RuntimeException('Could not completely delete '.$path.', aborting.'); throw new \RuntimeException('Could not completely delete '.$path.', aborting.');
} }

View File

@ -16,6 +16,9 @@ use Composer\Repository\InstalledRepositoryInterface;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
/** /**
* Metapackage installation manager. * Metapackage installation manager.
@ -76,7 +79,7 @@ class MetapackageInstaller implements InstallerInterface
*/ */
public function install(InstalledRepositoryInterface $repo, PackageInterface $package) public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
{ {
$this->io->writeError(" - Installing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)"); $this->io->writeError(" - " . InstallOperation::format($package));
$repo->addPackage(clone $package); $repo->addPackage(clone $package);
} }
@ -90,11 +93,7 @@ class MetapackageInstaller implements InstallerInterface
throw new \InvalidArgumentException('Package is not installed: '.$initial); throw new \InvalidArgumentException('Package is not installed: '.$initial);
} }
$name = $target->getName(); $this->io->writeError(" - " . UpdateOperation::format($initial, $target));
$from = $initial->getFullPrettyVersion();
$to = $target->getFullPrettyVersion();
$actionName = VersionParser::isUpgrade($initial->getVersion(), $target->getVersion()) ? 'Upgrading' : 'Downgrading';
$this->io->writeError(" - " . $actionName . " <info>" . $name . "</info> (<comment>" . $from . "</comment> => <comment>" . $to . "</comment>)");
$repo->removePackage($initial); $repo->removePackage($initial);
$repo->addPackage(clone $target); $repo->addPackage(clone $target);
@ -109,7 +108,7 @@ class MetapackageInstaller implements InstallerInterface
throw new \InvalidArgumentException('Package is not installed: '.$package); throw new \InvalidArgumentException('Package is not installed: '.$package);
} }
$this->io->writeError(" - Removing <info>" . $package->getName() . "</info> (<comment>" . $package->getFullPrettyVersion() . "</comment>)"); $this->io->writeError(" - " . UninstallOperation::format($package));
$repo->removePackage($package); $repo->removePackage($package);
} }

View File

@ -233,7 +233,7 @@ abstract class BasePackage implements PackageInterface
} }
// if source reference is a sha1 hash -- truncate // if source reference is a sha1 hash -- truncate
if ($truncate && \strlen($reference) === 40) { if ($truncate && \strlen($reference) === 40 && $this->getSourceType() !== 'svn') {
return $this->getPrettyVersion() . ' ' . substr($reference, 0, 7); return $this->getPrettyVersion() . ' ' . substr($reference, 0, 7);
} }

View File

@ -81,7 +81,7 @@ class BasePackageTest extends TestCase
$createPackage = function ($arr) use ($self) { $createPackage = function ($arr) use ($self) {
$package = $self->getMockForAbstractClass('\Composer\Package\BasePackage', array(), '', false); $package = $self->getMockForAbstractClass('\Composer\Package\BasePackage', array(), '', false);
$package->expects($self->once())->method('isDev')->will($self->returnValue(true)); $package->expects($self->once())->method('isDev')->will($self->returnValue(true));
$package->expects($self->once())->method('getSourceType')->will($self->returnValue('git')); $package->expects($self->any())->method('getSourceType')->will($self->returnValue('git'));
$package->expects($self->once())->method('getPrettyVersion')->will($self->returnValue('PrettyVersion')); $package->expects($self->once())->method('getPrettyVersion')->will($self->returnValue('PrettyVersion'));
$package->expects($self->any())->method('getSourceReference')->will($self->returnValue($arr['sourceReference'])); $package->expects($self->any())->method('getSourceReference')->will($self->returnValue($arr['sourceReference']));