Skip to content

Commit

Permalink
fix: installing beta packages #3792
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 8, 2023
1 parent c28e5a9 commit 323cc1a
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 44 deletions.
25 changes: 10 additions & 15 deletions extensions/package-manager/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
use Flarum\Extend;
use Flarum\Foundation\Paths;
use Flarum\Frontend\Document;
use Flarum\PackageManager\Exception\ComposerCommandFailedException;
use Flarum\PackageManager\Exception\ComposerRequireFailedException;
use Flarum\PackageManager\Exception\ComposerUpdateFailedException;
use Flarum\PackageManager\Exception\ExceptionHandler;
use Flarum\PackageManager\Exception\MajorUpdateFailedException;
use Flarum\PackageManager\Settings\LastUpdateCheck;
use Flarum\PackageManager\Settings\LastUpdateRun;
use Illuminate\Contracts\Queue\Queue;
use Illuminate\Queue\SyncQueue;

Expand All @@ -32,7 +25,8 @@
->post('/package-manager/minor-update', 'package-manager.minor-update', Api\Controller\MinorUpdateController::class)
->post('/package-manager/major-update', 'package-manager.major-update', Api\Controller\MajorUpdateController::class)
->post('/package-manager/global-update', 'package-manager.global-update', Api\Controller\GlobalUpdateController::class)
->get('/package-manager-tasks', 'package-manager.tasks.index', Api\Controller\ListTasksController::class),
->get('/package-manager-tasks', 'package-manager.tasks.index', Api\Controller\ListTasksController::class)
->post('/package-manager/composer', 'package-manager.composer', Api\Controller\ConfigureComposerController::class),

(new Extend\Frontend('admin'))
->css(__DIR__.'/less/admin.less')
Expand All @@ -52,18 +46,19 @@
new Extend\Locales(__DIR__.'/locale'),

(new Extend\Settings())
->default(LastUpdateCheck::key(), json_encode(LastUpdateCheck::default()))
->default(LastUpdateRun::key(), json_encode(LastUpdateRun::default()))
->default('flarum-package-manager.queue_jobs', false),
->default(Settings\LastUpdateCheck::key(), json_encode(Settings\LastUpdateCheck::default()))
->default(Settings\LastUpdateRun::key(), json_encode(Settings\LastUpdateRun::default()))
->default('flarum-package-manager.queue_jobs', false)
->default('flarum-package-manager.minimum_stability', 'stable'),

(new Extend\ServiceProvider)
->register(PackageManagerServiceProvider::class),

(new Extend\ErrorHandling)
->handler(ComposerCommandFailedException::class, ExceptionHandler::class)
->handler(ComposerRequireFailedException::class, ExceptionHandler::class)
->handler(ComposerUpdateFailedException::class, ExceptionHandler::class)
->handler(MajorUpdateFailedException::class, ExceptionHandler::class)
->handler(Exception\ComposerCommandFailedException::class, Exception\ExceptionHandler::class)
->handler(Exception\ComposerRequireFailedException::class, Exception\ExceptionHandler::class)
->handler(Exception\ComposerUpdateFailedException::class, Exception\ExceptionHandler::class)
->handler(Exception\MajorUpdateFailedException::class, Exception\ExceptionHandler::class)
->status('extension_already_installed', 409)
->status('extension_not_installed', 409)
->status('no_new_major_version', 409),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import app from 'flarum/admin/app';
import type Mithril from 'mithril';
import Component, { type ComponentAttrs } from 'flarum/common/Component';
import { CommonSettingsItemOptions, type SettingsComponentOptions } from '@flarum/core/src/admin/components/AdminPage';
import AdminPage from 'flarum/admin/components/AdminPage';
import type ItemList from 'flarum/common/utils/ItemList';
import Stream from 'flarum/common/utils/Stream';
import Button from 'flarum/common/components/Button';

export interface IConfigureComposer extends ComponentAttrs {
buildSettingComponent: (entry: ((this: this) => Mithril.Children) | SettingsComponentOptions) => Mithril.Children;
}

