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 assign = require('object-assign');
|
|
|
|
import remote = require('gulp-remote-src');
|
|
|
|
const flatmap = require('gulp-flatmap');
|
|
|
|
const vzip = require('gulp-vinyl-zip');
|
|
|
|
const filter = require('gulp-filter');
|
|
|
|
const rename = require('gulp-rename');
|
|
|
|
const util = require('gulp-util');
|
|
|
|
const buffer = require('gulp-buffer');
|
|
|
|
const 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-08-10 13:36:44 +00:00
|
|
|
export function fromLocal(extensionPath: string, sourceMappingURLBase?: string): Stream {
|
2018-08-08 14:53:55 +00:00
|
|
|
let 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
|
|
|
|
}));
|
|
|
|
|
|
|
|
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-08-25 18:48:56 +00:00
|
|
|
const pattern = path.join(extensionPath, '/**/extension.webpack.config.js');
|
|
|
|
const webpackConfigLocations = (<string[]>glob.sync(pattern, { ignore: ['**/node_modules'] }));
|
|
|
|
if (webpackConfigLocations.length) {
|
|
|
|
|
2018-08-27 12:40:46 +00:00
|
|
|
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 });
|
2017-06-21 14:17:13 +00:00
|
|
|
|
2018-08-08 14:53:55 +00:00
|
|
|
const patchFilesStream = filesStream
|
|
|
|
.pipe(packageJsonFilter)
|
|
|
|
.pipe(buffer())
|
2018-08-09 13:31:31 +00:00
|
|
|
.pipe(json(data => {
|
|
|
|
// hardcoded entry point directory!
|
|
|
|
data.main = data.main.replace('/out/', /dist/);
|
|
|
|
return data;
|
|
|
|
}))
|
2018-08-08 14:53:55 +00:00
|
|
|
.pipe(packageJsonFilter.restore);
|
|
|
|
|
2018-08-29 13:20:33 +00:00
|
|
|
|
2018-08-25 18:48:56 +00:00
|
|
|
const webpackStreams = webpackConfigLocations.map(webpackConfigPath => {
|
2018-08-29 15:53:47 +00:00
|
|
|
|
|
|
|
const webpackDone = (err, stats) => {
|
2018-08-29 17:05:58 +00:00
|
|
|
util.log(`Bundled extension: ${util.colors.yellow(path.join(path.basename(extensionPath), path.relative(extensionPath, webpackConfigPath)))}...`);
|
2018-08-29 15:53:47 +00:00
|
|
|
if (err) {
|
|
|
|
result.emit('error', err);
|
|
|
|
}
|
|
|
|
const { compilation } = stats;
|
|
|
|
if (compilation.errors.length > 0) {
|
|
|
|
result.emit('error', compilation.errors.join('\n'));
|
|
|
|
}
|
|
|
|
if (compilation.warnings.length > 0) {
|
|
|
|
result.emit('error', compilation.warnings.join('\n'));
|
|
|
|
}
|
|
|
|
};
|
2018-08-29 13:20:33 +00:00
|
|
|
|
2018-08-25 18:48:56 +00:00
|
|
|
const webpackConfig = {
|
|
|
|
...require(webpackConfigPath),
|
2018-08-29 13:20:33 +00:00
|
|
|
...{ mode: 'production' }
|
2018-08-25 18:48:56 +00:00
|
|
|
};
|
|
|
|
let relativeOutputPath = path.relative(extensionPath, webpackConfig.output.path);
|
|
|
|
|
2018-08-29 13:20:33 +00:00
|
|
|
return webpackGulp(webpackConfig, webpack, webpackDone)
|
2018-08-25 18:48:56 +00:00
|
|
|
.pipe(es.through(function (data) {
|
|
|
|
data.stat = data.stat || {};
|
|
|
|
data.base = extensionPath;
|
|
|
|
this.emit('data', data);
|
|
|
|
}))
|
|
|
|
.pipe(es.through(function (data: File) {
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
fs.writeFileSync(data.path, data.contents);
|
2018-08-13 16:52:12 +00:00
|
|
|
}
|
2018-08-13 16:23:46 +00:00
|
|
|
}
|
2018-08-25 18:48:56 +00:00
|
|
|
this.emit('data', data);
|
|
|
|
}));
|
|
|
|
});
|
2017-06-21 14:17:13 +00:00
|
|
|
|
2018-08-25 18:48:56 +00:00
|
|
|
es.merge(...webpackStreams, patchFilesStream)
|
2018-08-08 14:53:55 +00:00
|
|
|
// .pipe(es.through(function (data) {
|
|
|
|
// // debug
|
2018-08-10 13:36:44 +00:00
|
|
|
// console.log('out', data.path, data.contents.length);
|
2018-08-08 14:53:55 +00:00
|
|
|
// this.emit('data', data);
|
|
|
|
// }))
|
|
|
|
.pipe(result);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
filesStream.pipe(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
}).catch(err => result.emit('error', err));
|
2017-06-21 14:17:13 +00:00
|
|
|
|
2018-08-31 11:28:24 +00:00
|
|
|
return result.pipe(createStatsStream(path.basename(extensionPath), true));
|
2017-06-21 14:17:13 +00:00
|
|
|
}
|
2016-09-21 13:57:06 +00:00
|
|
|
|
|
|
|
function error(err: any): Stream {
|
2017-06-21 14:17:13 +00:00
|
|
|
const result = es.through();
|
2016-09-21 13:57:06 +00:00
|
|
|
setTimeout(() => result.emit('error', err));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2017-06-21 14:17:13 +00:00
|
|
|
export function fromMarketplace(extensionName: string, version: string): Stream {
|
2016-09-21 13:57:06 +00:00
|
|
|
const filterType = 7;
|
|
|
|
const value = extensionName;
|
|
|
|
const criterium = { filterType, value };
|
|
|
|
const criteria = [criterium];
|
|
|
|
const pageNumber = 1;
|
|
|
|
const pageSize = 1;
|
|
|
|
const sortBy = 0;
|
|
|
|
const sortOrder = 0;
|
|
|
|
const flags = 0x1 | 0x2 | 0x80;
|
|
|
|
const assetTypes = ['Microsoft.VisualStudio.Services.VSIXPackage'];
|
|
|
|
const filters = [{ criteria, pageNumber, pageSize, sortBy, sortOrder }];
|
|
|
|
const body = JSON.stringify({ filters, assetTypes, flags });
|
|
|
|
const headers: any = assign({}, baseHeaders, {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Accept': 'application/json;api-version=3.0-preview.1',
|
|
|
|
'Content-Length': body.length
|
|
|
|
});
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
base: 'https://marketplace.visualstudio.com/_apis/public/gallery',
|
|
|
|
requestOptions: {
|
|
|
|
method: 'POST',
|
|
|
|
gzip: true,
|
|
|
|
headers,
|
|
|
|
body: body
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return remote('/extensionquery', options)
|
|
|
|
.pipe(flatmap((stream, f) => {
|
|
|
|
const rawResult = f.contents.toString('utf8');
|
|
|
|
const result = JSON.parse(rawResult);
|
|
|
|
const extension = result.results[0].extensions[0];
|
|
|
|
if (!extension) {
|
2017-03-06 15:32:19 +00:00
|
|
|
return error(`No such extension: ${extension}`);
|
2016-09-21 13:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const metadata = {
|
|
|
|
id: extension.extensionId,
|
|
|
|
publisherId: extension.publisher,
|
|
|
|
publisherDisplayName: extension.publisher.displayName
|
|
|
|
};
|
|
|
|
|
|
|
|
const extensionVersion = extension.versions.filter(v => v.version === version)[0];
|
|
|
|
if (!extensionVersion) {
|
2017-03-06 15:32:19 +00:00
|
|
|
return error(`No such extension version: ${extensionName} @ ${version}`);
|
2016-09-21 13:57:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const asset = extensionVersion.files.filter(f => f.assetType === 'Microsoft.VisualStudio.Services.VSIXPackage')[0];
|
|
|
|
if (!asset) {
|
2017-03-06 15:32:19 +00:00
|
|
|
return error(`No VSIX found for extension version: ${extensionName} @ ${version}`);
|
2016-09-21 13:57:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 15:32:19 +00:00
|
|
|
util.log('Downloading extension:', util.colors.yellow(`${extensionName}@${version}`), '...');
|
2016-09-21 13:57:06 +00:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
base: asset.source,
|
|
|
|
requestOptions: {
|
|
|
|
gzip: true,
|
|
|
|
headers: baseHeaders
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return remote('', options)
|
|
|
|
.pipe(flatmap(stream => {
|
|
|
|
const packageJsonFilter = filter('package.json', { restore: true });
|
|
|
|
|
|
|
|
return stream
|
|
|
|
.pipe(vzip.src())
|
|
|
|
.pipe(filter('extension/**'))
|
|
|
|
.pipe(rename(p => p.dirname = p.dirname.replace(/^extension\/?/, '')))
|
|
|
|
.pipe(packageJsonFilter)
|
|
|
|
.pipe(buffer())
|
|
|
|
.pipe(json({ __metadata: metadata }))
|
|
|
|
.pipe(packageJsonFilter.restore);
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
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',
|
|
|
|
];
|
|
|
|
|
|
|
|
const builtInExtensions: { name: string, version: string, repo: string; }[] = require('../builtInExtensions.json');
|
|
|
|
|
|
|
|
export function packageExtensionsStream(opts?: IPackageExtensionsOptions): NodeJS.ReadWriteStream {
|
|
|
|
opts = opts || {};
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
const localExtensions = es.merge(...localExtensionDescriptions.map(extension => {
|
|
|
|
return fromLocal(extension.path, opts.sourceMappingURLBase)
|
|
|
|
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
|
|
|
|
}));
|
|
|
|
|
|
|
|
const localExtensionDependencies = gulp.src('extensions/node_modules/**', { base: '.' });
|
|
|
|
|
|
|
|
const marketplaceExtensions = es.merge(
|
|
|
|
...builtInExtensions
|
|
|
|
.filter(({ name }) => opts.desiredExtensions ? opts.desiredExtensions.indexOf(name) >= 0 : true)
|
|
|
|
.map(extension => {
|
|
|
|
return fromMarketplace(extension.name, extension.version)
|
|
|
|
.pipe(rename(p => p.dirname = `extensions/${extension.name}/${p.dirname}`));
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return es.merge(localExtensions, localExtensionDependencies, marketplaceExtensions)
|
|
|
|
.pipe(util2.setExecutableBit(['**/*.sh']))
|
|
|
|
.pipe(filter(['**', '!**/*.js.map']));
|
2018-08-29 13:20:33 +00:00
|
|
|
}
|