修复远程终端识别本地操作系统问题并完善工作流配置 #7
@@ -42,6 +42,10 @@
|
||||
{
|
||||
"protocol": "vscode",
|
||||
"path": "/openProject"
|
||||
},
|
||||
{
|
||||
"protocol": "trae",
|
||||
"path": "/openProject"
|
||||
}
|
||||
],
|
||||
"views": {
|
||||
|
||||
@@ -25,6 +25,43 @@ export default class RemoteContainer {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建打开项目的命令
|
||||
* 在远程环境下,通过本地终端实时检测本地系统类型和 PowerShell 版本
|
||||
* 支持 Windows/Linux/Mac 作为本地系统的所有场景
|
||||
*/
|
||||
private async buildOpenProjectCommand(
|
||||
host: string,
|
||||
hostname: string,
|
||||
port: number,
|
||||
username: string,
|
||||
path: string,
|
||||
devstarDomain?: string
|
||||
): Promise<string> {
|
||||
const baseUrl = `vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}`;
|
||||
const url = devstarDomain ? `${baseUrl}&devstar_domain=${devstarDomain}` : baseUrl;
|
||||
|
||||
// 检测本地系统类型和 PowerShell 版本
|
||||
try {
|
||||
const localSystemInfo = await utils.detectLocalSystemInfo();
|
||||
console.log('本地系统检测结果:', localSystemInfo);
|
||||
|
||||
// 只有本地是 Windows 系统才使用 PowerShell 语法
|
||||
if (localSystemInfo.isWindows) {
|
||||
// PowerShell 需要使用单引号包裹 URL,内部协议用双引号
|
||||
// 格式:code --new-window; code --open-url '"vscode://..."'
|
||||
console.log('使用 PowerShell 语法(单引号嵌套双引号)');
|
||||
return `code --new-window; code --open-url '"${url}"'`;
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('检测本地系统失败,使用默认命令格式:', error);
|
||||
}
|
||||
|
||||
// 默认使用 && 语法(适用于 Linux/macOS)
|
||||
console.log('使用默认命令语法 (&&)');
|
||||
return `code --new-window && code --open-url "${url}"`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 第一次打开远程项目
|
||||
*/
|
||||
@@ -40,29 +77,8 @@ export default class RemoteContainer {
|
||||
devstarDomain = undefined;
|
||||
}
|
||||
|
||||
const semver = require('semver');
|
||||
const powershellVersion = context.globalState.get('powershellVersion');
|
||||
const powershell_semver_compatible_version = semver.coerce(powershellVersion);
|
||||
|
||||
let command = '';
|
||||
if (devstarDomain === undefined) {
|
||||
if (powershellVersion === undefined) {
|
||||
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
||||
} else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
|
||||
command = `code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
||||
} else {
|
||||
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}"`;
|
||||
}
|
||||
} else {
|
||||
if (powershellVersion === undefined) {
|
||||
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
||||
} else if (semver.satisfies(powershell_semver_compatible_version, ">=5.1.26100")) {
|
||||
command = `code --new-window ; code --% --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
||||
} else {
|
||||
command = `code --new-window && code --open-url "vscode://mengning.devstar/openProjectSkippingLoginCheck?host=${host}&hostname=${hostname}&port=${port}&username=${username}&path=${path}&devstar_domain=${devstarDomain}"`;
|
||||
}
|
||||
}
|
||||
|
||||
// 在本地终端实时检测命令格式
|
||||
const command = await this.buildOpenProjectCommand(host, hostname, port, username, path, devstarDomain);
|
||||
terminal.sendText(command);
|
||||
} else {
|
||||
vscode.window.showErrorMessage('无法创建终端,请检查终端是否可用。');
|
||||
|
||||
67
src/utils.ts
67
src/utils.ts
@@ -132,3 +132,70 @@ export async function powershellVersion(): Promise<string> {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 在远程环境下,通过执行本地命令来检测本地系统信息
|
||||
* 不依赖 os.platform(),因为在远程环境下它返回的是远程系统类型
|
||||
*/
|
||||
export async function detectLocalSystemInfo(): Promise<{
|
||||
isWindows: boolean;
|
||||
isLinux: boolean;
|
||||
isMac: boolean;
|
||||
powershellVersion?: string;
|
||||
}> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error('系统检测超时'));
|
||||
}, 3000);
|
||||
|
||||
// 尝试执行 PowerShell 命令来检测是否为 Windows
|
||||
exec('powershell -Command "$PSVersionTable.PSVersion.ToString()"', (error, stdout, stderr) => {
|
||||
clearTimeout(timeout);
|
||||
|
||||
if (!error && stdout && !stderr) {
|
||||
// PowerShell 命令成功执行,说明是 Windows 系统
|
||||
resolve({
|
||||
isWindows: true,
|
||||
isLinux: false,
|
||||
isMac: false,
|
||||
powershellVersion: stdout.trim()
|
||||
});
|
||||
} else {
|
||||
// PowerShell 命令失败,尝试检测 Unix 系统类型
|
||||
exec('uname -s', (unameError, unameStdout) => {
|
||||
if (!unameError && unameStdout) {
|
||||
const osType = unameStdout.trim();
|
||||
resolve({
|
||||
isWindows: false,
|
||||
isLinux: osType === 'Linux',
|
||||
isMac: osType === 'Darwin',
|
||||
powershellVersion: undefined
|
||||
});
|
||||
} else {
|
||||
// 无法检测,返回默认值
|
||||
resolve({
|
||||
isWindows: false,
|
||||
isLinux: false,
|
||||
isMac: false,
|
||||
powershellVersion: undefined
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 在远程环境下,通过本地终端实时检测 PowerShell 版本
|
||||
* 这样可以确保获取到最准确的版本信息,而不依赖于 globalState
|
||||
* @deprecated 使用 detectLocalSystemInfo 替代
|
||||
*/
|
||||
export async function detectLocalPowershellVersion(): Promise<string> {
|
||||
try {
|
||||
const systemInfo = await detectLocalSystemInfo();
|
||||
return systemInfo.powershellVersion || '';
|
||||
} catch (error) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user