Conversation
…erived correctly - Support Google\Ads namespace by using vendor googleads/, hyphenating project name, and setting github repo to googleapis/php-ads-* - Support CommonProtos by detecting missing protobuf service definitions, stripping type/common subpackages, appending CommonProtos to component name, and setting repo metadata library_type to CORE - Allow empty api_shortname when no default_host is defined Fixes #9387
bshaffer
left a comment
There was a problem hiding this comment.
This looks good but the changes are way too complex for what I had in mind. I think there are a lot of opportunities to simplify things
| if (str_starts_with($phpNamespace, 'Google\\Ads')) { | ||
| if (count($parts) > 1 && 'v' === strtolower($parts[count($parts) - 1][0] ?? '')) { | ||
| array_pop($parts); | ||
| } | ||
| $name = str_replace( | ||
| ['google.', 'devtools.cloud', '.'], | ||
| ['', 'cloud-', '-'], | ||
| implode('.', $parts) | ||
| ); | ||
| if (str_starts_with($name, 'ads-')) { | ||
| $name = substr($name, 4); | ||
| } | ||
| if (str_ends_with($name, 'manager') && !str_ends_with($name, '-manager')) { | ||
| $name = substr($name, 0, -7) . '-manager'; | ||
| } | ||
| if (!$hasGapicClient && !str_ends_with($name, '-common-protos')) { | ||
| $name .= '-common-protos'; | ||
| } | ||
| return 'googleads/' . $name; | ||
| } |
There was a problem hiding this comment.
This is overly complicated. We don't need special handling for all of these cases, but rather should just use googleads instead of google if the phpNamespace starts with Google\Ads
| while (count($parts) > 1) { | ||
| $last = end($parts); | ||
| if ('v' === $last[0]) { | ||
| array_pop($parts); | ||
| } elseif (!$hasGapicClient && count($parts) > 2 && in_array($last, ['type', 'common'])) { | ||
| array_pop($parts); | ||
| } else { | ||
| break; | ||
| } |
There was a problem hiding this comment.
This is also over complicated. Could simplify to
| while (count($parts) > 1) { | |
| $last = end($parts); | |
| if ('v' === $last[0]) { | |
| array_pop($parts); | |
| } elseif (!$hasGapicClient && count($parts) > 2 && in_array($last, ['type', 'common'])) { | |
| array_pop($parts); | |
| } else { | |
| break; | |
| } | |
| $last = end($parts); | |
| if ('v' === $last[0] || in_array($last, ['type', 'common'])) { | |
| array_pop($parts); | |
| } |
| while (count($parts) > 1) { | ||
| $last = end($parts); | ||
| if ('v' === strtolower($last[0])) { | ||
| array_pop($parts); | ||
| } elseif (!$hasGapicClient && count($parts) > 2 && in_array(strtolower($last), ['type', 'common'])) { | ||
| array_pop($parts); | ||
| } else { | ||
| break; | ||
| } |
There was a problem hiding this comment.
We do not want to modify the proto namespace, we should take what the PHP namespace is from the file
| $hasGapicClient = !str_ends_with($new->componentName, 'CommonProtos'); | ||
| if (!$hasGapicClient && !str_ends_with($new->displayName, 'Common Protos')) { | ||
| $new->displayName .= ' Common Protos'; | ||
| } | ||
| $new->composerPackage = self::getComposerPackage( | ||
| $new->protoPackage, | ||
| $new->phpNamespace, | ||
| $hasGapicClient | ||
| ); |
There was a problem hiding this comment.
This logic is a bit odd, as it relies on the user to pass in CommonProtos as the component name. But if this is done, then we can also expect the user to pass in Common Protos as the display name, so there's no reason for us to write logic to do so.
There was a problem hiding this comment.
Done, removed the display name mutation from fromOptions.
| private static function getComposerPackage( | ||
| string $protoPackage, | ||
| string $phpNamespace, | ||
| bool $hasGapicClient = true |
There was a problem hiding this comment.
instead of $hasGapicClient, let's just make it $isCommonProtos
There was a problem hiding this comment.
Updated to $isCommonProtos across the class.
|
Updated to simplify per review feedback:
|
| $parts = explode('.', $protoPackage); | ||
| if ($isCommonProtos && in_array(end($parts), ['type', 'common'])) { | ||
| array_pop($parts); | ||
| } | ||
| if (str_starts_with($phpNamespace, 'Google\\Ads')) { | ||
| if (count($parts) > 1 && 'v' === strtolower(end($parts)[0])) { | ||
| array_pop($parts); | ||
| } | ||
| $name = str_replace( | ||
| ['google.ads.', 'ads.', 'google.', '.'], | ||
| ['', '', '', '-'], | ||
| implode('.', $parts) | ||
| ); | ||
| if (str_ends_with($name, 'manager') && !str_ends_with($name, '-manager')) { | ||
| $name = substr($name, 0, -7) . '-manager'; | ||
| } | ||
| $vendor = 'googleads'; | ||
| } else { | ||
| $name = str_replace( | ||
| ['google.', 'devtools.cloud', '.'], | ||
| ['', 'cloud-', '-'], | ||
| implode('.', $parts) | ||
| ); | ||
| $vendor = 'google'; | ||
| } | ||
|
|
||
| if ($isCommonProtos && !str_ends_with($name, '-common-protos')) { | ||
| $name .= '-common-protos'; | ||
| } | ||
| return $vendor . '/' . $name; |
There was a problem hiding this comment.
This logic is way too complex. I think AI is trying to cover ALL our current packages (and that's why there's logic for "ads manager" in there, which is not necessary). Something like this should work fine:
| $parts = explode('.', $protoPackage); | |
| if ($isCommonProtos && in_array(end($parts), ['type', 'common'])) { | |
| array_pop($parts); | |
| } | |
| if (str_starts_with($phpNamespace, 'Google\\Ads')) { | |
| if (count($parts) > 1 && 'v' === strtolower(end($parts)[0])) { | |
| array_pop($parts); | |
| } | |
| $name = str_replace( | |
| ['google.ads.', 'ads.', 'google.', '.'], | |
| ['', '', '', '-'], | |
| implode('.', $parts) | |
| ); | |
| if (str_ends_with($name, 'manager') && !str_ends_with($name, '-manager')) { | |
| $name = substr($name, 0, -7) . '-manager'; | |
| } | |
| $vendor = 'googleads'; | |
| } else { | |
| $name = str_replace( | |
| ['google.', 'devtools.cloud', '.'], | |
| ['', 'cloud-', '-'], | |
| implode('.', $parts) | |
| ); | |
| $vendor = 'google'; | |
| } | |
| if ($isCommonProtos && !str_ends_with($name, '-common-protos')) { | |
| $name .= '-common-protos'; | |
| } | |
| return $vendor . '/' . $name; | |
| $parts = explode('.', $protoPackage); | |
| $vendor = 'google'; | |
| if (str_starts_with($phpNamespace, 'Google\\Ads')) { | |
| $vendor = 'googleads'; | |
| } | |
| $name = str_replace( | |
| ['google.', 'devtools.cloud', 'google.ads.', '.'], | |
| ['', 'cloud-', '', '-'], | |
| implode('.', $parts) | |
| ); | |
| if ($isCommonProtos && !str_ends_with($name, '-common-protos')) { | |
| $name .= '-common-protos'; | |
| } | |
| return $vendor . '/' . $name; |
There was a problem hiding this comment.
Updated. Placed google.ads. before google. in str_replace so the longer prefix is matched and stripped first in PHP sequential array replacement, and passed $protoPackage directly.
Ensures that component parameters for Google Ads APIs and Common Protos libraries are derived correctly when running
component:new.Fixes #9387
Summary of Changes
Google\Ads):googleads/(e.g.googleads/ad-manager,googleads/data-manager,googleads/marketingplatform-admin).-managerand stripsads-prefix.googleapis/php-ads-[project-name].hasGapicClientdetection to check for protobufservicedefinitions in proto contents.typeandcommonproto and PHP namespace segments.CommonProtosto component name (e.g.GeoCommonProtos,ShoppingCommonProtos).-common-protosto Composer package and GitHub repository names.api_shortnamewithout erroring when nodefault_hostis defined in the proto.library_typetoCOREin.repo-metadata-full.json.NewComponentTestandComponentNewCommandTestcovering Ads APIs and Common Protos libraries.