@@ -248,6 +248,8 @@ func generateExamplePrograms(example resources.AzureAPIExample, body *model.Body
248248 return nil , err
249249 }
250250
251+ writeDebugHCL (programBody , example .Location )
252+
251253 debug .Log ("Generating example programs for %s\n %s\n " , example .Location , programBody )
252254 parser := syntax .NewParser ()
253255 if err := parser .ParseFile (strings .NewReader (programBody ), "program.pp" ); err != nil {
@@ -313,6 +315,35 @@ func generateExamplePrograms(example resources.AzureAPIExample, body *model.Body
313315 return languageExample , nil
314316}
315317
318+ // writeDebugHCL writes the given example program to DEBUG_CODEGEN_EXAMPLE_HCL/ as a .pp file if
319+ // the exampleLocation path matches the value of DEBUG_CODEGEN_EXAMPLE_HCL. Paths are from the
320+ // root of the provider repository. Wildcards are allowed.
321+ // Example: DEBUG_CODEGEN_EXAMPLE_HCL=azure-rest-api-specs/specification/containerservice/resource-manager/Microsoft.ContainerService/aks/stable/2023-08-01/examples/ManagedClustersCreate_*
322+ func writeDebugHCL (programBody string , exampleLocation string ) {
323+ debugExamplesPath := os .Getenv ("DEBUG_CODEGEN_EXAMPLE_HCL" )
324+ match , err := path .Match (debugExamplesPath , exampleLocation )
325+ if err != nil {
326+ log .Printf ("\n Malformed DEBUG_CODEGEN_EXAMPLE_HCL %s: %v" , debugExamplesPath , err )
327+ } else if match {
328+ log .Printf ("\n Generated HCL for %s:\n %s\n " , exampleLocation , programBody )
329+
330+ // Switch path from azure-rest-api-specs/specification/provider/... to reports/provider/...
331+ parts := strings .Split (exampleLocation , "/" )
332+ newParts := make ([]string , len (parts ))
333+ newParts = append (newParts , "DEBUG_CODEGEN_EXAMPLE_HCL" )
334+ newParts = append (newParts , parts [2 :]... )
335+ newParts [len (newParts )- 1 ] = newParts [len (newParts )- 1 ] + ".pp"
336+ dest := filepath .Join (newParts ... )
337+ log .Printf ("Writing example %s to %s" , exampleLocation , dest )
338+
339+ os .MkdirAll (filepath .Dir (dest ), 0755 )
340+ err := os .WriteFile (dest , []byte (programBody ), 0644 )
341+ if err != nil {
342+ log .Printf ("\n Failed to write example to %s: %v" , dest , err )
343+ }
344+ }
345+ }
346+
316347func recoverableProgramGen (name string , program * hcl2.Program , fn programGenFn ) (files map [string ][]byte , err error ) {
317348 const timeout = 3 * time .Minute
318349
0 commit comments