File size: 8,837 Bytes
6582e08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*!
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

import * as vscode from 'vscode'
import * as path from 'path'
import { Runtime } from '@aws-sdk/client-lambda'
import { getNormalizedRelativePath } from '../../utilities/pathUtils'
import {
    APIGatewayProperties,
    AwsSamDebuggerConfiguration,
    CodeTargetProperties,
    TemplateTargetProperties,
} from './awsSamDebugConfiguration.gen'
import { getLogger } from '../../logger/logger'

export * from './awsSamDebugConfiguration.gen'

export const AWS_SAM_DEBUG_TYPE = 'aws-sam' // eslint-disable-line @typescript-eslint/naming-convention
export const DIRECT_INVOKE_TYPE = 'direct-invoke' // eslint-disable-line @typescript-eslint/naming-convention
export const TEMPLATE_TARGET_TYPE = 'template' as const // eslint-disable-line @typescript-eslint/naming-convention
export const CODE_TARGET_TYPE = 'code' as const // eslint-disable-line @typescript-eslint/naming-convention
export const API_TARGET_TYPE = 'api' as const // eslint-disable-line @typescript-eslint/naming-convention
export const awsSamDebugRequestTypes = [DIRECT_INVOKE_TYPE]
export const awsSamDebugTargetTypes = [TEMPLATE_TARGET_TYPE, CODE_TARGET_TYPE, API_TARGET_TYPE]

export type AwsSamTargetType = 'api' | 'code' | 'template'

export type TargetProperties = AwsSamDebuggerConfiguration['invokeTarget']

export interface ReadonlyJsonObject {
    readonly [key: string]: string | number | boolean
}

export function isAwsSamDebugConfiguration(config: vscode.DebugConfiguration): config is AwsSamDebuggerConfiguration {
    return config.type === AWS_SAM_DEBUG_TYPE
}

export function isTemplateTargetProperties(props: TargetProperties): props is TemplateTargetProperties {
    return props.target === TEMPLATE_TARGET_TYPE || props.target === API_TARGET_TYPE
}

export function isCodeTargetProperties(props: TargetProperties): props is CodeTargetProperties {
    return props.target === CODE_TARGET_TYPE
}

/**
 * Ensures that the `projectRoot` or `templatePath` relative properties on
 * the given `config` are relative (not absolute) paths.
 *
 * @param folder  Workspace folder, or empty to use the workspace associated with `projectRoot` or `templatePath`.
 * @param config
 */
export function ensureRelativePaths(
    folder: vscode.WorkspaceFolder | undefined,
    config: AwsSamDebuggerConfiguration
): void {
    if (!config?.invokeTarget?.target) {
        // User has an invalid type=aws-sam launch-config.
        return
    }
    const filepath =
        config.invokeTarget.target === CODE_TARGET_TYPE
            ? config.invokeTarget.projectRoot
            : config.invokeTarget.templatePath
    if (!path.isAbsolute(filepath)) {
        return
    }
    const uri = vscode.Uri.file(filepath)
    folder = folder ? folder : vscode.workspace.getWorkspaceFolder(uri)
    if (!folder) {
        getLogger().warn(`ensureRelativePaths: no workspace for path: "${filepath}"`)
        return
    }
    const relPath = getNormalizedRelativePath(folder!.uri.fsPath, filepath)
    if (config.invokeTarget.target === CODE_TARGET_TYPE) {
        config.invokeTarget.projectRoot = relPath
    } else {
        config.invokeTarget.templatePath = relPath
    }
}

/**
 * Creates a description for a SAM debugconfig entry (in launch.json).
 *
 * Example: `makeName('foo', '/bar/baz', 'zub')` => `"baz:foo (zub)"
 *
 * @param primaryName
 * @param parentDir  Optional directory name (used as a prefix)
 * @param suffix  Optional info used to differentiate the name
 */
function makeName(primaryName: string, parentDir: string | undefined, suffix: string | undefined) {
    const withPrefix = parentDir ? `${parentDir}:${primaryName}` : primaryName
    return suffix ? `${withPrefix} (${suffix})` : withPrefix
}

/**
 * Creates a description for a SAM debugconfig entry (in launch.json), preprending API
 *
 * see: makeName for the format
 */
function makeNameApi(primaryName: string, parentDir: string | undefined, suffix: string | undefined) {
    return `API ${makeName(primaryName, parentDir, suffix)}`
}

