SQL Operations Studio Public Preview 1 (0.23) release source code

This commit is contained in:
Karl Burtram
2017-11-09 14:30:27 -08:00
parent b88ecb8d93
commit 3cdac41339
8829 changed files with 759707 additions and 286 deletions

View File

@@ -0,0 +1,26 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//constants
export const configLogDebugInfo: string = 'logDebugInfo';
export const serviceNotCompatibleError: string = "Client is not compatible with the service layer";
export const serviceDownloading: string = "Downloading";
export const serviceInstalling: string = "Installing";
export const unsupportedPlatformErrorMessage: string = "This platform is unsupported and application services may not function correctly";
export const extensionActivated: string = 'activated.';
export const extensionDeactivated: string = 'de-activated.';
export const configEnabled: string = 'enabled';
export const configUseDebugSource = 'useDebugSource';
export const serviceConfigKey = 'service';
export const executableFilesConfigKey = 'executableFiles';
export const versionConfigKey = 'version';
export const downloadUrlConfigKey = 'downloadUrl';
export const installDirConfigKey = 'installDir';
export const serviceCrashButton = "View Known Issues";
export const configDebugSourcePath = 'debugSourcePath';
export const neverShowAgain = "Don't show again";
export const ignorePlatformWarning = 'ignorePlatformWarning';
export const usingDefaultPlatformMessage = "Unknown platform detected, defaulting to Linux_x64 platform";
export const serverConnectionMetadata = "serverConnectionMetadata";

View File

@@ -0,0 +1,50 @@
/*---------------------------------------------------------------------------------------------
* 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 {RequestType} from 'dataprotocol-client';
import {Runtime, LinuxDistribution} from '../platform';
// --------------------------------- < Version Request > -------------------------------------------------
// Version request message callback declaration
export namespace VersionRequest {
export const type: RequestType<void, VersionResult, void> = { get method(): string { return 'version'; } };
}
// Version response format
export type VersionResult = string;
// ------------------------------- </ Version Request > --------------------------------------------------
// Constants interface for each extension
export interface IExtensionConstants {
// TODO: Fill in interface
// Definitely dependent on the extension
extensionName: string;
invalidServiceFilePath: string;
serviceName: string;
extensionConfigSectionName: string;
serviceCompatibleVersion: string;
outputChannelName: string;
languageId: string;
serviceInstallingTo: string;
serviceInitializing: string;
serviceInstalled: string;
serviceLoadingFailed: string;
serviceInstallationFailed: string;
serviceInitializingOutputChannelName: string;
commandsNotAvailableWhileInstallingTheService : string;
providerId: string;
serviceCrashMessage: string;
serviceCrashLink: string;
installFolderName: string;
telemetryExtensionName: string;
getRuntimeId(platform: string, architecture: string, distribution: LinuxDistribution): Runtime;
}

View File

@@ -0,0 +1,55 @@
import {NotificationType, ServerOptions} from 'dataprotocol-client';
import {ITelemetryEventProperties, ITelemetryEventMeasures} from '../telemetry';
import {Runtime} from '../platform';
// ------------------------------- < Telemetry Sent Event > ------------------------------------
/**
* Event sent when the language service send a telemetry event
*/
export namespace TelemetryNotification {
export const type: NotificationType<TelemetryParams> = { get method(): string { return 'telemetry/sqlevent'; } };
}
/**
* Update event parameters
*/
export class TelemetryParams {
public params: {
eventName: string;
properties: ITelemetryEventProperties;
measures: ITelemetryEventMeasures;
};
}
// ------------------------------- </ Telemetry Sent Event > ----------------------------------
// ------------------------------- < Status Event > ------------------------------------
/**
* Event sent when the language service send a status change event
*/
export namespace StatusChangedNotification {
export const type: NotificationType<StatusChangeParams> = { get method(): string { return 'textDocument/statusChanged'; } };
}
/**
* Update event parameters
*/
export class StatusChangeParams {
/**
* URI identifying the text document
*/
public ownerUri: string;
/**
* The new status of the document
*/
public status: string;
}
// ------------------------------- </ Status Sent Event > ----------------------------------
export interface ILanguageClientHelper {
createServerOptions(servicePath: string, runtimeId?: Runtime): ServerOptions;
}

