Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added '${workspaceHash}' #1055

Merged
merged 5 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/drivers/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,15 @@ export abstract class CMakeDriver implements vscode.Disposable {

// Fill in default replacements
const vars: expand.ExpansionVars = {
workspaceRoot: ws_root,
workspaceFolder: ws_root,
buildKit: this._kit ? this._kit.name : '__unknownkit__',
buildType: this.currentBuildType,
workspaceRootFolderName: path.basename(ws_root),
workspaceFolderBasename: path.basename(ws_root),
generator: this.generatorName || 'null',
workspaceFolder: ws_root,
workspaceFolderBasename: path.basename(ws_root),
workspaceHash: util.makeHashString(ws_root),
workspaceRoot: ws_root,
workspaceRootFolderName: path.basename(ws_root),
userHome: paths.userHome,
buildKit: this._kit ? this._kit.name : '__unknownkit__',
// DEPRECATED EXPANSION: Remove this in the future:
projectName: 'ProjectName',
};
Expand Down
11 changes: 6 additions & 5 deletions src/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ const log = createLogger('expand');
* variables are specified as properties on this interface.
*/
export interface RequiredExpansionContextVars {
workspaceRoot: string;
workspaceFolder: string;
buildType: string;
buildKit: string;
workspaceRootFolderName: string;
workspaceFolderBasename: string;
buildType: string;
generator: string;
workspaceFolder: string;
workspaceFolderBasename: string;
workspaceHash: string;
workspaceRoot: string;
workspaceRootFolderName: string;
userHome: string;
}

Expand Down
1 change: 1 addition & 0 deletions src/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,7 @@ async function expandKitVariables(kit: Kit): Promise<Kit> {
userHome: paths.userHome,
workspaceFolder: '${workspaceFolder}',
workspaceFolderBasename: '${workspaceFolderBasename}',
workspaceHash: '${workspaceHash}',
workspaceRoot: '${workspaceRoot}',
workspaceRootFolderName: '${workspaceRootFolderName}'
}
Expand Down
10 changes: 6 additions & 4 deletions src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as which from 'which';
import {vsInstallations} from './installs/visual-studio';
import {expandString} from './expand';
import {fs} from './pr';
import * as util from '@cmt/util';

interface VSCMakePaths {
cmake?: string;
Expand Down Expand Up @@ -133,14 +134,15 @@ class Paths {

const raw = await expandString(wsc.config.raw_cmakePath, {
vars: {
workspaceRoot: wsc.folder.uri.fsPath,
workspaceFolder: wsc.folder.uri.fsPath,
userHome: this.userHome,
buildKit: '',
buildType: '',
generator: '',
workspaceFolder: wsc.folder.uri.fsPath,
workspaceFolderBasename: path.basename(wsc.folder.uri.fsPath),
workspaceRoot: wsc.folder.uri.fsPath,
workspaceRootFolderName: path.basename(wsc.folder.uri.fsPath),
workspaceFolderBasename: path.basename(wsc.folder.uri.fsPath)
workspaceHash: util.makeHashString(wsc.folder.uri.fsPath),
userHome: this.userHome,
},
});

Expand Down
10 changes: 9 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import * as child_process from 'child_process';
import * as chokidar from 'chokidar';
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import * as fs from 'fs';

import {EnvironmentVariables, execute} from './proc';
import * as nls from 'vscode-nls';

nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
const localize: nls.LocalizeFunc = nls.loadMessageBundle();


/**
* Escape a string so it can be used as a regular expression
*/
Expand Down Expand Up @@ -585,6 +586,13 @@ export function isString(x: any): x is string {
return Object.prototype.toString.call(x) === "[object String]";
}

export function makeHashString(str: string): string {
const crypto = require('crypto');
const hash = crypto.createHash('sha256');
hash.update(str);
return hash.digest('hex');
}

export function isArray(x: any): x is any[] {
return x instanceof Array;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as api from '@cmt/api';
import {CMakeCache} from '@cmt/cache';
import {CMakeTools, ConfigureTrigger} from '@cmt/cmake-tools';
import paths from '@cmt/paths';
import {objectPairs, platformNormalizePath} from '@cmt/util';
import {objectPairs, platformNormalizePath, makeHashString} from '@cmt/util';
import {clearExistingKitConfigurationFile, DefaultEnvironment, expect, getFirstSystemKit} from '@test/util';
import * as path from 'path';

Expand Down Expand Up @@ -65,6 +65,25 @@ suite('[Variable Substitution]', async () => {
expect(typeof cacheEntry.value).to.eq('string', '[workspaceFolder] unexpected cache entry value type');
}).timeout(100000);


test('Check substitution for "workspaceHash"', async () => {
// Set fake settings
testEnv.config.updatePartial({configureSettings: {workspaceHash: '${workspaceHash}'}});

// Configure
expect(await cmt.configure()).to.be.eq(0, '[workspaceHash] configure failed');
expect(testEnv.projectFolder.buildDirectory.isCMakeCachePresent).to.eql(true, 'expected cache not present');
const cache = await CMakeCache.fromPath(await cmt.cachePath);

const cacheEntry = cache.get('workspaceHash') as api.CacheEntry;
expect(cacheEntry.type).to.eq(api.CacheEntryType.String, '[workspaceHash] unexpected cache entry type');
expect(cacheEntry.key)
.to.eq('workspaceHash', '[workspaceHash] unexpected cache entry key name');
expect(cacheEntry.as<string>())
.to.eq(makeHashString(testEnv.projectFolder.location), '[workspaceHash] substitution incorrect');
expect(typeof cacheEntry.value).to.eq('string', '[workspaceHash] unexpected cache entry value type');
}).timeout(100000);

test('Check substitution for "buildType"', async () => {
// Set fake settings
testEnv.config.updatePartial({configureSettings: {buildType: '${buildType}'}});
Expand Down