Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: split large compute file up #2546

Merged
merged 7 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
127,584 changes: 0 additions & 127,584 deletions compute/v0.alpha/compute-gen.go

Large diffs are not rendered by default.

68,204 changes: 68,204 additions & 0 deletions compute/v0.alpha/compute2-gen.go

Large diffs are not rendered by default.

59,414 changes: 59,414 additions & 0 deletions compute/v0.alpha/compute3-gen.go

Large diffs are not rendered by default.

119,556 changes: 0 additions & 119,556 deletions compute/v0.beta/compute-gen.go

Large diffs are not rendered by default.

63,725 changes: 63,725 additions & 0 deletions compute/v0.beta/compute2-gen.go

Large diffs are not rendered by default.

55,865 changes: 55,865 additions & 0 deletions compute/v0.beta/compute3-gen.go

Large diffs are not rendered by default.

108,251 changes: 0 additions & 108,251 deletions compute/v1/compute-gen.go

Large diffs are not rendered by default.

58,393 changes: 58,393 additions & 0 deletions compute/v1/compute2-gen.go

Large diffs are not rendered by default.

49,892 changes: 49,892 additions & 0 deletions compute/v1/compute3-gen.go

Large diffs are not rendered by default.

118 changes: 110 additions & 8 deletions google-api-go-generator/gen.go
Expand Up @@ -32,6 +32,8 @@ const (
googleDiscoveryURL = "https://www.googleapis.com/discovery/v1/apis"
googleDefaultUniverse = "googleapis.com"
universeDomainPlaceholder = "UNIVERSE_DOMAIN"

splitFileSeperator = `// =*=*"`
)

var (
Expand Down Expand Up @@ -75,6 +77,10 @@ var skipAPIGeneration = map[string]bool{
"datalineage:v1": true,
}

var apisToSplit = map[string]bool{
"compute": true,
}

// API represents an API to generate, as well as its state while it's
// generating.
type API struct {
Expand Down Expand Up @@ -226,7 +232,7 @@ func getAPIs() []*API {
} else {
bytes = slurpURL(*apisURL)
if *publicOnly {
if err := writeFile(apiListFile, bytes); err != nil {
if err := writeFile(apiListFile, "", bytes); err != nil {
log.Fatal(err)
}
}
Expand Down Expand Up @@ -300,7 +306,7 @@ func apiFromFile(file string) (*API, error) {
func checkAndUpdateSpecFile(file string, contents []byte) error {
// check if file exists
if _, err := os.Stat(file); os.IsNotExist(err) {
return writeFile(file, contents)
return writeFile(file, "", contents)
}
existing, err := os.ReadFile(file)
if err != nil {
Expand All @@ -309,7 +315,7 @@ func checkAndUpdateSpecFile(file string, contents []byte) error {
if err := isNewerRevision(existing, contents); err != nil {
return err
}
return writeFile(file, contents)
return writeFile(file, "", contents)
}

// isNewerRevision returns nil if the contents of new has a newer revision than
Expand All @@ -331,7 +337,7 @@ func isNewerRevision(old []byte, new []byte) error {
return nil
}

func writeFile(file string, contents []byte) error {
func writeFile(file, pkg string, contents []byte) error {
// Don't write it if the contents are identical.
existing, err := os.ReadFile(file)
if err == nil && (bytes.Equal(existing, contents) || basicallyEqual(existing, contents)) {
Expand All @@ -341,7 +347,49 @@ func writeFile(file string, contents []byte) error {
if err = os.MkdirAll(outdir, 0755); err != nil {
return fmt.Errorf("failed to Mkdir %s: %v", outdir, err)
}
return os.WriteFile(file, contents, 0644)
// Don't try to split spec files, json or non-allowlisted packages
if pkg == "" || !apisToSplit[pkg] {
return os.WriteFile(file, contents, 0644)
}

// Split generated file out into multiple
bs := bytes.Split(contents, []byte(splitFileSeperator))
for i, b := range bs {
var name string
var newB []byte
if i == 0 {
// For the base case, use the provided inputs as is
name = file
var err error
newB, err = format.Source(b)
if err != nil {
return err
}
} else {
// determine the new file name
base := filepath.Dir(file)
fileNum := i + 1
name = base + string(filepath.Separator) + pkg + fmt.Sprint(fileNum) + "-gen.go"
codyoss marked this conversation as resolved.
Show resolved Hide resolved

// prepend file header, package, and imports
var buf bytes.Buffer
// Size the buffer so there is room for the header and contents
buf.Grow(len(b) + 500)
codyoss marked this conversation as resolved.
Show resolved Hide resolved
splitFileHeading(&buf, pkg)
_, err := buf.Write(b)
if err != nil {
return err
}
newB, err = format.Source(buf.Bytes())
if err != nil {
return err
}
}
if err := os.WriteFile(name, newB, 0644); err != nil {
return err
}
}
return nil
}

var ignoreLines = regexp.MustCompile(`(?m)^\s+"(?:etag|revision)": ".+\n`)
Expand Down Expand Up @@ -589,7 +637,7 @@ func (a *API) WriteGeneratedCode() error {
}

code, err := a.GenerateCode()
errw := writeFile(genfilename, code)
errw := writeFile(genfilename, a.Package(), code)
if err == nil {
err = errw
}
Expand Down Expand Up @@ -862,8 +910,12 @@ func (a *API) GenerateCode() ([]byte, error) {
for _, meth := range a.APIMethods() {
meth.generateCode()
}

for _, res := range a.doc.Resources {
a.insertSplitFileComment()
rCnt := len(a.doc.Resources) / 2
for i, res := range a.doc.Resources {
if i == rCnt {
a.insertSplitFileComment()
}
a.generateResourceMethods(res)
}

Expand All @@ -874,6 +926,56 @@ func (a *API) GenerateCode() ([]byte, error) {
return clean, nil
}

func (a *API) insertSplitFileComment() {
if apisToSplit[a.Package()] {
a.pn("")
a.pn(splitFileSeperator)
a.pn("")
}
}

// splitFileHeading writes the file preamble used when generating a split file
// client like compute.
func splitFileHeading(w io.Writer, pkg string) {
pn := func(format string, args ...interface{}) {
_, err := fmt.Fprintf(w, format+"\n", args...)
if err != nil {
panic(err)
}
}

pn(`// Copyright %s Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Code generated file. DO NOT EDIT.
`, *copyrightYear)
codyoss marked this conversation as resolved.
Show resolved Hide resolved
pn("")
pn("package %s", pkg)
pn("")
pn("import (")
for _, imp := range []string{
"context",
"fmt",
"io",
"net/http",
} {
pn(" %q", imp)
}
pn("")
for _, imp := range []struct {
pkg string
lname string
}{
{*gensupportPkg, "gensupport"},
{*googleapiPkg, "googleapi"},
} {
pn(" %s %q", imp.lname, imp.pkg)
}
pn(")")
pn("")
}

func (a *API) generateScopeConstants() {
scopes := a.doc.Auth.OAuth2Scopes
if len(scopes) == 0 {
Expand Down