Skip to content

Commit b26ce93

Browse files
committed
Fix CMake, Ninja and Toolchain when switching SDK
Signed-off-by: William Vinnicombe <[email protected]>
1 parent 6ff006e commit b26ce93

File tree

1 file changed

+64
-47
lines changed

1 file changed

+64
-47
lines changed

src/utils/vscodeConfigUtil.mts

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,16 @@ async function updateTasksFile(
8989
await writeFile(tasksFile, content, "utf8");
9090
}
9191

92-
/**
93-
* Replication of the python function in pico_project.py
94-
*
95-
* @param toolchainVersion The toolchain version to create the path for.
96-
* @returns The path to the toolchain.
97-
*/
98-
function relativeToolchainPath(toolchainVersion: string): string {
99-
return `/.pico-sdk/toolchain/${toolchainVersion}`;
92+
function buildCMakePath(cmakeVersion: string): string {
93+
return `\${userHome}/.pico-sdk/cmake/${cmakeVersion}/bin/cmake`;
10094
}
10195

102-
/**
103-
* Replication of the python function in pico_project.py
104-
*
105-
* @param toolchainVersion The toolchain version to create the path for.
106-
* @returns The path to the toolchain.
107-
*/
108-
function buildPropertiesToolchainPathBin(toolchainVersion: string): string {
109-
return `\${userHome}${relativeToolchainPath(toolchainVersion)}/bin`;
96+
function buildCMakeHomePath(cmakeVersion: string): string {
97+
return `\${HOME}/.pico-sdk/cmake/${cmakeVersion}/bin/cmake`;
11098
}
11199

112-
function buildCMakePath(cmakeVersion: string): string {
113-
return `\${userHome}/.pico-sdk/cmake/${cmakeVersion}/bin/cmake`;
100+
function buildNinjaHomePath(ninjaVersion: string): string {
101+
return `\${HOME}/.pico-sdk/ninja/${ninjaVersion}/ninja`;
114102
}
115103

116104
async function updateSettingsFile(
@@ -148,59 +136,88 @@ async function updateSettingsFile(
148136
}}/.pico-sdk/toolchain/${newToolchainVersion}`;
149137

150138
// PATH
151-
let newPath = buildPropertiesToolchainPathBin(newToolchainVersion);
152-
if (
153-
(newCMakeVersion && newCMakeVersion !== "cmake") ||
154-
(newNinjaVersion && newNinjaVersion !== "ninja")
155-
) {
156-
if (key.includes("windows")) {
157-
newPath += ";";
158-
} else {
159-
newPath += ":";
139+
// replace env: with env_ before splitting at :
140+
const oldPath = currentValue[key.includes("windows") ? "Path" : "PATH"]
141+
.replaceAll("${env:", "${env_");
142+
Logger.log(`Oldpath ${oldPath}`);
143+
const pathList = oldPath.split(key.includes("windows") ? ";" : ":");
144+
let toolchainIdx = -1;
145+
let cmakeIdx = -1;
146+
let ninjaIdx = -1;
147+
148+
for (let i=0; i < pathList.length; i++) {
149+
pathList[i] = pathList[i].replaceAll("${env_", "${env:");
150+
Logger.log(pathList[i]);
151+
const item = pathList[i];
152+
if (item.includes(".pico-sdk/toolchain")) {
153+
toolchainIdx = i;
154+
}
155+
if (item.includes(".pico-sdk/cmake")) {
156+
cmakeIdx = i;
157+
}
158+
if (item.includes(".pico-sdk/ninja")) {
159+
ninjaIdx = i;
160160
}
161161
}
162+
163+
Logger.log(`PathList ${pathList.join(" - ")}`);
164+
165+
pathList[toolchainIdx] = currentValue["PICO_TOOLCHAIN_PATH"] + "/bin";
166+
162167
if (newCMakeVersion && newCMakeVersion !== "cmake") {
163-
if (newCMakeVersion !== "cmake" && !newCMakeVersion.includes("/")) {
164-
newPath += `\${${
168+
let newCmakePath = "";
169+
if (!newCMakeVersion.includes("/")) {
170+
newCmakePath = `\${${
165171
key.includes("windows") ? "env:USERPROFILE" : "env:HOME"
166172
}}/.pico-sdk/cmake/${newCMakeVersion}/bin`;
167-
} else if (newCMakeVersion.includes("/")) {
168-
newPath += dirname(newCMakeVersion);
173+
} else {
174+
newCmakePath = dirname(newCMakeVersion);
169175
}
170-
171-
if (key.includes("windows")) {
172-
newPath += ";";
176+
if (cmakeIdx > 0) {
177+
pathList[cmakeIdx] = newCmakePath;
173178
} else {
174-
newPath += ":";
179+
pathList.splice(pathList.length - 1, 0, newCmakePath);
175180
}
176181
}
177-
if (
178-
newNinjaVersion &&
179-
newNinjaVersion !== "ninja" &&
180-
!newNinjaVersion.includes("/")
181-
) {
182-
newPath += `\${${
183-
key.includes("windows") ? "env:USERPROFILE" : "env:HOME"
184-
}}/.pico-sdk/ninja/${newNinjaVersion}`;
185-
} else if (newNinjaVersion && newNinjaVersion.includes("/")) {
186-
newPath += dirname(newNinjaVersion);
182+
183+
if (newNinjaVersion && newNinjaVersion !== "ninja") {
184+
let newNinjaPath = "";
185+
if (!newNinjaVersion.includes("/")) {
186+
newNinjaPath = `\${${
187+
key.includes("windows") ? "env:USERPROFILE" : "env:HOME"
188+
}}/.pico-sdk/ninja/${newNinjaVersion}`;
189+
} else {
190+
newNinjaPath = dirname(newNinjaVersion);
191+
}
192+
if (ninjaIdx > 0) {
193+
pathList[ninjaIdx] = newNinjaPath;
194+
} else {
195+
pathList.splice(pathList.length - 1, 0, newNinjaPath);
196+
}
187197
}
188198

199+
const newPath = pathList.join(key.includes("windows") ? ";" : ":");
189200
currentValue[key.includes("windows") ? "Path" : "PATH"] = newPath;
190201

191202
await config.update(key, currentValue, null);
192203
}
193204

194205
if (newNinjaVersion) {
195-
await config.update(SettingsKey.ninjaPath, newNinjaVersion, null);
206+
await config.update(
207+
"raspberry-pi-pico." + SettingsKey.ninjaPath,
208+
buildNinjaHomePath(newNinjaVersion), null
209+
);
196210
}
197211
if (newCMakeVersion) {
198212
await config.update(
199213
"cmake.cmakePath",
200214
buildCMakePath(newCMakeVersion),
201215
null
202216
);
203-
await config.update(SettingsKey.cmakePath, newCMakeVersion, null);
217+
await config.update(
218+
"raspberry-pi-pico." + SettingsKey.cmakePath,
219+
buildCMakeHomePath(newCMakeVersion), null
220+
);
204221
}
205222
}
206223

0 commit comments

Comments
 (0)