Debian Codegraph @

internal/librariangen/module/module.go

raw
1 // Copyright 2025 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 // Package module provides functions for creating and updating Go module files.
16 package module
17
18 import (
19 _ "embed"
20 "errors"
21 "fmt"
22 "html/template"
23 "log/slog"
24 "os"
25 "path/filepath"
26 "regexp"
27 "strings"
28 "time"
29
30 "cloud.google.com/go/internal/postprocessor/librarian/librariangen/config"
31 "cloud.google.com/go/internal/postprocessor/librarian/librariangen/request"
32 )
33
34 var (
35 //go:embed _internal_version.go.txt
36 internalVersionTmpl string
37 )
38
39 // GenerateInternalVersionFile creates an internal/version.go file for the module.
40 func GenerateInternalVersionFile(moduleDir, version string) error {
41 internalDir := filepath.Join(moduleDir, "internal")
42 if err := os.MkdirAll(internalDir, 0755); err != nil {
43 return err
44 }
45 versionPath := filepath.Join(internalDir, "version.go")
46 slog.Debug("librariangen: creating file", "path", versionPath)
47 t := template.Must(template.New("internal_version").Parse(internalVersionTmpl))
48 internalVersionData := struct {
49 Year int
50 Version string
51 }{
52 Year: time.Now().Year(),
53 Version: version,
54 }
55 if err := os.MkdirAll(filepath.Dir(versionPath), 0755); err != nil {
56 return fmt.Errorf("librariangen: creating directory for version file: %w", err)
57 }
58
59 f, err := os.Create(versionPath)
60 if err != nil {
61 return err
62 }
63 defer f.Close()
64 return t.Execute(f, internalVersionData)
65 }
66
67 // UpdateSnippetsMetadata updates all snippet files to populate the $VERSION placeholder, reading them from
68 // the sourceDir and writing them to the destDir. These two may be the same, but don't have to be.
69 func UpdateSnippetsMetadata(lib *request.Library, sourceDir string, destDir string, moduleConfig *config.ModuleConfig) error {
70 moduleName := lib.ID
71 version := lib.Version
72
73 slog.Debug("librariangen: updating snippets metadata")
74 snpDir := filepath.Join("internal", "generated", "snippets", moduleName)
75
76 for _, api := range lib.APIs {
77 apiConfig := moduleConfig.GetAPIConfig(api.Path)
78 clientDirName, err := apiConfig.GetClientDirectory()
79 if err != nil {
80 return err
81 }
82
83 snippetFile := "snippet_metadata." + apiConfig.GetProtoPackage() + ".json"
84 path := filepath.Join(snpDir, clientDirName, snippetFile)
85 slog.Info("librariangen: updating snippet metadata file", "path", path)
86 read, err := os.ReadFile(filepath.Join(sourceDir, path))
87 if err != nil {
88 // If the snippet metadata doesn't exist, that's probably because this API path
89 // is proto-only (so the GAPIC generator hasn't run). Continue to the next API path.
90 if errors.Is(err, os.ErrNotExist) {
91 slog.Info("librariangen: snippet metadata file not found; assuming proto-only package", "path", path)
92 continue
93 }
94 return err
95 }
96
97 content := string(read)
98 var newContent string
99 var oldVersion string
100
101 if strings.Contains(content, "$VERSION") {
102 newContent = strings.Replace(content, "$VERSION", version, 1)
103 oldVersion = "$VERSION"
104 } else {
105 // This regex finds a version string like "1.2.3".
106 re := regexp.MustCompile(`\d+\.\d+\.\d+`)
107 if foundVersion := re.FindString(content); foundVersion != "" {
108 newContent = strings.Replace(content, foundVersion, version, 1)
109 oldVersion = foundVersion
110 }
111 }
112
113 if newContent == "" {
114 return fmt.Errorf("librariangen: no version number or placeholder found in '%s'", snippetFile)
115 }
116
117 destPath := filepath.Join(destDir, path)
118 slog.Info("librariangen: updating version in snippets metadata file", "destPath", path, "old", oldVersion, "new", version)
119 if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil {
120 return fmt.Errorf("librariangen: creating directory for snippet file: %w", err)
121 }
122 err = os.WriteFile(destPath, []byte(newContent), 0644)
123 if err != nil {
124 return err
125 }
126 }
127 return nil
128 }