Skip to content

Commit 9f84b2c

Browse files
committed
Add SwitchBuildType command
Switches between CMake build types, and displays the current one in the quick access bar Also add board type display in the quick access bar
1 parent 2b4326b commit 9f84b2c

File tree

6 files changed

+129
-8
lines changed

6 files changed

+129
-8
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@
187187
"category": "Raspberry Pi Pico",
188188
"enablement": "raspberry-pi-pico.isPicoProject"
189189
},
190+
{
191+
"command": "raspberry-pi-pico.switchBuildType",
192+
"title": "Switch Build Type",
193+
"category": "Raspberry Pi Pico",
194+
"enablement": "raspberry-pi-pico.isPicoProject"
195+
},
190196
{
191197
"command": "raspberry-pi-pico.importProject",
192198
"title": "Import Pico Project",

src/commands/configureCmake.mts

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { Command } from "./command.mjs";
22
import Logger from "../logger.mjs";
33
import { window, workspace } from "vscode";
4-
import { configureCmakeNinja } from "../utils/cmakeUtil.mjs";
4+
import { cmakeGetPicoVar, configureCmakeNinja } from "../utils/cmakeUtil.mjs";
55
import Settings, { SettingsKey } from "../settings.mjs";
66
import { join } from "path";
77
import { rimraf } from "rimraf";
88
import { unknownErrorToString } from "../utils/errorHelper.mjs";
9+
import type UI from "../ui.mjs";
910

1011
export default class ConfigureCmakeCommand extends Command {
1112
private _logger: Logger = new Logger("ConfigureCmakeCommand");
@@ -60,7 +61,7 @@ export class CleanCMakeCommand extends Command {
6061

6162
public static readonly id = "cleanCmake";
6263

63-
constructor() {
64+
constructor(private readonly _ui: UI) {
6465
super(CleanCMakeCommand.id);
6566
}
6667

@@ -115,5 +116,71 @@ export class CleanCMakeCommand extends Command {
115116
"to get more information about the error."
116117
);
117118
}
119+
120+
const ws = workspaceFolder.uri.fsPath;
121+
const cMakeCachePath = join(ws, "build","CMakeCache.txt");
122+
const newBuildType = cmakeGetPicoVar(cMakeCachePath, "CMAKE_BUILD_TYPE");
123+
this._ui.updateBuildType(newBuildType ?? "unknown");
124+
}
125+
}
126+
127+
export class SwitchBuildTypeCommand extends Command {
128+
private _logger: Logger = new Logger("SwitchBuildTypeCommand");
129+
130+
public static readonly id = "switchBuildType";
131+
132+
constructor(private readonly _ui: UI) {
133+
super(SwitchBuildTypeCommand.id);
134+
}
135+
136+
async execute(): Promise<void> {
137+
const workspaceFolder = workspace.workspaceFolders?.[0];
138+
139+
// check if is a pico project
140+
if (workspaceFolder === undefined) {
141+
this._logger.warn("No workspace folder found.");
142+
void window.showWarningMessage("No workspace folder found.");
143+
144+
return;
145+
}
146+
147+
const settings = Settings.getInstance();
148+
149+
if (
150+
settings !== undefined &&
151+
settings.getBoolean(SettingsKey.useCmakeTools)
152+
) {
153+
void window.showErrorMessage(
154+
"You must use the CMake Tools extension to configure your build. " +
155+
"To use this extension instead, change the useCmakeTools setting."
156+
);
157+
158+
return;
159+
}
160+
161+
const ws = workspaceFolder.uri.fsPath;
162+
const cMakeCachePath = join(ws, "build","CMakeCache.txt");
163+
const oldBuildType = cmakeGetPicoVar(cMakeCachePath, "CMAKE_BUILD_TYPE");
164+
165+
// QuickPick for the build type
166+
const quickPickItems = ["Debug", "Release", "MinSizeRel", "RelWithDebInfo"];
167+
const buildType = await window.showQuickPick(quickPickItems, {
168+
placeHolder: `Current: ${oldBuildType}`,
169+
});
170+
171+
if (await configureCmakeNinja(workspaceFolder.uri, buildType)) {
172+
void window.showInformationMessage("CMake has configured your build.");
173+
} else {
174+
void window.showWarningMessage(
175+
"CMake failed to configure your build. " +
176+
"See the developer console for details " +
177+
"(Help -> Toggle Developer Tools). " +
178+
"You can also use the CMake Tools Extension Integration " +
179+
"to get more information about the error."
180+
);
181+
}
182+
183+
const newBuildType = cmakeGetPicoVar(cMakeCachePath, "CMAKE_BUILD_TYPE");
184+
this._ui.updateBuildType(newBuildType ?? "unknown");
118185
}
119186
}

src/extension.mts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Logger, { LoggerSource } from "./logger.mjs";
1717
import {
1818
CMAKE_DO_NOT_EDIT_HEADER_PREFIX,
1919
CMAKE_DO_NOT_EDIT_HEADER_PREFIX_OLD,
20+
cmakeGetPicoVar,
2021
cmakeGetSelectedBoard,
2122
cmakeGetSelectedToolchainAndSDKVersions,
2223
configureCmakeNinja,
@@ -69,6 +70,7 @@ import DebugLayoutCommand from "./commands/debugLayout.mjs";
6970
import OpenSdkDocumentationCommand from "./commands/openSdkDocumentation.mjs";
7071
import ConfigureCmakeCommand, {
7172
CleanCMakeCommand,
73+
SwitchBuildTypeCommand,
7274
} from "./commands/configureCmake.mjs";
7375
import ImportProjectCommand from "./commands/importProject.mjs";
7476
import { homedir } from "os";
@@ -123,10 +125,11 @@ export async function activate(context: ExtensionContext): Promise<void> {
123125
new DebugLayoutCommand(),
124126
new OpenSdkDocumentationCommand(context.extensionUri),
125127
new ConfigureCmakeCommand(),
128+
new SwitchBuildTypeCommand(ui),
126129
new ImportProjectCommand(context.extensionUri),
127130
new NewExampleProjectCommand(context.extensionUri),
128131
new UninstallPicoSDKCommand(),
129-
new CleanCMakeCommand(),
132+
new CleanCMakeCommand(ui),
130133
];
131134

132135
// register all command handlers
@@ -774,6 +777,11 @@ export async function activate(context: ExtensionContext): Promise<void> {
774777
//run `cmake -G Ninja -B ./build ` in the root folder
775778
await configureCmakeNinja(workspaceFolder.uri);
776779

780+
const ws = workspaceFolder.uri.fsPath;
781+
const cMakeCachePath = join(ws, "build","CMakeCache.txt");
782+
const newBuildType = cmakeGetPicoVar(cMakeCachePath, "CMAKE_BUILD_TYPE");
783+
ui.updateBuildType(newBuildType ?? "unknown");
784+
777785
workspace.onDidChangeTextDocument(event => {
778786
// Check if the changed document is the file you are interested in
779787
if (basename(event.document.fileName) === "CMakeLists.txt") {

src/ui.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,18 @@ export default class UI {
6565
this._items[StatusBarItemKey.picoSDKQuickPick].text = STATUS_BAR_ITEMS[
6666
StatusBarItemKey.picoSDKQuickPick
6767
].text.replace("<version>", version);
68-
this._activityBarProvider.refresh(version);
68+
this._activityBarProvider.refreshSDK(version);
6969
}
7070

7171
public updateBoard(board: string): void {
7272
this._items[StatusBarItemKey.picoBoardQuickPick].text = STATUS_BAR_ITEMS[
7373
StatusBarItemKey.picoBoardQuickPick
7474
].text.replace("<board>", board);
75+
this._activityBarProvider.refreshBoard(board);
76+
}
77+
78+
public updateBuildType(buildType: string): void {
79+
this._activityBarProvider.refreshBuildType(buildType);
7580
}
7681

7782
/*

src/utils/cmakeUtil.mts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ export async function getPath(): Promise<string> {
101101
}`;
102102
}
103103

104-
export async function configureCmakeNinja(folder: Uri): Promise<boolean> {
104+
export async function configureCmakeNinja(
105+
folder: Uri,
106+
buildType?: string
107+
): Promise<boolean> {
105108
if (process.platform !== "win32" && folder.fsPath.includes("\\")) {
106109
const errorMsg =
107110
"CMake currently does not support folder names with backslashes.";
@@ -200,7 +203,8 @@ export async function configureCmakeNinja(folder: Uri): Promise<boolean> {
200203
pythonPath.includes("/")
201204
? `-DPython3_EXECUTABLE="${pythonPath.replaceAll("\\", "/")}" `
202205
: ""
203-
}` + `-G Ninja -B ./build "${folder.fsPath}"`;
206+
}` + `-G Ninja -B ./build "${folder.fsPath}"`
207+
+ (buildType ? ` -DCMAKE_BUILD_TYPE=${buildType}` : "");
204208

205209
await new Promise<void>((resolve, reject) => {
206210
// use exec to be able to cancel the process

src/webview/activityBar.mts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import OpenSdkDocumentationCommand, {
2121
} from "../commands/openSdkDocumentation.mjs";
2222
import ConfigureCmakeCommand, {
2323
CleanCMakeCommand,
24+
SwitchBuildTypeCommand,
2425
} from "../commands/configureCmake.mjs";
2526
import ImportProjectCommand from "../commands/importProject.mjs";
2627
import NewExampleProjectCommand from "../commands/newExampleProject.mjs";
@@ -52,6 +53,7 @@ const RUN_PROJECT_LABEL = "Run Project (USB)";
5253
const FLASH_PROJECT_LABEL = "Flash Project (SWD)";
5354
const CONFIGURE_CMAKE_PROJECT_LABEL = "Configure CMake";
5455
const CLEAN_CMAKE_PROJECT_LABEL = "Clean CMake";
56+
const SWITCH_BUILD_TYPE_LABEL = "Switch Build Type";
5557
const DEBUG_PROJECT_LABEL = "Debug Project";
5658
const DEBUG_LAYOUT_PROJECT_LABEL = "Debug Layout";
5759

@@ -60,6 +62,8 @@ export class PicoProjectActivityBar
6062
{
6163
public static readonly viewType = "raspberry-pi-pico-project-quick-access";
6264
private _sdkVersion: string = "N/A";
65+
private _board: string = "N/A";
66+
private _buildType: string = "N/A";
6367

6468
private _onDidChangeTreeData = new EventEmitter<
6569
QuickAccessCommand | undefined | void
@@ -71,13 +75,27 @@ export class PicoProjectActivityBar
7175

7276
constructor() {}
7377

74-
public refresh(newPicoSDKVersion?: string): void {
78+
public refreshSDK(newPicoSDKVersion?: string): void {
7579
if (newPicoSDKVersion) {
7680
this._sdkVersion = newPicoSDKVersion;
7781
}
7882
this._onDidChangeTreeData.fire();
7983
}
8084

85+
public refreshBoard(newBoard?: string): void {
86+
if (newBoard) {
87+
this._board = newBoard;
88+
}
89+
this._onDidChangeTreeData.fire();
90+
}
91+
92+
public refreshBuildType(newBuildType?: string): void {
93+
if (newBuildType) {
94+
this._buildType = newBuildType;
95+
}
96+
this._onDidChangeTreeData.fire();
97+
}
98+
8199
public getTreeItem(
82100
element: QuickAccessCommand
83101
): TreeItem | Thenable<TreeItem> {
@@ -119,13 +137,18 @@ export class PicoProjectActivityBar
119137
// or "trash" or "sync"
120138
element.iconPath = new ThemeIcon("squirrel");
121139
break;
140+
case SWITCH_BUILD_TYPE_LABEL:
141+
element.iconPath = new ThemeIcon("gear");
142+
element.description = `${this._buildType}`;
143+
break;
122144
case SWITCH_SDK_LABEL:
123145
// repo-forked or extensions; alt. "replace-all"
124146
element.iconPath = new ThemeIcon("find-replace-all");
125-
element.description = `Current: ${this._sdkVersion}`;
147+
element.description = `${this._sdkVersion}`;
126148
break;
127149
case SWITCH_BOARD_LABEL:
128150
element.iconPath = new ThemeIcon("circuit-board");
151+
element.description = `${this._board}`;
129152
break;
130153
case DEBUG_LAYOUT_PROJECT_LABEL:
131154
element.iconPath = new ThemeIcon("debug-console");
@@ -254,6 +277,14 @@ export class PicoProjectActivityBar
254277
title: CLEAN_CMAKE_PROJECT_LABEL,
255278
}
256279
),
280+
new QuickAccessCommand(
281+
SWITCH_BUILD_TYPE_LABEL,
282+
TreeItemCollapsibleState.None,
283+
{
284+
command: `${extensionName}.${SwitchBuildTypeCommand.id}`,
285+
title: SWITCH_BUILD_TYPE_LABEL,
286+
}
287+
),
257288
new QuickAccessCommand(
258289
SWITCH_SDK_LABEL,
259290
TreeItemCollapsibleState.None,

0 commit comments

Comments
 (0)