| |
| |
| |
| |
|
|
| import _ from 'lodash' |
| import * as nls from 'vscode-nls' |
| import * as path from 'path' |
| import * as vscode from 'vscode' |
| import { |
| AwsSamDebuggerConfiguration, |
| CodeTargetProperties, |
| ensureRelativePaths, |
| isAwsSamDebugConfiguration, |
| isCodeTargetProperties, |
| isTemplateTargetProperties, |
| AwsSamTargetType, |
| } from '../sam/debugger/awsSamDebugConfiguration' |
| import { |
| AwsSamDebugConfigurationValidator, |
| DefaultAwsSamDebugConfigurationValidator, |
| } from '../sam/debugger/awsSamDebugConfigurationValidator' |
| import * as pathutils from '../utilities/pathUtils' |
| import { tryGetAbsolutePath } from '../utilities/workspaceUtils' |
| import { getLogger } from '../logger/logger' |
| import { makeFailedWriteMessage, showViewLogsMessage } from '../utilities/messages' |
| import { openUrl } from '../utilities/vsCodeUtils' |
| import globals from '../extensionGlobals' |
| import { getLaunchConfigDocUrl } from '../extensionUtilities' |
|
|
| const localize = nls.loadMessageBundle() |
|
|
| |
| |
| |
| export interface DebugConfigurationSource { |
| getDebugConfigurations(): vscode.DebugConfiguration[] |
| setDebugConfigurations(value: vscode.DebugConfiguration[]): Promise<void> |
| } |
|
|
| |
| |
| |
| export class LaunchConfiguration { |
| public readonly workspaceFolder: vscode.WorkspaceFolder | undefined |
| |
| |
| |
| public constructor( |
| public readonly scopedResource: vscode.Uri, |
| private readonly configSource: DebugConfigurationSource = new DefaultDebugConfigSource(scopedResource), |
| private readonly samValidator: AwsSamDebugConfigurationValidator = new DefaultAwsSamDebugConfigurationValidator( |
| vscode.workspace.getWorkspaceFolder(scopedResource) |
| ) |
| ) { |
| this.workspaceFolder = vscode.workspace.getWorkspaceFolder(scopedResource) |
| } |
|
|
| public getDebugConfigurations(): vscode.DebugConfiguration[] { |
| const configs = this.configSource.getDebugConfigurations() |
| return configs.map((o) => { |
| if (isAwsSamDebugConfiguration(o)) { |
| ensureRelativePaths(undefined, o) |
| } |
| return o |
| }) |
| } |
|
|
| |
| |
| |
| public async getSamDebugConfigurations(): Promise<AwsSamDebuggerConfiguration[]> { |
| const configs = this.getDebugConfigurations().filter((o) => |
| isAwsSamDebugConfiguration(o) |
| ) as AwsSamDebuggerConfiguration[] |
| const registry = await globals.templateRegistry |
| |
| const validConfigs: AwsSamDebuggerConfiguration[] = [] |
| for (const c of configs) { |
| if ((await this.samValidator.validate(c, registry))?.isValid) { |
| validConfigs.push(c) |
| } |
| } |
| return validConfigs |
| } |
|
|
| |
| |
| |
| public async addDebugConfigurations(debugConfigs: vscode.DebugConfiguration[]): Promise<void> { |
| await this.configSource.setDebugConfigurations([...debugConfigs, ...this.getDebugConfigurations()]) |
| } |
|
|
| |
| |
| |
| public async addDebugConfiguration(debugConfig: vscode.DebugConfiguration): Promise<void> { |
| await this.configSource.setDebugConfigurations([debugConfig, ...this.getDebugConfigurations()]) |
| } |
|
|
| |
| |
| |
| |
| |
| public async editDebugConfiguration(editedDebugConfig: vscode.DebugConfiguration, index: number): Promise<void> { |
| const configs = this.getDebugConfigurations() |
| configs[index] = editedDebugConfig |
| await this.configSource.setDebugConfigurations(configs) |
| } |
| } |
|
|
| class DefaultDebugConfigSource implements DebugConfigurationSource { |
| private readonly launch: vscode.WorkspaceConfiguration |
|
|
| public constructor(resource: vscode.Uri) { |
| this.launch = vscode.workspace.getConfiguration('launch', resource) |
| } |
|
|
| public getDebugConfigurations(): vscode.DebugConfiguration[] { |
| return this.launch.get<vscode.DebugConfiguration[]>('configurations') ?? [] |
| } |
|
|
| public async setDebugConfigurations(value: vscode.DebugConfiguration[]): Promise<void> { |
| try { |
| await this.launch.update('configurations', value) |
| } catch (e) { |
| const helpText = localize('AWS.generic.message.getHelp', 'Get Help...') |
| getLogger().error('setDebugConfigurations failed: %O', e as Error) |
| await showViewLogsMessage(makeFailedWriteMessage('launch.json'), 'error', [helpText]).then( |
| async (buttonText) => { |
| if (buttonText === helpText) { |
| await openUrl(getLaunchConfigDocUrl()) |
| } |
| } |
| ) |
| } |
| } |
| } |
|
|
| async function getSamCodeTargets(launchConfig: LaunchConfiguration): Promise<CodeTargetProperties[]> { |
| const debugConfigs = await launchConfig.getSamDebugConfigurations() |
| return _(debugConfigs) |
| .map((samConfig) => samConfig.invokeTarget) |
| .filter(isCodeTargetProperties) |
| .value() |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function getConfigsMappedToTemplates( |
| launchConfig: LaunchConfiguration, |
| type: AwsSamTargetType | undefined |
| ): Promise<Set<AwsSamDebuggerConfiguration>> { |
| if (type === 'code') { |
| throw Error() |
| } |
| const folder = launchConfig.workspaceFolder |
| |
| const templateConfigs = (await launchConfig.getSamDebugConfigurations()).filter((o) => |
| isTemplateTargetProperties(o.invokeTarget) |
| ) |
| const filtered = templateConfigs.filter( |
| (t) => |
| (type === undefined || t.invokeTarget.target === type) && |
| pathutils.areEqual( |
| folder?.uri.fsPath, |
| (t.invokeTarget as any).templatePath, |
| launchConfig.scopedResource.fsPath |
| ) |
| ) |
| return _(filtered) |
| .thru((array) => new Set(array)) |
| .value() |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| export async function getReferencedHandlerPaths(launchConfig: LaunchConfiguration): Promise<Set<string>> { |
| const existingSamCodeTargets = await getSamCodeTargets(launchConfig) |
|
|
| return _(existingSamCodeTargets) |
| .map((target) => { |
| if (path.isAbsolute(target.projectRoot)) { |
| return pathutils.normalize(path.join(target.projectRoot, target.lambdaHandler)) |
| } |
| return pathutils.normalize( |
| path.join( |
| tryGetAbsolutePath(launchConfig.workspaceFolder, ''), |
| target.projectRoot, |
| target.lambdaHandler |
| ) |
| ) |
| }) |
| .thru((array) => new Set(array)) |
| .value() |
| } |
|
|