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
155 lines (127 loc) · 5.56 KB
/
extension.ts
File metadata and controls
155 lines (127 loc) · 5.56 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
'use strict';
// This line should always be right on top.
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 = {} as IStartupDurations;
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 { initializeStandard, initializeComponents, initializeGlobals } from './extensionInit';
import { IServiceContainer } from './ioc/types';
import { sendErrorTelemetry, sendStartupTelemetry } from './startupTelemetry';
import { IStartupDurations } from './types';
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
async function activateUnsafe(
context: IExtensionContext,
startupStopWatch: StopWatch,
startupDurations: IStartupDurations,
): Promise<[IExtensionApi, Promise<void>, IServiceContainer]> {
const activationDeferred = createDeferred<void>();
displayProgress(activationDeferred.promise);
startupDurations.startActivateTime = startupStopWatch.elapsedTime;
//===============================================
// activation starts here
// First we initialize.
const ext = initializeGlobals(context);
activatedServiceContainer = ext.legacyIOC.serviceContainer;
// Note standard utils especially experiment and platform code are fundamental to the extension
// and should be available before we activate anything else.Hence register them first.
initializeStandard(ext);
const components = await initializeComponents(ext);
// Then we finish activating.
const componentsActivated = await activateComponents(ext, components);
const nonBlocking = componentsActivated.map((r) => r.fullyReady);
const activationPromise = (async () => {
await Promise.all(nonBlocking);
})();
//===============================================
// activation ends here
startupDurations.totalActivateTime = startupStopWatch.elapsedTime - startupDurations.startActivateTime;
activationDeferred.resolve();
const api = buildApi(activationPromise, ext.legacyIOC.serviceManager, ext.legacyIOC.serviceContainer);
return [api, activationPromise, ext.legacyIOC.serviceContainer];
}
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: IStartupDurations) {
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 {
let appShell: IAppShell = (window as any) as IAppShell;
if (activatedServiceContainer) {
appShell = (activatedServiceContainer.get<IApplicationShell>(IApplicationShell) as any) as IAppShell;
}
appShell.showErrorMessage(msg).ignoreErrors();
} catch (ex) {
traceError('failed to notify user', ex);
}
}