/**
 *
 * @param folder
 * @param runtimeName  Optional runtime name used to enhance the config name
 * @addRuntimeToConfig
 * @param resourceName
 * @param templatePath
 * @param preloadedConfig
 */
export function createTemplateAwsSamDebugConfig(
    folder: vscode.WorkspaceFolder | undefined,
    runtimeName: string | undefined,
    addRuntimeToConfig: boolean,
    resourceName: string,
    templatePath: string,
    preloadedConfig?: {
        eventJson?: ReadonlyJsonObject
        environmentVariables?: { [key: string]: string }
        dockerNetwork?: string
        useContainer?: boolean
    }
): AwsSamDebuggerConfiguration {
    const workspaceRelativePath = makeWorkspaceRelativePath(folder, templatePath)
    const templateParentDir = path.basename(path.dirname(templatePath))

    const response: AwsSamDebuggerConfiguration = {
        type: AWS_SAM_DEBUG_TYPE,
        request: DIRECT_INVOKE_TYPE,
        name: makeName(resourceName, templateParentDir, runtimeName),
        invokeTarget: {
            target: TEMPLATE_TARGET_TYPE,
            templatePath: workspaceRelativePath,
            logicalId: resourceName,
        },
        lambda: {
            payload: {},
            environmentVariables: {},
        },
    }

    if (addRuntimeToConfig) {
        response.lambda!.runtime = runtimeName
    }

    if (preloadedConfig) {
        return {
            ...response,
            lambda:
                preloadedConfig.environmentVariables || preloadedConfig.eventJson
                    ? {
                          payload: preloadedConfig.eventJson ? { json: preloadedConfig.eventJson } : {},
                          environmentVariables: preloadedConfig.environmentVariables,
                      }
                    : {
                          payload: {},
                          environmentVariables: {},
                      },
            sam:
                preloadedConfig.dockerNetwork || preloadedConfig.useContainer
                    ? {
                          dockerNetwork: preloadedConfig.dockerNetwork,
                          containerBuild: preloadedConfig.useContainer,
                      }
                    : undefined,
        }
    }

    return response
}

export function createCodeAwsSamDebugConfig(
    folder: vscode.WorkspaceFolder | undefined,
    lambdaHandler: string,
    projectRoot: string,
    runtime: Runtime
): AwsSamDebuggerConfiguration {
    const workspaceRelativePath = makeWorkspaceRelativePath(folder, projectRoot)
    const parentDir = path.basename(projectRoot)

    return {
        type: AWS_SAM_DEBUG_TYPE,
        request: DIRECT_INVOKE_TYPE,
        name: makeName(lambdaHandler, parentDir, runtime),
        invokeTarget: {
            target: CODE_TARGET_TYPE,
            projectRoot: workspaceRelativePath,
            lambdaHandler: lambdaHandler,
        },
        lambda: {
            runtime,
            payload: {},
            environmentVariables: {},
        },
    }
}

export function createApiAwsSamDebugConfig(
    folder: vscode.WorkspaceFolder | undefined,
    runtimeName: string | undefined,
    resourceName: string,
    templatePath: string,
    preloadedConfig?: {
        path?: string
        httpMethod?: string
        payload?: APIGatewayProperties['payload']
    }
): AwsSamDebuggerConfiguration {
    const workspaceRelativePath = makeWorkspaceRelativePath(folder, templatePath)
    const templateParentDir = path.basename(path.dirname(templatePath))

    const withRuntime = runtimeName
        ? {
              lambda: {
                  runtime: runtimeName,
              },
          }
        : undefined

    return {
        type: AWS_SAM_DEBUG_TYPE,
        request: DIRECT_INVOKE_TYPE,
        name: makeNameApi(resourceName, templateParentDir, runtimeName),
        invokeTarget: {
            target: API_TARGET_TYPE,
            templatePath: workspaceRelativePath,
            logicalId: resourceName,
        },
        api: {
            path: preloadedConfig?.path ?? '/',
            // coerce it into the correct type. The types do not entirely overlap (there is an any)
            // so in that case let the user decide
            httpMethod: (preloadedConfig?.httpMethod as APIGatewayProperties['httpMethod']) ?? 'get',
            payload: preloadedConfig?.payload ?? { json: {} },
        },
        ...withRuntime,
    }
}

function makeWorkspaceRelativePath(folder: vscode.WorkspaceFolder | undefined, target: string): string {
    if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length <= 1) {
        return folder ? `\${workspaceFolder}/${getNormalizedRelativePath(folder.uri.fsPath, target)}` : target
    }

    return target
}