From 814a8ef20ec8c04aa71fc6b01b021df78570ba35 Mon Sep 17 00:00:00 2001 From: Philipp Polterauer <45892981+PhilippPolterauer@users.noreply.github.com> Date: Sun, 11 Feb 2024 03:39:48 +0100 Subject: [PATCH] added ${workspaceFolder} handling to rosSetupScript (#1275) * added ${workspaceFolder} handling to rosSetupScript * replaced errorMessage with outputchannel --- src/extension.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index a1e0d728..5c8bab48 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -240,7 +240,7 @@ async function activateEnvironment(context: vscode.ExtensionContext) { // http://www.ros.org/reps/rep-0149.html#environment-variables // Learn more about ROS_VERSION definition. selectROSApi(env.ROS_VERSION); - + // Do this again, after the build tool has been determined. await sourceRosAndWorkspace(); @@ -322,8 +322,19 @@ async function sourceRosAndWorkspace(): Promise { let attemptWorkspaceDiscovery = true; if (rosSetupScript) { + // Regular expression to match '${workspaceFolder}' + const regex = "\$\{workspaceFolder\}"; + if (rosSetupScript.includes(regex)) { + if (vscode.workspace.workspaceFolders.length === 1) { + // Replace all occurrences of '${workspaceFolder}' with the workspace string + rosSetupScript = rosSetupScript.replace(regex, vscode.workspace.workspaceFolders[0].uri.fsPath); + } else { + outputChannel.appendLine(`Multiple or no workspaces found, but the ROS setup script setting \"ros.rosSetupScript\" is configured with '${rosSetupScript}'`); + } + } + // Try to support cases where the setup script doesn't make sense on different environments, such as host vs container. - if (await pfs.exists(rosSetupScript)){ + if (await pfs.exists(rosSetupScript)) { try { newEnv = await ros_utils.sourceSetupFile(rosSetupScript, newEnv); @@ -335,14 +346,13 @@ async function sourceRosAndWorkspace(): Promise { } } } - + if (attemptWorkspaceDiscovery) { let distro = config.get("distro", ""); // Is there a distro defined either by setting or environment? outputChannel.appendLine(`Current ROS_DISTRO environment variable: ${process.env.ROS_DISTRO}`); - if (!distro) - { + if (!distro) { // No? Try to find one. const installedDistros = await ros_utils.getDistros(); if (!installedDistros.length) { @@ -351,7 +361,7 @@ async function sourceRosAndWorkspace(): Promise { throw new Error("ROS has not been found on this system."); } else if (installedDistros.length === 1) { outputChannel.appendLine(`Only one distro, selecting ${installedDistros[0]}`); - + // if there is only one distro installed, directly choose it config.update("distro", installedDistros[0]); distro = installedDistros[0]; @@ -420,7 +430,7 @@ async function sourceRosAndWorkspace(): Promise { if (await pfs.exists(wsSetupScript)) { outputChannel.appendLine(`Workspace overlay path: ${wsSetupScript}`); - + try { newEnv = await ros_utils.sourceSetupFile(wsSetupScript, newEnv); } catch (_err) {