-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.go
321 lines (276 loc) · 9.39 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2016-2020, Pulumi Corporation.
package main
import (
"bytes"
"fmt"
"os"
"path"
"path/filepath"
"text/template"
"github.com/segmentio/encoding/json"
"github.com/pkg/errors"
"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/debug"
"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/gen"
"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/resources"
"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/squeeze"
embeddedVersion "github.com/pulumi/pulumi-azure-native/v2/provider/pkg/version"
"github.com/pulumi/pulumi-azure-native/v2/provider/pkg/versioning"
gogen "github.com/pulumi/pulumi/pkg/v3/codegen/go"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
)
func main() {
var debugEnabled bool
debugEnv := os.Getenv("DEBUG_CODEGEN")
if debugEnabled = debugEnv == "true"; debugEnabled {
debug.Debug = &debugEnabled
}
languages := os.Args[1]
// The third argument is the optional version.
// Exactly one of the CLI arg or embedded version must be set.
if len(os.Args) == 3 {
if embeddedVersion.Version != "" {
panic(fmt.Sprintf("embedded version already set to %s", embeddedVersion.Version))
}
embeddedVersion.Version = os.Args[2]
} else if embeddedVersion.Version == "" {
panic("no version provided via CLI argument or ldflags")
}
// Use the one true version everywhere.
version := embeddedVersion.GetVersion()
wd, err := os.Getwd()
if err != nil {
panic(err)
}
schemaPath := filepath.Join(wd, "bin", "schema-full.json")
if len(os.Args) == 4 {
schemaPath = os.Args[3]
if !filepath.IsAbs(schemaPath) {
schemaPath = filepath.Join(wd, schemaPath)
}
}
// Use DEBUG_CODEGEN_NAMESPACES to just generate a single namespace (e.g. "Compute") for quick testing
namespaces := os.Getenv("DEBUG_CODEGEN_NAMESPACES")
if namespaces == "" {
namespaces = "*"
}
// Use DEBUG_CODEGEN_APIVERSIONS to just generate certain versions (e.g. "2019-09-01", "2019*") for quick testing,
// likely in combination with DEBUG_CODEGEN_NAMESPACES
apiVersions := os.Getenv("DEBUG_CODEGEN_APIVERSIONS")
codegenSchemaOutputPath := os.Getenv("CODEGEN_SCHEMA_OUTPUT_PATH")
codegenMetadataOutputPath := os.Getenv("CODEGEN_METADATA_OUTPUT_PATH")
buildSchemaArgs := versioning.BuildSchemaArgs{
Specs: versioning.ReadSpecsArgs{
SpecsDir: os.Getenv("CODEGEN_SPECS_DIR"),
NamespaceFilter: namespaces,
VersionsFilter: apiVersions,
},
RootDir: wd,
Version: version.String(),
}
switch languages {
case "schema":
buildSchemaResult, buildSchemaErr := versioning.BuildSchema(buildSchemaArgs)
// Attempt to write out the version metadata before failing as this might help us diagnose the issue.
if namespaces == "*" && apiVersions == "" && buildSchemaResult != nil {
written, err := buildSchemaResult.Version.WriteTo("versions")
if err != nil {
panic(err)
}
for _, v := range written {
fmt.Printf("Emitted %s\n", v)
}
written, err = buildSchemaResult.Reports.WriteTo("reports")
if err != nil {
panic(err)
}
for _, v := range written {
fmt.Printf("Emitted %s\n", v)
}
} else {
fmt.Println("Note: skipping writing version metadata and reports because DEBUG_CODEGEN_NAMESPACES or DEBUG_CODEGEN_APIVERSIONS is set.")
}
if buildSchemaErr != nil {
panic(buildSchemaErr)
}
if codegenSchemaOutputPath == "" {
codegenSchemaOutputPath = path.Join("bin", "schema-full.json")
}
if err = emitSchema(buildSchemaResult.PackageSpec, version.String(), codegenSchemaOutputPath); err != nil {
panic(err)
}
fmt.Printf("Emitted %s.\n", codegenSchemaOutputPath)
// We can't generate schema.json every time because it's slow and isn't reproducible.
// So we warn in case someone's expecting to see changes to schema.json after running this.
fmt.Println("Note: provider/cmd/pulumi-resource-azure-native/schema.json is generated by the `generate_docs` target.")
// Also, emit the resource metadata for the provider.
if codegenMetadataOutputPath == "" {
codegenMetadataOutputPath = path.Join("bin", "metadata-compact.json")
}
if err = emitMetadata(&buildSchemaResult.Metadata, codegenMetadataOutputPath); err != nil {
panic(err)
}
fmt.Printf("Emitted %s.\n", codegenMetadataOutputPath)
case "docs":
buildSchemaArgs.ExcludeExplicitVersions = true
buildSchemaArgs.ExampleLanguages = []string{"nodejs", "dotnet", "python", "go", "java", "yaml"}
buildSchemaResult, err := versioning.BuildSchema(buildSchemaArgs)
if err != nil {
panic(err)
}
if codegenSchemaOutputPath == "" {
codegenSchemaOutputPath = path.Join(".", "provider", "cmd", "pulumi-resource-azure-native", "schema.json")
}
err = emitDocsSchema(&buildSchemaResult.PackageSpec, codegenSchemaOutputPath)
if err != nil {
panic(err)
}
case "squeeze":
buildSchemaArgs.OnlyExplicitVersions = true
buildSchemaResult, err := versioning.BuildSchema(buildSchemaArgs)
if err != nil {
panic(err)
}
squeezedResources, err := squeeze.CompareAll(&buildSchemaResult.PackageSpec)
if err != nil {
panic(err)
}
squeezedInvokes := versioning.FindRemovedInvokesFromResources(buildSchemaResult.Modules, squeezedResources)
majorVersion := version.Major
err = gen.EmitFile(path.Join("versions", fmt.Sprintf("v%d-removed-resources.json", majorVersion)), squeezedResources)
if err != nil {
panic(err)
}
err = gen.EmitFile(path.Join("versions", fmt.Sprintf("v%d-removed-invokes.yaml", majorVersion)), squeezedInvokes)
if err != nil {
panic(err)
}
case "go":
// Just read existing schema if we're not re-generating
schemaBytes, err := os.ReadFile(schemaPath)
if err != nil {
panic(err)
}
var pkgSpec schema.PackageSpec
err = json.Unmarshal([]byte(schemaBytes), &pkgSpec)
if err != nil {
panic(err)
}
outdir := path.Join(".", "sdk", "pulumi-azure-native-sdk")
pkgSpec.Version = version.String()
err = emitSplitPackage(&pkgSpec, "go", outdir)
if err != nil {
panic(err)
}
default:
panic(fmt.Sprintf("unknown language %s", languages))
}
}
// emitSchema writes the Pulumi schema JSON to the 'schema.json' file in the given directory.
func emitSchema(pkgSpec schema.PackageSpec, version, outputPath string) error {
pkgSpec.Version = version
schemaJSON, err := json.Marshal(pkgSpec)
if err != nil {
return errors.Wrap(err, "marshaling Pulumi schema")
}
return gen.EmitFile(outputPath, schemaJSON)
}
// emitDocsSchema writes the Pulumi schema JSON to the 'schema.json' file in the given directory.
func emitDocsSchema(pkgSpec *schema.PackageSpec, outputPath string) error {
schemaJSON, err := json.MarshalIndent(pkgSpec, "", " ")
if err != nil {
return errors.Wrap(err, "marshaling Pulumi schema")
}
return gen.EmitFile(outputPath, schemaJSON)
}
func emitMetadata(metadata *resources.AzureAPIMetadata, outputPath string) error {
meta := bytes.Buffer{}
err := json.NewEncoder(&meta).Encode(metadata)
if err != nil {
return errors.Wrap(err, "marshaling metadata")
}
return gen.EmitFile(outputPath, meta.Bytes())
}
func emitSplitPackage(pkgSpec *schema.PackageSpec, language, outDir string) error {
moduleVersionPath := gen.GoModulePathVersion(pkgSpec.Version)
ppkg, err := schema.ImportSpec(*pkgSpec, nil)
if err != nil {
return errors.Wrap(err, "reading schema")
}
version := gen.GoModVersion(ppkg.Version)
files, err := gogen.GeneratePackage("the Pulumi SDK Generator", ppkg, nil)
if err != nil {
return errors.Wrapf(err, "generating %s package", language)
}
files["version.txt"] = []byte(version)
files["go.mod"] = []byte(goModTemplate(GoMod{
ModuleVersionPath: moduleVersionPath,
}))
for f, contents := range files {
if err := gen.EmitFile(path.Join(outDir, f), contents); err != nil {
return err
}
// Special case for identifying where we need modules in subdirectories.
matched, err := filepath.Match("*/init.go", f)
if err != nil {
return err
}
if matched {
dir := filepath.Dir(f)
module := filepath.Base(dir)
modPath := filepath.Join(dir, "go.mod")
modContent := goModTemplate(GoMod{
Version: version,
SubmoduleName: module,
ModuleVersionPath: moduleVersionPath,
})
if err := gen.EmitFile(path.Join(outDir, modPath), []byte(modContent)); err != nil {
return err
}
pluginPath := filepath.Join(dir, "pulumi-plugin.json")
pluginContent := files["pulumi-plugin.json"]
if err := gen.EmitFile(path.Join(outDir, pluginPath), pluginContent); err != nil {
return err
}
}
}
return nil
}
type GoMod struct {
Version string
SubmoduleName string
ModuleVersionPath string
}
var goModTemplateCache *template.Template
func goModTemplate(goMod GoMod) string {
var err error
if goModTemplateCache == nil {
goModTemplateCache, err = template.New("go-mod").Parse(`
{{ if eq .SubmoduleName "" }}
module github.com/pulumi/pulumi-azure-native-sdk{{ .ModuleVersionPath }}
{{ else }}
module github.com/pulumi/pulumi-azure-native-sdk/{{ .SubmoduleName }}{{ .ModuleVersionPath }}
{{ end }}
go 1.18
require (
github.com/blang/semver v3.5.1+incompatible
github.com/pkg/errors v0.9.1
{{ if ne .SubmoduleName "" }}
github.com/pulumi/pulumi-azure-native-sdk{{ .ModuleVersionPath }} {{ .Version }}
{{ end }}
github.com/pulumi/pulumi/sdk/v3 v3.37.2
)
{{ if ne .SubmoduleName "" }}
replace github.com/pulumi/pulumi-azure-native-sdk{{ .ModuleVersionPath }} => ../
{{ end }}
`)
if err != nil {
panic(err)
}
}
var result bytes.Buffer
err = goModTemplateCache.Execute(&result, goMod)
if err != nil {
panic(err)
}
return result.String()
}