bootstrap - move workbench.js to electron-code

This commit is contained in:
Benjamin Pasero 2018-09-10 07:48:52 +02:00
parent 9685411309
commit 1e98303d91
9 changed files with 111 additions and 77 deletions

2
.vscode/launch.json vendored
View file

@ -145,7 +145,7 @@
"linux": {
"runtimeExecutable": "${workspaceFolder}/scripts/code.sh"
},
"urlFilter": "*index.html*",
"urlFilter": "*workbench.html*",
"runtimeArgs": [
"--inspect=5875"
],

View file

@ -64,7 +64,6 @@ const vscodeResources = [
'out-build/vs/base/node/{stdForkStart.js,terminateProcess.sh,cpuUsage.sh}',
'out-build/vs/base/browser/ui/octiconLabel/octicons/**',
'out-build/vs/workbench/browser/media/*-theme.css',
'out-build/vs/workbench/electron-browser/bootstrap/**',
'out-build/vs/workbench/parts/debug/**/*.json',
'out-build/vs/workbench/parts/execution/**/*.scpt',
'out-build/vs/workbench/parts/webview/electron-browser/webview-pre.js',
@ -73,6 +72,7 @@ const vscodeResources = [
'out-build/vs/workbench/parts/welcome/walkThrough/**/*.md',
'out-build/vs/workbench/services/files/**/*.exe',
'out-build/vs/workbench/services/files/**/*.md',
'out-build/vs/code/electron-browser/workbench/**',
'out-build/vs/code/electron-browser/sharedProcess/sharedProcess.js',
'out-build/vs/code/electron-browser/issue/issueReporter.js',
'out-build/vs/code/electron-browser/processExplorer/processExplorer.js',
@ -99,7 +99,7 @@ gulp.task('optimize-vscode', ['clean-optimized-vscode', 'compile-build', 'compil
gulp.task('optimize-index-js', ['optimize-vscode'], () => {
const fullpath = path.join(process.cwd(), 'out-vscode/vs/workbench/electron-browser/bootstrap/index.js');
const fullpath = path.join(process.cwd(), 'out-vscode/vs/code/electron-browser/workbench/workbench.js');
const contents = fs.readFileSync(fullpath).toString();
const newContents = contents.replace('[/*BUILD->INSERT_NODE_MODULES*/]', JSON.stringify(nodeModules));
fs.writeFileSync(fullpath, newContents);
@ -214,8 +214,8 @@ function packageTask(platform, arch, opts) {
const checksums = computeChecksums(out, [
'vs/workbench/workbench.main.js',
'vs/workbench/workbench.main.css',
'vs/workbench/electron-browser/bootstrap/index.html',
'vs/workbench/electron-browser/bootstrap/index.js'
'vs/code/electron-browser/workbench/workbench.html',
'vs/code/electron-browser/workbench/workbench.js'
]);
const src = gulp.src(out + '/**', { base: '.' })

View file

@ -8,16 +8,6 @@
const bootstrap = require('./bootstrap');
exports.parseURLQueryArgs = function () {
const search = window.location.search || '';
return search.split(/[?&]/)
.filter(function (param) { return !!param; })
.map(function (param) { return param.split('='); })
.filter(function (param) { return param.length === 2; })
.reduce(function (r, param) { r[param[0]] = decodeURIComponent(param[1]); return r; }, {});
};
/**
* @param {object} destination
* @param {object} source
@ -30,20 +20,23 @@ exports.assign = function assign(destination, source) {
/**
*
* @param {string[]} modulePaths
* @param {(result, configuration) => any} resultCallback
* @param {{ forceEnableDeveloperKeybindings?: boolean, removeDeveloperKeybindingsAfterLoad?: boolean, canModifyDOM?: (config) => void, beforeLoaderConfig?: (config, loaderConfig) => void, beforeRequire?: () => void }=} options
* @param {(result, configuration: object) => any} resultCallback
* @param {{ forceEnableDeveloperKeybindings?: boolean, removeDeveloperKeybindingsAfterLoad?: boolean, canModifyDOM?: (config: object) => void, beforeLoaderConfig?: (config: object, loaderConfig: object) => void, beforeRequire?: () => void }=} options
*/
exports.load = function (modulePaths, resultCallback, options) {
// @ts-ignore
const webFrame = require('electron').webFrame;
const path = require('path');
const args = exports.parseURLQueryArgs();
const args = parseURLQueryArgs();
const configuration = JSON.parse(args['config'] || '{}') || {};
// Error handler
// @ts-ignore
process.on('uncaughtException', function (error) { onUnexpectedError(error, enableDeveloperTools); });
process.on('uncaughtException', function (error) {
onUnexpectedError(error, enableDeveloperTools);
});
// Developer tools
const enableDeveloperTools = (process.env['VSCODE_DEV'] || !!configuration.extensionDevelopmentPath) && !configuration.extensionTestsPath;
@ -137,9 +130,23 @@ exports.load = function (modulePaths, resultCallback, options) {
};
/**
* @returns () => void
* @returns {{[param: string]: string }}
*/
function parseURLQueryArgs() {
const search = window.location.search || '';
return search.split(/[?&]/)
.filter(function (param) { return !!param; })
.map(function (param) { return param.split('='); })
.filter(function (param) { return param.length === 2; })
.reduce(function (r, param) { r[param[0]] = decodeURIComponent(param[1]); return r; }, {});
}
/**
* @returns {() => void}
*/
function registerDeveloperKeybindings() {
// @ts-ignore
const ipc = require('electron').ipcRenderer;
@ -177,6 +184,7 @@ function registerDeveloperKeybindings() {
}
function onUnexpectedError(error, enableDeveloperTools) {
// @ts-ignore
const ipc = require('electron').ipcRenderer;

9
src/bootstrap.js vendored
View file

@ -25,9 +25,11 @@ process.on('SIGPIPE', () => {
* @param {string=} nodeModulesPath
*/
exports.enableASARSupport = function (nodeModulesPath) {
// @ts-ignore
const Module = require('module');
const path = require('path');
let NODE_MODULES_PATH = nodeModulesPath;
if (!NODE_MODULES_PATH) {
NODE_MODULES_PATH = path.join(__dirname, '../node_modules');
@ -72,7 +74,7 @@ exports.uriFromPath = function (_path) {
//#region FS helpers
/**
* @param {string} file
* @returns {Promise}
* @returns {Promise<string>}
*/
exports.readFile = function (file) {
const fs = require('fs');
@ -91,7 +93,7 @@ exports.readFile = function (file) {
/**
* @param {string} file
* @param {string} content
* @returns {Promise}
* @returns {Promise<void>}
*/
exports.writeFile = function (file, content) {
const fs = require('fs');
@ -109,6 +111,9 @@ exports.writeFile = function (file, content) {
//#endregion
//#region NLS helpers
/**
* @returns {{locale?: string, availableLanguages: {[lang: string]: string;}, pseudo?: boolean }}
*/
exports.setupNLS = function () {
const path = require('path');

View file

@ -35,9 +35,13 @@ setCurrentWorkingDirectory();
// Global app listeners
registerListeners();
// Support user defined locale
/**
* Support user defined locale
*
* @type {Promise}
*/
let nlsConfiguration = undefined;
let userDefinedLocale = getUserDefinedLocale();
const userDefinedLocale = getUserDefinedLocale();
userDefinedLocale.then((locale) => {
if (locale && !nlsConfiguration) {
nlsConfiguration = getNLSConfiguration(locale);
@ -45,7 +49,7 @@ userDefinedLocale.then((locale) => {
});
// Configure command line switches
const nodeCachedDataDir = getNodeCachedDir(userDataPath);
const nodeCachedDataDir = getNodeCachedDir();
configureCommandlineSwitches(args, nodeCachedDataDir);
// Load our code once ready
@ -68,10 +72,12 @@ app.once('ready', function () {
const startup = nlsConfig => {
nlsConfig._languagePackSupport = true;
process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfig);
if (cachedDataDir) {
process.env['VSCODE_NODE_CACHED_DATA_DIR_' + process.pid] = cachedDataDir;
}
// Load main in AMD
require('./bootstrap-amd').load('vs/code/electron-main/main');
};
@ -105,6 +111,12 @@ app.once('ready', function () {
}, console.error);
});
/**
* @typedef {import('minimist').ParsedArgs} ParsedArgs
*
* @param {ParsedArgs} cliArgs
* @param {{ jsFlags: () => string }} nodeCachedDataDir
*/
function configureCommandlineSwitches(cliArgs, nodeCachedDataDir) {
// TODO@Ben Electron 2.0.x: prevent localStorage migration from SQLite to LevelDB due to issues
@ -121,6 +133,8 @@ function configureCommandlineSwitches(cliArgs, nodeCachedDataDir) {
}
/**
* @param {ParsedArgs} cliArgs
* @param {string[]} jsFlags
* @returns {string}
*/
function resolveJSFlags(cliArgs, ...jsFlags) {
@ -136,6 +150,8 @@ function resolveJSFlags(cliArgs, ...jsFlags) {
}
/**
* @param {ParsedArgs} cliArgs
*
* @returns {string}
*/
function getUserDataPath(cliArgs) {
@ -146,6 +162,9 @@ function getUserDataPath(cliArgs) {
return path.resolve(cliArgs['user-data-dir'] || paths.getDefaultUserDataPath(process.platform));
}
/**
* @returns {ParsedArgs}
*/
function parseCLIArgs() {
const minimist = require('minimist');
@ -174,14 +193,23 @@ function setCurrentWorkingDirectory() {
function registerListeners() {
// Mac: when someone drops a file to the not-yet running VSCode, the open-file event fires even before
// the app-ready event. We listen very early for open-file and remember this upon startup as path to open.
/**
* Mac: when someone drops a file to the not-yet running VSCode, the open-file event fires even before
* the app-ready event. We listen very early for open-file and remember this upon startup as path to open.
*
* @type {string[]}
*/
const macOpenFiles = [];
global['macOpenFiles'] = macOpenFiles;
app.on('open-file', function (event, path) {
macOpenFiles.push(path);
});
/**
* React to open-url requests.
*
* @type {string[]}
*/
const openUrls = [];
const onOpenUrl = function (event, url) {
event.preventDefault();
@ -201,9 +229,9 @@ function registerListeners() {
}
/**
* @param {string} userDataPath
* @returns {{ jsFlags: () => string; ensureExists: () => Promise<string | void>, _compute: () => string; }}
*/
function getNodeCachedDir(userDataPath) {
function getNodeCachedDir() {
return new class {
constructor() {
@ -222,15 +250,18 @@ function getNodeCachedDir(userDataPath) {
if (process.argv.indexOf('--no-cached-data') > 0) {
return undefined;
}
// IEnvironmentService.isBuilt
if (process.env['VSCODE_DEV']) {
return undefined;
}
// find commit id
let commit = product.commit;
const commit = product.commit;
if (!commit) {
return undefined;
}
return path.join(userDataPath, 'CachedData', commit);
}
};
@ -242,15 +273,16 @@ function getNodeCachedDir(userDataPath) {
* @returns {string}
*/
function stripComments(content) {
let regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
let result = content.replace(regexp, function (match, m1, m2, m3, m4) {
const regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
return content.replace(regexp, function (match, m1, m2, m3, m4) {
// Only one of m1, m2, m3, m4 matches
if (m3) {
// A block comment. Replace with nothing
return '';
} else if (m4) {
// A line comment. If it ends in \r?\n then keep it.
let length_1 = m4.length;
const length_1 = m4.length;
if (length_1 > 2 && m4[length_1 - 1] === '\n') {
return m4[length_1 - 2] === '\r' ? '\r\n' : '\n';
}
@ -262,7 +294,6 @@ function stripComments(content) {
return match;
}
});
return result;
}
/**
@ -352,7 +383,7 @@ function rimraf(location) {
} else {
return unlink(location);
}
}, (err) => {
}, err => {
if (err.code === 'ENOENT') {
return void 0;
}
@ -368,18 +399,18 @@ function rimraf(location) {
* @returns {Promise<string>}
*/
function getUserDefinedLocale() {
let locale = args['locale'];
const locale = args['locale'];
if (locale) {
return Promise.resolve(locale.toLowerCase());
}
let localeConfig = path.join(userDataPath, 'User', 'locale.json');
const localeConfig = path.join(userDataPath, 'User', 'locale.json');
return exists(localeConfig).then((result) => {
if (result) {
return bootstrap.readFile(localeConfig).then((content) => {
content = stripComments(content);
try {
let value = JSON.parse(content).locale;
const value = JSON.parse(content).locale;
return value && typeof value === 'string' ? value.toLowerCase() : undefined;
} catch (e) {
return undefined;
@ -395,7 +426,7 @@ function getUserDefinedLocale() {
* @returns {object}
*/
function getLanguagePackConfigurations() {
let configFile = path.join(userDataPath, 'languagepacks.json');
const configFile = path.join(userDataPath, 'languagepacks.json');
try {
return require(configFile);
} catch (err) {
@ -415,7 +446,7 @@ function resolveLanguagePackLocale(config, locale) {
if (config[locale]) {
return locale;
} else {
let index = locale.lastIndexOf('-');
const index = locale.lastIndexOf('-');
if (index > 0) {
locale = locale.substring(0, index);
} else {
@ -450,20 +481,20 @@ function getNLSConfiguration(locale) {
return Promise.resolve({ locale: locale, availableLanguages: {} });
}
let initialLocale = locale;
const initialLocale = locale;
perf.mark('nlsGeneration:start');
let defaultResult = function (locale) {
const defaultResult = function (locale) {
perf.mark('nlsGeneration:end');
return Promise.resolve({ locale: locale, availableLanguages: {} });
};
try {
let commit = product.commit;
const commit = product.commit;
if (!commit) {
return defaultResult(initialLocale);
}
let configs = getLanguagePackConfigurations();
const configs = getLanguagePackConfigurations();
if (!configs) {
return defaultResult(initialLocale);
}
@ -471,7 +502,7 @@ function getNLSConfiguration(locale) {
if (!locale) {
return defaultResult(initialLocale);
}
let packConfig = configs[locale];
const packConfig = configs[locale];
let mainPack;
if (!packConfig || typeof packConfig.hash !== 'string' || !packConfig.translations || typeof (mainPack = packConfig.translations['vscode']) !== 'string') {
return defaultResult(initialLocale);
@ -480,12 +511,12 @@ function getNLSConfiguration(locale) {
if (!fileExists) {
return defaultResult(initialLocale);
}
let packId = packConfig.hash + '.' + locale;
let cacheRoot = path.join(userDataPath, 'clp', packId);
let coreLocation = path.join(cacheRoot, commit);
let translationsConfigFile = path.join(cacheRoot, 'tcf.json');
let corruptedFile = path.join(cacheRoot, 'corrupted.info');
let result = {
const packId = packConfig.hash + '.' + locale;
const cacheRoot = path.join(userDataPath, 'clp', packId);
const coreLocation = path.join(cacheRoot, commit);
const translationsConfigFile = path.join(cacheRoot, 'tcf.json');
const corruptedFile = path.join(cacheRoot, 'corrupted.info');
const result = {
locale: initialLocale,
availableLanguages: { '*': locale },
_languagePackId: packId,
@ -513,23 +544,23 @@ function getNLSConfiguration(locale) {
return mkdirp(coreLocation).then(() => {
return Promise.all([bootstrap.readFile(path.join(__dirname, 'nls.metadata.json')), bootstrap.readFile(mainPack)]);
}).then((values) => {
let metadata = JSON.parse(values[0]);
let packData = JSON.parse(values[1]).contents;
let bundles = Object.keys(metadata.bundles);
let writes = [];
const metadata = JSON.parse(values[0]);
const packData = JSON.parse(values[1]).contents;
const bundles = Object.keys(metadata.bundles);
const writes = [];
for (let bundle of bundles) {
let modules = metadata.bundles[bundle];
let target = Object.create(null);
const modules = metadata.bundles[bundle];
const target = Object.create(null);
for (let module of modules) {
let keys = metadata.keys[module];
let defaultMessages = metadata.messages[module];
let translations = packData[module];
const keys = metadata.keys[module];
const defaultMessages = metadata.messages[module];
const translations = packData[module];
let targetStrings;
if (translations) {
targetStrings = [];
for (let i = 0; i < keys.length; i++) {
let elem = keys[i];
let key = typeof elem === 'string' ? elem : elem.key;
const elem = keys[i];
const key = typeof elem === 'string' ? elem : elem.key;
let translatedMessage = translations[key];
if (translatedMessage === undefined) {
translatedMessage = defaultMessages[i];

View file

@ -8,6 +8,6 @@
<body class="monaco-shell vs-dark" aria-label="">
</body>
<!-- Startup via index.js -->
<script src="index.js"></script>
<!-- Startup via workbench.js -->
<script src="workbench.js"></script>
</html>

View file

@ -6,8 +6,6 @@
//@ts-check
'use strict';
/*global window,document,define*/
const perf = require('../../../base/common/performance');
perf.mark('renderer/started');

View file

@ -612,7 +612,7 @@ export class CodeWindow implements ICodeWindow {
}
}
return `${require.toUrl('vs/workbench/electron-browser/bootstrap/index.html')}?config=${encodeURIComponent(JSON.stringify(config))}`;
return `${require.toUrl('vs/code/electron-browser/workbench/workbench.html')}?config=${encodeURIComponent(JSON.stringify(config))}`;
}
serializeWindowState(): IWindowState {

View file

@ -122,7 +122,7 @@ function openWorkbench(configuration: IWindowConfiguration): TPromise<void> {
(<any>self).require.config({
onError: (err: any) => {
if (err.errorCode === 'load') {
shell.onUnexpectedError(loaderError(err));
shell.onUnexpectedError(new Error(nls.localize('loaderErrorNative', "Failed to load a required file. Please restart the application to try again. Details: {0}", JSON.stringify(err))));
}
}
});
@ -235,12 +235,4 @@ function createMainProcessServices(mainProcessClient: ElectronIPCClient, configu
serviceCollection.set(IWorkspacesService, new WorkspacesChannelClient(workspacesChannel));
return serviceCollection;
}
function loaderError(err: Error): Error {
if (platform.isWeb) {
return new Error(nls.localize('loaderError', "Failed to load a required file. Either you are no longer connected to the internet or the server you are connected to is offline. Please refresh the browser to try again."));
}
return new Error(nls.localize('loaderErrorNative', "Failed to load a required file. Please restart the application to try again. Details: {0}", JSON.stringify(err)));
}
}