Skip to content

Commit

Permalink
fix(internal/gapicgen): add generation of internal/version.go for new…
Browse files Browse the repository at this point in the history
… client modules (#5726)

This was discovered attempting to generate certificatemanager.
New client generation failed with apiv1/version.go: no such file or directory

- Move genVersionFile before genModule .
- Fix path for generation of apivX/version.go file.
- Add template and creation of the client's internal/version.go file, conditional on genModule.
  • Loading branch information
quartzmo committed Mar 4, 2022
1 parent 979ae7e commit 341e0df
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
20 changes: 20 additions & 0 deletions internal/gapicgen/generator/_internal_version.go.txt
@@ -0,0 +1,20 @@
// 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 internal

// Version is the current tagged release of the library.
const Version = "0.1.0"
30 changes: 26 additions & 4 deletions internal/gapicgen/generator/gapics.go
Expand Up @@ -40,6 +40,8 @@ var (
readmeTmpl string
//go:embed _version.go.txt
versionTmpl string
//go:embed _internal_version.go.txt
internalVersionTmpl string
)

// GapicGenerator is used to regenerate gapic libraries.
Expand Down Expand Up @@ -102,14 +104,14 @@ func (g *GapicGenerator) Regen(ctx context.Context) error {
if err := g.microgen(c); err != nil {
return err
}
if err := g.genVersionFile(c); err != nil {
return err
}
if g.genModule {
if err := gocmd.ModTidy(modPath); err != nil {
return nil
}
}
if err := g.genVersionFile(c); err != nil {
return err
}
}

if err := g.copyMicrogenFiles(); err != nil {
Expand Down Expand Up @@ -326,7 +328,7 @@ func (g *GapicGenerator) genVersionFile(conf *microgenConfig) error {
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"))
f, err := os.Create(filepath.Join(g.googleCloudDir, conf.importPath, "version.go"))
if err != nil {
return err
}
Expand All @@ -345,6 +347,26 @@ func (g *GapicGenerator) genVersionFile(conf *microgenConfig) error {
if err := t.Execute(f, versionData); err != nil {
return err
}

if g.genModule {
os.MkdirAll(filepath.Join(g.googleCloudDir, rootModInternal), os.ModePerm)

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

t2 := template.Must(template.New("internal_version").Parse(internalVersionTmpl))
internalVersionData := struct {
Year int
}{
Year: time.Now().Year(),
}
if err := t2.Execute(f2, internalVersionData); err != nil {
return err
}
}
return nil
}

Expand Down

0 comments on commit 341e0df

Please sign in to comment.