Skip to content

Commit 341e0df

Browse files
authored
fix(internal/gapicgen): add generation of internal/version.go for new 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.
1 parent 979ae7e commit 341e0df

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright {{.Year}} Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Code generated by gapicgen. DO NOT EDIT.
16+
17+
package internal
18+
19+
// Version is the current tagged release of the library.
20+
const Version = "0.1.0"

internal/gapicgen/generator/gapics.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ var (
4040
readmeTmpl string
4141
//go:embed _version.go.txt
4242
versionTmpl string
43+
//go:embed _internal_version.go.txt
44+
internalVersionTmpl string
4345
)
4446

4547
// GapicGenerator is used to regenerate gapic libraries.
@@ -102,14 +104,14 @@ func (g *GapicGenerator) Regen(ctx context.Context) error {
102104
if err := g.microgen(c); err != nil {
103105
return err
104106
}
107+
if err := g.genVersionFile(c); err != nil {
108+
return err
109+
}
105110
if g.genModule {
106111
if err := gocmd.ModTidy(modPath); err != nil {
107112
return nil
108113
}
109114
}
110-
if err := g.genVersionFile(c); err != nil {
111-
return err
112-
}
113115
}
114116

115117
if err := g.copyMicrogenFiles(); err != nil {
@@ -326,7 +328,7 @@ func (g *GapicGenerator) genVersionFile(conf *microgenConfig) error {
326328
rootPackage := strings.Split(relDir, "/")[0]
327329
rootModInternal := fmt.Sprintf("cloud.google.com/go/%s/internal", rootPackage)
328330

329-
f, err := os.Create(filepath.Join(g.googleCloudDir, relDir, "version.go"))
331+
f, err := os.Create(filepath.Join(g.googleCloudDir, conf.importPath, "version.go"))
330332
if err != nil {
331333
return err
332334
}
@@ -345,6 +347,26 @@ func (g *GapicGenerator) genVersionFile(conf *microgenConfig) error {
345347
if err := t.Execute(f, versionData); err != nil {
346348
return err
347349
}
350+
351+
if g.genModule {
352+
os.MkdirAll(filepath.Join(g.googleCloudDir, rootModInternal), os.ModePerm)
353+
354+
f2, err := os.Create(filepath.Join(g.googleCloudDir, rootModInternal, "version.go"))
355+
if err != nil {
356+
return err
357+
}
358+
defer f2.Close()
359+
360+
t2 := template.Must(template.New("internal_version").Parse(internalVersionTmpl))
361+
internalVersionData := struct {
362+
Year int
363+
}{
364+
Year: time.Now().Year(),
365+
}
366+
if err := t2.Execute(f2, internalVersionData); err != nil {
367+
return err
368+
}
369+
}
348370
return nil
349371
}
350372

0 commit comments

Comments
 (0)