Skip to content

Commit b0099d0

Browse files
committed
Use project ninja and cmake for zephyr setup on project opening
Signed-off-by: paulober <[email protected]>
1 parent 389dba0 commit b0099d0

File tree

3 files changed

+114
-25
lines changed

3 files changed

+114
-25
lines changed

src/extension.mts

Lines changed: 94 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
259259
return;
260260
}
261261

262-
void commands.executeCommand(
262+
await commands.executeCommand(
263263
"setContext",
264264
ContextKeys.isRustProject,
265265
isRustProject
@@ -315,15 +315,102 @@ export async function activate(context: ExtensionContext): Promise<void> {
315315
return;
316316
}
317317

318+
const cmakePath = settings.getString(SettingsKey.cmakePath);
319+
// TODO: or auto upgrade to latest cmake
320+
if (cmakePath === undefined) {
321+
Logger.error(
322+
LoggerSource.extension,
323+
"CMake path not set in settings. Cannot setup Zephyr project."
324+
);
325+
void window.showErrorMessage(
326+
"CMake path not set in settings. Cannot setup Zephyr project."
327+
);
328+
await commands.executeCommand(
329+
"setContext",
330+
ContextKeys.isPicoProject,
331+
false
332+
);
333+
334+
return;
335+
}
336+
let cmakeVersion = "";
337+
if (cmakePath && cmakePath.includes("/.pico-sdk/cmake")) {
338+
const version = /\/\.pico-sdk\/cmake\/([v.0-9A-Za-z-]+)\//.exec(
339+
cmakePath
340+
)?.[1];
341+
342+
if (version === undefined) {
343+
Logger.error(
344+
LoggerSource.extension,
345+
"Failed to get CMake version from path in the settings."
346+
);
347+
await commands.executeCommand(
348+
"setContext",
349+
ContextKeys.isPicoProject,
350+
false
351+
);
352+
353+
return;
354+
}
355+
356+
cmakeVersion = version;
357+
}
358+
359+
const ninjaPath = settings.getString(SettingsKey.ninjaPath);
360+
let ninjaVersion = "";
361+
if (ninjaPath === undefined) {
362+
Logger.error(
363+
LoggerSource.extension,
364+
"Ninja path not set in settings. Cannot setup Zephyr project."
365+
);
366+
void window.showErrorMessage(
367+
"Ninja path not set in settings. Cannot setup Zephyr project."
368+
);
369+
await commands.executeCommand(
370+
"setContext",
371+
ContextKeys.isPicoProject,
372+
false
373+
);
374+
375+
return;
376+
} else if (ninjaPath && ninjaPath.includes("/.pico-sdk/ninja")) {
377+
const version = /\/\.pico-sdk\/ninja\/([v.0-9]+)\//.exec(
378+
ninjaPath
379+
)?.[1];
380+
if (version === undefined) {
381+
Logger.error(
382+
LoggerSource.extension,
383+
"Failed to get Ninja version from path in the settings."
384+
);
385+
await commands.executeCommand(
386+
"setContext",
387+
ContextKeys.isPicoProject,
388+
false
389+
);
390+
391+
return;
392+
}
393+
394+
ninjaVersion = version;
395+
}
396+
318397
// TODO: read selected ninja and cmake versions from project
319398
const result = await setupZephyr({
320399
extUri: context.extensionUri,
321-
cmakeMode: 4,
322-
cmakePath: "",
323-
cmakeVersion: latest[1].cmake,
324-
ninjaMode: 4,
325-
ninjaPath: "",
326-
ninjaVersion: latest[1].ninja,
400+
cmakeMode: cmakeVersion !== "" ? 2 : 3,
401+
cmakePath:
402+
cmakeVersion !== ""
403+
? ""
404+
: cmakePath.replace(HOME_VAR, homedir().replaceAll("\\", "/")) ??
405+
"",
406+
cmakeVersion: cmakeVersion,
407+
ninjaMode: ninjaVersion !== "" ? 2 : 3,
408+
ninjaPath:
409+
ninjaVersion !== ""
410+
? ""
411+
: ninjaPath.replace(HOME_VAR, homedir().replaceAll("\\", "/")) ??
412+
"",
413+
ninjaVersion: ninjaVersion,
327414
});
328415
if (result === undefined) {
329416
void window.showErrorMessage(

src/utils/githubREST.mts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,27 @@ export async function getNinjaReleases(): Promise<string[]> {
308308
// sort (they are in form vX.Y.Z) and some are
309309
// release-DDMMYYYY (these are the very old ones)
310310
return releases.then(rel =>
311-
rel.sort((a, b) => {
312-
const aParts = a
313-
.replace(/^v/, "")
314-
.split(/[-.]/)
315-
.map(n => parseInt(n, 10));
316-
const bParts = b
317-
.replace(/^v/, "")
318-
.split(/[-.]/)
319-
.map(n => parseInt(n, 10));
320-
for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) {
321-
const aPart = aParts[i] ?? 0;
322-
const bPart = bParts[i] ?? 0;
323-
if (aPart !== bPart) {
324-
return bPart - aPart; // descending order
311+
rel
312+
.filter(v => !v.startsWith("release"))
313+
.sort((a, b) => {
314+
const aParts = a
315+
.replace(/^v/, "")
316+
.split(/[-.]/)
317+
.map(n => parseInt(n, 10));
318+
const bParts = b
319+
.replace(/^v/, "")
320+
.split(/[-.]/)
321+
.map(n => parseInt(n, 10));
322+
for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) {
323+
const aPart = aParts[i] ?? 0;
324+
const bPart = bParts[i] ?? 0;
325+
if (aPart !== bPart) {
326+
return bPart - aPart; // descending order
327+
}
325328
}
326-
}
327329

328-
return 0;
329-
})
330+
return 0;
331+
})
330332
);
331333
}
332334

src/utils/projectGeneration/projectZephyr.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ async function generateVSCodeConfig(
206206
const posixHomedir = homedir().replaceAll("\\", "/");
207207
if (ninjaPath.length > 0) {
208208
const ninjaDir = dirnamePosix(ninjaPath);
209-
settings[`${extensionName}.ninjaPath`] = ninjaDir.replace(
209+
settings[`${extensionName}.ninjaPath`] = ninjaPath.replace(
210210
posixHomedir,
211211
HOME_VAR
212212
);

0 commit comments

Comments
 (0)