0bfc5768cc
让Linux no-bundle smoke不再误用Windows随包资源目标。 统一dev与release的Cargo目标和feature解析,补充跨平台回归测试。 同步随包资源staging技术方案中的实际构建目标约定。
296 lines
9.4 KiB
JavaScript
296 lines
9.4 KiB
JavaScript
import { resolve } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
import { buildLocalRustProcessEnv } from '../../../scripts/dev.mjs';
|
|
import {
|
|
readCargoTarget,
|
|
resolveEditorFeatures,
|
|
withDefaultCargoFeatures,
|
|
} from './cargo-features.mjs';
|
|
import {
|
|
readAgcDevEndpoint,
|
|
resolveAgcDevEndpoint,
|
|
withAgcDevEndpointEnv,
|
|
} from './dev-port.mjs';
|
|
import {
|
|
prepareBundledResources,
|
|
supportedHostTarget,
|
|
} from './prepare-bundled-resources.mjs';
|
|
import {
|
|
isAiGameCreatorServer,
|
|
preflightExistingVite,
|
|
readChildFailure,
|
|
readExistingViteServer,
|
|
spawnChild,
|
|
stopChild,
|
|
terminateChildTree,
|
|
waitForChildTermination,
|
|
} from './start-dev-stack.mjs';
|
|
|
|
const appRoot = fileURLToPath(new URL('..', import.meta.url));
|
|
const repoRoot = resolve(appRoot, '../..');
|
|
const tauriCliPath = resolve(repoRoot, 'node_modules/@tauri-apps/cli/tauri.js');
|
|
const AGC_DESIGN_DEBUG_ENV = 'GENARRATIVE_AGC_DESIGN_DEBUG';
|
|
const designDebugEnabled =
|
|
process.env[AGC_DESIGN_DEBUG_ENV]?.trim() === '0' ? '0' : '1';
|
|
|
|
function buildTauriArguments(argv, devUrl = readAgcDevEndpoint().url) {
|
|
const args = [...argv];
|
|
const configOverride = JSON.stringify({
|
|
build: { devUrl, beforeDevCommand: '' },
|
|
});
|
|
const separatorIndex = args.indexOf('--');
|
|
if (separatorIndex < 0) {
|
|
return ['dev', ...args, '--config', configOverride];
|
|
}
|
|
const separatedArguments = args.slice(separatorIndex);
|
|
if (separatedArguments[1] !== '--') {
|
|
separatedArguments.unshift('--');
|
|
}
|
|
return [
|
|
'dev',
|
|
...args.slice(0, separatorIndex),
|
|
'--config',
|
|
configOverride,
|
|
...separatedArguments,
|
|
];
|
|
}
|
|
|
|
// 开发和发行构建使用同一平台编辑器 feature 集合。
|
|
// 可用 AGC_DEV_CARGO_FEATURES(逗号分隔)覆盖,传空串即关闭;
|
|
// 命令行显式 `--features` / `-f` 优先于环境变量(cargo 实际收到的是命令行参数)。
|
|
// 入参是「最终交给 Tauri 的参数」:默认平台由其中的显式 `--target` 决定,未指定才是宿主平台。
|
|
function readDevCargoFeatures(argv = []) {
|
|
return resolveEditorFeatures({
|
|
argv,
|
|
target: readCargoTarget(argv) ?? process.platform,
|
|
});
|
|
}
|
|
|
|
function withDevCargoFeatures(argv, features = readDevCargoFeatures(argv)) {
|
|
return withDefaultCargoFeatures(argv, features);
|
|
}
|
|
|
|
/// 随包资源必须在 Tauri 之前生成:构建脚本只做只读校验,不再生成资源。
|
|
/// 命中缓存的重复调用不写任何文件,因此每次 dev 启动都会先跑一次。
|
|
/// `argv` 是「最终交给 Tauri 的参数」;staging 目标与本次 cargo 构建目标同源:
|
|
/// 显式 `--target` 优先,未指定时回落宿主目标;宿主平台不受声明覆盖(如 Linux)时保持跳过,
|
|
/// 不为其新增随包资源准备。`features` 省略时按同一份参数解析,不允许「cargo 一套、staging 另一套」。
|
|
function prepareBundledResourcesBeforeTauri(
|
|
features,
|
|
argv = [],
|
|
{
|
|
prepare = prepareBundledResources,
|
|
log = console.log,
|
|
hostTarget = supportedHostTarget(),
|
|
} = {},
|
|
) {
|
|
const target = readCargoTarget(argv) ?? hostTarget;
|
|
if (!target) {
|
|
log(
|
|
'[ai-game-creator-shell] 当前平台不受随包资源声明覆盖,跳过随包资源准备',
|
|
);
|
|
return;
|
|
}
|
|
const summaries = prepare({
|
|
target,
|
|
features: new Set(features ?? readDevCargoFeatures(argv)),
|
|
log: (line) => log(`[ai-game-creator-shell] ${line}`),
|
|
});
|
|
for (const summary of summaries) {
|
|
log(`[ai-game-creator-shell] ${summary}`);
|
|
}
|
|
}
|
|
|
|
function spawnTauriCli(argv, { env = process.env } = {}) {
|
|
return spawnChild(process.execPath, [tauriCliPath, ...argv], {
|
|
cwd: appRoot,
|
|
env,
|
|
shell: false,
|
|
});
|
|
}
|
|
|
|
/// Tauri dev 的 Cargo 直接继承启动器环境,用户级 / 仓库级 Cargo 配置里的
|
|
/// `rustc-wrapper`(本地常见为 sccache)会在这里生效。本地 sccache daemon 状态
|
|
/// 一旦损坏,`cargo` 的首次 rustc 探测就会失败并阻断整个 AGC 启动;因此这里复用
|
|
/// `npm run dev` 的本地 Rust 环境规则,由脚本而不是本机 Cargo 配置决定 wrapper。
|
|
function buildTauriDevProcessEnv(endpoint, env = process.env) {
|
|
return buildLocalRustProcessEnv({
|
|
...withAgcDevEndpointEnv(endpoint, env),
|
|
[AGC_DESIGN_DEBUG_ENV]: designDebugEnabled,
|
|
});
|
|
}
|
|
|
|
async function runTauriDev(
|
|
argv = process.argv.slice(2),
|
|
{
|
|
resolveDevEndpoint = resolveAgcDevEndpoint,
|
|
preflight = preflightExistingVite,
|
|
prepareFrontend = prepareFrontendDev,
|
|
spawnCli = spawnTauriCli,
|
|
waitForCli = waitForChildTermination,
|
|
terminateTree = terminateChildTree,
|
|
prepareResources = prepareBundledResourcesBeforeTauri,
|
|
} = {},
|
|
) {
|
|
const endpoint = await resolveDevEndpoint();
|
|
await preflight({ endpoint });
|
|
|
|
let child = null;
|
|
let frontendChild = null;
|
|
const preparationAbort = new AbortController();
|
|
let resolveShutdown;
|
|
let shutdownSignal = '';
|
|
let repeatedSignal = false;
|
|
const shutdownRequested = new Promise((resolveRequest) => {
|
|
resolveShutdown = resolveRequest;
|
|
});
|
|
const signalHandlers = new Map();
|
|
|
|
for (const signal of ['SIGINT', 'SIGTERM']) {
|
|
const handler = () => {
|
|
if (!shutdownSignal) {
|
|
shutdownSignal = signal;
|
|
stopChild(child, 'SIGTERM');
|
|
stopChild(frontendChild, 'SIGTERM');
|
|
preparationAbort.abort();
|
|
resolveShutdown(signal);
|
|
return;
|
|
}
|
|
repeatedSignal = true;
|
|
stopChild(child, 'SIGKILL');
|
|
stopChild(frontendChild, 'SIGKILL');
|
|
};
|
|
signalHandlers.set(signal, handler);
|
|
process.on(signal, handler);
|
|
}
|
|
|
|
try {
|
|
// 目标与 feature 都从「最终交给 Tauri 的参数」解析:启动器自己的 `--` 之后是应用参数,
|
|
// 不能参与解析;注入默认 feature 也不改变这份解析结果。
|
|
const devTauriArguments = buildTauriArguments(argv, endpoint.url);
|
|
const devFeatures = readDevCargoFeatures(devTauriArguments);
|
|
prepareResources(devFeatures, devTauriArguments);
|
|
// Windows 下 Vite 会监听资源目录;必须在启动前完成目录替换,避免 rename 被占用。
|
|
const preparation = prepareFrontend(endpoint, {
|
|
signal: preparationAbort.signal,
|
|
onChild(frontend) {
|
|
frontendChild = frontend;
|
|
},
|
|
});
|
|
const prepared = await Promise.race([
|
|
preparation.then(() => true),
|
|
shutdownRequested.then(() => false),
|
|
]);
|
|
if (!prepared || shutdownSignal) return 1;
|
|
const tauriArguments = buildTauriArguments(
|
|
withDevCargoFeatures(argv, devFeatures),
|
|
endpoint.url,
|
|
);
|
|
child = spawnCli(tauriArguments, {
|
|
env: buildTauriDevProcessEnv(endpoint),
|
|
});
|
|
const childResult = waitForCli(child);
|
|
const outcome = await Promise.race([
|
|
childResult.then((failure) => ({ type: 'exit', failure })),
|
|
shutdownRequested.then((signal) => ({ type: 'signal', signal })),
|
|
...(frontendChild
|
|
? [
|
|
waitForChildTermination(frontendChild).then((failure) => ({
|
|
type: 'frontend-exit',
|
|
failure,
|
|
})),
|
|
]
|
|
: []),
|
|
]);
|
|
const cleanup = await terminateTree(child, {
|
|
gracefulTimeoutMs: repeatedSignal ? 0 : 2500,
|
|
});
|
|
if (!cleanup.stopped) {
|
|
console.error(
|
|
'[ai-game-creator-shell] Tauri dev exited, but its process tree could not be fully stopped.',
|
|
);
|
|
return 1;
|
|
}
|
|
|
|
if (outcome.type === 'signal') {
|
|
return 1;
|
|
}
|
|
if (outcome.type === 'frontend-exit') return 1;
|
|
const { failure } = outcome;
|
|
return failure.type === 'error' || failure.signal ? 1 : (failure.code ?? 0);
|
|
} finally {
|
|
for (const [signal, handler] of signalHandlers) {
|
|
process.off(signal, handler);
|
|
}
|
|
preparationAbort.abort();
|
|
if (frontendChild) {
|
|
const cleanup = await terminateTree(frontendChild);
|
|
if (!cleanup.stopped) {
|
|
console.error('[ai-game-creator-shell] 配套开发服务未能完全停止。');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
async function prepareFrontendDev(endpoint, { onChild, signal }) {
|
|
const frontend = spawnChild(
|
|
process.platform === 'win32' ? 'npm.cmd' : 'npm',
|
|
['run', 'agc:serve'],
|
|
{
|
|
cwd: repoRoot,
|
|
env: {
|
|
...withAgcDevEndpointEnv(endpoint),
|
|
},
|
|
},
|
|
);
|
|
onChild(frontend);
|
|
console.log(
|
|
'[ai-game-creator-shell] 正在准备前端与配套后端,完成后启动 Tauri',
|
|
);
|
|
const deadline = Date.now() + 660_000;
|
|
while (Date.now() < deadline) {
|
|
signal.throwIfAborted();
|
|
const failure = readChildFailure(frontend);
|
|
if (failure) {
|
|
throw new Error(
|
|
`配套开发服务退出,前端未就绪:${failure.error?.message ?? failure.signal ?? failure.code}`,
|
|
);
|
|
}
|
|
if (isAiGameCreatorServer(await readExistingViteServer(endpoint))) return;
|
|
await Promise.race([
|
|
new Promise((resolveWait) => setTimeout(resolveWait, 1000)),
|
|
waitForChildTermination(frontend),
|
|
]);
|
|
}
|
|
throw new Error(`等待前端与配套后端就绪超时:${endpoint.url}`);
|
|
}
|
|
|
|
function isDirectModuleExecution() {
|
|
return Boolean(
|
|
process.argv[1] &&
|
|
resolve(process.argv[1]) === fileURLToPath(import.meta.url),
|
|
);
|
|
}
|
|
|
|
export {
|
|
buildTauriArguments,
|
|
buildTauriDevProcessEnv,
|
|
isDirectModuleExecution,
|
|
prepareBundledResourcesBeforeTauri,
|
|
runTauriDev,
|
|
spawnTauriCli,
|
|
withDevCargoFeatures,
|
|
};
|
|
|
|
if (isDirectModuleExecution()) {
|
|
try {
|
|
process.exitCode = await runTauriDev();
|
|
} catch (error) {
|
|
console.error(
|
|
`[ai-game-creator-shell] ${error instanceof Error ? error.message : String(error)}`,
|
|
);
|
|
process.exitCode = 1;
|
|
}
|
|
}
|