-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmain.ts
More file actions
178 lines (153 loc) · 6.38 KB
/
main.ts
File metadata and controls
178 lines (153 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*---------------------------------------------------------------------------------------------
* 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 vscode from 'vscode';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { SqlOpsDataClient, ClientOptions } from 'dataprotocol-client';
import { IConfig, ServerProvider, Events } from '@microsoft/ads-service-downloader';
import { ServerOptions, TransportKind } from 'vscode-languageclient';
import * as Constants from './constants';
import ContextProvider from './contextProvider';
import * as Utils from './utils';
import { TelemetryReporter, LanguageClientErrorHandler } from './telemetry';
import { TelemetryFeature } from './features/telemetry';
import { ServerContextualizationServiceFeature } from './features/context';
const baseConfig = require('./config.json');
const outputChannel = vscode.window.createOutputChannel(Constants.serviceName);
const statusView = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
export async function activate(context: vscode.ExtensionContext) {
// lets make sure we support this platform first
let supported = await Utils.verifyPlatform();
if (!supported) {
vscode.window.showErrorMessage('Unsupported platform');
return;
}
let config: IConfig = JSON.parse(JSON.stringify(baseConfig));
config.installDirectory = path.join(__dirname, config.installDirectory);
config.proxy = vscode.workspace.getConfiguration('http').get('proxy');
config.strictSSL = vscode.workspace.getConfiguration('http').get('proxyStrictSSL') || true;
let languageClient: SqlOpsDataClient;
const serverdownloader = new ServerProvider(config);
serverdownloader.eventEmitter.onAny(generateHandleServerProviderEvent());
let clientOptions: ClientOptions = {
providerId: Constants.providerId,
errorHandler: new LanguageClientErrorHandler(),
documentSelector: ['sql'],
synchronize: {
configurationSection: Constants.providerId
},
features: [
// we only want to add new features
...SqlOpsDataClient.defaultFeatures,
TelemetryFeature,
ServerContextualizationServiceFeature
]
};
const installationStart = Date.now();
serverdownloader.getOrDownloadServer().then(e => {
const installationComplete = Date.now();
let serverOptions = generateServerOptions(e);
languageClient = new SqlOpsDataClient(Constants.serviceName, serverOptions, clientOptions);
const processStart = Date.now();
languageClient.onReady().then(() => {
const processEnd = Date.now();
statusView.text = Constants.providerId + ' service started';
setTimeout(() => {
statusView.hide();
}, 1500);
TelemetryReporter.sendTelemetryEvent('startup/LanguageClientStarted', {
installationTime: String(installationComplete - installationStart),
processStartupTime: String(processEnd - processStart),
totalTime: String(processEnd - installationStart),
beginningTimestamp: String(installationStart)
});
});
statusView.show();
statusView.text = 'Starting ' + Constants.providerId + ' service';
languageClient.start();
}, e => {
TelemetryReporter.sendTelemetryEvent('ServiceInitializingFailed');
vscode.window.showErrorMessage('Failed to start ' + Constants.providerId + ' tools service');
});
let contextProvider = new ContextProvider();
context.subscriptions.push(contextProvider);
context.subscriptions.push(TelemetryReporter);
context.subscriptions.push({ dispose: () => languageClient.stop() });
}
function generateServerOptions(executablePath: string): ServerOptions {
let serverArgs = [];
let serverCommand: string = executablePath;
let config = vscode.workspace.getConfiguration('pgsql');
if (config) {
// Override the server path with the local debug path if enabled
let useLocalSource = config["useDebugSource"];
if (useLocalSource) {
let localSourcePath = config["debugSourcePath"];
let filePath = path.join(localSourcePath, "ossdbtoolsservice/ossdbtoolsservice_main.py");
process.env.PYTHONPATH = localSourcePath;
serverCommand = process.platform === 'win32' ? 'python' : 'python3';
let enableStartupDebugging = config["enableStartupDebugging"];
let debuggingArg = enableStartupDebugging ? '--enable-remote-debugging-wait' : '--enable-remote-debugging';
let debugPort = config["debugServerPort"];
debuggingArg += '=' + debugPort;
serverArgs = [filePath, debuggingArg];
}
let logFileLocation = path.join(Utils.getDefaultLogLocation(), Constants.providerId);
serverArgs.push('--log-dir=' + logFileLocation);
serverArgs.push(logFileLocation);
// Enable diagnostic logging in the service if it is configured
let logDebugInfo = config["logDebugInfo"];
if (logDebugInfo) {
serverArgs.push('--enable-logging');
}
}
serverArgs.push('provider=' + Constants.providerId);
// run the service host
return { command: serverCommand, args: serverArgs, transport: TransportKind.stdio };
}
function generateHandleServerProviderEvent() {
let dots = 0;
return (e: string, ...args: any[]) => {
outputChannel.show();
statusView.show();
switch (e) {
case Events.INSTALL_START:
outputChannel.appendLine(`Installing ${Constants.serviceName} to ${args[0]}`);
statusView.text = 'Installing Service';
break;
case Events.INSTALL_END:
outputChannel.appendLine('Installed');
break;
case Events.DOWNLOAD_START:
outputChannel.appendLine(`Downloading ${args[0]}`);
outputChannel.append(`(${Math.ceil(args[1] / 1024)} KB)`);
statusView.text = 'Downloading Service';
break;
case Events.DOWNLOAD_PROGRESS:
let newDots = Math.ceil(args[0] / 5);
if (newDots > dots) {
outputChannel.append('.'.repeat(newDots - dots));
dots = newDots;
}
break;
case Events.DOWNLOAD_END:
outputChannel.appendLine('Done!');
break;
}
};
}
// this method is called when your extension is deactivated
export function deactivate(): void {
const tempFolder = fs.realpathSync(os.tmpdir());
const tempFolders = fs.readdirSync(tempFolder).filter((file) => file.startsWith('_MEI'));
tempFolders.forEach((folder) => {
const folderPath = path.join(tempFolder, folder);
if (fs.existsSync(folderPath)) {
fs.rmdirSync(folderPath, { recursive: true });
}
});
}