View File

@@ -0,0 +1,13 @@
'use strict';
export interface ILogger {
logDebug(message: string): void;
increaseIndent(): void;
decreaseIndent(): void;
append(message?: string): void;
appendLine(message?: string): void;
}
export interface IRuntime {
getRuntimeDisplayName();
}

View File

@@ -0,0 +1,68 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as os from 'os';
import {ILogger} from './interfaces';
import * as Utils from './utils';
import {IExtensionConstants} from './contracts/contracts';
/*
* Logger class handles logging messages using the Util functions.
*/
export class Logger implements ILogger {
private _writer: (message: string) => void;
private _prefix: string;
private _extensionConstants: IExtensionConstants;
private _indentLevel: number = 0;
private _indentSize: number = 4;
private _atLineStart: boolean = false;
constructor(writer: (message: string) => void, extensionConstants: IExtensionConstants, prefix?: string) {
this._writer = writer;
this._prefix = prefix;
this._extensionConstants = extensionConstants;
}
public logDebug(message: string): void {
Utils.logDebug(message, this._extensionConstants.extensionConfigSectionName);
}
private _appendCore(message: string): void {
if (this._atLineStart) {
if (this._indentLevel > 0) {
const indent = ' '.repeat(this._indentLevel * this._indentSize);
this._writer(indent);
}
if (this._prefix) {
this._writer(`[${this._prefix}] `);
}
this._atLineStart = false;
}
this._writer(message);
}
public increaseIndent(): void {
this._indentLevel += 1;
}
public decreaseIndent(): void {
if (this._indentLevel > 0) {
this._indentLevel -= 1;
}
}
public append(message?: string): void {
message = message || '';
this._appendCore(message);
}
public appendLine(message?: string): void {
message = message || '';
this._appendCore(message + os.EOL);
this._atLineStart = true;
}
}

View File

