Skip to content

Commit e54534b

Browse files
committed
Support for auto-configure cmake when snippets are selected
Signed-off-by: paulober <[email protected]>
1 parent cec99da commit e54534b

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/utils/cmakeUtil.mts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import State from "../state.mjs";
1818
import {
1919
generateCustomZephyrEnv,
2020
getBoardFromZephyrProject,
21+
zephyrGetSelectedSnippets,
2122
} from "./setupZephyr.mjs";
2223

2324
export const CMAKE_DO_NOT_EDIT_HEADER_PREFIX =
@@ -248,12 +249,21 @@ export async function configureCmakeNinja(
248249

249250
return false;
250251
}
251-
const zephyrCommand = `${
252+
let zephyrCommand = `${
252253
process.env.ComSpec?.endsWith("powershell.exe") ? "&" : ""
253254
}"${westPath}" build --cmake-only -b ${
254255
zephyrBoard ?? ""
255256
} -d "${buildDir}" "${folder.fsPath}"`;
256257

258+
// check for selected snippets and include for cmake configuration
259+
if (isZephyrProject) {
260+
const snippets = await zephyrGetSelectedSnippets(folder);
261+
262+
for (const snippet of snippets) {
263+
zephyrCommand += ` -S ${snippet}`;
264+
}
265+
}
266+
257267
await new Promise<void>((resolve, reject) => {
258268
// use exec to be able to cancel the process
259269
const child = exec(

src/utils/setupZephyr.mts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ async function showNoWgetError(): Promise<void> {
574574
"wget not found in Path. Please install wget and ensure " +
575575
"it is available in Path. " +
576576
"See the Zephyr notes in the pico-vscode README for guidance.",
577+
{
578+
modal: true,
579+
},
577580
"Open README"
578581
);
579582
if (response === "Open README") {
@@ -593,7 +596,7 @@ async function checkMacosLinuxDeps(): Promise<boolean> {
593596

594597
const wget = await which("wget", { nothrow: true });
595598
if (!wget) {
596-
await showNoWgetError();
599+
void showNoWgetError();
597600

598601
return false;
599602
}
@@ -628,7 +631,7 @@ async function checkWindowsDeps(): Promise<boolean> {
628631

629632
const wget = await which("wget", { nothrow: true });
630633
if (!wget) {
631-
await showNoWgetError();
634+
void showNoWgetError();
632635

633636
return false;
634637
}
@@ -1718,3 +1721,48 @@ export async function zephyrVerifyCMakeCache(
17181721
return;
17191722
}
17201723
}
1724+
1725+
export async function zephyrGetSelectedSnippets(
1726+
workspaceUri: Uri
1727+
): Promise<string[]> {
1728+
const snippetsUri = Uri.joinPath(workspaceUri, ".vscode", "tasks.json");
1729+
1730+
// search for "Compile Project" get every i+1 where i is an index and args[i]=="-S" || "--snippet"
1731+
try {
1732+
await workspace.fs.stat(snippetsUri);
1733+
1734+
const td = new TextDecoder("utf-8");
1735+
const tasksJson = JSON.parse(
1736+
td.decode(await workspace.fs.readFile(snippetsUri))
1737+
) as { tasks: ITask[] };
1738+
1739+
const compileTask = tasksJson.tasks.find(
1740+
t => t.label === "Compile Project"
1741+
);
1742+
if (compileTask === undefined) {
1743+
return [];
1744+
}
1745+
1746+
const selectedSnippets: string[] = [];
1747+
for (let i = 0; i < compileTask.args.length; i++) {
1748+
if (compileTask.args[i] === "-S" || compileTask.args[i] === "--snippet") {
1749+
if (i + 1 < compileTask.args.length) {
1750+
selectedSnippets.push(compileTask.args[i + 1]);
1751+
}
1752+
}
1753+
}
1754+
1755+
return selectedSnippets;
1756+
} catch (error) {
1757+
Logger.warn(
1758+
LoggerSource.zephyrSetup,
1759+
`Failed to read tasks.json file: ${unknownErrorToString(error)}`
1760+
);
1761+
void window.showWarningMessage(
1762+
"Failed to read tasks.json file. " +
1763+
"Make sure the file exists and has a Compile Project task."
1764+
);
1765+
1766+
return [];
1767+
}
1768+
}

0 commit comments

Comments
 (0)