Skip to content

Commit 7371ab2

Browse files
committed
Added CMake clean command
Signed-off-by: paulober <[email protected]>
1 parent 1ae2b0d commit 7371ab2

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@
201201
"title": "Flash Pico Project (SWD)",
202202
"category": "Raspberry Pi Pico",
203203
"enablement": "raspberry-pi-pico.isPicoProject"
204+
},
205+
{
206+
"command": "raspberry-pi-pico.cleanCmake",
207+
"title": "Clean CMake",
208+
"category": "Raspberry Pi Pico",
209+
"enablement": "raspberry-pi-pico.isPicoProject && !raspberry-pi-pico.isRustProject"
204210
}
205211
],
206212
"configuration": {

src/commands/configureCmake.mts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import Logger from "../logger.mjs";
33
import { window, workspace } from "vscode";
44
import { configureCmakeNinja } from "../utils/cmakeUtil.mjs";
55
import Settings, { SettingsKey } from "../settings.mjs";
6+
import { join } from "path";
7+
import { rimraf } from "rimraf";
8+
import { unknownErrorToString } from "../utils/errorHelper.mjs";
69

710
export default class ConfigureCmakeCommand extends Command {
811
private _logger: Logger = new Logger("ConfigureCmakeCommand");
@@ -34,7 +37,7 @@ export default class ConfigureCmakeCommand extends Command {
3437
"You must use the CMake Tools extension to configure your build. " +
3538
"To use this extension instead, change the useCmakeTools setting."
3639
);
37-
40+
3841
return;
3942
}
4043

@@ -45,3 +48,62 @@ export default class ConfigureCmakeCommand extends Command {
4548
}
4649
}
4750
}
51+
52+
export class CleanCMakeCommand extends Command {
53+
private _logger: Logger = new Logger("CleanCMakeCommand");
54+
55+
public static readonly id = "cleanCmake";
56+
57+
constructor() {
58+
super(CleanCMakeCommand.id);
59+
}
60+
61+
async execute(): Promise<void> {
62+
const workspaceFolder = workspace.workspaceFolders?.[0];
63+
64+
if (workspaceFolder === undefined) {
65+
this._logger.warn("No workspace folder found.");
66+
void window.showWarningMessage("No workspace folder found.");
67+
68+
return;
69+
}
70+
71+
const settings = Settings.getInstance();
72+
73+
if (
74+
settings !== undefined &&
75+
settings.getBoolean(SettingsKey.useCmakeTools)
76+
) {
77+
void window.showErrorMessage(
78+
"You must use the CMake Tools extension to clean your build. " +
79+
"To use this extension instead, change the useCmakeTools setting."
80+
);
81+
82+
return;
83+
}
84+
85+
try {
86+
// delete build dir if present
87+
const buildDir = join(workspaceFolder.uri.fsPath, "build");
88+
await rimraf(buildDir, { maxRetries: 2 });
89+
} catch (error) {
90+
this._logger.error(
91+
"Error cleaning build directory.",
92+
unknownErrorToString(error)
93+
);
94+
void window.showErrorMessage("Error cleaning build directory.");
95+
96+
return;
97+
}
98+
99+
if (await configureCmakeNinja(workspaceFolder.uri)) {
100+
void window.showInformationMessage(
101+
"CMake has been cleaned and reconfigured."
102+
);
103+
} else {
104+
void window.showWarningMessage(
105+
"CMake could not be reconfigured. See log for details."
106+
);
107+
}
108+
}
109+
}

src/extension.mts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ import { PicoProjectActivityBar } from "./webview/activityBar.mjs";
6565
import ConditionalDebuggingCommand from "./commands/conditionalDebugging.mjs";
6666
import DebugLayoutCommand from "./commands/debugLayout.mjs";
6767
import OpenSdkDocumentationCommand from "./commands/openSdkDocumentation.mjs";
68-
import ConfigureCmakeCommand from "./commands/configureCmake.mjs";
68+
import ConfigureCmakeCommand, {
69+
CleanCMakeCommand,
70+
} from "./commands/configureCmake.mjs";
6971
import ImportProjectCommand from "./commands/importProject.mjs";
7072
import { homedir } from "os";
7173
import NewExampleProjectCommand from "./commands/newExampleProject.mjs";
@@ -121,6 +123,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
121123
new ImportProjectCommand(context.extensionUri),
122124
new NewExampleProjectCommand(context.extensionUri),
123125
new UninstallPicoSDKCommand(),
126+
new CleanCMakeCommand(),
124127
];
125128

126129
// register all command handlers

src/webview/activityBar.mts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import OpenSdkDocumentationCommand, {
1919
DOCUMENTATION_LABEL_BY_ID,
2020
DocumentationId,
2121
} from "../commands/openSdkDocumentation.mjs";
22-
import ConfigureCmakeCommand from "../commands/configureCmake.mjs";
22+
import ConfigureCmakeCommand, {
23+
CleanCMakeCommand,
24+
} from "../commands/configureCmake.mjs";
2325
import ImportProjectCommand from "../commands/importProject.mjs";
2426
import NewExampleProjectCommand from "../commands/newExampleProject.mjs";
2527
import SwitchBoardCommand from "../commands/switchBoard.mjs";
@@ -49,6 +51,7 @@ const COMPILE_PROJECT_LABEL = "Compile Project";
4951
const RUN_PROJECT_LABEL = "Run Project (USB)";
5052
const FLASH_PROJECT_LABEL = "Flash Project (SWD)";
5153
const CONFIGURE_CMAKE_PROJECT_LABEL = "Configure CMake";
54+
const CLEAN_CMAKE_PROJECT_LABEL = "Clean CMake";
5255
const DEBUG_PROJECT_LABEL = "Debug Project";
5356
const DEBUG_LAYOUT_PROJECT_LABEL = "Debug Layout";
5457

@@ -112,6 +115,10 @@ export class PicoProjectActivityBar
112115
// alt. "gather"
113116
element.iconPath = new ThemeIcon("beaker");
114117
break;
118+
case CLEAN_CMAKE_PROJECT_LABEL:
119+
// or "trash" or "sync"
120+
element.iconPath = new ThemeIcon("squirrel");
121+
break;
115122
case SWITCH_SDK_LABEL:
116123
// repo-forked or extensions; alt. "replace-all"
117124
element.iconPath = new ThemeIcon("find-replace-all");
@@ -239,6 +246,14 @@ export class PicoProjectActivityBar
239246
title: CONFIGURE_CMAKE_PROJECT_LABEL,
240247
}
241248
),
249+
new QuickAccessCommand(
250+
CLEAN_CMAKE_PROJECT_LABEL,
251+
TreeItemCollapsibleState.None,
252+
{
253+
command: `${extensionName}.${CleanCMakeCommand.id}`,
254+
title: CLEAN_CMAKE_PROJECT_LABEL,
255+
}
256+
),
242257
new QuickAccessCommand(
243258
SWITCH_SDK_LABEL,
244259
TreeItemCollapsibleState.None,

0 commit comments

Comments
 (0)