Skip to content

Commit

Permalink
Merge branch '63975-extend-version' into 'master'
Browse files Browse the repository at this point in the history
Added version argument to extend command (fixes #63975)

See merge request iserv/cotor!6
  • Loading branch information
Dominik Scholz-Schulze committed Jun 15, 2023
2 parents 072eb47 + 30d89cc commit 46d437d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file, in reverse

## [Unreleased]

### Added

- Added `version` argument to `extend` command. #63975

## 1.5 - 2022-05-13

### Added
Expand Down
32 changes: 30 additions & 2 deletions src/Command/ExtendCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use IServ\ComposerToolsInstaller\Domain\Package;
use IServ\ComposerToolsInstaller\Tools\ToolPath;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -27,6 +28,7 @@ protected function configure(): void
->setHelp('This command allows you to add an extension to a composer based tool.')
->addArgument('name', InputArgument::REQUIRED, 'The short name of the tool or its composer name')
->addArgument('extension', InputArgument::REQUIRED, 'The composer package name of the extension to install')
->addArgument('version', InputArgument::OPTIONAL, 'The version of the extension to install (default: latest)')
;
}

Expand All @@ -35,9 +37,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);
$name = (string)$input->getArgument('name');
$extension = (string)$input->getArgument('extension');
try {
/** @var mixed $version Makes psalm happy */
$version = $input->getArgument('version');
if (null !== $version) {
$version = (string)$version;
}
} catch (InvalidArgumentException) {
$version = null;
}

if (str_contains($extension, ':')) {
[$extension, $version] = explode(':', $extension);
$io->warning('Do not add a version to the extension name!');
$io->text(sprintf('Try: cotor extend %s %s %s', $name, $extension, $version));


return self::FAILURE;
}

try {
$extensionPackage = Package::createFromComposerName($extension);
$extensionPackage = Package::createFromComposerName($extension, $version);
} catch (\InvalidArgumentException $e) {
$io->error($e->getMessage());

Expand Down Expand Up @@ -84,8 +104,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

// Install extension to tool
$extensionParam = $extension;
if ($version) {
$extensionParam = $extension . ':' . $version;
}

try {
$this->runComposerWithArguments('require', $targetDir, $extension);
$this->runComposerWithArguments('require', $targetDir, $extensionParam);
} catch (ProcessFailedException $e) {
/** @var Process $process */
$process = $e->getProcess(); // Make psalm happy :/
Expand All @@ -97,6 +122,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// Extract installed extension version and store everything in the project's composer file
if (null !== $composer) {
$extensionPackage = $this->getInstalledPackageVersion($toolsDir, $extensionPackage, $io, $package);
if (null !== $version) {
$extensionPackage = $extensionPackage->withVersion($version);
}
$this->updateComposerWithExtension($composer, $composerPath, $package, $extensionPackage, $io);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Domain/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public function __construct(string $vendor, string $name, string $version = self
$this->version = $version;
}

public static function createFromComposerName(string $vendorName, string $version = self::DEFAULT_VERSION): self
public static function createFromComposerName(string $vendorName, ?string $version = self::DEFAULT_VERSION): self
{
if (substr_count($vendorName, '/') !== 1) {
throw new \InvalidArgumentException(sprintf('%s is not valid composer package name!', $vendorName));
}

[$vendor, $name] = explode('/', $vendorName, 2);

return new self($vendor, $name, $version);
return new self($vendor, $name, $version ?? self::DEFAULT_VERSION);
}

public function withVersion(string $version): self
Expand Down

0 comments on commit 46d437d

Please sign in to comment.