Skip to content

Commit 6b31f8d

Browse files
authored
Add support for looking into PICO_BOARD_HEADER_DIRS (#123)
* Add support for looking into PICO_BOARD_HEADER_DIRS custom board header files * Add support for PICO_BOARD_HEADER_DIRS as a semi-colon separated path list
1 parent 8424972 commit 6b31f8d

File tree

1 file changed

+62
-7
lines changed

1 file changed

+62
-7
lines changed

src/commands/switchBoard.mts

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import {
1111
cmakeGetSelectedToolchainAndSDKVersions,
1212
cmakeUpdateBoard,
1313
cmakeUpdateSDK,
14+
cmakeGetPicoVar,
1415
} from "../utils/cmakeUtil.mjs";
1516
import { join } from "path";
17+
import { resolve } from "path";
18+
import { normalize } from "path";
1619
import { compareLt } from "../utils/semverUtil.mjs";
1720
import type UI from "../ui.mjs";
1821
import { updateVSCodeStaticConfigs } from "../utils/vscodeConfigUtil.mjs";
@@ -32,20 +35,74 @@ export default class SwitchBoardCommand extends Command {
3235
public static async askBoard(sdkVersion: string):
3336
Promise<[string, boolean] | undefined> {
3437
const quickPickItems: string[] = ["pico", "pico_w"];
38+
const workspaceFolder = workspace.workspaceFolders?.[0];
3539

3640
if (!compareLt(sdkVersion, "2.0.0")) {
3741
quickPickItems.push("pico2");
3842
}
39-
4043
if (!compareLt(sdkVersion, "2.1.0")) {
4144
quickPickItems.push("pico2_w");
4245
}
4346

4447
const sdkPath = buildSDKPath(sdkVersion);
48+
const boardHeaderDirList = [];
49+
50+
if(workspaceFolder !== undefined) {
51+
const ws = workspaceFolder.uri.fsPath;
52+
const cMakeCachePath = join(ws, "build","CMakeCache.txt");
53+
54+
let picoBoardHeaderDirs = cmakeGetPicoVar(
55+
cMakeCachePath,
56+
"PICO_BOARD_HEADER_DIRS");
57+
58+
if(picoBoardHeaderDirs){
59+
if(picoBoardHeaderDirs.startsWith("'")){
60+
const substrLen = picoBoardHeaderDirs.length-1;
61+
picoBoardHeaderDirs = picoBoardHeaderDirs.substring(1,substrLen);
62+
}
4563

46-
readdirSync(join(sdkPath, "src", "boards", "include", "boards")).forEach(
47-
file => {
48-
quickPickItems.push(file.split(".")[0]);
64+
const picoBoardHeaderDirList = picoBoardHeaderDirs.split(";");
65+
picoBoardHeaderDirList.forEach(
66+
item => {
67+
let boardPath = resolve(item);
68+
const normalized = normalize(item);
69+
70+
//If path is not absolute, join workspace path
71+
if(boardPath !== normalized){
72+
boardPath = join(ws,normalized);
73+
}
74+
75+
if(existsSync(boardPath)){
76+
boardHeaderDirList.push(boardPath);
77+
}
78+
}
79+
);
80+
}
81+
}
82+
83+
const systemBoardHeaderDir =
84+
join(sdkPath,"src", "boards", "include","boards");
85+
86+
boardHeaderDirList.push(systemBoardHeaderDir);
87+
88+
interface IBoardFile{
89+
[key: string]: string;
90+
};
91+
92+
const boardFiles:IBoardFile = {};
93+
94+
boardHeaderDirList.forEach(
95+
path =>{
96+
readdirSync(path).forEach(
97+
file => {
98+
const fullFilename = join(path, file);
99+
if(fullFilename.endsWith(".h")) {
100+
const boardName = file.split(".")[0];
101+
boardFiles[boardName] = fullFilename;
102+
quickPickItems.push(boardName);
103+
}
104+
}
105+
)
49106
}
50107
);
51108

@@ -60,9 +117,7 @@ export default class SwitchBoardCommand extends Command {
60117
}
61118

62119
// Check that board doesn't have an RP2040 on it
63-
const data = readFileSync(
64-
join(sdkPath, "src", "boards", "include", "boards", `${board}.h`)
65-
)
120+
const data = readFileSync(boardFiles[board])
66121

67122
if (data.includes("rp2040")) {
68123

0 commit comments

Comments
 (0)