/
report_resource_structure.go
78 lines (63 loc) · 2.47 KB
/
report_resource_structure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package pipeline
import (
"context"
"path/filepath"
"github.com/pkg/errors"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/astmodel"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/config"
"github.com/Azure/azure-service-operator/v2/tools/generator/internal/reporting"
)
// ReportResourceStructureStageId is the unique identifier for this stage
const ReportResourceStructureStageId = "reportResourceStructure"
// ReportResourceStructure creates a pipeline stage that reports the structure of resources in each package
func ReportResourceStructure(configuration *config.Configuration) *Stage {
return NewStage(
ReportResourceStructureStageId,
"Reports the structure of resources in each package",
func(ctx context.Context, state *State) (*State, error) {
report := NewResourceStructureReport(state.Definitions())
err := report.SaveReports(configuration.FullTypesOutputPath())
return state, err
})
}
type ResourceStructureReport struct {
lists map[astmodel.InternalPackageReference]astmodel.TypeDefinitionSet // A separate list of resources for each package
}
func NewResourceStructureReport(defs astmodel.TypeDefinitionSet) *ResourceStructureReport {
result := &ResourceStructureReport{
lists: make(map[astmodel.InternalPackageReference]astmodel.TypeDefinitionSet),
}
result.summarize(defs)
return result
}
// SaveReports writes the reports to the specified files
func (report *ResourceStructureReport) SaveReports(baseFolder string) error {
for pkg, defs := range report.lists {
folder := filepath.Join(baseFolder, pkg.FolderPath(), "structure.txt")
err := report.saveReport(folder, defs)
if err != nil {
return err
}
}
return nil
}
// summarize collates a list of all definitions, grouped by package
func (report *ResourceStructureReport) summarize(definitions astmodel.TypeDefinitionSet) {
for name, def := range definitions {
pkg := name.InternalPackageReference()
if _, ok := report.lists[pkg]; !ok {
report.lists[pkg] = make(astmodel.TypeDefinitionSet)
}
report.lists[pkg].Add(def)
}
}
func (report *ResourceStructureReport) saveReport(filePath string, defs astmodel.TypeDefinitionSet) error {
rpt := reporting.NewTypeCatalogReport(defs, reporting.InlineTypes)
rpt.AddHeader(astmodel.CodeGenerationComments...)
err := rpt.SaveTo(filePath)
return errors.Wrapf(err, "unable to save type catalog report to %q", filePath)
}