@@ -0,0 +1,369 @@
/*---------------------------------------------------------------------------------------------
* 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 child_process from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
const unknown = 'unknown';
export enum Runtime {
UnknownRuntime = <any>'Unknown',
UnknownVersion = <any>'Unknown',
Windows_86 = <any>'Windows_86',
Windows_64 = <any>'Windows_64',
OSX = <any> 'OSX',
CentOS_7 = <any>'CentOS_7',
Debian_8 = <any>'Debian_8',
Fedora_23 = <any>'Fedora_23',
OpenSUSE_13_2 = <any>'OpenSUSE_13_2',
SLES_12_2 = <any>'SLES_12_2',
RHEL_7 = <any>'RHEL_7',
Ubuntu_14 = <any>'Ubuntu_14',
Ubuntu_16 = <any>'Ubuntu_16',
Linux_64 = <any>'Linux_64',
Linux_86 = <any>'Linux-86'
}
export function getRuntimeDisplayName(runtime: Runtime): string {
switch (runtime) {
case Runtime.Windows_64:
return 'Windows';
case Runtime.Windows_86:
return 'Windows';
case Runtime.OSX:
return 'OSX';
case Runtime.CentOS_7:
return 'Linux';
case Runtime.Debian_8:
return 'Linux';
case Runtime.Fedora_23:
return 'Linux';
case Runtime.OpenSUSE_13_2:
return 'Linux';
case Runtime.SLES_12_2:
return 'Linux';
case Runtime.RHEL_7:
return 'Linux';
case Runtime.Ubuntu_14:
return 'Linux';
case Runtime.Ubuntu_16:
return 'Linux';
case Runtime.Linux_64:
return 'Linux';
case Runtime.Linux_86:
return 'Linux';
default:
return 'Unknown';
}
}
export class PlatformInformation {
public runtimeId: Runtime;
public constructor(
public platform: string,
public architecture: string,
public distribution: LinuxDistribution = undefined,
public getRuntimeId: (platform: string, architecture: string, distribution: LinuxDistribution) => Runtime) {
try {
this.runtimeId = this.getRuntimeId(platform, architecture, distribution);
} catch (err) {
this.runtimeId = undefined;
}
}
public isWindows(): boolean {
return this.platform === 'win32';
}
public isMacOS(): boolean {
return this.platform === 'darwin';
}
public isLinux(): boolean {
return this.platform === 'linux';
}
public isValidRuntime(): boolean {
return this.runtimeId !== undefined && this.runtimeId !== Runtime.UnknownRuntime && this.runtimeId !== Runtime.UnknownVersion;
}
public getRuntimeDisplayName(): string {
return getRuntimeDisplayName(this.runtimeId);
}
public toString(): string {
let result = this.platform;
if (this.architecture) {
if (result) {
result += ', ';
}
result += this.architecture;
}
if (this.distribution) {
if (result) {
result += ', ';
}
result += this.distribution.toString();
}
return result;
}
public static getCurrent(getRuntimeId: (platform: string, architecture: string, distribution: LinuxDistribution) => Runtime,
extensionName: string): Promise<any> {
let platform = os.platform();
let architecturePromise: Promise<string>;
let distributionPromise: Promise<LinuxDistribution>;
switch (platform) {
case 'win32':
architecturePromise = PlatformInformation.getWindowsArchitecture();
distributionPromise = Promise.resolve(undefined);
break;
case 'darwin':
let osVersion = os.release();
if (parseFloat(osVersion) < 16.0 && extensionName === 'mssql') {
return Promise.reject('The current version of macOS is not supported. Only macOS Sierra and above (>= 10.12) are supported.')
}
architecturePromise = PlatformInformation.getUnixArchitecture();
distributionPromise = Promise.resolve(undefined);
break;
case 'linux':
architecturePromise = PlatformInformation.getUnixArchitecture();
distributionPromise = LinuxDistribution.getCurrent();
break;
default:
return Promise.reject(`Unsupported platform: ${platform}`);
}
return architecturePromise.then( arch => {
return distributionPromise.then(distro => {
return new PlatformInformation(platform, arch, distro, getRuntimeId);
});
});
}
private static getWindowsArchitecture(): Promise<string> {
return new Promise<string>((resolve, reject) => {
// try to get the architecture from WMIC
PlatformInformation.getWindowsArchitectureWmic().then(architecture => {
if (architecture && architecture !== unknown) {
resolve(architecture);
} else {
// sometimes WMIC isn't available on the path so then try to parse the envvar
PlatformInformation.getWindowsArchitectureEnv().then(architecture => {
resolve(architecture);
});
}
});
});
}
private static getWindowsArchitectureWmic(): Promise<string> {
return this.execChildProcess('wmic os get osarchitecture')
.then(architecture => {
if (architecture) {
let archArray: string[] = architecture.split(os.EOL);
if (archArray.length >= 2) {
let arch = archArray[1].trim();
// Note: This string can be localized. So, we'll just check to see if it contains 32 or 64.
if (arch.indexOf('64') >= 0) {
return 'x86_64';
} else if (arch.indexOf('32') >= 0) {
return 'x86';
}
}
}
return unknown;
}).catch((error) => {
return unknown;
});
}
private static getWindowsArchitectureEnv(): Promise<string> {
return new Promise<string>((resolve, reject) => {
if (process.env.PROCESSOR_ARCHITECTURE === 'x86' && process.env.PROCESSOR_ARCHITEW6432 === undefined) {
resolve('x86');
}
else {
resolve('x86_64');
}
});
}
private static getUnixArchitecture(): Promise<string> {
return this.execChildProcess('uname -m')
.then(architecture => {
if (architecture) {
return architecture.trim();
}
return undefined;
});
}
private static execChildProcess(process: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
child_process.exec(process, { maxBuffer: 500 * 1024 }, (error: Error, stdout: string, stderr: string) => {
if (error) {
reject(error);
return;
}
if (stderr && stderr.length > 0) {
reject(new Error(stderr));
return;
}
resolve(stdout);
});
});
}
private static getRuntimeIdHelper(distributionName: string, distributionVersion: string): Runtime {
switch (distributionName) {
case 'ubuntu':
if (distributionVersion.startsWith('14')) {
// This also works for Linux Mint
return Runtime.Ubuntu_14;
} else if (distributionVersion.startsWith('16')) {
return Runtime.Ubuntu_16;
}
break;
case 'elementary':
case 'elementary OS':
if (distributionVersion.startsWith('0.3')) {
// Elementary OS 0.3 Freya is binary compatible with Ubuntu 14.04
return Runtime.Ubuntu_14;
} else if (distributionVersion.startsWith('0.4')) {
// Elementary OS 0.4 Loki is binary compatible with Ubuntu 16.04
return Runtime.Ubuntu_16;
}
break;
case 'linuxmint':
if (distributionVersion.startsWith('18')) {
// Linux Mint 18 is binary compatible with Ubuntu 16.04
return Runtime.Ubuntu_16;
}
break;
case 'centos':
case 'ol':
// Oracle Linux is binary compatible with CentOS
return Runtime.CentOS_7;
case 'fedora':
return Runtime.Fedora_23;
case 'opensuse':
return Runtime.OpenSUSE_13_2;
case 'sles':
return Runtime.SLES_12_2;
case 'rhel':
return Runtime.RHEL_7;
case 'debian':
return Runtime.Debian_8;
case 'galliumos':
if (distributionVersion.startsWith('2.0')) {
return Runtime.Ubuntu_16;
}
break;
default:
return Runtime.Linux_64;
}
return Runtime.Linux_64;
}
}
/**
* There is no standard way on Linux to find the distribution name and version.
* Recently, systemd has pushed to standardize the os-release file. This has
* seen adoption in "recent" versions of all major distributions.
* https://www.freedesktop.org/software/systemd/man/os-release.html
*/
export class LinuxDistribution {
public constructor(
public name: string,
public version: string,
public idLike?: string[]) { }
public static getCurrent(): Promise<LinuxDistribution> {
// Try /etc/os-release and fallback to /usr/lib/os-release per the synopsis
// at https://www.freedesktop.org/software/systemd/man/os-release.html.
return LinuxDistribution.fromFilePath('/etc/os-release')
.catch(() => LinuxDistribution.fromFilePath('/usr/lib/os-release'))
.catch(() => Promise.resolve(new LinuxDistribution(unknown, unknown)));
}
public toString(): string {
return `name=${this.name}, version=${this.version}`;
}
private static fromFilePath(filePath: string): Promise<LinuxDistribution> {
return new Promise<LinuxDistribution>((resolve, reject) => {
fs.readFile(filePath, 'utf8', (error, data) => {
if (error) {
reject(error);
} else {
resolve(LinuxDistribution.fromReleaseInfo(data));
}
});
});
}
public static fromReleaseInfo(releaseInfo: string, eol: string = os.EOL): LinuxDistribution {
let name = unknown;
let version = unknown;
let idLike: string[] = undefined;
const lines = releaseInfo.split(eol);
for (let line of lines) {
line = line.trim();
let equalsIndex = line.indexOf('=');
if (equalsIndex >= 0) {
let key = line.substring(0, equalsIndex);
let value = line.substring(equalsIndex + 1);
// Strip quotes if necessary
if (value.length > 1 && value.startsWith('"') && value.endsWith('"')) {
value = value.substring(1, value.length - 1);
} else if (value.length > 1 && value.startsWith('\'') && value.endsWith('\'')) {
value = value.substring(1, value.length - 1);
}
if (key === 'ID') {
name = value;
} else if (key === 'VERSION_ID') {
version = value;
} else if (key === 'ID_LIKE') {
idLike = value.split(' ');
}
if (name !== unknown && version !== unknown && idLike !== undefined) {
break;
}
}
}
return new LinuxDistribution(name, version, idLike);
}
}

