fix: 修复 OpenCode 运行时启动与本地打包
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { existsSync } from 'node:fs';
|
||||
import { delimiter, dirname, isAbsolute, join, resolve } from 'node:path';
|
||||
import { dirname, isAbsolute, join, resolve } from 'node:path';
|
||||
|
||||
export type PythonRuntimeSource = 'configured' | 'bundled' | 'system';
|
||||
|
||||
@@ -9,6 +9,12 @@ export type PythonRuntime = {
|
||||
source: PythonRuntimeSource;
|
||||
};
|
||||
|
||||
export type UvRuntime = {
|
||||
executable: string;
|
||||
binDir: string;
|
||||
source: 'bundled' | 'system';
|
||||
};
|
||||
|
||||
export type PythonRuntimeOptions = {
|
||||
isPackaged: boolean;
|
||||
resourcesPath: string;
|
||||
@@ -18,6 +24,8 @@ export type PythonRuntimeOptions = {
|
||||
configuredPath?: string;
|
||||
};
|
||||
|
||||
export type UvRuntimeOptions = Omit<PythonRuntimeOptions, 'configuredPath'>;
|
||||
|
||||
function configuredRuntime(configuredPath: string | undefined, appPath: string): PythonRuntime | null {
|
||||
const trimmed = configuredPath?.trim();
|
||||
if (!trimmed) return null;
|
||||
@@ -33,7 +41,9 @@ function bundledExecutable(resourcesPath: string, platform: NodeJS.Platform): st
|
||||
}
|
||||
|
||||
export function resolvePythonRuntime(options: PythonRuntimeOptions): PythonRuntime {
|
||||
const configured = configuredRuntime(options.configuredPath, options.appPath);
|
||||
const configured = options.isPackaged
|
||||
? null
|
||||
: configuredRuntime(options.configuredPath, options.appPath);
|
||||
if (configured) return configured;
|
||||
|
||||
const executable = bundledExecutable(options.resourcesPath, options.platform);
|
||||
@@ -52,17 +62,71 @@ export function resolvePythonRuntime(options: PythonRuntimeOptions): PythonRunti
|
||||
};
|
||||
}
|
||||
|
||||
function uvExecutableName(platform: NodeJS.Platform): string {
|
||||
return platform === 'win32' ? 'uv.exe' : 'uv';
|
||||
}
|
||||
|
||||
export function resolveUvRuntime(options: UvRuntimeOptions): UvRuntime {
|
||||
const executable = options.isPackaged
|
||||
? join(options.resourcesPath, 'bin', uvExecutableName(options.platform))
|
||||
: join(
|
||||
options.appPath,
|
||||
'resources',
|
||||
'bin',
|
||||
`${options.platform}-${options.arch}`,
|
||||
uvExecutableName(options.platform),
|
||||
);
|
||||
if (existsSync(executable)) {
|
||||
return { executable, binDir: dirname(executable), source: 'bundled' };
|
||||
}
|
||||
|
||||
if (options.isPackaged) {
|
||||
throw new Error(`Bundled uv runtime is missing: ${executable}`);
|
||||
}
|
||||
|
||||
return { executable: 'uv', binDir: '', source: 'system' };
|
||||
}
|
||||
|
||||
function prependRuntimeBin(
|
||||
environment: Record<string, string>,
|
||||
binDir: string,
|
||||
platform: NodeJS.Platform,
|
||||
): Record<string, string> {
|
||||
if (!binDir) return environment;
|
||||
const result = { ...environment };
|
||||
const existingKey = Object.keys(result).find((key) => key.toLowerCase() === 'path');
|
||||
const pathKey = platform === 'win32' && existingKey ? existingKey : 'PATH';
|
||||
const existingPath = result[pathKey];
|
||||
const separator = platform === 'win32' ? ';' : ':';
|
||||
result[pathKey] = existingPath ? `${binDir}${separator}${existingPath}` : binDir;
|
||||
return result;
|
||||
}
|
||||
|
||||
export function prependPythonToPath(
|
||||
environment: Record<string, string>,
|
||||
runtime: PythonRuntime,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
): Record<string, string> {
|
||||
const result = { ...environment, NIANCODE_PYTHON_PATH: runtime.executable };
|
||||
if (!runtime.binDir) return result;
|
||||
|
||||
const existingKey = Object.keys(result).find((key) => key.toLowerCase() === 'path');
|
||||
const pathKey = platform === 'win32' && existingKey ? existingKey : 'PATH';
|
||||
const existingPath = result[pathKey];
|
||||
result[pathKey] = existingPath ? `${runtime.binDir}${delimiter}${existingPath}` : runtime.binDir;
|
||||
return result;
|
||||
return prependRuntimeBin(
|
||||
{ ...environment, NIANCODE_PYTHON_PATH: runtime.executable },
|
||||
runtime.binDir,
|
||||
platform,
|
||||
);
|
||||
}
|
||||
|
||||
export function prependManagedRuntimesToPath(
|
||||
environment: Record<string, string>,
|
||||
pythonRuntime?: PythonRuntime,
|
||||
uvRuntime?: UvRuntime,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
): Record<string, string> {
|
||||
const withPython = pythonRuntime
|
||||
? prependPythonToPath(environment, pythonRuntime, platform)
|
||||
: { ...environment };
|
||||
if (!uvRuntime) return withPython;
|
||||
return prependRuntimeBin(
|
||||
{ ...withPython, NIANCODE_UV_PATH: uvRuntime.executable },
|
||||
uvRuntime.binDir,
|
||||
platform,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user