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.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
|
2017-06-21 14:17:13 +00:00
|
|
|
import * as es from 'event-stream';
|
2018-08-29 13:20:33 +00:00
|
|
|
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';
|
2018-08-29 13:20:33 +00:00
|
|
|
import * as File from 'vinyl';
|
|
|
|
import * as vsce from 'vsce';
|
2018-08-31 09:21:09 +00:00
|
|
|
import { createStatsStream } from './stats';
|
2018-08-29 13:20:33 +00:00
|
|
|
import * as util2 from './util';
|
2016-09-21 13:57:06 +00:00
|
|
|
import remote = require('gulp-remote-src');
|
|
|
|
const vzip = require('gulp-vinyl-zip');
|
2018-10-03 05:10:41 +00:00
|
|
|
import filter = require('gulp-filter');
|
|
|
|
import rename = require('gulp-rename');
|
2016-09-21 13:57:06 +00:00
|
|
|
const util = require('gulp-util');
|
|
|
|
const buffer = require('gulp-buffer');
|
2018-10-03 05:10:41 +00:00
|
|
|
import json = require('gulp-json-editor');
|
2018-08-08 14:53:55 +00:00
|
|
|
const webpack = require('webpack');
|
|
|
|
const webpackGulp = require('webpack-stream');
|
2018-08-21 10:19:10 +00:00
|
|
|
|
|
|
|
const root = path.resolve(path.join(__dirname, '..', '..'));
|
2017-06-21 14:17:13 +00:00
|
|
|
|
2018-09-26 12:41:56 +00:00
|
|
|
function fromLocal(extensionPath: string, sourceMappingURLBase?: string): Stream {
|
2018-09-04 10:29:23 +00:00
|
|
|
const webpackFilename = path.join(extensionPath, 'extension.webpack.config.js');
|
|
|
|
if (fs.existsSync(webpackFilename)) {
|
|
|
|
return fromLocalWebpack(extensionPath, sourceMappingURLBase);
|
|
|
|
} else {
|
|
|
|
return fromLocalNormal(extensionPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 00:19:03 +00:00
|
|
|
function fromLocalWebpack(extensionPath: string, sourceMappingURLBase: string | undefined): Stream {
|
2018-08-08 14:53:55 +00:00
|
|
|
let result = es.through();
|
|
|
|
|
2018-09-04 10:29:23 +00:00
|
|
|
let packagedDependencies: string[] = [];
|
|
|
|
let packageJsonConfig = require(path.join(extensionPath, 'package.json'));
|
|
|
|
let webpackRootConfig = require(path.join(extensionPath, 'extension.webpack.config.js'));
|
|
|
|
for (const key in webpackRootConfig.externals) {
|
|
|
|
if (key in packageJsonConfig.dependencies) {
|
|
|
|
packagedDependencies.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 09:37:40 +00:00
|
|
|
|
2018-09-04 10:29:23 +00:00
|
|
|
vsce.listFiles({ cwd: extensionPath, packageManager: vsce.PackageManager.Yarn, packagedDependencies }).then(fileNames => {
|
2018-08-08 14:53:55 +00:00
|
|
|
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
|
|
|
|
}));
|
|
|
|
|
|
|
|
const filesStream = es.readArray(files);
|
|
|
|
|
2018-08-25 18:48:56 +00:00
|
|
|
// check for a webpack configuration files, then invoke webpack
|
2018-08-08 14:53:55 +00:00
|
|
|
// and merge its output with the files stream. also rewrite the package.json
|
|
|
|
// file to a new entry point
|
2018-09-04 10:29:23 +00:00
|
|
|
const webpackConfigLocations = (<string[]>glob.sync(
|
|
|
|
path.join(extensionPath, '/**/extension.webpack.config.js'),
|
|
|
|
{ ignore: ['**/node_modules'] }
|
|
|
|
));
|
|
|
|
|
|
|
|
const packageJsonFilter = filter(f => {
|
|
|
|
if (path.basename(f.path) === 'package.json') {
|
|
|
|
// only modify package.json's next to the webpack file.
|
|
|
|
// to be safe, use existsSync instead of path comparison.
|
|
|
|
return fs.existsSync(path.join(path.dirname(f.path), 'extension.webpack.config.js'));
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}, { restore: true });
|
|
|
|
|
|
|
|
const patchFilesStream = filesStream
|
|
|
|
.pipe(packageJsonFilter)
|
|
|
|
.pipe(buffer())
|
2018-10-03 05:10:41 +00:00
|
|
|
.pipe(json((data: any) => {
|
2018-09-04 10:29:23 +00:00
|
|
|
// hardcoded entry point directory!
|
|
|
|
data.main = data.main.replace('/out/', /dist/);
|
|
|
|
return data;
|
|
|
|
}))
|
|
|
|
.pipe(packageJsonFilter.restore);
|
|
|
|
|
|
|
|
|
|
|
|
const webpackStreams = webpackConfigLocations.map(webpackConfigPath => {
|
|
|
|
|
2018-10-03 05:10:41 +00:00
|
|
|
const webpackDone = (err: any, stats: any) => {
|
2018-09-04 10:29:23 +00:00
|
|
|
util.log(`Bundled extension: ${util.colors.yellow(path.join(path.basename(extensionPath), path.relative(extensionPath, webpackConfigPath)))}...`);
|
|
|
|
if (err) {
|
|
|
|
result.emit('error', err);
|
|
|
|
}
|
|
|
|
const { compilation } = stats;
|
|
|
|
if (compilation.errors.length > 0) {
|
|
|
|
result.emit('error', compilation.errors.join('\n'));
|
2018-08-27 12:40:46 +00:00
|
|
|
}
|
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' }
|
|
|
|
};
|
|
|
|
let relativeOutputPath = path.relative(extensionPath, webpackConfig.output.path);
|
|
|
|
|
|
|
|
return webpackGulp(webpackConfig, webpack, webpackDone)
|
2018-10-03 22:41:32 +00:00
|
|
|
.pipe(es.through(function (this: any, data) {
|
2018-09-04 10:29:23 +00:00
|
|
|
data.stat = data.stat || {};
|
|
|
|
data.base = extensionPath;
|
|
|
|
this.emit('data', data);
|
2018-08-09 13:31:31 +00:00
|
|
|
}))
|
2018-10-03 22:41:32 +00:00
|
|
|
.pipe(es.through(function (this: any, 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
|
|
|
|
if (sourceMappingURLBase) {
|
|
|
|
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');
|
|
|
|
|
|
|
|
if (/\.js\.map$/.test(data.path)) {
|
|
|
|
if (!fs.existsSync(path.dirname(data.path))) {
|
|
|
|
fs.mkdirSync(path.dirname(data.path));
|
2018-08-13 16:52:12 +00:00
|
|
|
}
|
2018-09-04 10:29:23 +00:00
|
|
|
fs.writeFileSync(data.path, data.contents);
|
2018-08-13 16:23:46 +00:00
|
|
|
}
|
2018-09-04 10:29:23 +00:00
|
|
|
}
|
|
|
|
this.emit('data', data);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
es.merge(...webpackStreams, patchFilesStream)
|
|
|
|
// .pipe(es.through(function (data) {
|
|
|
|
// // debug
|
|
|
|
// console.log('out', data.path, data.contents.length);
|
|
|
|
// this.emit('data', data);
|
|
|
|
// }))
|
|
|
|
.pipe(result);
|
2018-08-08 14:53:55 +00:00
|
|
|
|
2018-09-20 09:37:40 +00:00
|
|
|
}).catch(err => {
|
|
|
|
console.error(extensionPath);
|
|
|
|
console.error(packagedDependencies);
|
|
|
|
result.emit('error', err);
|
|
|
|
});
|
2017-06-21 14:17:13 +00:00
|
|
|
|
2018-09-03 09:51:26 +00:00
|
|
|
return result.pipe(createStatsStream(path.basename(extensionPath)));
|
2017-06-21 14:17:13 +00:00
|
|
|
}
|
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',
|
2018-05-29 06:28:36 +00:00
|
|
|
'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`;
|
|
|
|
|
|
|
|
util.log('Downloading extension:', util.colors.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
|
|
|
}
|
2018-08-21 10:19:10 +00:00
|
|
|
|
|
|
|
interface IPackageExtensionsOptions {
|
|
|
|
/**
|
|
|
|
* Set to undefined to package all of them.
|
|
|
|
*/
|
|
|
|
desiredExtensions?: string[];
|
|
|
|
sourceMappingURLBase?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const excludedExtensions = [
|
|
|
|
'vscode-api-tests',
|
|
|
|
'vscode-colorize-tests',
|
|
|
|
'ms-vscode.node-debug',
|
|
|
|
'ms-vscode.node-debug2',
|
|
|
|
];
|
|
|
|
|
2018-09-21 10:16:23 +00:00
|
|
|
interface IBuiltInExtension {
|
|
|
|
name: string;
|
|
|
|
version: string;
|
|
|
|
repo: string;
|
|
|
|
metadata: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
const builtInExtensions: IBuiltInExtension[] = require('../builtInExtensions.json');
|
2018-08-21 10:19:10 +00:00
|
|
|
|
2018-09-26 13:05:10 +00:00
|
|
|
/**
|
|
|
|
* We're doing way too much stuff at once, with webpack et al. So much stuff
|
|
|
|
* that while downloading extensions from the marketplace, node js doesn't get enough
|
|
|
|
* stack frames to complete the download in under 2 minutes, at which point the
|
|
|
|
* marketplace server cuts off the http request. So, we sequentialize the extensino tasks.
|
|
|
|
*/
|
|
|
|
function sequence(streamProviders: { (): Stream }[]): Stream {
|
|
|
|
const result = es.through();
|
|
|
|
|
|
|
|
function pop() {
|
|
|
|
if (streamProviders.length === 0) {
|
|
|
|
result.emit('end');
|
|
|
|
} else {
|
2018-10-03 00:19:03 +00:00
|
|
|
const fn = streamProviders.shift()!;
|
2018-09-26 13:05:10 +00:00
|
|
|
fn()
|
|
|
|
.on('end', function () { setTimeout(pop, 0); })
|
|
|
|
.pipe(result, { end: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pop();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-10-03 00:19:03 +00:00
|
|
|
export function packageExtensionsStream(optsIn?: IPackageExtensionsOptions): NodeJS.ReadWriteStream {
|
|
|
|
const opts = optsIn || {};
|
2018-08-21 10:19:10 +00:00
|
|
|
|
|
|
|
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 }) => opts.desiredExtensions ? opts.desiredExtensions.indexOf(name) >= 0 : true)
|
|
|
|
.filter(({ name }) => builtInExtensions.every(b => b.name !== name));
|
|
|
|
|
2018-09-26 13:05:10 +00:00
|
|
|
const localExtensions = () => es.merge(...localExtensionDescriptions.map(extension => {
|
2018-08-21 10:19:10 +00:00
|
|
|
return fromLocal(extension.path, opts.sourceMappingURLBase)
|
|
|
|
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
|
|
|
|
}));
|
|
|
|
|
2018-09-26 13:05:10 +00:00
|
|
|
const localExtensionDependencies = () => gulp.src('extensions/node_modules/**', { base: '.' });
|
2018-08-21 10:19:10 +00:00
|
|
|
|
2018-09-26 13:05:10 +00:00
|
|
|
const marketplaceExtensions = () => es.merge(
|
2018-08-21 10:19:10 +00:00
|
|
|
...builtInExtensions
|
|
|
|
.filter(({ name }) => opts.desiredExtensions ? opts.desiredExtensions.indexOf(name) >= 0 : true)
|
|
|
|
.map(extension => {
|
2018-09-21 10:16:23 +00:00
|
|
|
return fromMarketplace(extension.name, extension.version, extension.metadata)
|
2018-08-21 10:19:10 +00:00
|
|
|
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2018-09-26 13:05:10 +00:00
|
|
|
return sequence([localExtensions, localExtensionDependencies, marketplaceExtensions])
|
2018-08-21 10:19:10 +00:00
|
|
|
.pipe(util2.setExecutableBit(['**/*.sh']))
|
|
|
|
.pipe(filter(['**', '!**/*.js.map']));
|
2018-08-29 13:20:33 +00:00
|
|
|
}
|