forked from aws/aws-toolkit-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.ts
214 lines (186 loc) · 9.15 KB
/
extension.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
/**
* This module contains shared code between the main extension and browser/web
* extension entrypoints.
*
* See `arch_develop.md` in `docs/` for more info.
*/
import vscode from 'vscode'
import * as nls from 'vscode-nls'
import globals, { initialize, isWeb } from './shared/extensionGlobals'
import { join } from 'path'
import { Commands } from './shared/vscode/commands2'
import { endpointsFileUrl, githubCreateIssueUrl, githubUrl } from './shared/constants'
import { getIdeProperties, aboutExtension, getDocUrl } from './shared/extensionUtilities'
import { logAndShowError, logAndShowWebviewError } from './shared/utilities/logAndShowUtils'
import { telemetry } from './shared/telemetry/telemetry'
import { openUrl } from './shared/utilities/vsCodeUtils'
import { activateViewsShared } from './awsexplorer/activationShared'
import fs from './shared/fs/fs'
import * as errors from './shared/errors'
import { activate as activateLogger } from './shared/logger/activation'
import { initializeComputeRegion } from './shared/extensionUtilities'
import { activate as activateTelemetry } from './shared/telemetry/activation'
import { DefaultAwsContext } from './shared/awsContext'
import { Settings } from './shared/settings'
import { DefaultAWSClientBuilder } from './shared/awsClientBuilder'
import { initialize as initializeAuth } from './auth/activation'
import { LoginManager } from './auth/deprecated/loginManager'
import { CredentialsStore } from './auth/credentials/store'
import { initializeAwsCredentialsStatusBarItem } from './auth/ui/statusBarItem'
import { RegionProvider, getEndpointsFromFetcher } from './shared/regions/regionProvider'
import { getMachineId, isAutomation } from './shared/vscode/env'
import { registerCommandErrorHandler } from './shared/vscode/commands2'
import { registerWebviewErrorHandler } from './webviews/server'
import { ExtContext, VSCODE_EXTENSION_ID } from './shared/extensions'
import { getSamCliContext } from './shared/sam/cli/samCliContext'
import { UriHandler } from './shared/vscode/uriHandler'
import { disableAwsSdkWarning } from './shared/awsClientBuilder'
import { FileResourceFetcher } from './shared/resourcefetcher/fileResourceFetcher'
import { ResourceFetcher } from './shared/resourcefetcher/resourcefetcher'
import { registerCommands } from './commands'
// In web mode everything must be in a single file, so things like the endpoints file will not be available.
// The following imports the endpoints file, which causes webpack to bundle it in the final output file
import endpoints from '../resources/endpoints.json'
import { showViewLogsMessage } from './shared/utilities/messages'
import { setupUninstallHandler } from './shared/handleUninstall'
import { maybeShowMinVscodeWarning } from './shared/extensionStartup'
import { getLogger } from './shared/logger/logger'
disableAwsSdkWarning()
let localize: nls.LocalizeFunc
/**
* Activation/setup code that is shared by the regular (nodejs) extension AND web mode extension.
* Most setup code should live here, unless there is a reason not to.
*/
export async function activateCommon(
context: vscode.ExtensionContext,
contextPrefix: string,
isWeb: boolean
): Promise<ExtContext> {
localize = nls.loadMessageBundle()
initialize(context, isWeb)
const homeDirLogs = await fs.init(context, (homeDir) => {
void showViewLogsMessage(`Invalid home directory (check $HOME): "${homeDir}"`)
})
errors.init(fs.getUsername(), isAutomation())
await initializeComputeRegion()
globals.contextPrefix = '' // todo: disconnect supplied argument
registerCommandErrorHandler((info, error) => {
const defaultMessage = localize('AWS.generic.message.error', 'Failed to run command: {0}', info.id)
void logAndShowError(localize, error, info.id, defaultMessage)
})
registerWebviewErrorHandler((error: unknown, webviewId: string, command: string) => {
return logAndShowWebviewError(localize, error, webviewId, command)
})
// Setup the logger
const toolkitOutputChannel = vscode.window.createOutputChannel('AWS Toolkit', { log: true })
const toolkitLogChannel = vscode.window.createOutputChannel('AWS Toolkit Logs', { log: true })
await activateLogger(context, contextPrefix, toolkitLogChannel, toolkitOutputChannel)
globals.outputChannel = toolkitOutputChannel
globals.logOutputChannel = toolkitLogChannel
if (homeDirLogs.length > 0) {
getLogger().error('fs.init: invalid home directory given by env vars: %O', homeDirLogs)
}
void maybeShowMinVscodeWarning('1.83.0')
// setup globals
globals.machineId = await getMachineId()
globals.awsContext = new DefaultAwsContext()
globals.sdkClientBuilder = new DefaultAWSClientBuilder(globals.awsContext)
globals.loginManager = new LoginManager(globals.awsContext, new CredentialsStore())
// order matters here
globals.manifestPaths.endpoints = context.asAbsolutePath(join('resources', 'endpoints.json'))
globals.manifestPaths.lambdaSampleRequests = context.asAbsolutePath(
join('resources', 'vs-lambda-sample-request-manifest.xml')
)
globals.regionProvider = RegionProvider.fromEndpointsProvider(makeEndpointsProvider())
// telemetry
await activateTelemetry(context, globals.awsContext, Settings.instance, 'AWS Toolkit For VS Code')
// Create this now, but don't call vscode.window.registerUriHandler() until after all
// Toolkit services have a chance to register their path handlers. #4105
globals.uriHandler = new UriHandler()
// Generic extension commands
registerGenericCommands(context, contextPrefix)
// Toolkit specific commands
registerCommands(context)
context.subscriptions.push(
// No-op command used for decoration-only codelenses.
vscode.commands.registerCommand('aws.doNothingCommand', () => {}),
// "Show AWS Commands..."
Commands.register('aws.listCommands', () =>
vscode.commands.executeCommand('workbench.action.quickOpen', `> ${getIdeProperties().company}:`)
),
// register URLs in extension menu
Commands.register(`aws.toolkit.help`, async () => {
void openUrl(getDocUrl())
telemetry.aws_help.emit()
})
)
// Handle AWS Toolkit un-installation.
setupUninstallHandler(VSCODE_EXTENSION_ID.awstoolkit, context.extension.id, context)
// auth
await initializeAuth(globals.loginManager)
await initializeAwsCredentialsStatusBarItem(globals.awsContext, context)
const extContext: ExtContext = {
extensionContext: context,
awsContext: globals.awsContext,
samCliContext: getSamCliContext,
regionProvider: globals.regionProvider,
outputChannel: globals.outputChannel,
telemetryService: globals.telemetry,
uriHandler: globals.uriHandler,
credentialsStore: globals.loginManager.store,
}
await activateViewsShared(extContext.extensionContext)
return extContext
}
/** Deactivation code that is shared between nodejs and web implementations */
export async function deactivateCommon() {
await globals.telemetry.shutdown()
}
/**
* Registers generic commands used by both web and node versions of the toolkit.
*/
export function registerGenericCommands(extensionContext: vscode.ExtensionContext, contextPrefix: string) {
extensionContext.subscriptions.push(
// register URLs in extension menu
Commands.register(`aws.${contextPrefix}.github`, async () => {
void openUrl(vscode.Uri.parse(githubUrl))
telemetry.aws_showExtensionSource.emit()
}),
Commands.register(`aws.${contextPrefix}.createIssueOnGitHub`, async () => {
void openUrl(vscode.Uri.parse(githubCreateIssueUrl))
telemetry.aws_reportPluginIssue.emit()
}),
Commands.register(`aws.${contextPrefix}.aboutExtension`, async () => {
await aboutExtension()
})
)
}
/**
* Returns an object that provides AWS service endpoints that the toolkit supports.
*
* https://docs.aws.amazon.com/general/latest/gr/rande.html
*/
export function makeEndpointsProvider() {
let localManifestFetcher: ResourceFetcher<string>
let remoteManifestFetcher: ResourceFetcher<Response>
if (isWeb()) {
localManifestFetcher = { get: async () => JSON.stringify(endpoints) }
// Cannot use HttpResourceFetcher due to web mode breaking on import
remoteManifestFetcher = { get: async () => await fetch(endpointsFileUrl) }
} else {
localManifestFetcher = new FileResourceFetcher(globals.manifestPaths.endpoints)
// HACK: HttpResourceFetcher breaks web mode when imported, so we use webpack.IgnorePlugin()
// to exclude it from the bundle.
// TODO: Make HttpResourceFetcher web mode compatible
const { HttpResourceFetcher } = require('./shared/resourcefetcher/httpResourceFetcher')
remoteManifestFetcher = new HttpResourceFetcher(endpointsFileUrl, { showUrl: true })
}
return {
local: () => getEndpointsFromFetcher(localManifestFetcher),
remote: () => getEndpointsFromFetcher(remoteManifestFetcher),
}
}