Skip to content

Commit

Permalink
API Update API to reflect changes to CLI interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 16, 2024
1 parent 392a8c2 commit fd67584
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 287 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ composer require bringyourownideas/silverstripe-maintenance
Build schema and queue an initial job to populate the database:

```sh
sake dev/build
sake db:build --flush
```

If you haven't already, you need to [configure the job queue](https://github.com/symbiote/silverstripe-queuedjobs)
Expand All @@ -58,8 +58,9 @@ BringYourOwnIdeas\Maintenance\Jobs\CheckForUpdatesJob:
By default, tasks are run through a job queue. You can also choose to manually refresh via the command line.
Run the update task (includes the [update-checker](https://github.com/bringyourownideas/silverstripe-composer-update-checker))
```
sake dev/tasks/UpdatePackageInfoTask
```sh
sake tasks:UpdatePackageInfoTask
```

## How your composer.json influences the report
Expand Down
7 changes: 6 additions & 1 deletion src/Jobs/CheckForUpdatesJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use SilverStripe\ORM\FieldType\DBDatetime;
use Symbiote\QueuedJobs\Services\QueuedJob;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\PolyExecution\PolyOutput;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJobService;
use Symfony\Component\Console\Input\ArrayInput;

/**
* Refresh report job. Runs as a queued job.
Expand Down Expand Up @@ -59,7 +61,10 @@ public function process()
{
// Run the UpdatePackageInfo task
$updateTask = Injector::inst()->create(UpdatePackageInfoTask::class);
$updateTask->run(null);
$output = PolyOutput::create(PolyOutput::FORMAT_ANSI);
$input = new ArrayInput([]);
$input->setInteractive(false);
$updateTask->run($input, $output);

// mark job as completed
$this->isComplete = true;
Expand Down
106 changes: 12 additions & 94 deletions src/Tasks/UpdatePackageInfoTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,26 @@
namespace BringYourOwnIdeas\Maintenance\Tasks;

use BringYourOwnIdeas\Maintenance\Util\ComposerLoader;
use BringYourOwnIdeas\Maintenance\Util\ModuleHealthLoader;
use BringYourOwnIdeas\Maintenance\Util\SupportedAddonsLoader;
use RuntimeException;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Environment;
use SilverStripe\ORM\Queries\SQLDelete;
use SilverStripe\ORM\DataObjectSchema;
use BringYourOwnIdeas\Maintenance\Model\Package;
use SilverStripe\Core\Manifest\VersionProvider;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Deprecation;
use SilverStripe\PolyExecution\PolyOutput;
use SilverStripe\SupportedModules\BranchLogic;
use SilverStripe\SupportedModules\MetaData;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

/**
* Parses a composer lock file in order to cache information about the installation.
*/
class UpdatePackageInfoTask extends BuildTask
{
/**
* {@inheritDoc}
* @var string
*/
private static $segment = 'UpdatePackageInfoTask';
protected static string $commandName = 'UpdatePackageInfoTask';

/**
* A custom memory limit to set for this to increase to (or do nothing if the memory is already set high enough)
Expand All @@ -43,8 +38,6 @@ class UpdatePackageInfoTask extends BuildTask
*/
private static $dependencies = [
'ComposerLoader' => '%$BringYourOwnIdeas\\Maintenance\\Util\\ComposerLoader',
'SupportedAddonsLoader' => '%$BringYourOwnIdeas\\Maintenance\\Util\\SupportedAddonsLoader',
'ModuleHealthLoader' => '%$BringYourOwnIdeas\\Maintenance\\Util\\ModuleHealthLoader',
];

/**
Expand All @@ -63,17 +56,6 @@ class UpdatePackageInfoTask extends BuildTask
*/
protected $composerLoader;

/**
* @var SupportedAddonsLoader
*/
protected $supportedAddonsLoader;

/**
* @var ModuleHealthLoader
* @deprecated 3.2.0 Will be removed without equivalent functionality
*/
protected $moduleHealthLoader;

/**
* Fetch the composer loader
*
Expand All @@ -97,54 +79,12 @@ public function setComposerLoader($composerLoader)
return $this;
}

/**
* @return SupportedAddonsLoader
*/
public function getSupportedAddonsLoader()
{
return $this->supportedAddonsLoader;
}

/**
* @param SupportedAddonsLoader $supportedAddonsLoader
* @return $this
*/
public function setSupportedAddonsLoader(SupportedAddonsLoader $supportedAddonsLoader)
{
$this->supportedAddonsLoader = $supportedAddonsLoader;
return $this;
}

/**
* @return ModuleHealthLoader
* @deprecated 3.2.0 Will be removed without equivalent functionality
*/
public function getModuleHealthLoader()
{
Deprecation::notice('3.2.0', 'Will be removed without equivalent functionality');
return $this->moduleHealthLoader;
}

/**
* @param ModuleHealthLoader $moduleHealthLoader
* @return $this
* @deprecated 3.2.0 Will be removed without equivalent functionality
*/
public function setModuleHealthLoader(ModuleHealthLoader $moduleHealthLoader)
{
Deprecation::withNoReplacement(
fn() => Deprecation::notice('3.2.0', 'Will be removed without equivalent functionality')
);
$this->moduleHealthLoader = $moduleHealthLoader;
return $this;
}

public function getTitle()
public function getTitle(): string
{
return _t(__CLASS__ . '.TITLE', 'Refresh installed package info');
}

public function getDescription()
public static function getDescription(): string
{
return _t(
__CLASS__ . '.DESCRIPTION',
Expand All @@ -155,10 +95,8 @@ public function getDescription()

/**
* Update database cached information about this site.
*
* @param HTTPRequest $request unused, can be null (must match signature of parent function).
*/
public function run($request)
protected function execute(InputInterface $input, PolyOutput $output): int
{
// Loading packages and all their updates can be quite memory intensive.
$memoryLimit = $this->config()->get('memory_limit');
Expand All @@ -172,7 +110,7 @@ public function run($request)
$composerLock = $this->getComposerLoader()->getLock();
$rawPackages = array_merge($composerLock->packages, (array) $composerLock->{'packages-dev'});
$packages = $this->getPackageInfo($rawPackages);
$supportedPackages = $this->getSupportedPackages();
$supportedPackages = $this->getSupportedPackages($output);

// Extensions to the process that add data may rely on external services.
// There may be a communication issue between the site and the external service,
Expand All @@ -190,6 +128,8 @@ public function run($request)
Package::create()->update($package)->write();
}
}

return Command::SUCCESS;
}

/**
Expand Down Expand Up @@ -218,10 +158,8 @@ public function getPackageInfo($packageList)
/**
* Return an array of supported modules as fetched from silverstripe/supported-modules.
* Outputs a message and returns null if an error occurs
*
* @return null|array
*/
public function getSupportedPackages()
public function getSupportedPackages(PolyOutput $output): ?array
{
try {
$repos = MetaData::getAllRepositoryMetaData()[MetaData::CATEGORY_SUPPORTED];
Expand All @@ -236,27 +174,7 @@ public function getSupportedPackages()
$repos
));
} catch (RuntimeException $exception) {
echo $exception->getMessage() . PHP_EOL;
}

return null;
}

/**
* Return an array of module health information as fetched from addons.silverstripe.org.
* Outputs a message and returns null if an error occurs
*
* @param string[] $moduleNames
* @return null|array
* @deprecated 3.2.0 Will be removed without equivalent functionality
*/
public function getHealthIndicator(array $moduleNames)
{
Deprecation::notice('3.2.0', 'Will be removed without equivalent functionality');
try {
return $this->getModuleHealthLoader()->setModuleNames($moduleNames)->getModuleHealthInfo() ?: [];
} catch (RuntimeException $exception) {
echo $exception->getMessage() . PHP_EOL;
$output->writeln('<error>'.$exception->getMessage().'</>');
}

return null;
Expand Down
46 changes: 0 additions & 46 deletions src/Util/SupportedAddonsLoader.php

This file was deleted.

9 changes: 8 additions & 1 deletion tests/Tasks/UpdatePackageInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use SilverStripe\Dev\SapphireTest;
use SilverStripe\SupportedModules\MetaData;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\PolyExecution\PolyOutput;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;

/**
* @mixin PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -82,7 +85,11 @@ public function getModuleVersion(string $module): string

$task = UpdatePackageInfoTask::create();
$task->setComposerLoader($composerLoader);
$task->run(null);
$output = PolyOutput::create(PolyOutput::FORMAT_ANSI);
$output->setWrappedOutput(new BufferedOutput());
$input = new ArrayInput([]);
$input->setInteractive(false);
$task->run($input, $output);

$packages = Package::get();
$this->assertCount(2, $packages);
Expand Down
32 changes: 0 additions & 32 deletions tests/Util/ModuleHealthLoaderTest.php

This file was deleted.

Loading

0 comments on commit fd67584

Please sign in to comment.