Skip to content

Commit

Permalink
Fix1914 - Variable substitution stopped working with release 1.7, rel…
Browse files Browse the repository at this point in the history
…ated to sourceDirectory and kit info
  • Loading branch information
andreeis committed Jul 27, 2023
1 parent 2014508 commit 75332f1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
28 changes: 21 additions & 7 deletions src/cmakeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ export class CMakeProject {
}
if (selectedFile) {
const newSourceDirectory = path.dirname(selectedFile);
await this.setSourceDir(await util.normalizeAndVerifySourceDir(newSourceDirectory, CMakeDriver.sourceDirExpansionOptions(this.workspaceContext.folder.uri.fsPath)));
let kit = this.getActiveKit();
await this.setSourceDir(await util.normalizeAndVerifySourceDir(newSourceDirectory, await CMakeDriver.sourceDirExpansionOptions(this.workspaceContext.folder.uri.fsPath, kit)));
void vscode.workspace.getConfiguration('cmake', this.workspaceFolder.uri).update("sourceDirectory", this._sourceDir);
if (config) {
// Updating sourceDirectory here, at the beginning of the configure process,
Expand Down Expand Up @@ -907,7 +908,9 @@ export class CMakeProject {
*/
private async init(sourceDirectory: string) {
log.debug(localize('second.phase.init', 'Starting CMake Tools second-phase init'));
await this.setSourceDir(await util.normalizeAndVerifySourceDir(sourceDirectory, CMakeDriver.sourceDirExpansionOptions(this.workspaceContext.folder.uri.fsPath)));
this.kitsController = await KitsController.init(this);
let kit = this.getActiveKit();
await this.setSourceDir(await util.normalizeAndVerifySourceDir(sourceDirectory, await CMakeDriver.sourceDirExpansionOptions(this.workspaceContext.folder.uri.fsPath, kit)));
this.hideBuildButton = (this.workspaceContext.config.statusbar.advanced?.build?.visibility === "hidden") ? true : false;
this.hideDebugButton = (this.workspaceContext.config.statusbar.advanced?.debug?.visibility === "hidden") ? true : false;
this.hideLaunchButton = (this.workspaceContext.config.statusbar.advanced?.launch?.visibility === "hidden") ? true : false;
Expand Down Expand Up @@ -935,7 +938,6 @@ export class CMakeProject {

this.statusMessage.set(localize('ready.status', 'Ready'));

this.kitsController = await KitsController.init(this);
this.presetsController = await PresetsController.init(this, this.kitsController, this.isMultiProjectFolder);

await this.doUseCMakePresetsChange();
Expand Down Expand Up @@ -1005,6 +1007,20 @@ export class CMakeProject {
return this.presetsController.onUserPresetsChanged(listener);
}

public getActiveKit(): Kit | null {
if (this.activeKit) {
return this.activeKit;
}

const kitName: string | null = this.workspaceContext.state.getActiveKitName(this.folderName, this.isMultiProjectFolder);
if (kitName) {
// It remembers a kit. Find it in the kits avail in this dir:
return this.kitsController.availableKits.find(k => k.name === kitName) || null;
}

return null;
}

async initializeKitOrPresets() {
if (this.useCMakePresets) {
const latestConfigPresetName = this.workspaceContext.state.getConfigurePresetName(this.folderName, this.isMultiProjectFolder);
Expand All @@ -1018,10 +1034,8 @@ export class CMakeProject {
}
} else {
// Check if the CMakeProject remembers what kit it was last using in this dir:
const kitName = this.workspaceContext.state.getActiveKitName(this.folderName, this.isMultiProjectFolder);
if (kitName) {
// It remembers a kit. Find it in the kits avail in this dir:
const kit = this.kitsController.availableKits.find(k => k.name === kitName) || null;
const kit = this.getActiveKit();
if (kit) {
// Set the kit: (May do nothing if no kit was found)
await this.setKit(kit);
}
Expand Down
19 changes: 17 additions & 2 deletions src/drivers/cmakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { getValue } from '@cmt/preset';
import { CacheEntry } from '@cmt/cache';
import { CMakeBuildRunner } from '@cmt/cmakeBuildRunner';
import { DebuggerInformation } from '@cmt/debug/debuggerConfigureDriver';
import { getActiveProject } from '@cmt/extension';

nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
Expand Down Expand Up @@ -320,6 +321,9 @@ export abstract class CMakeDriver implements vscode.Disposable {

private _kitDetect: KitDetect | null = null;

public getKit(): Kit | null { return this._kit; }
public getKitDetect(): KitDetect | null { return this._kitDetect; }

private _useCMakePresets: boolean = true;

get useCMakePresets(): boolean {
Expand Down Expand Up @@ -390,12 +394,22 @@ export abstract class CMakeDriver implements vscode.Disposable {
return { vars, variantVars };
}

static sourceDirExpansionOptions(workspaceFolderFspath: string | null): expand.ExpansionOptions {
static async sourceDirExpansionOptions(workspaceFolderFspath: string | null, kit?: Kit | null): Promise<expand.ExpansionOptions> {
const ws_root = util.lightNormalizePath(workspaceFolderFspath || '.');

// Fill in default replacements
const prj = getActiveProject();
if (!kit) {
kit = prj ? prj.getActiveKit() : undefined;
}
const kitName: string = kit ? kit.name : '';
const kitDetect = kit ? await getKitDetect(kit) : undefined;
const kitVendor: string = kitDetect ? kitDetect.vendor || '' : '';

const vars: expand.MinimalPresetContextVars = {
generator: 'generator',
buildKit: kitName,
buildKitVendor: kitVendor,
workspaceFolder: ws_root,
workspaceFolderBasename: path.basename(ws_root),
sourceDir: '${sourceDir}',
Expand Down Expand Up @@ -680,7 +694,8 @@ export abstract class CMakeDriver implements vscode.Disposable {

private async _refreshExpansions(configurePreset?: preset.ConfigurePreset | null) {
return this.doRefreshExpansions(async () => {
this.sourceDir = await util.normalizeAndVerifySourceDir(this.sourceDirUnexpanded, CMakeDriver.sourceDirExpansionOptions(this.workspaceFolder));
let kit = this._kit;
this.sourceDir = await util.normalizeAndVerifySourceDir(this.sourceDirUnexpanded, await CMakeDriver.sourceDirExpansionOptions(this.workspaceFolder, kit));

const opts = this.expansionOptions;
opts.envOverride = await this.getConfigureEnvironment(configurePreset);
Expand Down
2 changes: 1 addition & 1 deletion src/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async function expandStringHelper(input: string, opts: ExpansionOptions) {
if (key !== 'dollar') {
// Replace dollar sign at the very end of the expanding process
const replacement = replacements[key];
if (!replacement) {
if (replacement === undefined) {
log.warning(localize('invalid.variable.reference', 'Invalid variable reference {0} in string: {1}', full, input));
} else {
subs.set(full, replacement);
Expand Down
9 changes: 8 additions & 1 deletion src/presetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import rollbar from '@cmt/rollbar';
import { ExpansionOptions } from '@cmt/expand';
import paths from '@cmt/paths';
import { KitsController } from '@cmt/kitsController';
import { descriptionForKit, Kit, SpecialKits } from '@cmt/kit';
import { descriptionForKit, Kit, getKitDetect, SpecialKits } from '@cmt/kit';
import { getHostTargetArchString } from '@cmt/installs/visualStudio';
import { loadSchema } from '@cmt/schema';
import json5 = require('json5');
Expand Down Expand Up @@ -42,12 +42,19 @@ export class PresetsController {
const presetsController = new PresetsController(project, kitsController, isMultiProject);
const expandSourceDir = async (dir: string) => {
const workspaceFolder = project.workspaceFolder.uri.fsPath;
let kit = project.getActiveKit();
const kitName: string = kit ? kit.name : '';
const kitDetect = kit ? await getKitDetect(kit) : undefined;
const kitVendor: string = kitDetect ? kitDetect.vendor || '' : '';

const expansionOpts: ExpansionOptions = {
vars: {
workspaceFolder,
workspaceFolderBasename: path.basename(workspaceFolder),
workspaceHash: util.makeHashString(workspaceFolder),
workspaceRoot: workspaceFolder,
buildKit: kitName,
buildKitVendor: kitVendor,
workspaceRootFolderName: path.dirname(workspaceFolder),
userHome: paths.userHome,
// Following fields are not supported for sourceDir expansion
Expand Down
6 changes: 4 additions & 2 deletions src/projectController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ export class ProjectController implements vscode.Disposable {
}

async getProjectForFolder(folder: string): Promise<CMakeProject | undefined> {
const sourceDir = util.platformNormalizePath(await util.normalizeAndVerifySourceDir(folder, CMakeDriver.sourceDirExpansionOptions(folder)));
let kit = this.activeProject?.activeKit;
const sourceDir = util.platformNormalizePath(await util.normalizeAndVerifySourceDir(folder, await CMakeDriver.sourceDirExpansionOptions(folder, kit)));
const allCMakeProjects: CMakeProject[] = this.getAllCMakeProjects();
for (const project of allCMakeProjects) {
if (util.platformNormalizePath(project.sourceDir) === sourceDir ||
Expand Down Expand Up @@ -390,7 +391,8 @@ export class ProjectController implements vscode.Disposable {
}
// Normalize the paths.
for (let i = 0; i < sourceDirectories.length; i++) {
sourceDirectories[i] = await util.normalizeAndVerifySourceDir(sourceDirectories[i], CMakeDriver.sourceDirExpansionOptions(folder.uri.fsPath));
let kit = this.activeProject?.activeKit;
sourceDirectories[i] = await util.normalizeAndVerifySourceDir(sourceDirectories[i], await CMakeDriver.sourceDirExpansionOptions(folder.uri.fsPath, kit));
}
const projects: CMakeProject[] | undefined = this.getProjectsForWorkspaceFolder(folder);

Expand Down

0 comments on commit 75332f1

Please sign in to comment.