@@ -46,8 +46,9 @@ public void Execute(GeneratorExecutionContext context)
4646 return ;
4747 }
4848
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 ) )
5152 {
5253 return ;
5354 }
@@ -69,7 +70,6 @@ public void Execute(GeneratorExecutionContext context)
6970 var annotationReport = new AnnotationReport ( ) ;
7071
7172 var templateFinder = new CloudFormationTemplateFinder ( _fileManager , _directoryManager ) ;
72- var projectRootDirectory = string . Empty ;
7373
7474 foreach ( var lambdaMethod in receiver . LambdaMethods )
7575 {
@@ -112,15 +112,19 @@ public void Execute(GeneratorExecutionContext context)
112112 diagnosticReporter . Report ( Diagnostic . Create ( DiagnosticDescriptors . CodeGeneration , Location . None , $ "{ model . GeneratedMethod . ContainingType . Name } .g.cs", sourceText ) ) ;
113113
114114 annotationReport . LambdaFunctions . Add ( model ) ;
115+ }
115116
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 ) ;
118126 }
119127
120- annotationReport . CloudFormationTemplatePath = templateFinder . FindCloudFormationTemplate ( projectRootDirectory ) ;
121- annotationReport . ProjectRootDirectory = projectRootDirectory ;
122- var cloudFormationJsonWriter = new CloudFormationJsonWriter ( _fileManager , _directoryManager , _jsonWriter , diagnosticReporter ) ;
123- cloudFormationJsonWriter . ApplyReport ( annotationReport ) ;
124128 }
125129 catch ( Exception e )
126130 {
@@ -135,7 +139,7 @@ public void Execute(GeneratorExecutionContext context)
135139 public void Initialize ( GeneratorInitializationContext context )
136140 {
137141 // Register a syntax receiver that will be created for each generation pass
138- context . RegisterForSyntaxNotifications ( ( ) => new SyntaxReceiver ( ) ) ;
142+ context . RegisterForSyntaxNotifications ( ( ) => new SyntaxReceiver ( _fileManager , _directoryManager ) ) ;
139143 }
140144 }
141145}
0 commit comments