Skip to content

Commit

Permalink
Add SwitchBuildType command
Browse files Browse the repository at this point in the history
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
  • Loading branch information
will-v-pi committed Jan 23, 2025
1 parent 2b4326b commit 9f84b2c
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 8 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@
"category": "Raspberry Pi Pico",
"enablement": "raspberry-pi-pico.isPicoProject"
},
{
"command": "raspberry-pi-pico.switchBuildType",
"title": "Switch Build Type",
"category": "Raspberry Pi Pico",
"enablement": "raspberry-pi-pico.isPicoProject"
},
{
"command": "raspberry-pi-pico.importProject",
"title": "Import Pico Project",
Expand Down
71 changes: 69 additions & 2 deletions src/commands/configureCmake.mts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Command } from "./command.mjs";
import Logger from "../logger.mjs";
import { window, workspace } from "vscode";
import { configureCmakeNinja } from "../utils/cmakeUtil.mjs";
import { cmakeGetPicoVar, configureCmakeNinja } from "../utils/cmakeUtil.mjs";
import Settings, { SettingsKey } from "../settings.mjs";
import { join } from "path";
import { rimraf } from "rimraf";
import { unknownErrorToString } from "../utils/errorHelper.mjs";
import type UI from "../ui.mjs";

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

public static readonly id = "cleanCmake";

constructor() {
constructor(private readonly _ui: UI) {
super(CleanCMakeCommand.id);
}

Expand Down Expand Up @@ -115,5 +116,71 @@ export class CleanCMakeCommand extends Command {
"to get more information about the error."
);
}

const ws = workspaceFolder.uri.fsPath;
const cMakeCachePath = join(ws, "build","CMakeCache.txt");
const newBuildType = cmakeGetPicoVar(cMakeCachePath, "CMAKE_BUILD_TYPE");
this._ui.updateBuildType(newBuildType ?? "unknown");
}
}

