дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 01:01:17 +02:00
Flutter apps started with Run now run through flutter run --machine. Hot Reload and Hot Restart send app.restart requests with the app's id instead of typing r / R into the process. The buttons are enabled once the app reports app.started and supportsRestart. The JSON protocol on stdout is decoded into plain console lines (app.log, progress, daemon messages, failed restarts). Lines outside the protocol show unchanged, and these runs never go through a terminal emulator. Run-mode apps are kept in a workbench store, so the Flutter Inspector, DevTools and Extensions windows connect to them through the wsUri of app.debugPort, the same way they connect to Debug sessions. The Inspector lists Debug and Run apps together. Part of #4096. Co-authored-by: wildcatDownstairs <[email protected]>
91 рядки
3.1 KiB
TypeScript
91 рядки
3.1 KiB
TypeScript
/**
|
|
* `flutter run --machine`, as the Flutter plugin runs apps: stdout is JSON-RPC, one bracketed
|
|
* array of messages per line, as from `flutter daemon`. The events keep the Run tab's app in
|
|
* `useFlutterRunApps` (its id, device, VM Service and whether it started), and the console
|
|
* shows the app's own output and the tool's messages as plain lines.
|
|
*/
|
|
import type { RunConfig } from "@/platform/ipc";
|
|
import { useFlutterRunApps, type FlutterRunApp } from "@/workbench/flutterRunApps";
|
|
import { TestStdout } from "./testing/types";
|
|
|
|
export const isFlutterMachineRun = (c: RunConfig) =>
|
|
c.kind === "flutter" && c.args[0] === "run" && c.args.includes("--machine");
|
|
|
|
type Params = Record<string, unknown>;
|
|
|
|
const text = (v: unknown) => (typeof v === "string" ? v : null);
|
|
const line = (v: unknown) => {
|
|
const s = text(v);
|
|
return s ? (s.endsWith("\n") ? s : `${s}\n`) : "";
|
|
};
|
|
|
|
function patch(session: string, p: Partial<FlutterRunApp>) {
|
|
const app = useFlutterRunApps.getState()[session];
|
|
if (app) useFlutterRunApps.setState({ [session]: { ...app, ...p } });
|
|
}
|
|
|
|
/** The console text of one message. */
|
|
function message(session: string, m: { event?: unknown; params?: Params; result?: unknown; error?: unknown }) {
|
|
const p = m.params ?? {};
|
|
switch (m.event) {
|
|
case "app.start":
|
|
patch(session, { appId: text(p.appId), device: text(p.deviceId), supportsRestart: p.supportsRestart === true });
|
|
return "";
|
|
case "app.debugPort":
|
|
patch(session, { vmServiceUri: text(p.wsUri) ?? undefined });
|
|
return "";
|
|
case "app.started":
|
|
patch(session, { started: true });
|
|
return "";
|
|
case "app.stop":
|
|
patch(session, { started: false });
|
|
return line(p.error);
|
|
case "app.log":
|
|
case "daemon.logMessage":
|
|
return line(p.log ?? p.message);
|
|
case "app.progress":
|
|
return p.finished ? "" : line(p.message);
|
|
case undefined: {
|
|
// The answer to `app.restart`: only a failure says something.
|
|
if (m.error) return line(typeof m.error === "string" ? m.error : JSON.stringify(m.error));
|
|
const result = m.result as { code?: unknown; message?: unknown } | null | undefined;
|
|
return result && result.code !== 0 ? line(result.message) : "";
|
|
}
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
/** The console text of a stdout line; lines outside the protocol show as they are. */
|
|
export function machineLine(session: string, raw: string): string {
|
|
if (!raw.startsWith("[{")) return `${raw}\n`;
|
|
let messages: unknown;
|
|
try {
|
|
messages = JSON.parse(raw);
|
|
} catch {
|
|
return `${raw}\n`;
|
|
}
|
|
if (!Array.isArray(messages)) return `${raw}\n`;
|
|
return messages.map((m) => message(session, m ?? {})).join("");
|
|
}
|
|
|
|
/** Starts following the app of a Run tab about to launch `config`; the result decodes its stdout. */
|
|
export function flutterMachineOutput(session: string, config: RunConfig): TestStdout {
|
|
useFlutterRunApps.setState({
|
|
[session]: {
|
|
session,
|
|
name: config.name,
|
|
appId: null,
|
|
device: null,
|
|
started: false,
|
|
supportsRestart: false,
|
|
live: true,
|
|
startedAt: Date.now(),
|
|
},
|
|
});
|
|
return new TestStdout(
|
|
() => undefined,
|
|
(raw) => machineLine(session, raw),
|
|
);
|
|
}
|