From 05334aeaa32e70f92b2950f1fe18cf5c95dcb38d Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Thu, 6 Oct 2016 08:46:53 +0200 Subject: [PATCH] more use of IEnvironmentService (for #10656) --- src/vs/code/electron-main/env.ts | 53 ++----------------- src/vs/code/electron-main/lifecycle.ts | 6 +-- src/vs/code/electron-main/log.ts | 8 ++- src/vs/code/electron-main/main.ts | 6 +-- src/vs/code/electron-main/storage.ts | 10 ++-- src/vs/code/electron-main/window.ts | 11 ++-- src/vs/code/electron-main/windows.ts | 4 +- .../environment/common/environment.ts | 1 + .../environment/node/environmentService.ts | 1 + 9 files changed, 29 insertions(+), 71 deletions(-) diff --git a/src/vs/code/electron-main/env.ts b/src/vs/code/electron-main/env.ts index 41c2546ea2f..e038bd5c30c 100644 --- a/src/vs/code/electron-main/env.ts +++ b/src/vs/code/electron-main/env.ts @@ -7,17 +7,13 @@ import * as fs from 'original-fs'; import * as path from 'path'; -import * as os from 'os'; -import { app } from 'electron'; import * as arrays from 'vs/base/common/arrays'; import * as strings from 'vs/base/common/strings'; import * as paths from 'vs/base/common/paths'; import * as platform from 'vs/base/common/platform'; -import URI from 'vs/base/common/uri'; import * as types from 'vs/base/common/types'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; import { parseArgs, ParsedArgs } from 'vs/platform/environment/node/argv'; -import product from 'vs/platform/product'; export interface IProcessEnvironment { [key: string]: string; @@ -32,14 +28,6 @@ export const IEnvService = createDecorator('mainEnvironmentService' export interface IEnvService { _serviceBrand: any; cliArgs: ICommandLineArguments; - isBuilt: boolean; - userHome: string; - appRoot: string; - currentWorkingDirectory: string; - appHome: string; - appSettingsHome: string; - appSettingsPath: string; - appKeybindingsPath: string; } export class EnvService implements IEnvService { @@ -49,45 +37,14 @@ export class EnvService implements IEnvService { private _cliArgs: ICommandLineArguments; get cliArgs(): ICommandLineArguments { return this._cliArgs; } - private _userExtensionsHome: string; - get userExtensionsHome(): string { return this._userExtensionsHome; } - - get isBuilt(): boolean { return !process.env['VSCODE_DEV']; } - - private _userHome: string; - get userHome(): string { return this._userHome; } - - private _appRoot: string; - get appRoot(): string { return this._appRoot; } - - private _currentWorkingDirectory: string; - get currentWorkingDirectory(): string { return this._currentWorkingDirectory; } - - private _appHome: string; - get appHome(): string { return this._appHome; } - - private _appSettingsHome: string; - get appSettingsHome(): string { return this._appSettingsHome; } - - private _appSettingsPath: string; - get appSettingsPath(): string { return this._appSettingsPath; } - - private _appKeybindingsPath: string; - get appKeybindingsPath(): string { return this._appKeybindingsPath; } - constructor() { - this._appRoot = path.dirname(URI.parse(require.toUrl('')).fsPath); - this._currentWorkingDirectory = process.env['VSCODE_CWD'] || process.cwd(); - this._appHome = app.getPath('userData'); - this._appSettingsHome = path.join(this._appHome, 'User'); - this._appSettingsPath = path.join(this._appSettingsHome, 'settings.json'); - this._appKeybindingsPath = path.join(this._appSettingsHome, 'keybindings.json'); // Remove the Electron executable const [, ...args] = process.argv; // If dev, remove the first non-option argument: it's the app location - if (!this.isBuilt) { + const isBuilt = !process.env['VSCODE_DEV']; + if (!isBuilt) { const index = arrays.firstIndex(args, a => !/^-/.test(a)); if (index > -1) { @@ -96,7 +53,8 @@ export class EnvService implements IEnvService { } const argv = parseArgs(args); - const paths = parsePathArguments(this._currentWorkingDirectory, argv._, argv.goto); + const cwd = process.env['VSCODE_CWD'] || process.cwd(); + const paths = parsePathArguments(cwd, argv._, argv.goto); this._cliArgs = Object.freeze({ _: [], @@ -118,9 +76,6 @@ export class EnvService implements IEnvService { locale: argv.locale, wait: argv.wait }); - - this._userHome = path.join(os.homedir(), product.dataFolderName); - this._userExtensionsHome = this.cliArgs.extensionHomePath || path.join(this._userHome, 'extensions'); } } diff --git a/src/vs/code/electron-main/lifecycle.ts b/src/vs/code/electron-main/lifecycle.ts index 66f3e5aae83..ec6bbb74702 100644 --- a/src/vs/code/electron-main/lifecycle.ts +++ b/src/vs/code/electron-main/lifecycle.ts @@ -9,7 +9,7 @@ import { EventEmitter } from 'events'; import { ipcMain as ipc, app } from 'electron'; import { TPromise, TValueCallback } from 'vs/base/common/winjs.base'; import { ReadyState, VSCodeWindow } from 'vs/code/electron-main/window'; -import { IEnvService } from 'vs/code/electron-main/env'; +import { IEnvironmentService } from 'vs/platform/environment/common/environment'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; import { ILogService } from 'vs/code/electron-main/log'; import { IStorageService } from 'vs/code/electron-main/storage'; @@ -50,7 +50,7 @@ export class LifecycleService implements ILifecycleService { private _wasUpdated: boolean; constructor( - @IEnvService private envService: IEnvService, + @IEnvironmentService private environmentService: IEnvironmentService, @ILogService private logService: ILogService, @IStorageService private storageService: IStorageService ) { @@ -109,7 +109,7 @@ export class LifecycleService implements ILifecycleService { // Windows/Linux: we quit when all windows have closed // Mac: we only quit when quit was requested // --wait: we quit when all windows are closed - if (this.quitRequested || process.platform !== 'darwin' || this.envService.cliArgs.wait) { + if (this.quitRequested || process.platform !== 'darwin' || this.environmentService.wait) { app.quit(); } }); diff --git a/src/vs/code/electron-main/log.ts b/src/vs/code/electron-main/log.ts index 352363d63ca..27285dccdd3 100644 --- a/src/vs/code/electron-main/log.ts +++ b/src/vs/code/electron-main/log.ts @@ -6,7 +6,7 @@ 'use strict'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; -import { IEnvService } from 'vs/code/electron-main/env'; +import { IEnvironmentService } from 'vs/platform/environment/common/environment'; export const ILogService = createDecorator('logService'); @@ -19,13 +19,11 @@ export class MainLogService implements ILogService { _serviceBrand: any; - constructor(@IEnvService private envService: IEnvService) { + constructor(@IEnvironmentService private environmentService: IEnvironmentService) { } log(...args: any[]): void { - const { verbose } = this.envService.cliArgs; - - if (verbose) { + if (this.environmentService.verbose) { console.log(`\x1b[93m[main ${new Date().toLocaleTimeString()}]\x1b[0m`, ...args); } } diff --git a/src/vs/code/electron-main/main.ts b/src/vs/code/electron-main/main.ts index 630046fca2e..5e4ec501f28 100644 --- a/src/vs/code/electron-main/main.ts +++ b/src/vs/code/electron-main/main.ts @@ -102,7 +102,7 @@ function main(accessor: ServicesAccessor, mainIpcServer: Server, userEnv: IProce }); logService.log('Starting VS Code in verbose mode'); - logService.log(`from: ${envService.appRoot}`); + logService.log(`from: ${environmentService.appRoot}`); logService.log('args:', envService.cliArgs); // Setup Windows mutex @@ -134,8 +134,8 @@ function main(accessor: ServicesAccessor, mainIpcServer: Server, userEnv: IProce // Spawn shared process const initData = { args: environmentService.args }; const options = { - allowOutput: !envService.isBuilt || envService.cliArgs.verbose, - debugPort: envService.isBuilt ? null : 5871 + allowOutput: !environmentService.isBuilt || environmentService.verbose, + debugPort: environmentService.isBuilt ? null : 5871 }; let sharedProcessDisposable; diff --git a/src/vs/code/electron-main/storage.ts b/src/vs/code/electron-main/storage.ts index decedc16d3a..3f337a5495d 100644 --- a/src/vs/code/electron-main/storage.ts +++ b/src/vs/code/electron-main/storage.ts @@ -7,7 +7,7 @@ import * as path from 'path'; import * as fs from 'original-fs'; -import { IEnvService } from 'vs/code/electron-main/env'; +import { IEnvironmentService } from 'vs/platform/environment/common/environment'; import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; export const IStorageService = createDecorator('storageService'); @@ -26,8 +26,8 @@ export class StorageService implements IStorageService { private dbPath: string; private database: any = null; - constructor(@IEnvService private envService: IEnvService) { - this.dbPath = path.join(envService.appHome, 'storage.json'); + constructor(@IEnvironmentService private environmentService: IEnvironmentService) { + this.dbPath = path.join(environmentService.userDataPath, 'storage.json'); } getItem(key: string, defaultValue?: T): T { @@ -74,7 +74,7 @@ export class StorageService implements IStorageService { try { return JSON.parse(fs.readFileSync(this.dbPath).toString()); // invalid JSON or permission issue can happen here } catch (error) { - if (this.envService.cliArgs.verbose) { + if (this.environmentService.verbose) { console.error(error); } @@ -86,7 +86,7 @@ export class StorageService implements IStorageService { try { fs.writeFileSync(this.dbPath, JSON.stringify(this.database, null, 4)); // permission issue can happen here } catch (error) { - if (this.envService.cliArgs.verbose) { + if (this.environmentService.verbose) { console.error(error); } } diff --git a/src/vs/code/electron-main/window.ts b/src/vs/code/electron-main/window.ts index 13ccfc249e2..843869fb3a2 100644 --- a/src/vs/code/electron-main/window.ts +++ b/src/vs/code/electron-main/window.ts @@ -11,7 +11,8 @@ import * as objects from 'vs/base/common/objects'; import { IStorageService } from 'vs/code/electron-main/storage'; import { shell, screen, BrowserWindow } from 'electron'; import { TPromise, TValueCallback } from 'vs/base/common/winjs.base'; -import { ICommandLineArguments, IEnvService, IProcessEnvironment } from 'vs/code/electron-main/env'; +import { ICommandLineArguments, IProcessEnvironment } from 'vs/code/electron-main/env'; +import { IEnvironmentService } from 'vs/platform/environment/common/environment'; import { ILogService } from 'vs/code/electron-main/log'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; import { parseArgs } from 'vs/platform/environment/node/argv'; @@ -135,7 +136,7 @@ export class VSCodeWindow { constructor( config: IWindowCreationOptions, @ILogService private logService: ILogService, - @IEnvService private envService: IEnvService, + @IEnvironmentService private environmentService: IEnvironmentService, @IConfigurationService private configurationService: IConfigurationService, @IStorageService private storageService: IStorageService ) { @@ -173,7 +174,7 @@ export class VSCodeWindow { }; if (platform.isLinux) { - options.icon = path.join(this.envService.appRoot, 'resources/linux/code.png'); // Windows and Mac are better off using the embedded icon(s) + options.icon = path.join(this.environmentService.appRoot, 'resources/linux/code.png'); // Windows and Mac are better off using the embedded icon(s) } // Create the browser window. @@ -340,7 +341,7 @@ export class VSCodeWindow { // Prevent any kind of navigation triggered by the user! // But do not touch this in dev version because it will prevent "Reload" from dev tools - if (this.envService.isBuilt) { + if (this.environmentService.isBuilt) { this._win.webContents.on('will-navigate', (event: Event) => { if (event) { event.preventDefault(); @@ -375,7 +376,7 @@ export class VSCodeWindow { this._win.loadURL(this.getUrl(config)); // Make window visible if it did not open in N seconds because this indicates an error - if (!this.envService.isBuilt) { + if (!this.environmentService.isBuilt) { this.showTimeoutHandle = setTimeout(() => { if (this._win && !this._win.isVisible() && !this._win.isMinimized()) { this._win.show(); diff --git a/src/vs/code/electron-main/windows.ts b/src/vs/code/electron-main/windows.ts index 5534b35fd91..a6438697c3d 100644 --- a/src/vs/code/electron-main/windows.ts +++ b/src/vs/code/electron-main/windows.ts @@ -14,6 +14,7 @@ import * as types from 'vs/base/common/types'; import * as arrays from 'vs/base/common/arrays'; import { assign, mixin } from 'vs/base/common/objects'; import { EventEmitter } from 'events'; +import { IEnvironmentService } from 'vs/platform/environment/common/environment'; import { IStorageService } from 'vs/code/electron-main/storage'; import { IPath, VSCodeWindow, ReadyState, IWindowConfiguration, IWindowState as ISingleWindowState, defaultWindowState, IWindowSettings } from 'vs/code/electron-main/window'; import { ipcMain as ipc, app, screen, crashReporter, BrowserWindow, dialog } from 'electron'; @@ -162,6 +163,7 @@ export class WindowsManager implements IWindowsService { @ILogService private logService: ILogService, @IStorageService private storageService: IStorageService, @IEnvService private envService: IEnvService, + @IEnvironmentService private environmentService: IEnvironmentService, @ILifecycleService private lifecycleService: ILifecycleService, @IUpdateService private updateService: IUpdateService, @IConfigurationService private configurationService: IConfigurationService @@ -843,7 +845,7 @@ export class WindowsManager implements IWindowsService { private toConfiguration(userEnv: IProcessEnvironment, cli: ICommandLineArguments, workspacePath?: string, filesToOpen?: IPath[], filesToCreate?: IPath[], filesToDiff?: IPath[]): IWindowConfiguration { const configuration: IWindowConfiguration = mixin({}, cli); // inherit all properties from CLI - configuration.appRoot = this.envService.appRoot; + configuration.appRoot = this.environmentService.appRoot; configuration.execPath = process.execPath; configuration.userEnv = userEnv; configuration.workspacePath = workspacePath; diff --git a/src/vs/platform/environment/common/environment.ts b/src/vs/platform/environment/common/environment.ts index 57d50f59025..3a54ba179b7 100644 --- a/src/vs/platform/environment/common/environment.ts +++ b/src/vs/platform/environment/common/environment.ts @@ -34,6 +34,7 @@ export interface IEnvironmentService { isBuilt: boolean; verbose: boolean; + wait: boolean; performance: boolean; mainIPCHandle: string; diff --git a/src/vs/platform/environment/node/environmentService.ts b/src/vs/platform/environment/node/environmentService.ts index 830231abab4..f3bb9a38948 100644 --- a/src/vs/platform/environment/node/environmentService.ts +++ b/src/vs/platform/environment/node/environmentService.ts @@ -89,6 +89,7 @@ export class EnvironmentService implements IEnvironmentService { get isBuilt(): boolean { return !process.env['VSCODE_DEV']; } get verbose(): boolean { return this._args.verbose; } + get wait(): boolean { return this._args.wait; } get performance(): boolean { return this._args.performance; } get logExtensionHostCommunication(): boolean { return this._args.logExtensionHostCommunication; } -- GitLab