forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
150 lines (123 loc) · 5.28 KB
/
extension.ts
File metadata and controls
150 lines (123 loc) · 5.28 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
'use strict';
// tslint:disable:no-var-requires no-require-imports
// This line should always be right on top.
// tslint:disable:no-any
if ((Reflect as any).metadata === undefined) {
require('reflect-metadata');
}
// Initialize source maps (this must never be moved up nor further down).
import { initialize } from './sourceMapSupport';
initialize(require('vscode'));
// Initialize the logger first.
require('./common/logger');
//===============================================
// We start tracking the extension's startup time at this point. The
// locations at which we record various Intervals are marked below in
// the same way as this.
const durations: Record<string, number> = {};
import { StopWatch } from './common/utils/stopWatch';
// Do not move this line of code (used to measure extension load times).
const stopWatch = new StopWatch();
//===============================================
// loading starts here
import { ProgressLocation, ProgressOptions, window } from 'vscode';
import { buildApi, IExtensionApi } from './api';
import { IApplicationShell } from './common/application/types';
import { traceError } from './common/logger';
import { IAsyncDisposableRegistry, IExtensionContext } from './common/types';
import { createDeferred } from './common/utils/async';
import { Common } from './common/utils/localize';
import { activateComponents } from './extensionActivation';
import { initializeComponents, initializeGlobals } from './extensionInit';
import { IServiceContainer } from './ioc/types';
import { sendErrorTelemetry, sendStartupTelemetry } from './startupTelemetry';
durations.codeLoadingTime = stopWatch.elapsedTime;
//===============================================
// loading ends here
// These persist between activations:
let activatedServiceContainer: IServiceContainer | undefined;
/////////////////////////////
// public functions
export async function activate(context: IExtensionContext): Promise<IExtensionApi> {
let api: IExtensionApi;
let ready: Promise<void>;
let serviceContainer: IServiceContainer;
try {
[api, ready, serviceContainer] = await activateUnsafe(context, stopWatch, durations);
} catch (ex) {
// We want to completely handle the error
// before notifying VS Code.
await handleError(ex, durations);
throw ex; // re-raise
}
// Send the "success" telemetry only if activation did not fail.
// Otherwise Telemetry is send via the error handler.
sendStartupTelemetry(ready, durations, stopWatch, serviceContainer)
// Run in the background.
.ignoreErrors();
return api;
}
export function deactivate(): Thenable<void> {
// Make sure to shutdown anybody who needs it.
if (activatedServiceContainer) {
const registry = activatedServiceContainer.get<IAsyncDisposableRegistry>(IAsyncDisposableRegistry);
if (registry) {
return registry.dispose();
}
}
return Promise.resolve();
}
/////////////////////////////
// activation helpers
// tslint:disable-next-line:max-func-body-length
async function activateUnsafe(
context: IExtensionContext,
startupStopWatch: StopWatch,
startupDurations: Record<string, number>
): Promise<[IExtensionApi, Promise<void>, IServiceContainer]> {
const activationDeferred = createDeferred<void>();
displayProgress(activationDeferred.promise);
startupDurations.startActivateTime = startupStopWatch.elapsedTime;
//===============================================
// activation starts here
const [serviceManager, serviceContainer] = initializeGlobals(context);
activatedServiceContainer = serviceContainer;
initializeComponents(context, serviceManager, serviceContainer);
const activationPromise = activateComponents(context, serviceManager, serviceContainer);
//===============================================
// activation ends here
startupDurations.endActivateTime = startupStopWatch.elapsedTime;
activationDeferred.resolve();
const api = buildApi(activationPromise, serviceManager, serviceContainer);
return [api, activationPromise, serviceContainer];
}
// tslint:disable-next-line:no-any
function displayProgress(promise: Promise<any>) {
const progressOptions: ProgressOptions = { location: ProgressLocation.Window, title: Common.loadingExtension() };
window.withProgress(progressOptions, () => promise);
}
/////////////////////////////
// error handling
async function handleError(ex: Error, startupDurations: Record<string, number>) {
notifyUser(
"Extension activation failed, run the 'Developer: Toggle Developer Tools' command for more information."
);
traceError('extension activation failed', ex);
await sendErrorTelemetry(ex, startupDurations, activatedServiceContainer);
}
interface IAppShell {
showErrorMessage(string: string): Promise<void>;
}
function notifyUser(msg: string) {
try {
// tslint:disable-next-line:no-any
let appShell: IAppShell = (window as any) as IAppShell;
if (activatedServiceContainer) {
// tslint:disable-next-line:no-any
appShell = (activatedServiceContainer.get<IApplicationShell>(IApplicationShell) as any) as IAppShell;
}
appShell.showErrorMessage(msg).ignoreErrors();
} catch (ex) {
traceError('failed to notify user', ex);
}
}