mirror of
https://github.com/pelican-dev/panel.git
synced 2025-11-10 22:59:29 +01:00
refactor composer package management
This commit is contained in:
parent
5d5ee27c3b
commit
8882f9b6bb
@ -3,7 +3,6 @@
|
||||
namespace App\Console\Commands\Plugin;
|
||||
|
||||
use App\Facades\Plugins;
|
||||
use App\Models\Plugin;
|
||||
use Exception;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
@ -11,18 +10,12 @@ class ComposerPluginsCommand extends Command
|
||||
{
|
||||
protected $signature = 'p:plugin:composer';
|
||||
|
||||
protected $description = 'Runs "composer require" on all installed plugins.';
|
||||
protected $description = 'Makes sure the needed composer packages for all installed plugins are available.';
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$plugins = Plugin::all();
|
||||
foreach ($plugins as $plugin) {
|
||||
if (!$plugin->shouldLoad()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
Plugins::requireComposerPackages($plugin);
|
||||
Plugins::manageComposerPackages();
|
||||
} catch (Exception $exception) {
|
||||
report($exception);
|
||||
|
||||
@ -30,4 +23,3 @@ class ComposerPluginsCommand extends Command
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,8 +11,7 @@ use Illuminate\Support\Facades\Facade;
|
||||
/**
|
||||
* @method static void loadPlugins()
|
||||
* @method static void loadPanelPlugins(Panel $panel)
|
||||
* @method static void requireComposerPackages(Plugin $plugin)
|
||||
* @method static void removeComposerPackages(Plugin $plugin)
|
||||
* @method static void manageComposerPackages(?array $oldPackages = null)
|
||||
* @method static void runPluginMigrations(Plugin $plugin)
|
||||
* @method static void rollbackPluginMigrations(Plugin $plugin)
|
||||
* @method static void installPlugin(Plugin $plugin, bool $enable = true)
|
||||
|
||||
@ -150,34 +150,54 @@ class PluginService
|
||||
}
|
||||
}
|
||||
|
||||
public function requireComposerPackages(Plugin $plugin): void
|
||||
/** @param null|string[] $oldPackages */
|
||||
public function manageComposerPackages(?array $newPackages = [], ?array $oldPackages = null): void
|
||||
{
|
||||
if ($plugin->composer_packages) {
|
||||
$composerPackages = collect(json_decode($plugin->composer_packages, true, 512, JSON_THROW_ON_ERROR))
|
||||
$newPackages ??= [];
|
||||
|
||||
$plugins = Plugin::query()->orderBy('load_order')->get();
|
||||
foreach ($plugins as $plugin) {
|
||||
if (!$plugin->composer_packages) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$plugin->shouldLoad()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$pluginPackages = json_decode($plugin->composer_packages, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
$newPackages = array_merge($newPackages, $pluginPackages);
|
||||
} catch (Exception $exception) {
|
||||
report($exception);
|
||||
}
|
||||
}
|
||||
|
||||
$oldPackages = collect($oldPackages)
|
||||
->filter(fn ($version, $package) => !array_key_exists($package, $newPackages))
|
||||
->map(fn ($version, $package) => "$package:$version")
|
||||
->flatten()
|
||||
->unique()
|
||||
->toArray();
|
||||
|
||||
$result = Process::path(base_path())->timeout(600)->run(['composer', 'require', ...$composerPackages]);
|
||||
if (count($oldPackages) > 0) {
|
||||
$result = Process::path(base_path())->timeout(600)->run(['composer', 'remove', ...$oldPackages]);
|
||||
if ($result->failed()) {
|
||||
throw new Exception('Could not require composer packages: ' . $result->errorOutput());
|
||||
}
|
||||
throw new Exception('Could not remove old composer packages: ' . $result->errorOutput());
|
||||
}
|
||||
}
|
||||
|
||||
public function removeComposerPackages(Plugin $plugin): void
|
||||
{
|
||||
if ($plugin->composer_packages) {
|
||||
$composerPackages = collect(json_decode($plugin->composer_packages, true, 512, JSON_THROW_ON_ERROR))
|
||||
$newPackages = collect($newPackages)
|
||||
->map(fn ($version, $package) => "$package:$version")
|
||||
->flatten()
|
||||
->unique()
|
||||
->toArray();
|
||||
|
||||
$result = Process::path(base_path())->timeout(600)->run(['composer', 'remove', ...$composerPackages]);
|
||||
if (count($newPackages) > 0) {
|
||||
$result = Process::path(base_path())->timeout(600)->run(['composer', 'require', ...$newPackages]);
|
||||
if ($result->failed()) {
|
||||
throw new Exception('Could not remove composer packages: ' . $result->errorOutput());
|
||||
throw new Exception('Could not require new composer packages: ' . $result->errorOutput());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -234,7 +254,7 @@ class PluginService
|
||||
public function installPlugin(Plugin $plugin, bool $enable = true): void
|
||||
{
|
||||
try {
|
||||
$this->requireComposerPackages($plugin);
|
||||
$this->manageComposerPackages(json_decode($plugin->composer_packages, true, 512));
|
||||
|
||||
$this->runPluginMigrations($plugin);
|
||||
|
||||
@ -268,7 +288,7 @@ class PluginService
|
||||
public function uninstallPlugin(Plugin $plugin, bool $deleteFiles = false): void
|
||||
{
|
||||
try {
|
||||
$this->removeComposerPackages($plugin);
|
||||
$pluginPackages = json_decode($plugin->composer_packages, true, 512);
|
||||
|
||||
$this->rollbackPluginMigrations($plugin);
|
||||
|
||||
@ -279,6 +299,8 @@ class PluginService
|
||||
} else {
|
||||
$this->setStatus($plugin, PluginStatus::NotInstalled);
|
||||
}
|
||||
|
||||
$this->manageComposerPackages(oldPackages: $pluginPackages);
|
||||
} catch (Exception $exception) {
|
||||
$this->handlePluginException($plugin, $exception);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user