4
4
*/
5
5
6
6
import * as _ from 'lodash'
7
+ import * as path from 'path'
7
8
import * as vscode from 'vscode'
9
+ import { CloudFormationTemplateRegistry } from '../cloudformation/templateRegistry'
8
10
import {
9
11
AwsSamDebuggerConfiguration ,
10
- isAwsSamDebugConfiguration ,
12
+ CodeTargetProperties ,
11
13
ensureRelativePaths ,
14
+ isAwsSamDebugConfiguration ,
15
+ isCodeTargetProperties ,
16
+ isTemplateTargetProperties ,
17
+ TemplateTargetProperties ,
12
18
} from '../sam/debugger/awsSamDebugConfiguration'
13
19
import {
14
20
AwsSamDebugConfigurationValidator ,
15
21
DefaultAwsSamDebugConfigurationValidator ,
16
22
} from '../sam/debugger/awsSamDebugConfigurationValidator'
17
- import { CloudFormationTemplateRegistry } from '../cloudformation/templateRegistry'
23
+ import * as pathutils from '../utilities/pathUtils'
24
+ import { tryGetAbsolutePath } from '../utilities/workspaceUtils'
18
25
19
26
/**
20
27
* Reads and writes DebugConfigurations.
@@ -28,17 +35,20 @@ export interface DebugConfigurationSource {
28
35
* Wraps read and write operations on launch.json.
29
36
*/
30
37
export class LaunchConfiguration {
38
+ public readonly workspaceFolder : vscode . WorkspaceFolder | undefined
31
39
/**
32
40
* Creates a Launch Configuration scoped to the given resource.
33
41
*/
34
42
public constructor (
35
- resource : vscode . Uri ,
36
- private readonly configSource : DebugConfigurationSource = new DefaultDebugConfigSource ( resource ) ,
43
+ public readonly scopedResource : vscode . Uri ,
44
+ private readonly configSource : DebugConfigurationSource = new DefaultDebugConfigSource ( scopedResource ) ,
37
45
private readonly samValidator : AwsSamDebugConfigurationValidator = new DefaultAwsSamDebugConfigurationValidator (
38
46
CloudFormationTemplateRegistry . getRegistry ( ) ,
39
- vscode . workspace . getWorkspaceFolder ( resource )
47
+ vscode . workspace . getWorkspaceFolder ( scopedResource )
40
48
)
41
- ) { }
49
+ ) {
50
+ this . workspaceFolder = vscode . workspace . getWorkspaceFolder ( scopedResource )
51
+ }
42
52
43
53
public getDebugConfigurations ( ) : vscode . DebugConfiguration [ ] {
44
54
return _ ( this . configSource . getDebugConfigurations ( ) )
@@ -84,3 +94,60 @@ class DefaultDebugConfigSource implements DebugConfigurationSource {
84
94
await this . launch . update ( 'configurations' , value )
85
95
}
86
96
}
97
+
98
+ function getSamTemplateTargets ( launchConfig : LaunchConfiguration ) : TemplateTargetProperties [ ] {
99
+ return _ ( launchConfig . getSamDebugConfigurations ( ) )
100
+ . map ( samConfig => samConfig . invokeTarget )
101
+ . filter ( isTemplateTargetProperties )
102
+ . value ( )
103
+ }
104
+
105
+ function getSamCodeTargets ( launchConfig : LaunchConfiguration ) : CodeTargetProperties [ ] {
106
+ return _ ( launchConfig . getSamDebugConfigurations ( ) )
107
+ . map ( samConfig => samConfig . invokeTarget )
108
+ . filter ( isCodeTargetProperties )
109
+ . value ( )
110
+ }
111
+
112
+ /**
113
+ * Returns a Set containing the samTemplateResources from the launch.json file that the launch config is scoped to.
114
+ * @param launchConfig Launch config to check
115
+ */
116
+ export function getReferencedTemplateResources ( launchConfig : LaunchConfiguration ) : Set < string > {
117
+ const existingSamTemplateTargets = getSamTemplateTargets ( launchConfig )
118
+ const folder = launchConfig . workspaceFolder
119
+
120
+ return _ ( existingSamTemplateTargets )
121
+ . filter ( target =>
122
+ pathutils . areEqual ( folder ?. uri . fsPath , target . samTemplatePath , launchConfig . scopedResource . fsPath )
123
+ )
124
+ . map ( target => target . samTemplateResource )
125
+ . thru ( array => new Set ( array ) )
126
+ . value ( )
127
+ }
128
+
129
+ /**
130
+ * Returns a Set containing the full path for all `code`-type `aws-sam` debug configs in a launch.json file.
131
+ * The full path represents `path.join(workspaceFolder, projectRoot, lambdaHandler)`
132
+ * (without workspaceFolder if the projectRoot is relative).
133
+ * @param launchConfig Launch config to check
134
+ */
135
+ export function getReferencedHandlerPaths ( launchConfig : LaunchConfiguration ) : Set < string > {
136
+ const existingSamCodeTargets = getSamCodeTargets ( launchConfig )
137
+
138
+ return _ ( existingSamCodeTargets )
139
+ . map ( target => {
140
+ if ( path . isAbsolute ( target . projectRoot ) ) {
141
+ return pathutils . normalize ( path . join ( target . projectRoot , target . lambdaHandler ) )
142
+ }
143
+ return pathutils . normalize (
144
+ path . join (
145
+ tryGetAbsolutePath ( launchConfig . workspaceFolder , '' ) ,
146
+ target . projectRoot ,
147
+ target . lambdaHandler
148
+ )
149
+ )
150
+ } )
151
+ . thru ( array => new Set ( array ) )
152
+ . value ( )
153
+ }
0 commit comments