View File

@@ -0,0 +1,161 @@
/*---------------------------------------------------------------------------------------------
* 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 vscode = require('vscode');
import TelemetryReporter from 'vscode-extension-telemetry';
import Utils = require('./utils');
import { PlatformInformation, Runtime, LinuxDistribution } from './platform';
import { IExtensionConstants } from './contracts/contracts';
export interface ITelemetryEventProperties {
[key: string]: string;
}
export interface ITelemetryEventMeasures {
[key: string]: number;
}
/**
* Filters error paths to only include source files. Exported to support testing
*/
export function FilterErrorPath(line: string): string {
if (line) {
let values: string[] = line.split('/out/');
if (values.length <= 1) {
// Didn't match expected format
return line;
} else {
return values[1];
}
}
}
export class Telemetry {
private static reporter: TelemetryReporter;
private static userId: string;
private static platformInformation: PlatformInformation;
private static disabled: boolean;
private static _getRuntimeId: (platform: string, architecture: string, distribution: LinuxDistribution) => Runtime;
public static get getRuntimeId() {
return this._getRuntimeId;
}
public static set getRuntimeId(runtimeIdGetter: (platform: string, architecture: string, distribution: LinuxDistribution) => Runtime) {
this._getRuntimeId = runtimeIdGetter;
}
// Get the unique ID for the current user of the extension
public static getUserId(): Promise<string> {
return new Promise<string>(resolve => {
// Generate the user id if it has not been created already
if (typeof this.userId === 'undefined') {
let id = Utils.generateUserId();
id.then(newId => {
this.userId = newId;
resolve(this.userId);
});
} else {
resolve(this.userId);
}
});
}
public static getPlatformInformation(): Promise<PlatformInformation> {
if (this.platformInformation) {
return Promise.resolve(this.platformInformation);
} else {
return new Promise<PlatformInformation>(resolve => {
PlatformInformation.getCurrent(this.getRuntimeId, 'telemetry').then(info => {
this.platformInformation = info;
resolve(this.platformInformation);
});
});
}
}
/**
* Disable telemetry reporting
*/
public static disable(): void {
this.disabled = true;
}
/**
* Initialize the telemetry reporter for use.
*/
public static initialize(context: vscode.ExtensionContext, extensionConstants: IExtensionConstants): void {
if (typeof this.reporter === 'undefined') {
// Check if the user has opted out of telemetry
if (!vscode.workspace.getConfiguration('telemetry').get<boolean>('enableTelemetry', true)) {
this.disable();
return;
}
let packageInfo = Utils.getPackageInfo(context);
this.reporter = new TelemetryReporter(extensionConstants.telemetryExtensionName, packageInfo.version, packageInfo.aiKey);
}
}
/**
* Send a telemetry event for an exception
*/
public static sendTelemetryEventForException(
err: any, methodName: string, extensionConfigName: string): void {
try {
let stackArray: string[];
let firstLine: string = '';
if (err !== undefined && err.stack !== undefined) {
stackArray = err.stack.split('\n');
if (stackArray !== undefined && stackArray.length >= 2) {
firstLine = stackArray[1]; // The fist line is the error message and we don't want to send that telemetry event
firstLine = FilterErrorPath(firstLine);
}
}
// Only adding the method name and the fist line of the stack trace. We don't add the error message because it might have PII
this.sendTelemetryEvent('Exception', { methodName: methodName, errorLine: firstLine });
Utils.logDebug('Unhandled Exception occurred. error: ' + err + ' method: ' + methodName, extensionConfigName);
} catch (telemetryErr) {
// If sending telemetry event fails ignore it so it won't break the extension
Utils.logDebug('Failed to send telemetry event. error: ' + telemetryErr, extensionConfigName);
}
}
/**
* Send a telemetry event using application insights
*/
public static sendTelemetryEvent(
eventName: string,
properties?: ITelemetryEventProperties,
measures?: ITelemetryEventMeasures): void {
if (typeof this.disabled === 'undefined') {
this.disabled = false;
}
if (this.disabled || typeof (this.reporter) === 'undefined') {
// Don't do anything if telemetry is disabled
return;
}
if (!properties || typeof properties === 'undefined') {
properties = {};
}
// Augment the properties structure with additional common properties before sending
Promise.all([this.getUserId, this.getPlatformInformation]).then(() => {
properties['userId'] = this.userId;
properties['distribution'] = (this.platformInformation && this.platformInformation.distribution) ?
`${this.platformInformation.distribution.name}, ${this.platformInformation.distribution.version}` : '';
this.reporter.sendTelemetryEvent(eventName, properties, measures);
});
}
}
export default Telemetry;

