|
| 1 | +// Copyright 2026 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 | +// https://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 java |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/googleapis/librarian/internal/config" |
| 24 | + "github.com/googleapis/librarian/internal/serviceconfig" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + protoPomTemplateName = "proto_pom.xml.tmpl" |
| 29 | + grpcPomTemplateName = "grpc_pom.xml.tmpl" |
| 30 | + grcpProtoGroupID = "com.google.api.grpc" |
| 31 | +) |
| 32 | + |
| 33 | +// grpcProtoPomData holds the data for rendering POM templates. |
| 34 | +type grpcProtoPomData struct { |
| 35 | + Proto coordinates |
| 36 | + Grpc coordinates |
| 37 | + Parent coordinates |
| 38 | + Version string |
| 39 | + MainArtifactID string |
| 40 | +} |
| 41 | + |
| 42 | +type coordinates struct { |
| 43 | + GroupID string |
| 44 | + ArtifactID string |
| 45 | +} |
| 46 | + |
| 47 | +// javaModule represents a Maven module and its POM generation state. |
| 48 | +type javaModule struct { |
| 49 | + artifactID string |
| 50 | + dir string |
| 51 | + isMissing bool |
| 52 | + data grpcProtoPomData |
| 53 | + template string |
| 54 | +} |
| 55 | + |
| 56 | +// generatePomsIfMissing generates missing proto-* and grpc-* POMs. |
| 57 | +func generatePomsIfMissing(library *config.Library, libraryDir, googleapisDir string) error { |
| 58 | + modules, err := collectModules(library, libraryDir, googleapisDir) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + for _, m := range modules { |
| 63 | + if !m.isMissing { |
| 64 | + continue |
| 65 | + } |
| 66 | + if err := writePom(filepath.Join(m.dir, "pom.xml"), m.template, m.data); err != nil { |
| 67 | + return fmt.Errorf("failed to generate %s: %w", m.artifactID, err) |
| 68 | + } |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// collectModules identifies all expected proto-* and grpc-* modules |
| 74 | +// for the given library based on its configuration and checks a pom.xml presence |
| 75 | +// on the filesystem. |
| 76 | +// |
| 77 | +// All expected modules are collected (even if they exist) because the client |
| 78 | +// module's POM requires a full list of all proto and gRPC dependencies |
| 79 | +// to ensure its dependency list is fully synchronized. |
| 80 | +func collectModules(library *config.Library, libraryDir, googleapisDir string) ([]javaModule, error) { |
| 81 | + distName := deriveDistributionName(library) |
| 82 | + parts := strings.SplitN(distName, ":", 2) |
| 83 | + if len(parts) != 2 { |
| 84 | + return nil, fmt.Errorf("invalid distribution name %q: expected format groupID:artifactID", distName) |
| 85 | + } |
| 86 | + gapicGroupID := parts[0] |
| 87 | + gapicArtifactID := parts[1] |
| 88 | + |
| 89 | + var modules []javaModule |
| 90 | + for _, api := range library.APIs { |
| 91 | + version := serviceconfig.ExtractVersion(api.Path) |
| 92 | + if version == "" { |
| 93 | + return nil, fmt.Errorf("failed to extract version from API path %q", api.Path) |
| 94 | + } |
| 95 | + |
| 96 | + names := deriveModuleNames(gapicArtifactID, version) |
| 97 | + |
| 98 | + apiCfg, err := serviceconfig.Find(googleapisDir, api.Path, config.LanguageJava) |
| 99 | + if err != nil { |
| 100 | + return nil, fmt.Errorf("failed to find api config for %s: %w", api.Path, err) |
| 101 | + } |
| 102 | + transport := apiCfg.Transport(config.LanguageJava) |
| 103 | + |
| 104 | + data := grpcProtoPomData{ |
| 105 | + Proto: coordinates{ |
| 106 | + GroupID: grcpProtoGroupID, |
| 107 | + ArtifactID: names.proto, |
| 108 | + }, |
| 109 | + Grpc: coordinates{ |
| 110 | + GroupID: grcpProtoGroupID, |
| 111 | + ArtifactID: names.grpc, |
| 112 | + }, |
| 113 | + Parent: coordinates{ |
| 114 | + GroupID: gapicGroupID, |
| 115 | + ArtifactID: fmt.Sprintf("%s-parent", gapicArtifactID), |
| 116 | + }, |
| 117 | + MainArtifactID: gapicArtifactID, |
| 118 | + Version: library.Version, |
| 119 | + } |
| 120 | + |
| 121 | + // Proto module |
| 122 | + protoDir := filepath.Join(libraryDir, names.proto) |
| 123 | + isProtoMissing, err := isPomMissing(protoDir) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + modules = append(modules, javaModule{ |
| 128 | + artifactID: names.proto, |
| 129 | + dir: protoDir, |
| 130 | + isMissing: isProtoMissing, |
| 131 | + data: data, |
| 132 | + template: protoPomTemplateName, |
| 133 | + }) |
| 134 | + |
| 135 | + // gRPC module |
| 136 | + if transport == serviceconfig.GRPC || transport == serviceconfig.GRPCRest { |
| 137 | + grpcDir := filepath.Join(libraryDir, names.grpc) |
| 138 | + isGrpcMissing, err := isPomMissing(grpcDir) |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + modules = append(modules, javaModule{ |
| 143 | + artifactID: names.grpc, |
| 144 | + dir: grpcDir, |
| 145 | + isMissing: isGrpcMissing, |
| 146 | + data: data, |
| 147 | + template: grpcPomTemplateName, |
| 148 | + }) |
| 149 | + } |
| 150 | + } |
| 151 | + return modules, nil |
| 152 | +} |
| 153 | + |
| 154 | +func isPomMissing(dir string) (bool, error) { |
| 155 | + pomPath := filepath.Join(dir, "pom.xml") |
| 156 | + if _, err := os.Stat(pomPath); err == nil { |
| 157 | + return false, nil |
| 158 | + } |
| 159 | + if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 160 | + return false, fmt.Errorf("target directory %s does not exist: %w", dir, err) |
| 161 | + } |
| 162 | + return true, nil |
| 163 | +} |
| 164 | + |
| 165 | +func writePom(pomPath, templateName string, data any) (err error) { |
| 166 | + f, err := os.Create(pomPath) |
| 167 | + if err != nil { |
| 168 | + return fmt.Errorf("failed to create %s: %w", pomPath, err) |
| 169 | + } |
| 170 | + defer func() { |
| 171 | + cerr := f.Close() |
| 172 | + if err == nil { |
| 173 | + err = cerr |
| 174 | + } |
| 175 | + }() |
| 176 | + if terr := templates.ExecuteTemplate(f, templateName, data); terr != nil { |
| 177 | + return fmt.Errorf("failed to execute template %s: %w", templateName, terr) |
| 178 | + } |
| 179 | + return nil |
| 180 | +} |
0 commit comments