132 lines
8.7 KiB
JavaScript
132 lines
8.7 KiB
JavaScript
// "A package is missing, install it": (1) API Platform (or the JWT bundle) offered under the checkbox "API Platform resource" of the
|
|
// New entity dialog and by the New… hub, run through the Composer window's backend (the project's runner: Docker with its PHP version or
|
|
// the computer's PHP), its output shown live, the API Platform window appearing at the end; (2) the quick fix "Install the missing
|
|
// package…" of an "Undefined class 'X\Y'" problem (candidates: composer.candidates, the built-in map then Packagist).
|
|
// The pure flows are in api-install-model.js (tested with fakes).
|
|
import { backend } from './backend.js';
|
|
import { el, dialog, confirm, notify, t } from './ui.js';
|
|
import { installPackage, missingClasses, requireLine, validPackage } from './api-install-model.js';
|
|
|
|
const cssHref = new URL('../css/api-install.css', import.meta.url).href;
|
|
if (![...document.querySelectorAll('link[rel=stylesheet]')].some((l) => l.href === cssHref)) document.head.append(el('link', { rel: 'stylesheet', href: cssHref }));
|
|
|
|
/** Runs `composer require` with the Composer window's backend; `onOutput(chunk)` gets the live output. Resolves with {code}. */
|
|
export async function requireWithComposer(app, params, onOutput = null) {
|
|
const composer = app.composer;
|
|
if (!composer) throw new Error(t('No Composer window here: it needs a project on this computer'));
|
|
await composer.useTarget('');
|
|
const off = onOutput ? backend.on('run.output', (p) => onOutput(String(p.chunk || ''))) : null;
|
|
try {
|
|
return await composer.run({ action: 'require', ...params });
|
|
} finally {
|
|
off?.();
|
|
}
|
|
}
|
|
|
|
/** Plan, ask, install: resolves with installPackage()'s result. `onOutput` shows the live output inside a dialog. */
|
|
export function installFlow(app, what = 'api', onOutput = null) {
|
|
return installPackage({
|
|
plan: (w) => backend.invoke('apiplatform.install.plan', { what: w }),
|
|
confirm: (plan) => confirm(what === 'jwt' ? t('Install the JWT bundle') : t('Install API Platform'),
|
|
t('This runs {command} in the project (its output is shown live), then the API Platform window appears.', { command: plan.command }), { ok: t('Install') }),
|
|
run: (params) => requireWithComposer(app, params, onOutput),
|
|
enable: () => app.enableApiPlatform?.(),
|
|
}, what);
|
|
}
|
|
|
|
/**
|
|
* The notice under a checkbox that needs a package. `wanted()` says whether the checkbox is ticked; call `.update()` when it changes.
|
|
* `.node` is hidden while there is nothing to say; `.state` is 'present' | 'missing' | 'installing' | 'installed' | 'declined' | 'failed'.
|
|
*/
|
|
export function packageNotice(app, what = 'api') {
|
|
const title = what === 'jwt' ? t('The JWT bundle is not installed in this project') : t('API Platform is not installed in this project');
|
|
const button = el('button.btn.small.primary', { type: 'button', onclick: () => run() }, what === 'jwt' ? t('Install the JWT bundle') : t('Install API Platform'));
|
|
const text = el('span', {}, title);
|
|
const log = el('pre.pn-log', { hidden: true });
|
|
const node = el('div.pn-notice', { hidden: true }, el('div.pn-row', {}, el('span.pn-ico', {}, '!'), text, button), log);
|
|
const me = { node, state: 'present', wanted: () => true, update, installed: () => false };
|
|
async function update() {
|
|
if (!me.wanted()) { node.hidden = true; return; }
|
|
if (me.state === 'installed') { node.hidden = false; return; }
|
|
try {
|
|
const plan = await backend.invoke('apiplatform.install.plan', { what });
|
|
me.state = plan.installed ? 'present' : (me.state === 'present' ? 'missing' : me.state);
|
|
} catch { me.state = 'present'; }
|
|
node.hidden = me.state === 'present';
|
|
}
|
|
async function run() {
|
|
button.disabled = true;
|
|
me.state = 'installing';
|
|
log.hidden = false;
|
|
log.textContent = '';
|
|
try {
|
|
const r = await installFlow(app, what, (chunk) => { log.textContent = (log.textContent + chunk.replace(/\x1b\[[0-9;?]*[A-Za-z]/g, '')).slice(-1500); log.scrollTop = log.scrollHeight; });
|
|
me.state = r.status === 'installed' || r.status === 'present' ? 'installed' : r.status;
|
|
if (r.status === 'installed') { text.textContent = t('Installed: {package}', { package: r.plan.package }); node.classList.add('ok'); button.hidden = true; }
|
|
else if (r.status === 'failed') { text.textContent = t('The installation failed: see the output below, or the Run window.'); button.disabled = false; }
|
|
else if (r.status === 'declined') { text.textContent = title + ' ' + t('(the class will be undefined until it is installed)'); button.disabled = false; log.hidden = true; }
|
|
else if (r.status === 'present') { node.hidden = true; }
|
|
} catch (e) {
|
|
me.state = 'failed';
|
|
text.textContent = e.message || String(e);
|
|
button.disabled = false;
|
|
}
|
|
}
|
|
return me;
|
|
}
|
|
|
|
/** The warning when something is written while its package is missing. */
|
|
export function warnMissing(notice, what = 'api') {
|
|
if (notice.state === 'present' || notice.state === 'installed') return;
|
|
notify(what === 'jwt' ? t('The JWT bundle is not installed: its classes are undefined until it is.') : t('API Platform is not installed: #[ApiResource] is undefined until it is.'), 'warning', { title: 'Composer' });
|
|
}
|
|
|
|
/** The dialog of the quick fix: candidates, an editable package name, --dev, a constraint; the install runs in the Run window. */
|
|
export async function missingPackageDialog(app, name) {
|
|
let list = [];
|
|
try { list = (await backend.invoke('composer.candidates', { name })).candidates || []; } catch { /* the name is typed by hand */ }
|
|
const pick = el('select.input', { onchange: () => choose(list[pick.selectedIndex]) },
|
|
...(list.length ? list.map((c) => el('option', {}, `${c.package}${c.dev ? ' (dev)' : ''}${c.description ? ' — ' + c.description : ''}`)) : [el('option', {}, '—')]));
|
|
const pkg = el('input.input', { value: list[0]?.package || '', placeholder: 'vendor/package', oninput: () => preview() });
|
|
const constraint = el('input.input', { value: list[0]?.constraint || '', placeholder: t('(newest compatible)'), oninput: () => preview() });
|
|
const dev = el('input', { type: 'checkbox', checked: !!list[0]?.dev, onchange: () => preview() });
|
|
const line = el('code.pn-cmd');
|
|
const choose = (c) => { if (!c) return; pkg.value = c.package; constraint.value = c.constraint || ''; dev.checked = !!c.dev; preview(); };
|
|
const preview = () => { line.textContent = requireLine({ package: pkg.value || '…', constraint: constraint.value, dev: dev.checked }); };
|
|
preview();
|
|
const row = (label, node) => el('label.db-field', {}, el('span', {}, label), node);
|
|
dialog({
|
|
title: t('Install the missing package…'),
|
|
body: el('div.db-form', {}, el('p', {}, t('{name} is not defined: no installed package provides it.', { name })),
|
|
row(t('Candidates'), pick), row(t('Package'), pkg), row(t('Version constraint (optional)'), constraint),
|
|
el('label.db-check', {}, dev, t('Development dependency (--dev)')), line),
|
|
buttons: [{ label: t('Cancel') }, { label: t('Install'), primary: true, action: () => {
|
|
if (!validPackage(pkg.value)) { pkg.classList.add('bad'); return false; }
|
|
// the output goes to the Run window; the index follows the end of the run by itself, the squiggle goes
|
|
requireWithComposer(app, { package: pkg.value.trim(), constraint: constraint.value.trim(), dev: dev.checked }).catch(() => null);
|
|
} }],
|
|
});
|
|
}
|
|
|
|
/** The New… hub's entry: install API Platform (confirmation, Composer in the Run window), the API Platform window then appears. */
|
|
export async function installApiPlatform(app) {
|
|
const r = await installFlow(app, 'api');
|
|
if (r.status === 'installed') { notify(t('API Platform is installed: its window is in the bottom tool bar.'), 'success', { title: 'API Platform' }); app.showTool?.('apiplatform', true); }
|
|
else if (r.status === 'present') notify(t('API Platform is already installed in this project.'), 'info', { title: 'API Platform' });
|
|
else if (r.status === 'failed') notify(t('The installation failed: see the output below, or the Run window.'), 'error', { title: 'API Platform' });
|
|
return r;
|
|
}
|
|
|
|
/** The Monaco quick fix on every "Undefined class" marker (also what the hover's "Quick Fix…" lists). */
|
|
export function registerMissingPackageFix(monaco, app) {
|
|
monaco.editor.registerCommand('jumphp.installMissingPackage', (_accessor, name) => missingPackageDialog(app, name));
|
|
monaco.languages.registerCodeActionProvider('php', {
|
|
provideCodeActions: (_model, _range, context) => {
|
|
const actions = missingClasses(context.markers).map(({ name, marker }) => ({
|
|
title: t('Install the missing package…'), kind: 'quickfix', diagnostics: [marker],
|
|
command: { id: 'jumphp.installMissingPackage', title: t('Install the missing package…'), arguments: [name] },
|
|
}));
|
|
return { actions, dispose() {} };
|
|
},
|
|
}, { providedCodeActionKinds: ['quickfix'] });
|
|
}
|