Skip to content

Commit 932b931

Browse files
committed
If in interactive mode and gitify finds multiple versions of the same package, a list will be presented to the user asking them to choose.
1 parent 892a8eb commit 932b931

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

src/Command/InstallPackageCommand.php

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Input\InputOption;
88
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\Console\Question\ChoiceQuestion;
910
use Symfony\Component\Console\Question\ConfirmationQuestion;
1011

1112
/**
@@ -266,6 +267,9 @@ private function download($packageName, $provider, $options = []) {
266267
return false;
267268
}
268269

270+
$helper = $this->getHelper('question');
271+
$selectedFromMultiVersions = false;
272+
269273
$provider->getClient();
270274
$this->output->writeln("Searching <comment>{$provider->get('name')}</comment> for <comment>$packageName</comment>...");
271275

@@ -336,7 +340,7 @@ private function download($packageName, $provider, $options = []) {
336340
if (!empty($response)) {
337341

338342
$foundPackages = simplexml_load_string($response->response);
339-
343+
$this->output->writeln($foundPackages);
340344
// No matches, simply return
341345
if ($foundPackages['total'] == 0) {
342346
return true;
@@ -349,7 +353,7 @@ private function download($packageName, $provider, $options = []) {
349353
$name = strtolower((string)$foundPkg->name);
350354

351355
// Only accept exact match on name
352-
if ($name == $packageName) {
356+
if ($name === $packageName) {
353357
$packages[$name] = array (
354358
'name' => (string) $foundPkg->name,
355359
'version' => (string) $foundPkg->version,
@@ -366,27 +370,47 @@ private function download($packageName, $provider, $options = []) {
366370
}
367371
}
368372

369-
// If there are multiple versions of the same package, use the latest
370373
if (count($packageVersions) > 1) {
371-
$i = 0;
372-
$latest = '';
373-
374-
// Compare versions
375-
foreach (array_keys($packageVersions) as $version) {
376-
if ($i == 0) {
377-
// First iteration
378-
$latest = $version;
379-
} else {
380-
// Replace latest version with current one if it's higher
381-
if (version_compare($version, $latest, '>=')) {
374+
if($this->interactive) {
375+
// If in interactive mode and more than one package, let user select which version to install.
376+
$selectPackages = [];
377+
foreach ($packageVersions as $k => $version) {
378+
$selectPackages[] = $k;
379+
}
380+
$question = new ChoiceQuestion(
381+
'Choose a package version to install...',
382+
$selectPackages,
383+
0
384+
);
385+
$question->setErrorMessage('Please select a valid option.');
386+
$answer = $helper->ask($this->input, $this->output, $question);
387+
$this->output->writeln('Installing: ' . $answer);
388+
389+
$packages[$packageName] = $packageVersions[$answer];
390+
$selectedFromMultiVersions = true;
391+
}
392+
else {
393+
// If there are multiple versions of the same package and not interactive, use the latest
394+
$i = 0;
395+
$latest = '';
396+
397+
// Compare versions
398+
foreach (array_keys($packageVersions) as $version) {
399+
if ($i === 0) {
400+
// First iteration
382401
$latest = $version;
402+
} else {
403+
// Replace latest version with current one if it's higher
404+
if (version_compare($version, $latest, '>=')) {
405+
$latest = $version;
406+
}
383407
}
408+
$i++;
384409
}
385-
$i++;
386-
}
387410

388-
// Use latest
389-
$packages[$packageName] = $packageVersions[$latest];
411+
// Use latest
412+
$packages[$packageName] = $packageVersions[$latest];
413+
}
390414
}
391415

392416
// If there's still no match, revisit the response and just grab all hits...
@@ -406,9 +430,9 @@ private function download($packageName, $provider, $options = []) {
406430
// Process found packages
407431
if (!empty($packages)) {
408432

409-
$this->output->writeln('Found ' . count($packages) . ' package(s).');
410-
411-
$helper = $this->getHelper('question');
433+
if (!$selectedFromMultiVersions) {
434+
$this->output->writeln('Found ' . count($packages) . ' package(s).');
435+
}
412436

413437
foreach ($packages as $package) {
414438
if ($this->modx->getCount('transport.modTransportPackage', ['signature' => $package['signature']])) {
@@ -422,7 +446,7 @@ private function download($packageName, $provider, $options = []) {
422446
}
423447
}
424448

425-
if ($this->interactive) {
449+
if ($this->interactive && !$selectedFromMultiVersions) {
426450
if (!$helper->ask(
427451
$this->input,
428452
$this->output,

0 commit comments

Comments
 (0)