export default class ConfigureComposer<CustomAttrs extends IConfigureComposer = IConfigureComposer> extends Component<CustomAttrs> {
protected settings: Record<string, Stream<any>> = {};
protected loading: boolean = false;

oninit(vnode: Mithril.Vnode<CustomAttrs, this>) {
super.oninit(vnode);

this.submit();
}

view(): Mithril.Children {
return (
<div className="Form">
{this.attrs.buildSettingComponent.call(this, {
setting: 'minimum-stability',
label: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.label'),
help: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.help'),
type: 'select',
options: {
stable: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.stable'),
RC: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.rc'),
beta: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.beta'),
alpha: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.alpha'),
dev: app.translator.trans('flarum-package-manager.admin.composer.minimum_stability.options.dev'),
},
})}

<div className="Form-group">
<Button className="Button Button--primary" loading={this.loading} onclick={this.submit.bind(this)}>
{app.translator.trans('core.admin.settings.submit_button')}
</Button>
</div>
</div>
);
}

customSettingComponents(): ItemList<(attributes: CommonSettingsItemOptions) => Mithril.Children> {
return AdminPage.prototype.customSettingComponents();
}

setting(key: string) {
return this.settings[key] ?? (() => null);
}

submit() {
this.loading = true;

const configuration: any = {};

Object.keys(this.settings).forEach((key) => {
configuration[key] = this.settings[key]();
});

app
.request({
method: 'POST',
url: app.forum.attribute('apiUrl') + '/package-manager/composer',
body: { data: configuration },
})
.then(({ data }: any) => {
Object.keys(data).forEach((key) => {
this.settings[key] = Stream(data[key]);
});
})
.finally(() => {
this.loading = false;
m.redraw();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,42 @@ import ItemList from 'flarum/common/utils/ItemList';

import QueueSection from './QueueSection';
import ControlSection from './ControlSection';
import ConfigureComposer from './ConfigureComposer';
import Alert from 'flarum/common/components/Alert';
import listItems from 'flarum/common/helpers/listItems';

export default class SettingsPage extends ExtensionPage {
content() {
const settings = app.extensionData.getSettings(this.extension.id);

const warnings = [app.translator.trans('flarum-package-manager.admin.settings.access_warning')];

if (app.data.debugEnabled) warnings.push(app.translator.trans('flarum-package-manager.admin.settings.debug_mode_warning'));

return (
<div className="ExtensionPage-settings">
<div className="container">
<div className="Form-group">
<Alert className="PackageManager-primaryWarning" type="warning" dismissible={false}>
<ul>{listItems(warnings)}</ul>
</Alert>
</div>
{settings ? (
<div className="SettingsGroups">
<div className="Form">
{settings.map(this.buildSettingComponent.bind(this))}
<div className="Form-group">{this.submitButton()}</div>
</div>
<ConfigureComposer buildSettingComponent={this.buildSettingComponent} />
</div>
) : (
<h3 className="ExtensionPage-subHeader">{app.translator.trans('core.admin.extension.no_settings')}</h3>
)}
</div>
</div>
);
}

sections(vnode: Mithril.VnodeDOM<ExtensionPageAttrs, this>): ItemList<unknown> {
const items = super.sections(vnode);

Expand Down
16 changes: 0 additions & 16 deletions extensions/package-manager/js/src/admin/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ app.initializers.add('flarum-package-manager', (app) => {

app.extensionData
.for('flarum-package-manager')
.registerSetting(() => (
<div className="Form-group">
<Alert type="warning" dismissible={false}>
{app.translator.trans('flarum-package-manager.admin.settings.access_warning')}
</Alert>
</div>
))
.registerSetting(() =>
app.data.debugEnabled ? (
<div className="Form-group">
<Alert type="warning" dismissible={false}>
{app.translator.trans('flarum-package-manager.admin.settings.debug_mode_warning')}
</Alert>
</div>
) : null
)
.registerSetting({
setting: 'flarum-package-manager.queue_jobs',
label: app.translator.trans('flarum-package-manager.admin.settings.queue_jobs'),
Expand Down
4 changes: 4 additions & 0 deletions extensions/package-manager/less/admin/ControlSection.less
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,7 @@
.PackageManager-controlSection .container {
max-width: 1030px;
}

.PackageManager-primaryWarning ul {
margin: 0;
}
11 changes: 11 additions & 0 deletions extensions/package-manager/locale/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
flarum-package-manager:
admin:
composer:
minimum_stability:
label: Minimum Stability
help: The type of packages allowed to be installed. Do not change this unless you know what you are doing.
options:
stable: Stable (Recommended)
rc: Release Candidate
beta: Beta
alpha: Alpha
dev: Dev

exceptions:
composer_command_failure: Failed to execute. Check the composer logs in storage/logs/composer.
extension_already_installed: Extension is already installed.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\PackageManager\Api\Controller;

use Flarum\Foundation\Paths;
use Flarum\Http\RequestUtil;
use Flarum\PackageManager\ConfigureComposerValidator;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ConfigureComposerController implements RequestHandlerInterface
{
protected $configurable = [
'minimum-stability',
];

/**
* @var ConfigureComposerValidator
*/
protected $validator;

/**
* @var Paths
*/
protected $paths;

public function __construct(ConfigureComposerValidator $validator, Paths $paths)
{
$this->validator = $validator;
$this->paths = $paths;
}

public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);
$data = Arr::only(Arr::get($request->getParsedBody(), 'data'), $this->configurable);

$actor->assertAdmin();

$this->validator->assertValid($data);
$composerJson = $this->readComposerJson();

if (! empty($data)) {
foreach ($data as $key => $value) {
Arr::set($composerJson, $key, $value);
}

$this->writeComposerJson($composerJson);
}

return new JsonResponse([
'data' => Arr::only($composerJson, $this->configurable),
]);
}

protected function readComposerJson()
{
$composerJson = file_get_contents($this->paths->base.'/composer.json');
$composerJson = json_decode($composerJson, true);

return $composerJson;
}

protected function writeComposerJson($composerJson)
{
$composerJson = json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
file_put_contents($this->paths->base.'/composer.json', $composerJson);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);

dd('wat');

$response = $this->bus->dispatch(
new GlobalUpdate($actor)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function handle(CheckForUpdates $command)
foreach ($firstOutput['installed'] as &$mainPackageUpdate) {
$mainPackageUpdate['latest-minor'] = $mainPackageUpdate['latest-major'] = null;

if ($mainPackageUpdate['latest-status'] === 'up-to-date') {
continue;
}

if (isset($mainPackageUpdate['latest-status']) && $mainPackageUpdate['latest-status'] === 'update-possible' && Util::isMajorUpdate($mainPackageUpdate['version'], $mainPackageUpdate['latest'])) {
$mainPackageUpdate['latest-major'] = $mainPackageUpdate['latest'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public function handle(GlobalUpdate $command)
'--with-all-dependencies' => true,
];

dd($this->config->inDebugMode());

$output = $this->composer->run(
new ArrayInput($input),
$command->task ?? null
Expand Down
Loading

0 comments on commit 323cc1a

Please sign in to comment.