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

feat(internal/gapicgen): change versionClient for gapics #5687

Merged
merged 5 commits into from Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 deletions internal/gapicgen/generator/_version.go.txt
@@ -0,0 +1,23 @@
// Copyright {{.Year}} Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Code generated by gapicgen. DO NOT EDIT.

package {{.Package}}

import "{{.ModuleRootInternal}}"

func init() {
versionClient = internal.Version
}
70 changes: 33 additions & 37 deletions internal/gapicgen/generator/gapics.go
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"cloud.google.com/go/internal/gapicgen/execv"
"cloud.google.com/go/internal/gapicgen/execv/gocmd"
Expand All @@ -37,6 +38,8 @@ var (
changesTmpl string
//go:embed _README.md.txt
readmeTmpl string
//go:embed _version.go.txt
versionTmpl string
)

// GapicGenerator is used to regenerate gapic libraries.
Expand Down Expand Up @@ -106,6 +109,9 @@ func (g *GapicGenerator) Regen(ctx context.Context) error {
return nil
}
}
if err := g.genVersionFile(c); err != nil {
return err
}
}

if err := g.copyMicrogenFiles(); err != nil {
Expand All @@ -117,10 +123,6 @@ func (g *GapicGenerator) Regen(ctx context.Context) error {
return err
}

if err := g.resetUnknownVersion(); err != nil {
return err
}

if g.regenOnly {
return nil
}
Expand Down Expand Up @@ -254,39 +256,6 @@ go mod edit -dropreplace "google.golang.org/genproto"
return c.Run()
}

// resetUnknownVersion resets doc.go files that have only had their version
// changed to UNKNOWN by the generator.
func (g *GapicGenerator) resetUnknownVersion() error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good bye!!!!! 👍🏻 👍🏻 👍🏻

files, err := git.FindModifiedFiles(g.googleCloudDir)
if err != nil {
return err
}

for _, file := range files {
if !strings.HasSuffix(file, "doc.go") {
continue
}
diff, err := git.FileDiff(g.googleCloudDir, file)
if err != nil {
return err
}
// More than one diff, don't reset.
if strings.Count(diff, "@@") != 2 {
log.Println(diff)
continue
}
// Not related to version, don't reset.
if !strings.Contains(diff, "+const versionClient = \"UNKNOWN\"") {
continue
}

if err := git.ResetFile(g.googleCloudDir, file); err != nil {
return err
}
}
return nil
}

// setVersion updates the versionClient constant in all .go files. It may create
// .backup files on certain systems (darwin), and so should be followed by a
// clean-up of .backup files.
Expand Down Expand Up @@ -359,6 +328,33 @@ func (g *GapicGenerator) microgen(conf *microgenConfig) error {
return c.Run()
}

func (g *GapicGenerator) genVersionFile(conf *microgenConfig) error {
relDir := strings.TrimPrefix(conf.importPath, "cloud.google.com/go/")
rootPackage := strings.Split(relDir, "/")[0]
rootModInternal := fmt.Sprintf("cloud.google.com/go/%s/internal", rootPackage)

f, err := os.Create(filepath.Join(g.googleCloudDir, relDir, "version.go"))
if err != nil {
return err
}
defer f.Close()

t := template.Must(template.New("version").Parse(versionTmpl))
versionData := struct {
Year int
Package string
ModuleRootInternal string
}{
Year: time.Now().Year(),
Package: conf.pkg,
ModuleRootInternal: rootModInternal,
}
if err := t.Execute(f, versionData); err != nil {
return err
}
return nil
}

// manifestEntry is used for JSON marshaling in manifest.
type manifestEntry struct {
DistributionName string `json:"distribution_name"`
Expand Down
26 changes: 23 additions & 3 deletions internal/gapicgen/go.mod
@@ -1,14 +1,12 @@
module cloud.google.com/go/internal/gapicgen

go 1.16
go 1.17

require (
cloud.google.com/go v0.100.2
cloud.google.com/go/internal/godocfx v0.0.0-20220124075323-fc08c49fc013
github.com/google/go-github/v35 v35.3.0
github.com/shurcooL/githubv4 v0.0.0-20220115235240-a14260e6f8a2
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9
Expand All @@ -17,3 +15,25 @@ require (
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/emirpasic/gods v1.12.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/sergi/go-diff v1.0.0 // indirect
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
github.com/src-d/gcfg v1.4.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/xanzy/ssh-agent v0.2.1 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
golang.org/x/tools v0.1.8 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/src-d/go-billy.v4 v4.3.2 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
2 changes: 1 addition & 1 deletion secretmanager/apiv1/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions secretmanager/apiv1/version.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.