mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
Settings › Languages & Frameworks › Language Servers looks for newer versions of the tools in the IDE's tools folder once per session and from its refresh button: release downloads resolve their latest release as an install does, npm packages ask npm view through the user's registry. A tool with a newer version gets an Update tag, the available version in its details and an Update button that installs it.
226 lines
7.4 KiB
TypeScript
226 lines
7.4 KiB
TypeScript
/** Tool list + running installs. Installs show in the status-bar task list too. */
|
|
import { create } from "zustand";
|
|
import type { RequestId, ToolInfo, ToolUpdate } from "@/bindings";
|
|
import { executeAction } from "@/platform/actions";
|
|
import { isCancelled, nextRequestId } from "@/platform/ipc";
|
|
import { notifications } from "@/workbench/contributions";
|
|
import { cancelTask, queueTask, upsertTask, useTasks } from "@/workbench/tasks";
|
|
import { toolsBackend } from "./backend";
|
|
import { tx } from "@/platform/i18n";
|
|
|
|
export const SETTINGS_PAGE_ID = "languages.servers";
|
|
/** Tool installs and JDK downloads take turns (one network download / package manager at a time). */
|
|
export const DOWNLOAD_QUEUE = "downloads";
|
|
const MAX_LOG_LINES = 400;
|
|
|
|
export interface InstallJob {
|
|
request: RequestId;
|
|
lines: string[];
|
|
fraction: number | null;
|
|
message: string | null;
|
|
}
|
|
|
|
interface ToolsState {
|
|
tools: ToolInfo[];
|
|
loaded: boolean;
|
|
loading: boolean;
|
|
error: string | null;
|
|
jobs: Readonly<Record<string, InstallJob>>;
|
|
/** Output of the last finished install per tool (kept for the log view). */
|
|
logs: Readonly<Record<string, { lines: string[]; error: string | null }>>;
|
|
/** Newer versions of the tools the IDE installed, from the last check. */
|
|
updates: Readonly<Record<string, ToolUpdate>>;
|
|
checkingUpdates: boolean;
|
|
/** Whether updates were looked for in this session (the Language Servers page looks once). */
|
|
updatesChecked: boolean;
|
|
}
|
|
|
|
export const useTools = create<ToolsState>(() => ({
|
|
tools: [],
|
|
loaded: false,
|
|
loading: false,
|
|
error: null,
|
|
jobs: {},
|
|
logs: {},
|
|
updates: {},
|
|
checkingUpdates: false,
|
|
updatesChecked: false,
|
|
}));
|
|
|
|
const errorText = (e: unknown) => (e instanceof Error ? e.message : String(e));
|
|
|
|
let refreshing: Promise<void> | null = null;
|
|
|
|
export function refreshTools(): Promise<void> {
|
|
refreshing ??= (async () => {
|
|
useTools.setState({ loading: true });
|
|
try {
|
|
const tools = await toolsBackend().list();
|
|
useTools.setState({ tools, loaded: true, error: null });
|
|
} catch (e) {
|
|
useTools.setState({ error: errorText(e), loaded: true });
|
|
} finally {
|
|
useTools.setState({ loading: false });
|
|
refreshing = null;
|
|
}
|
|
})();
|
|
return refreshing;
|
|
}
|
|
|
|
let checkingUpdates: Promise<void> | null = null;
|
|
|
|
/** Looks for newer versions of the tools the IDE installed; offline, none are shown. */
|
|
export function checkToolUpdates(): Promise<void> {
|
|
checkingUpdates ??= (async () => {
|
|
useTools.setState({ checkingUpdates: true });
|
|
try {
|
|
const found = await toolsBackend().updates(nextRequestId());
|
|
useTools.setState({ updates: Object.fromEntries(found.map((u) => [u.name, u])) });
|
|
} catch {
|
|
// A failed lookup leaves the last result.
|
|
} finally {
|
|
useTools.setState({ checkingUpdates: false, updatesChecked: true });
|
|
checkingUpdates = null;
|
|
}
|
|
})();
|
|
return checkingUpdates;
|
|
}
|
|
|
|
function upsertTool(info: ToolInfo) {
|
|
useTools.setState((s) => {
|
|
const i = s.tools.findIndex((t) => t.name === info.name);
|
|
const tools = [...s.tools];
|
|
if (i >= 0) tools[i] = info;
|
|
else tools.push(info);
|
|
return { tools };
|
|
});
|
|
}
|
|
|
|
function patchJob(name: string, f: (j: InstallJob) => InstallJob) {
|
|
useTools.setState((s) => {
|
|
const j = s.jobs[name];
|
|
return j ? { jobs: { ...s.jobs, [name]: f(j) } } : s;
|
|
});
|
|
}
|
|
|
|
function endJob(name: string, error: string | null) {
|
|
useTools.setState((s) => {
|
|
const { [name]: job, ...jobs } = s.jobs;
|
|
return { jobs, logs: { ...s.logs, [name]: { lines: job?.lines ?? [], error } } };
|
|
});
|
|
}
|
|
|
|
const running = new Map<string, Promise<ToolInfo | null>>();
|
|
|
|
export function toolByName(name: string): ToolInfo | undefined {
|
|
return useTools.getState().tools.find((t) => t.name === name);
|
|
}
|
|
|
|
/**
|
|
* Installs `name` (or reinstalls it). Resolves with the new info, or null on
|
|
* failure / cancellation (already reported). Restarts language servers of that
|
|
* name afterwards so open files get code insight right away.
|
|
*/
|
|
export function installTool(name: string, opts: { notificationKey?: string } = {}): Promise<ToolInfo | null> {
|
|
const existing = running.get(name);
|
|
if (existing) return existing;
|
|
const display = toolByName(name)?.displayName ?? name;
|
|
const request = nextRequestId();
|
|
const taskId = `tools:${name}`;
|
|
const key = opts.notificationKey ?? `tools-install-${name}`;
|
|
useTools.setState((s) => ({ jobs: { ...s.jobs, [name]: { request, lines: [], fraction: null, message: null } } }));
|
|
const title = tx`Installing ${display}`;
|
|
const cancel = () => toolsBackend().cancel(request);
|
|
const task = (message: string | null, fraction: number | null) =>
|
|
upsertTask({ id: taskId, title, message, fraction, done: false, cancel });
|
|
|
|
const p = (async () => {
|
|
try {
|
|
// Downloads and package managers run one at a time; the rest wait, cancellable.
|
|
const info = await queueTask(DOWNLOAD_QUEUE, { id: taskId, title }, () => {
|
|
task(null, null);
|
|
return toolsBackend().install(name, request, (e) => {
|
|
if (e.type === "line") {
|
|
patchJob(name, (j) => ({ ...j, lines: [...j.lines, e.text].slice(-MAX_LOG_LINES), message: e.text }));
|
|
task(e.text, null);
|
|
} else {
|
|
patchJob(name, (j) => ({ ...j, fraction: e.fraction, message: e.message }));
|
|
task(e.message, e.fraction);
|
|
}
|
|
});
|
|
});
|
|
upsertTool(info);
|
|
endJob(name, null);
|
|
useTools.setState((s) => {
|
|
const { [name]: _updated, ...updates } = s.updates;
|
|
return { updates };
|
|
});
|
|
void toolsBackend()
|
|
.restartServers(name)
|
|
.catch(() => {});
|
|
notifications().notify({
|
|
group: "tools",
|
|
level: "info",
|
|
title: `${info.displayName} installed`,
|
|
body: info.version ? tx`Version ${info.version}` : undefined,
|
|
key,
|
|
});
|
|
// Shared packages (e.g. vscode-langservers-extracted) change sibling rows too.
|
|
void refreshTools();
|
|
return info;
|
|
} catch (e) {
|
|
if (isCancelled(e)) {
|
|
endJob(name, "Cancelled");
|
|
return null;
|
|
}
|
|
const message = errorText(e);
|
|
endJob(name, message);
|
|
notifications().notify({
|
|
group: "tools",
|
|
level: "error",
|
|
title: `Failed to install ${display}`,
|
|
body: message,
|
|
key,
|
|
actions: [{ label: "Show details", run: () => void executeAction("file.settings", SETTINGS_PAGE_ID) }],
|
|
});
|
|
return null;
|
|
} finally {
|
|
upsertTask({ id: taskId, title, message: null, fraction: 1, done: true });
|
|
running.delete(name);
|
|
}
|
|
})();
|
|
running.set(name, p);
|
|
return p;
|
|
}
|
|
|
|
/** Cancels the install of `name`, queued or running (as the task's Cancel does). */
|
|
export function cancelInstall(name: string) {
|
|
const taskId = `tools:${name}`;
|
|
if (useTasks.getState().tasks.some((t) => t.id === taskId)) return cancelTask(taskId);
|
|
const job = useTools.getState().jobs[name];
|
|
if (job) toolsBackend().cancel(job.request);
|
|
}
|
|
|
|
export async function uninstallTool(name: string): Promise<boolean> {
|
|
try {
|
|
upsertTool(await toolsBackend().uninstall(name));
|
|
void refreshTools();
|
|
return true;
|
|
} catch (e) {
|
|
notifications().notify({
|
|
group: "tools",
|
|
level: "error",
|
|
title: `Failed to uninstall ${toolByName(name)?.displayName ?? name}`,
|
|
body: errorText(e),
|
|
});
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export const STATUS_LABEL: Record<ToolInfo["status"], string> = {
|
|
installed: "Installed",
|
|
onPath: "On PATH",
|
|
custom: "Custom",
|
|
missing: "Missing",
|
|
};
|