export class SwitchBuildTypeCommand extends Command {
private _logger: Logger = new Logger("SwitchBuildTypeCommand");

public static readonly id = "switchBuildType";

constructor(private readonly _ui: UI) {
super(SwitchBuildTypeCommand.id);
}

async execute(): Promise<void> {
const workspaceFolder = workspace.workspaceFolders?.[0];

// check if is a pico project
if (workspaceFolder === undefined) {
this._logger.warn("No workspace folder found.");
void window.showWarningMessage("No workspace folder found.");

return;
}

const settings = Settings.getInstance();

if (
settings !== undefined &&
settings.getBoolean(SettingsKey.useCmakeTools)
) {
void window.showErrorMessage(
"You must use the CMake Tools extension to configure your build. " +
"To use this extension instead, change the useCmakeTools setting."
);

return;
}

const ws = workspaceFolder.uri.fsPath;
const cMakeCachePath = join(ws, "build","CMakeCache.txt");
const oldBuildType = cmakeGetPicoVar(cMakeCachePath, "CMAKE_BUILD_TYPE");

// QuickPick for the build type
const quickPickItems = ["Debug", "Release", "MinSizeRel", "RelWithDebInfo"];
const buildType = await window.showQuickPick(quickPickItems, {
placeHolder: `Current: ${oldBuildType}`,
});

if (await configureCmakeNinja(workspaceFolder.uri, buildType)) {
void window.showInformationMessage("CMake has configured your build.");
} else {
void window.showWarningMessage(
"CMake failed to configure your build. " +
"See the developer console for details " +
"(Help -> Toggle Developer Tools). " +
"You can also use the CMake Tools Extension Integration " +
"to get more information about the error."
);
}

const newBuildType = cmakeGetPicoVar(cMakeCachePath, "CMAKE_BUILD_TYPE");
this._ui.updateBuildType(newBuildType ?? "unknown");
}
}
10 changes: 9 additions & 1 deletion src/extension.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Logger, { LoggerSource } from "./logger.mjs";
import {
CMAKE_DO_NOT_EDIT_HEADER_PREFIX,
CMAKE_DO_NOT_EDIT_HEADER_PREFIX_OLD,
cmakeGetPicoVar,
cmakeGetSelectedBoard,
cmakeGetSelectedToolchainAndSDKVersions,
configureCmakeNinja,
Expand Down Expand Up @@ -69,6 +70,7 @@ import DebugLayoutCommand from "./commands/debugLayout.mjs";
import OpenSdkDocumentationCommand from "./commands/openSdkDocumentation.mjs";
import ConfigureCmakeCommand, {
CleanCMakeCommand,
SwitchBuildTypeCommand,
} from "./commands/configureCmake.mjs";
import ImportProjectCommand from "./commands/importProject.mjs";
import { homedir } from "os";
Expand Down Expand Up @@ -123,10 +125,11 @@ export async function activate(context: ExtensionContext): Promise<void> {
new DebugLayoutCommand(),
new OpenSdkDocumentationCommand(context.extensionUri),
new ConfigureCmakeCommand(),
new SwitchBuildTypeCommand(ui),
new ImportProjectCommand(context.extensionUri),
new NewExampleProjectCommand(context.extensionUri),
new UninstallPicoSDKCommand(),
new CleanCMakeCommand(),
new CleanCMakeCommand(ui),
];

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

const ws = workspaceFolder.uri.fsPath;
const cMakeCachePath = join(ws, "build","CMakeCache.txt");
const newBuildType = cmakeGetPicoVar(cMakeCachePath, "CMAKE_BUILD_TYPE");
ui.updateBuildType(newBuildType ?? "unknown");

workspace.onDidChangeTextDocument(event => {
// Check if the changed document is the file you are interested in
if (basename(event.document.fileName) === "CMakeLists.txt") {
Expand Down
7 changes: 6 additions & 1 deletion src/ui.mts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ export default class UI {
this._items[StatusBarItemKey.picoSDKQuickPick].text = STATUS_BAR_ITEMS[
StatusBarItemKey.picoSDKQuickPick
].text.replace("<version>", version);
this._activityBarProvider.refresh(version);
this._activityBarProvider.refreshSDK(version);
}

public updateBoard(board: string): void {
this._items[StatusBarItemKey.picoBoardQuickPick].text = STATUS_BAR_ITEMS[
StatusBarItemKey.picoBoardQuickPick
].text.replace("<board>", board);
this._activityBarProvider.refreshBoard(board);
}

public updateBuildType(buildType: string): void {
this._activityBarProvider.refreshBuildType(buildType);
}

/*
Expand Down
8 changes: 6 additions & 2 deletions src/utils/cmakeUtil.mts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ export async function getPath(): Promise<string> {
}`;
}

export async function configureCmakeNinja(folder: Uri): Promise<boolean> {
export async function configureCmakeNinja(
folder: Uri,
buildType?: string
): Promise<boolean> {
if (process.platform !== "win32" && folder.fsPath.includes("\\")) {
const errorMsg =
"CMake currently does not support folder names with backslashes.";
Expand Down Expand Up @@ -200,7 +203,8 @@ export async function configureCmakeNinja(folder: Uri): Promise<boolean> {
pythonPath.includes("/")
? `-DPython3_EXECUTABLE="${pythonPath.replaceAll("\\", "/")}" `
: ""
}` + `-G Ninja -B ./build "${folder.fsPath}"`;
}` + `-G Ninja -B ./build "${folder.fsPath}"`
+ (buildType ? ` -DCMAKE_BUILD_TYPE=${buildType}` : "");

await new Promise<void>((resolve, reject) => {
// use exec to be able to cancel the process
Expand Down
35 changes: 33 additions & 2 deletions src/webview/activityBar.mts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import OpenSdkDocumentationCommand, {
} from "../commands/openSdkDocumentation.mjs";
import ConfigureCmakeCommand, {
CleanCMakeCommand,
SwitchBuildTypeCommand,
} from "../commands/configureCmake.mjs";
import ImportProjectCommand from "../commands/importProject.mjs";
import NewExampleProjectCommand from "../commands/newExampleProject.mjs";
Expand Down Expand Up @@ -52,6 +53,7 @@ const RUN_PROJECT_LABEL = "Run Project (USB)";
const FLASH_PROJECT_LABEL = "Flash Project (SWD)";
const CONFIGURE_CMAKE_PROJECT_LABEL = "Configure CMake";
const CLEAN_CMAKE_PROJECT_LABEL = "Clean CMake";
const SWITCH_BUILD_TYPE_LABEL = "Switch Build Type";
const DEBUG_PROJECT_LABEL = "Debug Project";
const DEBUG_LAYOUT_PROJECT_LABEL = "Debug Layout";

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

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

constructor() {}

public refresh(newPicoSDKVersion?: string): void {
public refreshSDK(newPicoSDKVersion?: string): void {
if (newPicoSDKVersion) {
this._sdkVersion = newPicoSDKVersion;
}
this._onDidChangeTreeData.fire();
}

public refreshBoard(newBoard?: string): void {
if (newBoard) {
this._board = newBoard;
}
this._onDidChangeTreeData.fire();
}

public refreshBuildType(newBuildType?: string): void {
if (newBuildType) {
this._buildType = newBuildType;
}
this._onDidChangeTreeData.fire();
}

public getTreeItem(
element: QuickAccessCommand
): TreeItem | Thenable<TreeItem> {
Expand Down Expand Up @@ -119,13 +137,18 @@ export class PicoProjectActivityBar
// or "trash" or "sync"
element.iconPath = new ThemeIcon("squirrel");
break;
case SWITCH_BUILD_TYPE_LABEL:
element.iconPath = new ThemeIcon("gear");
element.description = `${this._buildType}`;
break;
case SWITCH_SDK_LABEL:
// repo-forked or extensions; alt. "replace-all"
element.iconPath = new ThemeIcon("find-replace-all");
element.description = `Current: ${this._sdkVersion}`;
element.description = `${this._sdkVersion}`;
break;
case SWITCH_BOARD_LABEL:
element.iconPath = new ThemeIcon("circuit-board");
element.description = `${this._board}`;
break;
case DEBUG_LAYOUT_PROJECT_LABEL:
element.iconPath = new ThemeIcon("debug-console");
Expand Down Expand Up @@ -254,6 +277,14 @@ export class PicoProjectActivityBar
title: CLEAN_CMAKE_PROJECT_LABEL,
}
),
new QuickAccessCommand(
SWITCH_BUILD_TYPE_LABEL,
TreeItemCollapsibleState.None,
{
command: `${extensionName}.${SwitchBuildTypeCommand.id}`,
title: SWITCH_BUILD_TYPE_LABEL,
}
),
new QuickAccessCommand(
SWITCH_SDK_LABEL,
TreeItemCollapsibleState.None,
Expand Down

0 comments on commit 9f84b2c

Please sign in to comment.