View File

@@ -0,0 +1,270 @@
'use strict';
import * as crypto from 'crypto';
import * as os from 'os';
import vscode = require('vscode');
import Constants = require('./constants');
import {ExtensionContext} from 'vscode';
import fs = require('fs');
var path = require('path');
// CONSTANTS //////////////////////////////////////////////////////////////////////////////////////
const msInH = 3.6e6;
const msInM = 60000;
const msInS = 1000;
// INTERFACES /////////////////////////////////////////////////////////////////////////////////////
// Interface for package.json information
export interface IPackageInfo {
name: string;
version: string;
aiKey: string;
}
// FUNCTIONS //////////////////////////////////////////////////////////////////////////////////////
// Get information from the extension's package.json file
export function getPackageInfo(context: ExtensionContext): IPackageInfo {
let extensionPackage = require(context.asAbsolutePath('./package.json'));
if (extensionPackage) {
return {
name: extensionPackage.name,
version: extensionPackage.version,
aiKey: extensionPackage.aiKey
};
}
}
// Generate a new GUID
export function generateGuid(): string {
let hexValues: string[] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
// c.f. rfc4122 (UUID version 4 = xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx)
let oct: string = '';
let tmp: number;
/* tslint:disable:no-bitwise */
for (let a: number = 0; a < 4; a++) {
tmp = (4294967296 * Math.random()) | 0;
oct += hexValues[tmp & 0xF] +
hexValues[tmp >> 4 & 0xF] +
hexValues[tmp >> 8 & 0xF] +
hexValues[tmp >> 12 & 0xF] +
hexValues[tmp >> 16 & 0xF] +
hexValues[tmp >> 20 & 0xF] +
hexValues[tmp >> 24 & 0xF] +
hexValues[tmp >> 28 & 0xF];
}
// 'Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively'
let clockSequenceHi: string = hexValues[8 + (Math.random() * 4) | 0];
return oct.substr(0, 8) + '-' + oct.substr(9, 4) + '-4' + oct.substr(13, 3) + '-' + clockSequenceHi + oct.substr(16, 3) + '-' + oct.substr(19, 12);
/* tslint:enable:no-bitwise */
}
// Generate a unique, deterministic ID for the current user of the extension
export function generateUserId(): Promise<string> {
return new Promise<string>(resolve => {
try {
let interfaces = os.networkInterfaces();
let mac;
for(let key of Object.keys(interfaces)) {
let item = interfaces[key][0];
if (!item.internal) {
mac = item.mac;
break;
}
}
if (mac) {
resolve(crypto.createHash('sha256').update(mac + os.homedir(), 'utf8').digest('hex'));
} else {
resolve(generateGuid());
}
} catch (err) {
resolve(generateGuid()); // fallback
}
});
}
// Return 'true' if the active editor window has a .sql file, false otherwise
export function isEditingSqlFile(languageId: string): boolean {
let sqlFile = false;
let editor = getActiveTextEditor();
if (editor) {
if (editor.document.languageId === languageId) {
sqlFile = true;
}
}
return sqlFile;
}
// Return the active text editor if there's one
export function getActiveTextEditor(): vscode.TextEditor {
let editor = undefined;
if (vscode.window && vscode.window.activeTextEditor) {
editor = vscode.window.activeTextEditor;
}
return editor;
}
// Retrieve the URI for the currently open file if there is one; otherwise return the empty string
export function getActiveTextEditorUri(): string {
if (typeof vscode.window.activeTextEditor !== 'undefined' &&
typeof vscode.window.activeTextEditor.document !== 'undefined') {
return vscode.window.activeTextEditor.document.uri.toString();
}
return '';
}
// Helper to log messages to output channel
export function logToOutputChannel(msg: any, outputChannelName: string): void {
let outputChannel = vscode.window.createOutputChannel(outputChannelName);
outputChannel.show();
if (msg instanceof Array) {
msg.forEach(element => {
outputChannel.appendLine(element.toString());
});
} else {
outputChannel.appendLine(msg.toString());
}
}
// Helper to log debug messages
export function logDebug(msg: any, extensionConfigSectionName: string): void {
let config = vscode.workspace.getConfiguration(extensionConfigSectionName);
let logDebugInfo = config[Constants.configLogDebugInfo];
if (logDebugInfo === true) {
let currentTime = new Date().toLocaleTimeString();
let outputMsg = '[' + currentTime + ']: ' + msg ? msg.toString() : '';
console.log(outputMsg);
}
}
// Helper to show an info message
export function showInfoMsg(msg: string, extensionName: string): void {
vscode.window.showInformationMessage(extensionName + ': ' + msg );
}
// Helper to show an warn message
export function showWarnMsg(msg: string, extensionName: string): void {
vscode.window.showWarningMessage(extensionName + ': ' + msg );
}
// Helper to show an error message
export function showErrorMsg(msg: string, extensionName: string): void {
vscode.window.showErrorMessage(extensionName + ': ' + msg );
}
export function isEmpty(str: any): boolean {
return (!str || '' === str);
}
export function isNotEmpty(str: any): boolean {
return <boolean>(str && '' !== str);
}
/**
* Format a string. Behaves like C#'s string.Format() function.
*/
export function formatString(str: string, ...args: any[]): string {
// This is based on code originally from https://github.com/Microsoft/vscode/blob/master/src/vs/nls.js
// License: https://github.com/Microsoft/vscode/blob/master/LICENSE.txt
let result: string;
if (args.length === 0) {
result = str;
} else {
result = str.replace(/\{(\d+)\}/g, (match, rest) => {
let index = rest[0];
return typeof args[index] !== 'undefined' ? args[index] : match;
});
}
return result;
}
/**
* Check if a file exists on disk
*/
export function isFileExisting(filePath: string): boolean {
try {
fs.statSync(filePath);
return true;
} catch (err) {
return false;
}
}
/**
* Takes a string in the format of HH:MM:SS.MS and returns a number representing the time in
* miliseconds
* @param value The string to convert to milliseconds
* @return False is returned if the string is an invalid format,
* the number of milliseconds in the time string is returned otherwise.
*/
export function parseTimeString(value: string): number | boolean {
if (!value) {
return false;
}
let tempVal = value.split('.');
if (tempVal.length !== 2) {
return false;
}
let ms = parseInt(tempVal[1].substring(0, 3), 10);
tempVal = tempVal[0].split(':');
if (tempVal.length !== 3) {
return false;
}
let h = parseInt(tempVal[0], 10);
let m = parseInt(tempVal[1], 10);
let s = parseInt(tempVal[2], 10);
return ms + (h * msInH) + (m * msInM) + (s * msInS);
}
/**
* Takes a number of milliseconds and converts it to a string like HH:MM:SS.fff
* @param value The number of milliseconds to convert to a timespan string
* @returns A properly formatted timespan string.
*/
export function parseNumAsTimeString(value: number): string {
let tempVal = value;
let h = Math.floor(tempVal / msInH);
tempVal %= msInH;
let m = Math.floor(tempVal / msInM);
tempVal %= msInM;
let s = Math.floor(tempVal / msInS);
tempVal %= msInS;
let hs = h < 10 ? '0' + h : '' + h;
let ms = m < 10 ? '0' + m : '' + m;
let ss = s < 10 ? '0' + s : '' + s;
let mss = tempVal < 10 ? '00' + tempVal : tempVal < 100 ? '0' + tempVal : '' + tempVal;
let rs = hs + ':' + ms + ':' + ss;
return tempVal > 0 ? rs + '.' + mss : rs;
}
// The function is a duplicate of carbon\src\paths.js. IT would be better to import path.js but it doesn't
// work for now because the extension is running in different process.
export function getAppDataPath() {
var platform = process.platform;
switch (platform) {
case 'win32': return process.env['APPDATA'] || path.join(process.env['USERPROFILE'], 'AppData', 'Roaming');
case 'darwin': return path.join(os.homedir(), 'Library', 'Application Support');
case 'linux': return process.env['XDG_CONFIG_HOME'] || path.join(os.homedir(), '.config');
default: throw new Error('Platform not supported');
}
}
export function getDefaultLogLocation() {
var platform = process.platform;
let rootFolderName: string = '.sqlops';
if (platform === 'win32') {
rootFolderName = 'sqlops';
}
return path.join(getAppDataPath(), rootFolderName);
}