Skip to content

Commit

Permalink
Merge pull request #430 from modmore/better-packages
Browse files Browse the repository at this point in the history
Automatically update the list of packages with versions during extract + improve install
  • Loading branch information
muzzwood authored Mar 23, 2023
2 parents 60bf37d + 8ea7544 commit e5f7290
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/Command/ExtractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

use modmore\Gitify\BaseCommand;
use modmore\Gitify\Gitify;
use modTransportPackage;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use xPDO\xPDOIterator;
use xPDOTransport;

/**
* Class BuildCommand
Expand All @@ -33,6 +36,13 @@ protected function configure()
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
'Specify the data partition key (folder name), or keys separated by a space, that you want to extract. '
)
->addOption(
'packages',
'p',
InputOption::VALUE_NEGATABLE,
'Skip extracting installed package when set.',
true
)
;
}

Expand Down Expand Up @@ -78,6 +88,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if ($input->getOption('packages')) {
$this->extractPackages($input->getOption('config'));
}

$output->writeln('Done! ' . $this->getRunStats());
return 0;
}
Expand Down Expand Up @@ -502,4 +516,55 @@ protected function filterPathSegment($path)
}
return \modResource::filterPathSegment($this->modx, $path, $options);
}

private function extractPackages(string $file = null): void
{
$this->output->writeln('<info>Extracting installed packages...</info>');
$data = Gitify::loadConfig($file);
$file = GITIFY_WORKING_DIR . $file;

$result = $this->modx->call('transport.modTransportPackage', 'listPackages', [
&$this->modx,
1,
0,
0,
''
]);

$providers = [];
/** @var modTransportPackage $package */
foreach ($result['collection'] as $package) {
$signature = $package->get('signature');
$sig = xPDOTransport::parseSignature($signature);

if (!$provider = $package->getOne('Provider')) {
$this->output->writeln("- <comment>Package {$sig[0]} is not assigned to a provider, skipping</comment>");
continue;
}

$providerKey = $provider->get('name');
if (!isset($providers[$providerKey])) {
$providers[$providerKey] = [
'service_url' => $provider->get('service_url')
];
if ($provider->get('description')) {
$providers[$providerKey]['description'] = $provider->get('description');
}
if ($provider->get('username')) {
$providers[$providerKey]['username'] = $provider->get('username');
}
if ($provider->get('api_key') && !file_exists(GITIFY_WORKING_DIR . '.' . $providerKey . '.key')) {
$key = $provider->get('api_key');
file_put_contents(GITIFY_WORKING_DIR . '.' . $providerKey . '.key', $key);
$providers[$providerKey]['api_key'] = '.' . $providerKey . '.key';
}
}

$providers[$providerKey]['packages'][] = $signature;
}
$data['packages'] = $providers;

file_put_contents($file, Gitify::toYAML($data));
$this->output->writeln('<info>Packages updated.</info>');
}
}
33 changes: 33 additions & 0 deletions src/Command/InstallPackageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use xPDOTransport;

/**
* Class InstallPackageCommand
Expand Down Expand Up @@ -235,6 +236,37 @@ private function install($package, $provider = 0, array $installOptions = [])
return false;
}

$verbosity = $this->input->getOption('all') ? OutputInterface::VERBOSITY_VERBOSE : OutputInterface::VERBOSITY_NORMAL;

$installed = $this->modx->getObject('transport.modTransportPackage', [
'signature' => $package,
'provider' => $provider->get('id'),
]);
if ($installed) {
$this->output->writeln("- <info>$package</info> is already installed, skipping", $verbosity);
return true;
}

[$name, $version] = xPDOTransport::parseSignature($package);
$c = $this->modx->newQuery('transport.modTransportPackage');
$c->where([
'provider' => $provider->get('id'),
'signature:LIKE' => $name . '-%',
]);
$c->sortby('installed', 'DESC'); // @todo is this sufficient to get highest installed version

/** @var \modTransportPackage $lastVersion */
$lastVersion = $this->modx->getObject('transport.modTransportPackage', $c);
if ($lastVersion) {
$sig = xPDOTransport::parseSignature($lastVersion->get('signature'));
$installedVersion = $sig[1];

if (version_compare($installedVersion, $version, '>=')) {
$this->output->writeln("- <info>$package</info>, found higher version {$installedVersion} already installed", $verbosity);
return true;
}
}

// Download and install the package from the chosen provider
$completed = $this->download($package, $provider, $installOptions);
if (!$completed) {
Expand Down Expand Up @@ -482,6 +514,7 @@ private function download($packageName, $provider, $options = []) {

// Grab the package object
$obj = $response->getObject();
/** @var \modTransportPackage $package */
if ($package = $this->modx->getObject('transport.modTransportPackage', ['signature' => $obj['signature']])) {
// Install the package
return $package->install($options);
Expand Down

0 comments on commit e5f7290

Please sign in to comment.