vscode/build/lib/extensions.ts

294 lines
9.8 KiB
TypeScript
Raw Normal View History

2016-09-21 13:57:06 +00:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as es from 'event-stream';
import * as fs from 'fs';
import * as glob from 'glob';
import * as gulp from 'gulp';
import * as path from 'path';
2016-09-21 13:57:06 +00:00
import { Stream } from 'stream';
import * as File from 'vinyl';
import * as vsce from 'vsce';
import { createStatsStream } from './stats';
import * as util2 from './util';
2019-07-26 14:08:27 +00:00
import remote = require('gulp-remote-retry-src');
2016-09-21 13:57:06 +00:00
const vzip = require('gulp-vinyl-zip');
2018-10-03 05:10:41 +00:00
import filter = require('gulp-filter');
import rename = require('gulp-rename');
2019-02-05 21:21:05 +00:00
import * as fancyLog from 'fancy-log';
import * as ansiColors from 'ansi-colors';
2016-09-21 13:57:06 +00:00
const buffer = require('gulp-buffer');
2018-10-03 05:10:41 +00:00
import json = require('gulp-json-editor');
const webpack = require('webpack');
const webpackGulp = require('webpack-stream');
2019-07-01 10:01:47 +00:00
const util = require('./util');
const root = path.dirname(path.dirname(__dirname));
const commit = util.getVersion(root);
const sourceMappingURLBase = `https://ticino.blob.core.windows.net/sourcemaps/${commit}`;
2020-06-10 12:20:51 +00:00
function minimizeLanguageJSON(input: Stream): Stream {
2019-08-12 14:45:12 +00:00
const tmLanguageJsonFilter = filter('**/*.tmLanguage.json', { restore: true });
return input
.pipe(tmLanguageJsonFilter)
.pipe(buffer())
.pipe(es.mapSync((f: File) => {
f.contents = Buffer.from(JSON.stringify(JSON.parse(f.contents.toString('utf8'))));
return f;
}))
.pipe(tmLanguageJsonFilter.restore);
2018-09-04 10:29:23 +00:00
}
function updateExtensionPackageJSON(input: Stream, update: (data: any) => any): Stream {
const packageJsonFilter = filter('extensions/*/package.json', { restore: true });
2020-06-10 12:20:51 +00:00
return input
.pipe(packageJsonFilter)
.pipe(buffer())
.pipe(es.mapSync((f: File) => {
const data = JSON.parse(f.contents.toString('utf8'));
f.contents = Buffer.from(JSON.stringify(update(data)));
return f;
}))
.pipe(packageJsonFilter.restore);
}
function fromLocal(extensionPath: string, forWeb: boolean): Stream {
const webpackConfigFileName = forWeb ? 'extension-browser.webpack.config.js' : 'extension.webpack.config.js';
const isWebPacked = fs.existsSync(path.join(extensionPath, webpackConfigFileName));
let input = isWebPacked
? fromLocalWebpack(extensionPath, webpackConfigFileName)
: fromLocalNormal(extensionPath);
if (forWeb) {
input = updateExtensionPackageJSON(input, (data: any) => {
2020-06-10 12:20:51 +00:00
if (data.browser) {
data.main = data.browser;
}
data.extensionKind = ['web'];
return data;
});
} else if (isWebPacked) {
input = updateExtensionPackageJSON(input, (data: any) => {
2020-06-10 12:20:51 +00:00
if (data.main) {
data.main = data.main.replace('/out/', /dist/);
}
return data;
});
}
return minimizeLanguageJSON(input)
}
function fromLocalWebpack(extensionPath: string, webpackConfigFileName: string): Stream {
2018-10-04 21:04:23 +00:00
const result = es.through();
2018-10-04 21:04:23 +00:00
const packagedDependencies: string[] = [];
const packageJsonConfig = require(path.join(extensionPath, 'package.json'));
2018-11-16 08:04:02 +00:00
if (packageJsonConfig.dependencies) {
2020-06-10 12:20:51 +00:00
const webpackRootConfig = require(path.join(extensionPath, webpackConfigFileName));
2018-11-16 08:04:02 +00:00
for (const key in webpackRootConfig.externals) {
if (key in packageJsonConfig.dependencies) {
packagedDependencies.push(key);
}
2018-09-04 10:29:23 +00:00
}
}
vsce.listFiles({ cwd: extensionPath, packageManager: vsce.PackageManager.Yarn, packagedDependencies }).then(fileNames => {
const files = fileNames
.map(fileName => path.join(extensionPath, fileName))
.map(filePath => new File({
path: filePath,
stat: fs.statSync(filePath),
base: extensionPath,
contents: fs.createReadStream(filePath) as any
}));
2018-08-25 18:48:56 +00:00
// check for a webpack configuration files, then invoke webpack
2020-06-10 12:20:51 +00:00
// and merge its output with the files stream.
2018-09-04 10:29:23 +00:00
const webpackConfigLocations = (<string[]>glob.sync(
2020-06-10 12:20:51 +00:00
path.join(extensionPath, '**', webpackConfigFileName),
2018-09-04 10:29:23 +00:00
{ ignore: ['**/node_modules'] }
));
2019-07-05 14:11:50 +00:00
const webpackStreams = webpackConfigLocations.map(webpackConfigPath => {
2018-09-04 10:29:23 +00:00
2018-10-03 05:10:41 +00:00
const webpackDone = (err: any, stats: any) => {
2019-02-05 21:21:05 +00:00
fancyLog(`Bundled extension: ${ansiColors.yellow(path.join(path.basename(extensionPath), path.relative(extensionPath, webpackConfigPath)))}...`);
2018-09-04 10:29:23 +00:00
if (err) {
result.emit('error', err);
}
const { compilation } = stats;
if (compilation.errors.length > 0) {
result.emit('error', compilation.errors.join('\n'));
}
2018-09-04 10:29:23 +00:00
if (compilation.warnings.length > 0) {
result.emit('error', compilation.warnings.join('\n'));
}
};
const webpackConfig = {
...require(webpackConfigPath),
...{ mode: 'production' }
};
2018-10-04 21:04:23 +00:00
const relativeOutputPath = path.relative(extensionPath, webpackConfig.output.path);
2018-09-04 10:29:23 +00:00
return webpackGulp(webpackConfig, webpack, webpackDone)
.pipe(es.through(function (data) {
2018-09-04 10:29:23 +00:00
data.stat = data.stat || {};
data.base = extensionPath;
this.emit('data', data);
}))
.pipe(es.through(function (data: File) {
2018-09-04 10:29:23 +00:00
// source map handling:
// * rewrite sourceMappingURL
// * save to disk so that upload-task picks this up
const contents = (<Buffer>data.contents).toString('utf8');
data.contents = Buffer.from(contents.replace(/\n\/\/# sourceMappingURL=(.*)$/gm, function (_m, g1) {
return `\n//# sourceMappingURL=${sourceMappingURLBase}/extensions/${path.basename(extensionPath)}/${relativeOutputPath}/${g1}`;
}), 'utf8');
2018-09-04 10:29:23 +00:00
this.emit('data', data);
}));
});
2020-06-10 12:20:51 +00:00
es.merge(...webpackStreams, es.readArray(files))
2018-09-04 10:29:23 +00:00
// .pipe(es.through(function (data) {
// // debug
// console.log('out', data.path, data.contents.length);
// this.emit('data', data);
// }))
.pipe(result);
2018-09-20 09:37:40 +00:00
}).catch(err => {
console.error(extensionPath);
console.error(packagedDependencies);
result.emit('error', err);
});
return result.pipe(createStatsStream(path.basename(extensionPath)));
}
2016-09-21 13:57:06 +00:00
2018-09-04 10:29:23 +00:00
function fromLocalNormal(extensionPath: string): Stream {
const result = es.through();
vsce.listFiles({ cwd: extensionPath, packageManager: vsce.PackageManager.Yarn })
.then(fileNames => {
const files = fileNames
.map(fileName => path.join(extensionPath, fileName))
.map(filePath => new File({
path: filePath,
stat: fs.statSync(filePath),
base: extensionPath,
contents: fs.createReadStream(filePath) as any
}));
es.readArray(files).pipe(result);
})
.catch(err => result.emit('error', err));
2018-09-07 09:33:53 +00:00
return result.pipe(createStatsStream(path.basename(extensionPath)));
2018-09-04 10:29:23 +00:00
}
2016-09-21 13:57:06 +00:00
const baseHeaders = {
'X-Market-Client-Id': 'VSCode Build',
'User-Agent': 'VSCode Build',
'X-Market-User-Id': '291C1CD0-051A-4123-9B4B-30D60EF52EE2',
2016-09-21 13:57:06 +00:00
};
2018-09-21 10:16:23 +00:00
export function fromMarketplace(extensionName: string, version: string, metadata: any): Stream {
const [publisher, name] = extensionName.split('.');
const url = `https://marketplace.visualstudio.com/_apis/public/gallery/publishers/${publisher}/vsextensions/${name}/${version}/vspackage`;
2019-02-05 21:21:05 +00:00
fancyLog('Downloading extension:', ansiColors.yellow(`${extensionName}@${version}`), '...');
2016-09-21 13:57:06 +00:00
const options = {
2018-09-21 10:16:23 +00:00
base: url,
2016-09-21 13:57:06 +00:00
requestOptions: {
gzip: true,
2018-09-21 10:16:23 +00:00
headers: baseHeaders
2016-09-21 13:57:06 +00:00
}
};
2018-09-26 12:41:56 +00:00
const packageJsonFilter = filter('package.json', { restore: true });
2018-09-21 10:16:23 +00:00
return remote('', options)
2018-09-26 12:41:56 +00:00
.pipe(vzip.src())
.pipe(filter('extension/**'))
2018-10-03 05:10:41 +00:00
.pipe(rename(p => p.dirname = p.dirname!.replace(/^extension\/?/, '')))
2018-09-26 12:41:56 +00:00
.pipe(packageJsonFilter)
.pipe(buffer())
.pipe(json({ __metadata: metadata }))
.pipe(packageJsonFilter.restore);
2016-09-21 13:57:06 +00:00
}
const excludedExtensions = [
'vscode-api-tests',
'vscode-web-playground',
'vscode-colorize-tests',
2019-04-11 10:34:05 +00:00
'vscode-test-resolver',
'ms-vscode.node-debug',
'ms-vscode.node-debug2',
2020-04-23 18:55:23 +00:00
'vscode-notebook-tests'
];
2018-09-21 10:16:23 +00:00
interface IBuiltInExtension {
name: string;
version: string;
repo: string;
metadata: any;
}
const builtInExtensions: IBuiltInExtension[] = JSON.parse(fs.readFileSync(path.join(__dirname, '../../product.json'), 'utf8')).builtInExtensions;
2019-07-05 14:11:50 +00:00
export function packageLocalExtensionsStream(): NodeJS.ReadWriteStream {
const localExtensionDescriptions = (<string[]>glob.sync('extensions/*/package.json'))
.map(manifestPath => {
const extensionPath = path.dirname(path.join(root, manifestPath));
const extensionName = path.basename(extensionPath);
return { name: extensionName, path: extensionPath };
})
.filter(({ name }) => excludedExtensions.indexOf(name) === -1)
.filter(({ name }) => builtInExtensions.every(b => b.name !== name));
2020-06-10 12:20:51 +00:00
2019-07-14 08:31:07 +00:00
const localExtensions = localExtensionDescriptions.map(extension => {
2020-06-10 12:20:51 +00:00
return fromLocal(extension.path, false)
2019-07-14 08:31:07 +00:00
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
});
2020-06-10 12:20:51 +00:00
const nodeModules = gulp.src('extensions/node_modules/**', { base: '.' });
2019-07-14 08:31:07 +00:00
return es.merge(nodeModules, ...localExtensions)
.pipe(util2.setExecutableBit(['**/*.sh']));
2019-07-05 14:11:50 +00:00
}
2020-06-10 12:20:51 +00:00
export function packageLocalWebExtensionsStream(): NodeJS.ReadWriteStream {
const localExtensionDescriptions = (<string[]>glob.sync('extensions/*/package.json'))
.filter(manifestPath => {
const packageJsonConfig = require(path.join(root, manifestPath));
return !packageJsonConfig.main || packageJsonConfig.browser;
})
.map(manifestPath => {
const extensionPath = path.dirname(path.join(root, manifestPath));
const extensionName = path.basename(extensionPath);
return { name: extensionName, path: extensionPath };
});
return es.merge(...localExtensionDescriptions.map(extension => {
return fromLocal(extension.path, true)
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
}));
}
2019-07-05 14:11:50 +00:00
export function packageMarketplaceExtensionsStream(): NodeJS.ReadWriteStream {
2019-07-14 08:31:07 +00:00
const extensions = builtInExtensions.map(extension => {
2019-07-05 14:11:50 +00:00
return fromMarketplace(extension.name, extension.version, extension.metadata)
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
2019-07-14 08:31:07 +00:00
});
return es.merge(extensions)
.pipe(util2.setExecutableBit(['**/*.sh']));
}