|
| 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 | + "strings" |
| 20 | + |
| 21 | + "github.com/googleapis/librarian/internal/config" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + googleGroupID = "com.google" |
| 26 | + protoGrpcSuffix = ".api.grpc" |
| 27 | + cloudPrefix = "google-cloud-" |
| 28 | + grpcPrefix = "grpc-" |
| 29 | + protoPrefix = "proto-" |
| 30 | +) |
| 31 | + |
| 32 | +var groupInclusions = map[string]bool{ |
| 33 | + "com.google.cloud": true, |
| 34 | + "com.google.analytics": true, |
| 35 | + "com.google.area120": true, |
| 36 | +} |
| 37 | + |
| 38 | +type coordinate struct { |
| 39 | + GroupID string |
| 40 | + ArtifactID string |
| 41 | + Version string |
| 42 | +} |
| 43 | + |
| 44 | +type libCoord struct { |
| 45 | + gapic coordinate |
| 46 | + parent coordinate |
| 47 | + bom coordinate |
| 48 | +} |
| 49 | + |
| 50 | +type apiCoord struct { |
| 51 | + libCoord |
| 52 | + proto coordinate |
| 53 | + grpc coordinate |
| 54 | +} |
| 55 | + |
| 56 | +func deriveLibCoord(library *config.Library) libCoord { |
| 57 | + distName := deriveDistributionName(library) |
| 58 | + parts := strings.SplitN(distName, ":", 2) |
| 59 | + groupID := parts[0] |
| 60 | + artifactID := groupID |
| 61 | + if len(parts) == 2 { |
| 62 | + artifactID = parts[1] |
| 63 | + } |
| 64 | + gapic := coordinate{ |
| 65 | + GroupID: groupID, |
| 66 | + ArtifactID: artifactID, |
| 67 | + Version: library.Version, |
| 68 | + } |
| 69 | + return libCoord{ |
| 70 | + gapic: gapic, |
| 71 | + parent: coordinate{ |
| 72 | + GroupID: gapic.GroupID, |
| 73 | + ArtifactID: fmt.Sprintf("%s-parent", gapic.ArtifactID), |
| 74 | + Version: gapic.Version, |
| 75 | + }, |
| 76 | + bom: coordinate{ |
| 77 | + GroupID: gapic.GroupID, |
| 78 | + ArtifactID: fmt.Sprintf("%s-bom", gapic.ArtifactID), |
| 79 | + Version: gapic.Version, |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func deriveAPICoord(lc libCoord, version string) apiCoord { |
| 85 | + protoGrpcGroupID := protoGroupID(lc.gapic.GroupID) |
| 86 | + return apiCoord{ |
| 87 | + libCoord: lc, |
| 88 | + proto: coordinate{ |
| 89 | + GroupID: protoGrpcGroupID, |
| 90 | + ArtifactID: fmt.Sprintf("%s%s-%s", protoPrefix, lc.gapic.ArtifactID, version), |
| 91 | + Version: lc.gapic.Version, |
| 92 | + }, |
| 93 | + grpc: coordinate{ |
| 94 | + GroupID: protoGrpcGroupID, |
| 95 | + ArtifactID: fmt.Sprintf("%s%s-%s", grpcPrefix, lc.gapic.ArtifactID, version), |
| 96 | + Version: lc.gapic.Version, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// protoGroupID returns the Maven Group ID for the generated proto and gRPC |
| 102 | +// artifacts. It maps the GAPIC library's Group ID to a standard format and |
| 103 | +// checks for special cases in groupInclusions (e.g., mapping |
| 104 | +// "com.google.cloud" to "com.google.api.grpc"). |
| 105 | +func protoGroupID(mainArtifactGroupID string) string { |
| 106 | + prefix := mainArtifactGroupID |
| 107 | + if groupInclusions[mainArtifactGroupID] { |
| 108 | + prefix = googleGroupID |
| 109 | + } |
| 110 | + return prefix + protoGrpcSuffix |
| 111 | +} |
| 112 | + |
| 113 | +// ensureCloudPrefix returns name with the "google-cloud-" prefix, |
| 114 | +// adding it if not already present. |
| 115 | +func ensureCloudPrefix(name string) string { |
| 116 | + if !strings.HasPrefix(name, cloudPrefix) { |
| 117 | + return cloudPrefix + name |
| 118 | + } |
| 119 | + return name |
| 120 | +} |
| 121 | + |
| 122 | +func deriveDistributionName(library *config.Library) string { |
| 123 | + if library.Java != nil && library.Java.DistributionNameOverride != "" { |
| 124 | + return library.Java.DistributionNameOverride |
| 125 | + } |
| 126 | + groupID := "com.google.cloud" |
| 127 | + if library.Java != nil && library.Java.GroupID != "" { |
| 128 | + groupID = library.Java.GroupID |
| 129 | + } |
| 130 | + artifactID := ensureCloudPrefix(library.Name) |
| 131 | + return fmt.Sprintf("%s:%s", groupID, artifactID) |
| 132 | +} |
0 commit comments