forked from microsoft/vscode-azurearmtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.ts
374 lines (323 loc) · 16.4 KB
/
gulpfile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// tslint:disable:no-unsafe-any no-console prefer-template no-implicit-dependencies export-name
import * as assert from 'assert';
import * as cp from 'child_process';
import { File } from 'decompress';
import * as fse from 'fs-extra';
import * as glob from 'glob';
import * as gulp from 'gulp';
import * as os from 'os';
import * as path from 'path';
import * as process from 'process';
import * as recursiveReadDir from 'recursive-readdir';
import * as shelljs from 'shelljs';
import { Stream } from 'stream';
import { gulp_webpack } from 'vscode-azureextensiondev';
import { langServerDotnetVersion, languageServerFolderName } from './src/constants';
import { getTempFilePath } from './test/support/getTempFilePath';
// tslint:disable:no-require-imports
import decompress = require('gulp-decompress');
import download = require('gulp-download');
import rimraf = require('rimraf');
// tslint:enable:no-require-imports
const filesAndFoldersToPackage: string[] = [
'dist',
'assets',
'icons',
'AzureRMTools128x128.png',
'CHANGELOG.md',
'main.js',
'NOTICE.html', // License.{md,txt} is handled specially
'node_modules', // Must be present for vsce package to work, but will be ignored during packaging
'README.md',
'.vscodeignore'
];
const env = process.env;
const preserveStagingFolder = !!env.ARMTOOLS_PRESERVE_STAGING_FOLDER;
// Official builds will download and include the language server bits (which are licensed differently than the code in the public repo)
const packageLanguageServer = !env.DISABLE_LANGUAGE_SERVER && !!env.LANGSERVER_NUGET_USERNAME && !!env.LANGSERVER_NUGET_PASSWORD;
const publicLicenseFileName = 'LICENSE.md';
const languageServerLicenseFileName = 'License.txt';
const languageServerVersionEnv = 'npm_package_config_ARM_LANGUAGE_SERVER_NUGET_VERSION'; // Set the value in package.json's config section
const languageServerVersion = env[languageServerVersionEnv];
const languageServerNugetPackage = 'Microsoft.ArmLanguageServer';
const jsonArmGrammarSourcePath: string = path.resolve('grammars', 'JSONC.arm.tmLanguage.json');
const jsonArmGrammarDestPath: string = path.resolve('dist', 'grammars', 'JSONC.arm.tmLanguage.json');
const tleGrammarSourcePath: string = path.resolve('grammars', 'arm-expression-string.tmLanguage.json');
const tleGrammarBuiltPath: string = path.resolve('dist', 'grammars', 'arm-expression-string.tmLanguage.json');
const armConfigurationSourcePath: string = path.resolve('grammars', 'jsonc.arm.language-configuration.json');
const armConfigurationDestPath: string = path.resolve('dist', 'grammars', 'jsonc.arm.language-configuration.json');
interface IGrammar {
preprocess?: {
"builtin-functions": string;
[key: string]: string;
};
[key: string]: unknown;
}
interface IExpressionMetadata {
functionSignatures: {
name: string;
}[];
}
function test(): cp.ChildProcess {
env.DEBUGTELEMETRY = 'verbose';
env.CODE_TESTS_PATH = path.join(__dirname, 'dist/test');
env.MOCHA_timeout = "60000";
return cp.spawn('node', ['./node_modules/vscode/bin/test'], { stdio: 'inherit', env });
}
function buildTLEGrammar(): void {
const sourceGrammar: string = fse.readFileSync(tleGrammarSourcePath).toString();
let grammar: string = sourceGrammar;
const expressionMetadataPath: string = path.resolve("assets/ExpressionMetadata.json");
const expressionMetadata = <IExpressionMetadata>JSON.parse(fse.readFileSync(expressionMetadataPath).toString());
// Add list of built-in functions from our metadata and place at beginning of grammar's preprocess section
let builtinFunctions: string[] = expressionMetadata.functionSignatures.map(sig => sig.name);
let grammarAsObject = <IGrammar>JSON.parse(grammar);
grammarAsObject.preprocess = {
"builtin-functions": `(?:(?i)${builtinFunctions.join('|')})`,
// tslint:disable-next-line: strict-boolean-expressions
... (grammarAsObject.preprocess || {})
};
grammarAsObject = {
$comment: `DO NOT EDIT - This file was built from ${path.relative(__dirname, tleGrammarSourcePath)}`,
...grammarAsObject
};
grammar = JSON.stringify(grammarAsObject, null, 4);
// Get the replacement keys from the preprocess section (ignore those that start with "$")
const replacementKeys = Object.getOwnPropertyNames((<IGrammar>JSON.parse(grammar)).preprocess).filter(key => !key.startsWith("$"));
// Build grammar: Make replacements specified
for (let key of replacementKeys) {
let replacementKey = `{{${key}}}`;
// Re-read value from current grammar contents because the replacement value might contain replacements, too
let value = JSON.parse(grammar).preprocess[key];
let valueString = JSON.stringify(value);
// remove quotes
valueString = valueString.slice(1, valueString.length - 1);
if (!sourceGrammar.includes(replacementKey)) {
console.log(`WARNING: Preprocess key ${replacementKey} is never referenced in ${tleGrammarSourcePath}`);
}
grammar = grammar.replace(new RegExp(replacementKey, "g"), valueString);
}
// Remove preprocess section from the output grammar file
let outputGrammarAsObject = (<IGrammar>JSON.parse(grammar));
delete outputGrammarAsObject.preprocess;
grammar = JSON.stringify(outputGrammarAsObject, null, 4);
fse.writeFileSync(tleGrammarBuiltPath, grammar);
console.log(`Built ${tleGrammarBuiltPath}`);
if (grammar.includes('{{')) {
throw new Error("At least one replacement key could not be found in the grammar - '{{' was found in the final file");
}
}
async function buildGrammars(): Promise<void> {
fse.ensureDirSync('dist');
fse.ensureDirSync('dist/grammars');
buildTLEGrammar();
fse.copyFileSync(jsonArmGrammarSourcePath, jsonArmGrammarDestPath);
console.log(`Copied ${jsonArmGrammarDestPath}`);
fse.copyFileSync(armConfigurationSourcePath, armConfigurationDestPath);
console.log(`Copied ${armConfigurationDestPath}`);
}
function executeInShell(command: string): void {
console.log(command);
const result = shelljs.exec(command);
console.log(result.stdout);
console.log(result.stderr);
if (result.code !== 0) {
throw new Error(`Error executing ${command}`);
}
}
async function getLanguageServer(): Promise<void> {
if (packageLanguageServer) {
console.log(`Retrieving language server ${languageServerNugetPackage}@${languageServerVersion}`);
// Create temporary config file with credentials
const config = fse.readFileSync(path.join(__dirname, 'NuGet.Config')).toString();
const withCreds = config.
// tslint:disable-next-line: strict-boolean-expressions
replace('$LANGSERVER_NUGET_USERNAME', env.LANGSERVER_NUGET_USERNAME || '').
// tslint:disable-next-line: strict-boolean-expressions
replace('$LANGSERVER_NUGET_PASSWORD', env.LANGSERVER_NUGET_PASSWORD || '');
const configPath = getTempFilePath('nuget', '.config');
fse.writeFileSync(configPath, withCreds);
// Nuget install to pkgs folder
let app = 'nuget';
const args = [
'install',
languageServerNugetPackage,
'-Version', languageServerVersion,
'-Framework', `netcoreapp${langServerDotnetVersion}`,
'-OutputDirectory', 'pkgs',
//'-Verbosity', 'detailed',
'-ExcludeVersion', // Keeps the package version from being included in the output folder name
'-NonInteractive',
'-ConfigFile', configPath
];
if (os.platform() === 'linux') {
app = 'mono';
args.unshift('nuget.exe');
}
const command = `${app} ${args.join(' ')}`;
executeInShell(command);
fse.unlinkSync(configPath);
// Copy binaries and license into dist\languageServer
console.log(`Removing ${languageServerFolderName}`);
rimraf.sync(languageServerFolderName);
console.log(`Copying language server binaries to ${languageServerFolderName}`);
const srcPath = path.join(__dirname, 'pkgs', languageServerNugetPackage, 'lib', `netcoreapp${langServerDotnetVersion}`);
let destPath = path.join(__dirname, languageServerFolderName);
fse.mkdirpSync(destPath);
copyFolder(srcPath, destPath);
if (packageLanguageServer) {
const licenseSrc = path.join(__dirname, 'pkgs', languageServerNugetPackage, languageServerLicenseFileName);
const licenseDest = path.join(languageServerFolderName, languageServerLicenseFileName);
console.log(`Copying language server license ${licenseSrc} to ${licenseDest}`);
fse.copyFileSync(licenseSrc, licenseDest);
}
console.log(`Language server binaries and license are in ${languageServerFolderName}`);
} else {
console.warn(`Language server not available, skipping packaging of language server binaries.`);
}
}
function copyFolder(sourceFolder: string, destFolder: string, sourceRoot: string = sourceFolder): void {
fse.ensureDirSync(destFolder);
fse.readdirSync(sourceFolder).forEach(fn => {
let src = path.join(sourceFolder, fn);
let dest = path.join(destFolder, fn);
if (fse.statSync(src).isFile()) {
fse.copyFileSync(src, dest);
} else if (fse.statSync(src).isDirectory()) {
copyFolder(src, dest, sourceRoot);
} else {
assert("Unexpected path type");
}
});
}
async function packageVsix(): Promise<void> {
// We use a staging folder so we have more control over exactly what goes into the .vsix
function copyToStagingFolder(relativeSource: string, relativeDest: string = relativeSource): void {
console.log(`Copying ${relativeSource} to staging folder`);
if (fse.statSync(relativeSource).isDirectory()) {
copyFolder(path.join(__dirname, relativeSource), path.join(stagingFolder, relativeDest));
} else {
fse.copyFileSync(path.join(__dirname, relativeSource), path.join(stagingFolder, relativeDest));
}
}
let stagingFolder = getTempFilePath('staging', '');
console.log(`Staging folder: ${stagingFolder}`);
fse.mkdirpSync(stagingFolder);
// Copy files/folders to staging
for (let p of filesAndFoldersToPackage) {
copyToStagingFolder(p);
}
let filesInStaging = fse.readdirSync(stagingFolder);
filesInStaging.forEach(fn => assert(!/license/i.test(fn), `Should not have copied the original license file into staging: ${fn}`));
let expectedLicenseFileName: string;
if (packageLanguageServer) {
// Copy language server bits
copyToStagingFolder(languageServerFolderName);
// Copy license to staging main folder
copyToStagingFolder(path.join(languageServerFolderName, languageServerLicenseFileName), languageServerLicenseFileName);
expectedLicenseFileName = languageServerLicenseFileName;
} else {
copyToStagingFolder(publicLicenseFileName);
expectedLicenseFileName = publicLicenseFileName;
}
// Copy package.json to staging
let packageContents = fse.readJsonSync(path.join(__dirname, 'package.json'));
assert(packageContents.license, "package.json doesn't contain a license field?");
// ... modify package.json to point to language server license
packageContents.license = `SEE LICENSE IN ${expectedLicenseFileName}`;
// ... remove vscode:prepublish script from package, since everything's already built
delete packageContents.scripts['vscode:prepublish'];
fse.writeFileSync(path.join(stagingFolder, 'package.json'), JSON.stringify(packageContents, null, 2));
assert(fse.readFileSync(path.join(stagingFolder, 'package.json')).toString().includes(`"license": "SEE LICENSE IN ${expectedLicenseFileName}"`), "Language server license not correctly installed into staged package.json");
try {
console.log(`Running vsce package in staging folder ${stagingFolder}`);
shelljs.cd(stagingFolder);
executeInShell('vsce package');
} finally {
shelljs.cd(__dirname);
}
// Copy vsix to current folder
let vsixName = fse.readdirSync(stagingFolder).find(fn => /\.vsix$/.test(fn));
if (!vsixName) {
throw new Error("Couldn't find a .vsix file");
}
let vsixDestPath = path.join(__dirname, vsixName);
if (!packageLanguageServer) {
vsixDestPath = vsixDestPath.replace('.vsix', '-no-languageserver.vsix');
}
if (fse.existsSync(vsixDestPath)) {
fse.unlinkSync(vsixDestPath);
}
fse.copyFileSync(path.join(stagingFolder, vsixName), vsixDestPath);
// Remove staging folder since packaging was a success
if (!preserveStagingFolder) {
rimraf.sync(stagingFolder);
}
console.log(`Packaged successfully to ${vsixDestPath}`);
}
// When webpacked, the tests cannot touch any code under src/, or it will end up getting loaded
// twice (because it's also in the bundle), which causes problems with objects that are supposed to
// be singletons. The test errors are somewhat mysterious, so verify that condition here during build.
async function verifyTestsReferenceOnlyExtensionBundle(testFolder: string): Promise<void> {
const errors: string[] = [];
for (let filePath of await recursiveReadDir(testFolder)) {
await verifyFile(filePath);
}
async function verifyFile(file: string): Promise<void> {
const regex = /import .*['"]\.\.\/(\.\.\/)?src\/.*['"]/mg;
if (path.extname(file) === ".ts") {
const contents: string = (await fse.readFile(file)).toString();
const matches = contents.match(regex);
if (matches) {
for (let match of matches) {
errors.push(
os.EOL +
`${path.relative(__dirname, file)}: error: Test code may not import from the src folder, it should import from '../extension.bundle'${os.EOL}` +
` Error is here: ===> ${match}${os.EOL}`
);
console.error(match);
}
}
}
}
if (errors.length > 0) {
throw new Error(errors.join("\n"));
}
}
export function gulp_installDotNetExtension(): Promise<void> | Stream {
const extensionName = '.NET Install Tool for Extension Authors';
console.log(`Installing ${extensionName}`);
const version: string = '0.1.0';
const extensionPath: string = path.join(os.homedir(), `.vscode/extensions/ms-dotnettools.vscode-dotnet-runtime-${version}`);
console.log(extensionPath);
const existingExtensions: string[] = glob.sync(extensionPath.replace(version, '*'));
if (existingExtensions.length === 0) {
// tslint:disable-next-line:no-http-string
return download(`http://ms-vscode.gallery.vsassets.io/_apis/public/gallery/publisher/ms-dotnettools/extension/vscode-dotnet-runtime/${version}/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage`)
.pipe(decompress({
filter: (file: File): boolean => file.path.startsWith('extension/'),
map: (file: File): File => {
file.path = file.path.slice(10);
return file;
}
}))
.pipe(gulp.dest(extensionPath));
} else {
console.log(`${extensionName} already installed.`);
// We need to signal to gulp that we've completed this async task
return Promise.resolve();
}
}
exports['webpack-dev'] = gulp.series(() => gulp_webpack('development'), buildGrammars);
exports['webpack-prod'] = gulp.series(() => gulp_webpack('production'), buildGrammars);
exports.test = gulp.series(gulp_installDotNetExtension, test);
exports['build-grammars'] = buildGrammars;
exports['watch-grammars'] = (): unknown => gulp.watch('grammars/**', buildGrammars);
exports['get-language-server'] = getLanguageServer;
exports.package = packageVsix;
exports['error-vsce-package'] = (): never => { throw new Error(`Please do not run vsce package, instead use 'npm run package`); };
exports['verify-test-uses-extension-bundle'] = (): Promise<void> => verifyTestsReferenceOnlyExtensionBundle(path.resolve("test"));