Skip to content

Commit

Permalink
Add command substitutions launchTargetName and getLaunchTargetName (
Browse files Browse the repository at this point in the history
#3979)

Co-authored-by: Garrett Campbell <[email protected]>
  • Loading branch information
chaosink and gcampbell-msft authored Oct 7, 2024
1 parent 49e4e31 commit ed37b19
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/cmake-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ Supported commands for substitution:
|`cmake.getLaunchTargetPath`|The full path to the target executable, including the filename. The existence of the target is not validated.|
|`cmake.getLaunchTargetDirectory`|The full path to the target executable's directory. The existence of the directory is not validated.|
|`cmake.getLaunchTargetFilename`|The name of the target executable file without any path information. The existence of the target is not validated.|
|`cmake.getLaunchTargetName`|The name to the target. The existence of the target is not validated.|
|`cmake.launchTargetPath`|The full path to the target executable, including the filename. If `cmake.buildBeforeRun` is true, invoking this substitution will also start a build.|
|`cmake.launchTargetDirectory`|The full path to the target executable's directory. If `cmake.buildBeforeRun` is true, invoking this substitution will also start a build.|
|`cmake.launchTargetFilename`|The name of the target executable file without any path information. If `cmake.buildBeforeRun` is true, invoking this substitution will also start a build.|
|`cmake.launchTargetName`|The name of the target. If `cmake.buildBeforeRun` is true, invoking this substitution will also start a build.|
|`cmake.buildTargetName`|The current target selected for build.|
|`cmake.buildType`|Same as `${buildType}`. The current CMake build type.|
|`cmake.buildKit`|Same as `${buildKit}`. The current CMake kit name.|
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
"onCommand:cmake.launchTargetPath",
"onCommand:cmake.launchTargetDirectory",
"onCommand:cmake.launchTargetFilename",
"onCommand:cmake.launchTargetName",
"onCommand:cmake.getLaunchTargetPath",
"onCommand:cmake.getLaunchTargetDirectory",
"onCommand:cmake.getLaunchTargetFilename",
"onCommand:cmake.getlaunchTargetName",
"onCommand:cmake.buildType",
"onCommand:cmake.buildDirectory",
"onCommand:cmake.executableTargets",
Expand Down
22 changes: 22 additions & 0 deletions src/cmakeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2567,6 +2567,17 @@ export class CMakeProject {
return path.basename(targetPath);
}

/**
* Implementation of `cmake.launchTargetName`. This also ensures the target exists if `cmake.buildBeforeRun` is set.
*/
async launchTargetNameForSubstitution(): Promise<string | null> {
const targetPath = await this.launchTargetPath();
if (targetPath === null) {
return null;
}
return path.parse(targetPath).name;
}

/**
* Implementation of `cmake.getLaunchTargetPath`. This does not ensure the target exists.
*/
Expand Down Expand Up @@ -2613,6 +2624,17 @@ export class CMakeProject {
return path.basename(targetPath);
}

/**
* Implementation of `cmake.getLaunchTargetName`. This does not ensure the target exists.
*/
async getLaunchTargetName(): Promise<string | null> {
const targetPath = await this.getLaunchTargetPath();
if (targetPath === null) {
return null;
}
return path.parse(targetPath).name;
}

/**
* Implementation of `cmake.buildType`
*/
Expand Down
28 changes: 28 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,19 @@ export class ExtensionManager implements vscode.Disposable {
}, folder);
}

launchTargetName(args?: FolderTargetNameArgsType) {
const [folder, targetName] = this.resolveFolderTargetNameArgs(args);

telemetry.logEvent("substitution", { command: "launchTargetName" });
return this.queryCMakeProject(async cmakeProject => {
if (targetName !== undefined && targetName !== null) {
await cmakeProject.setLaunchTargetByName(targetName);
}
const targetFilename = await cmakeProject.launchTargetNameForSubstitution();
return targetFilename;
}, folder);
}

getLaunchTargetPath(args?: FolderTargetNameArgsType) {
const [folder, targetName] = this.resolveFolderTargetNameArgs(args);

Expand Down Expand Up @@ -1640,6 +1653,19 @@ export class ExtensionManager implements vscode.Disposable {
}, folder);
}

getLaunchTargetName(args?: FolderTargetNameArgsType) {
const [folder, targetName] = this.resolveFolderTargetNameArgs(args);

telemetry.logEvent("substitution", { command: "getLaunchTargetName" });
return this.queryCMakeProject(async cmakeProject => {
if (targetName !== undefined && targetName !== null) {
await cmakeProject.setLaunchTargetByName(targetName);
}
const targetFilename = await cmakeProject.getLaunchTargetName();
return targetFilename;
}, folder);
}

buildTargetName(folder?: vscode.WorkspaceFolder | string) {
telemetry.logEvent("substitution", { command: "buildTargetName" });
return this.queryCMakeProject(cmakeProject => cmakeProject.buildTargetName(), folder);
Expand Down Expand Up @@ -2203,9 +2229,11 @@ async function setup(context: vscode.ExtensionContext, progress?: ProgressHandle
'launchTargetPath',
'launchTargetDirectory',
'launchTargetFilename',
'launchTargetName',
'getLaunchTargetPath',
'getLaunchTargetDirectory',
'getLaunchTargetFilename',
'getLaunchTargetName',
'buildTargetName',
'buildKit',
'buildType',
Expand Down
19 changes: 18 additions & 1 deletion test/extension-tests/successful-build/test/debugger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ suite('Debug/Launch interface', () => {
expect(await cmakeProject.launchTargetFilename()).to.be.eq(path.basename(executablesTargets[0].path));
});

test('Test launchTargetNameForSubstitution for use in other extensions or launch.json', async () => {
const executablesTargets = await cmakeProject.executableTargets;
expect(executablesTargets.length).to.be.not.eq(0);

await cmakeProject.setLaunchTargetByName(executablesTargets[0].name);

expect(await cmakeProject.launchTargetNameForSubstitution()).to.be.eq(path.parse(executablesTargets[0].path).name);
});

test('Test getLaunchTargetPath for use in other extensions or launch.json', async () => {
const executablesTargets = await cmakeProject.executableTargets;
expect(executablesTargets.length).to.be.not.eq(0);
Expand Down Expand Up @@ -89,6 +98,15 @@ suite('Debug/Launch interface', () => {
expect(await cmakeProject.getLaunchTargetFilename()).to.be.eq(path.basename(executablesTargets[0].path));
});

test('Test getLaunchTargetName for use in other extensions or launch.json', async () => {
const executablesTargets = await cmakeProject.executableTargets;
expect(executablesTargets.length).to.be.not.eq(0);

await cmakeProject.setLaunchTargetByName(executablesTargets[0].name);

expect(await cmakeProject.getLaunchTargetName()).to.be.eq(path.parse(executablesTargets[0].path).name);
});

test('Test build on launch (default)', async () => {
testEnv.config.updatePartial({ buildBeforeRun: undefined });

Expand Down Expand Up @@ -271,4 +289,3 @@ suite('Debug/Launch interface', () => {
await new Promise((resolve) => setTimeout(resolve, 3000));
}).timeout(60000);
});

0 comments on commit ed37b19

Please sign in to comment.