@@ -46,8 +46,9 @@ public void Execute(GeneratorExecutionContext context)
46
46
return ;
47
47
}
48
48
49
- // If there are no Lambda methods, return early
50
- if ( ! receiver . LambdaMethods . Any ( ) )
49
+ // If no project directory was detected then skip the generator.
50
+ // This is most likely to happen when the project is empty and doesn't have any classes in it yet.
51
+ if ( string . IsNullOrEmpty ( receiver . ProjectDirectory ) )
51
52
{
52
53
return ;
53
54
}
@@ -69,7 +70,6 @@ public void Execute(GeneratorExecutionContext context)
69
70
var annotationReport = new AnnotationReport ( ) ;
70
71
71
72
var templateFinder = new CloudFormationTemplateFinder ( _fileManager , _directoryManager ) ;
72
- var projectRootDirectory = string . Empty ;
73
73
74
74
foreach ( var lambdaMethod in receiver . LambdaMethods )
75
75
{
@@ -112,15 +112,19 @@ public void Execute(GeneratorExecutionContext context)
112
112
diagnosticReporter . Report ( Diagnostic . Create ( DiagnosticDescriptors . CodeGeneration , Location . None , $ "{ model . GeneratedMethod . ContainingType . Name } .g.cs", sourceText ) ) ;
113
113
114
114
annotationReport . LambdaFunctions . Add ( model ) ;
115
+ }
115
116
116
- if ( string . IsNullOrEmpty ( projectRootDirectory ) )
117
- projectRootDirectory = templateFinder . DetermineProjectRootDirectory ( lambdaMethod . SyntaxTree . FilePath ) ;
117
+ // Run the CloudFormation sync if any LambdaMethods exists. Also run if no LambdaMethods exists but there is a
118
+ // CloudFormation template in case orphaned functions in the template need to be removed.
119
+ // Both checks are required because if there is no template but there are LambdaMethods the CF template the template will be created.
120
+ if ( receiver . LambdaMethods . Any ( ) || templateFinder . DoesCloudFormationTemplateExist ( receiver . ProjectDirectory ) )
121
+ {
122
+ annotationReport . CloudFormationTemplatePath = templateFinder . FindCloudFormationTemplate ( receiver . ProjectDirectory ) ;
123
+ annotationReport . ProjectRootDirectory = receiver . ProjectDirectory ;
124
+ var cloudFormationJsonWriter = new CloudFormationJsonWriter ( _fileManager , _directoryManager , _jsonWriter , diagnosticReporter ) ;
125
+ cloudFormationJsonWriter . ApplyReport ( annotationReport ) ;
118
126
}
119
127
120
- annotationReport . CloudFormationTemplatePath = templateFinder . FindCloudFormationTemplate ( projectRootDirectory ) ;
121
- annotationReport . ProjectRootDirectory = projectRootDirectory ;
122
- var cloudFormationJsonWriter = new CloudFormationJsonWriter ( _fileManager , _directoryManager , _jsonWriter , diagnosticReporter ) ;
123
- cloudFormationJsonWriter . ApplyReport ( annotationReport ) ;
124
128
}
125
129
catch ( Exception e )
126
130
{
@@ -135,7 +139,7 @@ public void Execute(GeneratorExecutionContext context)
135
139
public void Initialize ( GeneratorInitializationContext context )
136
140
{
137
141
// Register a syntax receiver that will be created for each generation pass
138
- context . RegisterForSyntaxNotifications ( ( ) => new SyntaxReceiver ( ) ) ;
142
+ context . RegisterForSyntaxNotifications ( ( ) => new SyntaxReceiver ( _fileManager , _directoryManager ) ) ;
139
143
}
140
144
}
141
145
}
0 commit comments