mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-29 01:25:37 -05:00
SQL Operations Studio Public Preview 1 (0.23) release source code
This commit is contained in:
132
src/vs/code/node/cli.ts
Normal file
132
src/vs/code/node/cli.ts
Normal file
@@ -0,0 +1,132 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { spawn } from 'child_process';
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { assign } from 'vs/base/common/objects';
|
||||
import { parseCLIProcessArgv, buildHelpMessage } from 'vs/platform/environment/node/argv';
|
||||
import { ParsedArgs } from 'vs/platform/environment/common/environment';
|
||||
import product from 'vs/platform/node/product';
|
||||
import pkg from 'vs/platform/node/package';
|
||||
|
||||
import * as fs from 'fs';
|
||||
import * as paths from 'path';
|
||||
import * as os from 'os';
|
||||
|
||||
function shouldSpawnCliProcess(argv: ParsedArgs): boolean {
|
||||
return argv['list-extensions'] || !!argv['install-extension'] || !!argv['uninstall-extension'];
|
||||
}
|
||||
|
||||
interface IMainCli {
|
||||
main: (argv: ParsedArgs) => TPromise<void>;
|
||||
}
|
||||
|
||||
export function main(argv: string[]): TPromise<void> {
|
||||
let args: ParsedArgs;
|
||||
|
||||
try {
|
||||
args = parseCLIProcessArgv(argv);
|
||||
} catch (err) {
|
||||
console.error(err.message);
|
||||
return TPromise.as(null);
|
||||
}
|
||||
|
||||
if (args.help) {
|
||||
console.log(buildHelpMessage(product.nameLong, product.applicationName, pkg.version));
|
||||
} else if (args.version) {
|
||||
console.log(`${pkg.version}\n${product.commit}`);
|
||||
} else if (shouldSpawnCliProcess(args)) {
|
||||
const mainCli = new TPromise<IMainCli>(c => require(['vs/code/node/cliProcessMain'], c));
|
||||
return mainCli.then(cli => cli.main(args));
|
||||
} else {
|
||||
const env = assign({}, process.env, {
|
||||
// this will signal Code that it was spawned from this module
|
||||
'VSCODE_CLI': '1',
|
||||
'ELECTRON_NO_ATTACH_CONSOLE': '1'
|
||||
});
|
||||
|
||||
delete env['ELECTRON_RUN_AS_NODE'];
|
||||
|
||||
if (args.verbose) {
|
||||
env['ELECTRON_ENABLE_LOGGING'] = '1';
|
||||
}
|
||||
|
||||
// If we are started with --wait create a random temporary file
|
||||
// and pass it over to the starting instance. We can use this file
|
||||
// to wait for it to be deleted to monitor that the edited file
|
||||
// is closed and then exit the waiting process.
|
||||
let waitMarkerFilePath: string;
|
||||
if (args.wait) {
|
||||
let waitMarkerError: Error;
|
||||
const randomTmpFile = paths.join(os.tmpdir(), Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10));
|
||||
try {
|
||||
fs.writeFileSync(randomTmpFile, '');
|
||||
waitMarkerFilePath = randomTmpFile;
|
||||
argv.push('--waitMarkerFilePath', waitMarkerFilePath);
|
||||
} catch (error) {
|
||||
waitMarkerError = error;
|
||||
}
|
||||
|
||||
if (args.verbose) {
|
||||
if (waitMarkerError) {
|
||||
console.error(`Failed to create marker file for --wait: ${waitMarkerError.toString()}`);
|
||||
} else {
|
||||
console.log(`Marker file for --wait created: ${waitMarkerFilePath}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const options = {
|
||||
detached: true,
|
||||
env
|
||||
};
|
||||
|
||||
if (!args.verbose) {
|
||||
options['stdio'] = 'ignore';
|
||||
}
|
||||
|
||||
const child = spawn(process.execPath, argv.slice(2), options);
|
||||
|
||||
if (args.verbose) {
|
||||
child.stdout.on('data', (data: Buffer) => console.log(data.toString('utf8').trim()));
|
||||
child.stderr.on('data', (data: Buffer) => console.log(data.toString('utf8').trim()));
|
||||
}
|
||||
|
||||
if (args.verbose) {
|
||||
return new TPromise<void>(c => child.once('exit', () => c(null)));
|
||||
}
|
||||
|
||||
if (args.wait && waitMarkerFilePath) {
|
||||
return new TPromise<void>(c => {
|
||||
|
||||
// Complete when process exits
|
||||
child.once('exit', () => c(null));
|
||||
|
||||
// Complete when wait marker file is deleted
|
||||
const interval = setInterval(() => {
|
||||
fs.exists(waitMarkerFilePath, exists => {
|
||||
if (!exists) {
|
||||
clearInterval(interval);
|
||||
c(null);
|
||||
}
|
||||
});
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return TPromise.as(null);
|
||||
}
|
||||
|
||||
function eventuallyExit(code: number): void {
|
||||
setTimeout(() => process.exit(code), 0);
|
||||
}
|
||||
|
||||
main(process.argv)
|
||||
.then(() => eventuallyExit(0))
|
||||
.then(null, err => {
|
||||
console.error(err.stack ? err.stack : err);
|
||||
eventuallyExit(1);
|
||||
});
|
||||
201
src/vs/code/node/cliProcessMain.ts
Normal file
201
src/vs/code/node/cliProcessMain.ts
Normal file
@@ -0,0 +1,201 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { localize } from 'vs/nls';
|
||||
import product from 'vs/platform/node/product';
|
||||
import pkg from 'vs/platform/node/package';
|
||||
import * as path from 'path';
|
||||
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { sequence } from 'vs/base/common/async';
|
||||
import { IPager } from 'vs/base/common/paging';
|
||||
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
|
||||
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
|
||||
import { IEnvironmentService, ParsedArgs } from 'vs/platform/environment/common/environment';
|
||||
import { EnvironmentService } from 'vs/platform/environment/node/environmentService';
|
||||
import { IExtensionManagementService, IExtensionGalleryService, IExtensionManifest, IGalleryExtension, LocalExtensionType } from 'vs/platform/extensionManagement/common/extensionManagement';
|
||||
import { ExtensionManagementService } from 'vs/platform/extensionManagement/node/extensionManagementService';
|
||||
import { ExtensionGalleryService } from 'vs/platform/extensionManagement/node/extensionGalleryService';
|
||||
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
|
||||
import { combinedAppender, NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
|
||||
import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
|
||||
import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProperties';
|
||||
import { IRequestService } from 'vs/platform/request/node/request';
|
||||
import { RequestService } from 'vs/platform/request/node/requestService';
|
||||
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
|
||||
import { ConfigurationService } from 'vs/platform/configuration/node/configurationService';
|
||||
import { AppInsightsAppender } from 'vs/platform/telemetry/node/appInsightsAppender';
|
||||
import { mkdirp } from 'vs/base/node/pfs';
|
||||
import { IChoiceService } from 'vs/platform/message/common/message';
|
||||
import { ChoiceCliService } from 'vs/platform/message/node/messageCli';
|
||||
|
||||
const notFound = id => localize('notFound', "Extension '{0}' not found.", id);
|
||||
const notInstalled = id => localize('notInstalled', "Extension '{0}' is not installed.", id);
|
||||
const useId = localize('useId', "Make sure you use the full extension ID, including the publisher, eg: {0}", 'ms-vscode.csharp');
|
||||
|
||||
function getId(manifest: IExtensionManifest, withVersion?: boolean): string {
|
||||
if (withVersion) {
|
||||
return `${manifest.publisher}.${manifest.name}@${manifest.version}`;
|
||||
} else {
|
||||
return `${manifest.publisher}.${manifest.name}`;
|
||||
}
|
||||
}
|
||||
|
||||
type Task = { (): TPromise<void> };
|
||||
|
||||
class Main {
|
||||
|
||||
constructor(
|
||||
@IExtensionManagementService private extensionManagementService: IExtensionManagementService,
|
||||
@IExtensionGalleryService private extensionGalleryService: IExtensionGalleryService
|
||||
) { }
|
||||
|
||||
run(argv: ParsedArgs): TPromise<any> {
|
||||
// TODO@joao - make this contributable
|
||||
|
||||
if (argv['list-extensions']) {
|
||||
return this.listExtensions(argv['show-versions']);
|
||||
} else if (argv['install-extension']) {
|
||||
const arg = argv['install-extension'];
|
||||
const args: string[] = typeof arg === 'string' ? [arg] : arg;
|
||||
return this.installExtension(args);
|
||||
} else if (argv['uninstall-extension']) {
|
||||
const arg = argv['uninstall-extension'];
|
||||
const ids: string[] = typeof arg === 'string' ? [arg] : arg;
|
||||
return this.uninstallExtension(ids);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private listExtensions(showVersions: boolean): TPromise<any> {
|
||||
return this.extensionManagementService.getInstalled(LocalExtensionType.User).then(extensions => {
|
||||
extensions.forEach(e => console.log(getId(e.manifest, showVersions)));
|
||||
});
|
||||
}
|
||||
|
||||
private installExtension(extensions: string[]): TPromise<any> {
|
||||
const vsixTasks: Task[] = extensions
|
||||
.filter(e => /\.vsix$/i.test(e))
|
||||
.map(id => () => {
|
||||
const extension = path.isAbsolute(id) ? id : path.join(process.cwd(), id);
|
||||
|
||||
return this.extensionManagementService.install(extension).then(() => {
|
||||
console.log(localize('successVsixInstall', "Extension '{0}' was successfully installed!", path.basename(extension)));
|
||||
});
|
||||
});
|
||||
|
||||
const galleryTasks: Task[] = extensions
|
||||
.filter(e => !/\.vsix$/i.test(e))
|
||||
.map(id => () => {
|
||||
return this.extensionManagementService.getInstalled(LocalExtensionType.User).then(installed => {
|
||||
const isInstalled = installed.some(e => getId(e.manifest) === id);
|
||||
|
||||
if (isInstalled) {
|
||||
console.log(localize('alreadyInstalled', "Extension '{0}' is already installed.", id));
|
||||
return TPromise.as(null);
|
||||
}
|
||||
|
||||
return this.extensionGalleryService.query({ names: [id] })
|
||||
.then<IPager<IGalleryExtension>>(null, err => {
|
||||
if (err.responseText) {
|
||||
try {
|
||||
const response = JSON.parse(err.responseText);
|
||||
return TPromise.wrapError(response.message);
|
||||
} catch (e) {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
return TPromise.wrapError(err);
|
||||
})
|
||||
.then(result => {
|
||||
const [extension] = result.firstPage;
|
||||
|
||||
if (!extension) {
|
||||
return TPromise.wrapError(new Error(`${notFound(id)}\n${useId}`));
|
||||
}
|
||||
|
||||
console.log(localize('foundExtension', "Found '{0}' in the marketplace.", id));
|
||||
console.log(localize('installing', "Installing..."));
|
||||
|
||||
return this.extensionManagementService.installFromGallery(extension, false)
|
||||
.then(() => console.log(localize('successInstall', "Extension '{0}' v{1} was successfully installed!", id, extension.version)));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return sequence([...vsixTasks, ...galleryTasks]);
|
||||
}
|
||||
|
||||
private uninstallExtension(ids: string[]): TPromise<any> {
|
||||
return sequence(ids.map(id => () => {
|
||||
return this.extensionManagementService.getInstalled(LocalExtensionType.User).then(installed => {
|
||||
const [extension] = installed.filter(e => getId(e.manifest) === id);
|
||||
|
||||
if (!extension) {
|
||||
return TPromise.wrapError(new Error(`${notInstalled(id)}\n${useId}`));
|
||||
}
|
||||
|
||||
console.log(localize('uninstalling', "Uninstalling {0}...", id));
|
||||
|
||||
return this.extensionManagementService.uninstall(extension, true)
|
||||
.then(() => console.log(localize('successUninstall', "Extension '{0}' was successfully uninstalled!", id)));
|
||||
});
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
const eventPrefix = 'monacoworkbench';
|
||||
|
||||
export function main(argv: ParsedArgs): TPromise<void> {
|
||||
const services = new ServiceCollection();
|
||||
services.set(IEnvironmentService, new SyncDescriptor(EnvironmentService, argv, process.execPath));
|
||||
|
||||
const instantiationService: IInstantiationService = new InstantiationService(services);
|
||||
|
||||
return instantiationService.invokeFunction(accessor => {
|
||||
const envService = accessor.get(IEnvironmentService);
|
||||
|
||||
return TPromise.join([envService.appSettingsHome, envService.extensionsPath].map(p => mkdirp(p))).then(() => {
|
||||
const { appRoot, extensionsPath, extensionDevelopmentPath, isBuilt } = envService;
|
||||
|
||||
const services = new ServiceCollection();
|
||||
services.set(IConfigurationService, new SyncDescriptor(ConfigurationService));
|
||||
services.set(IRequestService, new SyncDescriptor(RequestService));
|
||||
services.set(IExtensionManagementService, new SyncDescriptor(ExtensionManagementService));
|
||||
services.set(IExtensionGalleryService, new SyncDescriptor(ExtensionGalleryService));
|
||||
services.set(IChoiceService, new SyncDescriptor(ChoiceCliService));
|
||||
|
||||
if (isBuilt && !extensionDevelopmentPath && product.enableTelemetry) {
|
||||
const appenders: AppInsightsAppender[] = [];
|
||||
|
||||
if (product.aiConfig && product.aiConfig.asimovKey) {
|
||||
appenders.push(new AppInsightsAppender(eventPrefix, null, product.aiConfig.asimovKey));
|
||||
}
|
||||
|
||||
// It is important to dispose the AI adapter properly because
|
||||
// only then they flush remaining data.
|
||||
process.once('exit', () => appenders.forEach(a => a.dispose()));
|
||||
|
||||
const config: ITelemetryServiceConfig = {
|
||||
appender: combinedAppender(...appenders),
|
||||
commonProperties: resolveCommonProperties(product.commit, pkg.version),
|
||||
piiPaths: [appRoot, extensionsPath]
|
||||
};
|
||||
|
||||
services.set(ITelemetryService, new SyncDescriptor(TelemetryService, config));
|
||||
} else {
|
||||
services.set(ITelemetryService, NullTelemetryService);
|
||||
}
|
||||
|
||||
const instantiationService2 = instantiationService.createChild(services);
|
||||
const main = instantiationService2.createInstance(Main);
|
||||
|
||||
return main.run(argv);
|
||||
});
|
||||
});
|
||||
}
|
||||
141
src/vs/code/node/paths.ts
Normal file
141
src/vs/code/node/paths.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import * as path from 'path';
|
||||
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 * as types from 'vs/base/common/types';
|
||||
import { ParsedArgs } from 'vs/platform/environment/common/environment';
|
||||
import { realpathSync } from 'vs/base/node/extfs';
|
||||
|
||||
export function validatePaths(args: ParsedArgs): ParsedArgs {
|
||||
|
||||
// Realpath/normalize paths and watch out for goto line mode
|
||||
const paths = doValidatePaths(args._, args.goto);
|
||||
|
||||
// Update environment
|
||||
args._ = paths;
|
||||
args.diff = args.diff && paths.length === 2;
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
function doValidatePaths(args: string[], gotoLineMode?: boolean): string[] {
|
||||
const cwd = process.env['VSCODE_CWD'] || process.cwd();
|
||||
const result = args.map(arg => {
|
||||
let pathCandidate = String(arg);
|
||||
|
||||
let parsedPath: IPathWithLineAndColumn;
|
||||
if (gotoLineMode) {
|
||||
parsedPath = parseLineAndColumnAware(pathCandidate);
|
||||
pathCandidate = parsedPath.path;
|
||||
}
|
||||
|
||||
if (pathCandidate) {
|
||||
pathCandidate = preparePath(cwd, pathCandidate);
|
||||
}
|
||||
|
||||
let realPath: string;
|
||||
try {
|
||||
realPath = realpathSync(pathCandidate);
|
||||
} catch (error) {
|
||||
// in case of an error, assume the user wants to create this file
|
||||
// if the path is relative, we join it to the cwd
|
||||
realPath = path.normalize(path.isAbsolute(pathCandidate) ? pathCandidate : path.join(cwd, pathCandidate));
|
||||
}
|
||||
|
||||
const basename = path.basename(realPath);
|
||||
if (basename /* can be empty if code is opened on root */ && !paths.isValidBasename(basename)) {
|
||||
return null; // do not allow invalid file names
|
||||
}
|
||||
|
||||
if (gotoLineMode) {
|
||||
parsedPath.path = realPath;
|
||||
return toPath(parsedPath);
|
||||
}
|
||||
|
||||
return realPath;
|
||||
});
|
||||
|
||||
const caseInsensitive = platform.isWindows || platform.isMacintosh;
|
||||
const distinct = arrays.distinct(result, e => e && caseInsensitive ? e.toLowerCase() : e);
|
||||
|
||||
return arrays.coalesce(distinct);
|
||||
}
|
||||
|
||||
function preparePath(cwd: string, p: string): string {
|
||||
|
||||
// Trim trailing quotes
|
||||
if (platform.isWindows) {
|
||||
p = strings.rtrim(p, '"'); // https://github.com/Microsoft/vscode/issues/1498
|
||||
}
|
||||
|
||||
// Trim whitespaces
|
||||
p = strings.trim(strings.trim(p, ' '), '\t');
|
||||
|
||||
if (platform.isWindows) {
|
||||
|
||||
// Resolve the path against cwd if it is relative
|
||||
p = path.resolve(cwd, p);
|
||||
|
||||
// Trim trailing '.' chars on Windows to prevent invalid file names
|
||||
p = strings.rtrim(p, '.');
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
export interface IPathWithLineAndColumn {
|
||||
path: string;
|
||||
line?: number;
|
||||
column?: number;
|
||||
}
|
||||
|
||||
export function parseLineAndColumnAware(rawPath: string): IPathWithLineAndColumn {
|
||||
const segments = rawPath.split(':'); // C:\file.txt:<line>:<column>
|
||||
|
||||
let path: string;
|
||||
let line: number = null;
|
||||
let column: number = null;
|
||||
|
||||
segments.forEach(segment => {
|
||||
const segmentAsNumber = Number(segment);
|
||||
if (!types.isNumber(segmentAsNumber)) {
|
||||
path = !!path ? [path, segment].join(':') : segment; // a colon can well be part of a path (e.g. C:\...)
|
||||
} else if (line === null) {
|
||||
line = segmentAsNumber;
|
||||
} else if (column === null) {
|
||||
column = segmentAsNumber;
|
||||
}
|
||||
});
|
||||
|
||||
if (!path) {
|
||||
throw new Error('Format for `--goto` should be: `FILE:LINE(:COLUMN)`');
|
||||
}
|
||||
|
||||
return {
|
||||
path: path,
|
||||
line: line !== null ? line : void 0,
|
||||
column: column !== null ? column : line !== null ? 1 : void 0 // if we have a line, make sure column is also set
|
||||
};
|
||||
}
|
||||
|
||||
function toPath(p: IPathWithLineAndColumn): string {
|
||||
const segments = [p.path];
|
||||
|
||||
if (types.isNumber(p.line)) {
|
||||
segments.push(String(p.line));
|
||||
}
|
||||
|
||||
if (types.isNumber(p.column)) {
|
||||
segments.push(String(p.column));
|
||||
}
|
||||
|
||||
return segments.join(':');
|
||||
}
|
||||
92
src/vs/code/node/shellEnv.ts
Normal file
92
src/vs/code/node/shellEnv.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import * as cp from 'child_process';
|
||||
import { assign } from 'vs/base/common/objects';
|
||||
import { generateUuid } from 'vs/base/common/uuid';
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { isWindows } from 'vs/base/common/platform';
|
||||
|
||||
function getUnixShellEnvironment(): TPromise<typeof process.env> {
|
||||
const promise = new TPromise((c, e) => {
|
||||
const runAsNode = process.env['ELECTRON_RUN_AS_NODE'];
|
||||
const noAttach = process.env['ELECTRON_NO_ATTACH_CONSOLE'];
|
||||
const mark = generateUuid().replace(/-/g, '').substr(0, 12);
|
||||
const regex = new RegExp(mark + '(.*)' + mark);
|
||||
|
||||
const env = assign({}, process.env, {
|
||||
ELECTRON_RUN_AS_NODE: '1',
|
||||
ELECTRON_NO_ATTACH_CONSOLE: '1'
|
||||
});
|
||||
|
||||
const command = `'${process.execPath}' -p '"${mark}" + JSON.stringify(process.env) + "${mark}"'`;
|
||||
const child = cp.spawn(process.env.SHELL, ['-ilc', command], {
|
||||
detached: true,
|
||||
stdio: ['ignore', 'pipe', process.stderr],
|
||||
env
|
||||
});
|
||||
|
||||
const buffers: Buffer[] = [];
|
||||
child.on('error', () => c({}));
|
||||
child.stdout.on('data', b => buffers.push(b as Buffer));
|
||||
|
||||
child.on('close', (code: number, signal: any) => {
|
||||
if (code !== 0) {
|
||||
return e(new Error('Failed to get environment'));
|
||||
}
|
||||
|
||||
const raw = Buffer.concat(buffers).toString('utf8');
|
||||
const match = regex.exec(raw);
|
||||
const rawStripped = match ? match[1] : '{}';
|
||||
|
||||
try {
|
||||
const env = JSON.parse(rawStripped);
|
||||
|
||||
if (runAsNode) {
|
||||
env['ELECTRON_RUN_AS_NODE'] = runAsNode;
|
||||
} else {
|
||||
delete env['ELECTRON_RUN_AS_NODE'];
|
||||
}
|
||||
|
||||
if (noAttach) {
|
||||
env['ELECTRON_NO_ATTACH_CONSOLE'] = noAttach;
|
||||
} else {
|
||||
delete env['ELECTRON_NO_ATTACH_CONSOLE'];
|
||||
}
|
||||
|
||||
c(env);
|
||||
} catch (err) {
|
||||
e(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// swallow errors
|
||||
return promise.then(null, () => ({}));
|
||||
}
|
||||
|
||||
|
||||
let _shellEnv: TPromise<typeof process.env>;
|
||||
|
||||
/**
|
||||
* We need to get the environment from a user's shell.
|
||||
* This should only be done when Code itself is not launched
|
||||
* from within a shell.
|
||||
*/
|
||||
export function getShellEnvironment(): TPromise<typeof process.env> {
|
||||
if (_shellEnv === undefined) {
|
||||
if (isWindows) {
|
||||
_shellEnv = TPromise.as({});
|
||||
} else if (process.env['VSCODE_CLI'] === '1') {
|
||||
_shellEnv = TPromise.as({});
|
||||
} else {
|
||||
_shellEnv = getUnixShellEnvironment();
|
||||
}
|
||||
}
|
||||
|
||||
return _shellEnv;
|
||||
}
|
||||
168
src/vs/code/node/windowsFinder.ts
Normal file
168
src/vs/code/node/windowsFinder.ts
Normal file
@@ -0,0 +1,168 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as platform from 'vs/base/common/platform';
|
||||
import * as paths from 'vs/base/common/paths';
|
||||
import { OpenContext } from 'vs/platform/windows/common/windows';
|
||||
import { IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, isSingleFolderWorkspaceIdentifier, IResolvedWorkspace } from 'vs/platform/workspaces/common/workspaces';
|
||||
|
||||
export interface ISimpleWindow {
|
||||
openedWorkspace?: IWorkspaceIdentifier;
|
||||
openedFolderPath?: string;
|
||||
openedFilePath?: string;
|
||||
extensionDevelopmentPath?: string;
|
||||
lastFocusTime: number;
|
||||
}
|
||||
|
||||
export interface IBestWindowOrFolderOptions<W extends ISimpleWindow> {
|
||||
windows: W[];
|
||||
newWindow: boolean;
|
||||
reuseWindow: boolean;
|
||||
context: OpenContext;
|
||||
filePath?: string;
|
||||
userHome?: string;
|
||||
codeSettingsFolder?: string;
|
||||
workspaceResolver: (workspace: IWorkspaceIdentifier) => IResolvedWorkspace;
|
||||
}
|
||||
|
||||
export function findBestWindowOrFolderForFile<W extends ISimpleWindow>({ windows, newWindow, reuseWindow, context, filePath, userHome, codeSettingsFolder, workspaceResolver }: IBestWindowOrFolderOptions<W>): W | string {
|
||||
if (!newWindow && filePath && (context === OpenContext.DESKTOP || context === OpenContext.CLI || context === OpenContext.DOCK)) {
|
||||
const windowOnFilePath = findWindowOnFilePath(windows, filePath, workspaceResolver);
|
||||
|
||||
// 1) window wins if it has a workspace opened
|
||||
if (windowOnFilePath && !!windowOnFilePath.openedWorkspace) {
|
||||
return windowOnFilePath;
|
||||
}
|
||||
|
||||
// 2) window wins if it has a folder opened that is more specific than settings folder
|
||||
const folderWithCodeSettings = !reuseWindow && findFolderWithCodeSettings(filePath, userHome, codeSettingsFolder);
|
||||
if (windowOnFilePath && !(folderWithCodeSettings && folderWithCodeSettings.length > windowOnFilePath.openedFolderPath.length)) {
|
||||
return windowOnFilePath;
|
||||
}
|
||||
|
||||
// 3) finally return path to folder with settings
|
||||
if (folderWithCodeSettings) {
|
||||
return folderWithCodeSettings;
|
||||
}
|
||||
}
|
||||
|
||||
return !newWindow ? getLastActiveWindow(windows) : null;
|
||||
}
|
||||
|
||||
function findWindowOnFilePath<W extends ISimpleWindow>(windows: W[], filePath: string, workspaceResolver: (workspace: IWorkspaceIdentifier) => IResolvedWorkspace): W {
|
||||
|
||||
// First check for windows with workspaces that have a parent folder of the provided path opened
|
||||
const workspaceWindows = windows.filter(window => !!window.openedWorkspace);
|
||||
for (let i = 0; i < workspaceWindows.length; i++) {
|
||||
const window = workspaceWindows[i];
|
||||
const resolvedWorkspace = workspaceResolver(window.openedWorkspace);
|
||||
if (resolvedWorkspace && resolvedWorkspace.folders.some(folder => paths.isEqualOrParent(filePath, folder.path, !platform.isLinux /* ignorecase */))) {
|
||||
return window;
|
||||
}
|
||||
}
|
||||
|
||||
// Then go with single folder windows that are parent of the provided file path
|
||||
const singleFolderWindowsOnFilePath = windows.filter(window => typeof window.openedFolderPath === 'string' && paths.isEqualOrParent(filePath, window.openedFolderPath, !platform.isLinux /* ignorecase */));
|
||||
if (singleFolderWindowsOnFilePath.length) {
|
||||
return singleFolderWindowsOnFilePath.sort((a, b) => -(a.openedFolderPath.length - b.openedFolderPath.length))[0];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function findFolderWithCodeSettings(filePath: string, userHome?: string, codeSettingsFolder?: string): string {
|
||||
let folder = path.dirname(paths.normalize(filePath, true));
|
||||
let homeFolder = userHome && paths.normalize(userHome, true);
|
||||
if (!platform.isLinux) {
|
||||
homeFolder = homeFolder && homeFolder.toLowerCase();
|
||||
}
|
||||
|
||||
let previous = null;
|
||||
while (folder !== previous) {
|
||||
if (hasCodeSettings(folder, homeFolder, codeSettingsFolder)) {
|
||||
return folder;
|
||||
}
|
||||
|
||||
previous = folder;
|
||||
folder = path.dirname(folder);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// {{SQL CARBON EDIT}}
|
||||
function hasCodeSettings(folder: string, normalizedUserHome?: string, codeSettingsFolder = '.sqlops') {
|
||||
try {
|
||||
if ((platform.isLinux ? folder : folder.toLowerCase()) === normalizedUserHome) {
|
||||
return fs.statSync(path.join(folder, codeSettingsFolder, 'settings.json')).isFile(); // ~/.vscode/extensions is used for extensions
|
||||
}
|
||||
|
||||
return fs.statSync(path.join(folder, codeSettingsFolder)).isDirectory();
|
||||
} catch (err) {
|
||||
// assume impossible to access
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function getLastActiveWindow<W extends ISimpleWindow>(windows: W[]): W {
|
||||
const lastFocusedDate = Math.max.apply(Math, windows.map(window => window.lastFocusTime));
|
||||
|
||||
return windows.filter(window => window.lastFocusTime === lastFocusedDate)[0];
|
||||
}
|
||||
|
||||
export function findWindowOnWorkspace<W extends ISimpleWindow>(windows: W[], workspace: (IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier)): W {
|
||||
return windows.filter(window => {
|
||||
|
||||
// match on folder
|
||||
if (isSingleFolderWorkspaceIdentifier(workspace)) {
|
||||
if (typeof window.openedFolderPath === 'string' && (paths.isEqual(window.openedFolderPath, workspace, !platform.isLinux /* ignorecase */))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// match on workspace
|
||||
else {
|
||||
if (window.openedWorkspace && window.openedWorkspace.id === workspace.id) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
})[0];
|
||||
}
|
||||
|
||||
export function findWindowOnExtensionDevelopmentPath<W extends ISimpleWindow>(windows: W[], extensionDevelopmentPath: string): W {
|
||||
return windows.filter(window => {
|
||||
|
||||
// match on extension development path
|
||||
if (paths.isEqual(window.extensionDevelopmentPath, extensionDevelopmentPath, !platform.isLinux /* ignorecase */)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
})[0];
|
||||
}
|
||||
|
||||
export function findWindowOnWorkspaceOrFolderPath<W extends ISimpleWindow>(windows: W[], path: string): W {
|
||||
return windows.filter(window => {
|
||||
|
||||
// check for workspace config path
|
||||
if (window.openedWorkspace && paths.isEqual(window.openedWorkspace.configPath, path, !platform.isLinux /* ignorecase */)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// check for folder path
|
||||
if (window.openedFolderPath && paths.isEqual(window.openedFolderPath, path, !platform.isLinux /* ignorecase */)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
})[0];
|
||||
}
|
||||
Reference in New Issue
Block a user