Skip to content

Commit

Permalink
feat: add googleapis-gen as a tracked source (#944)
Browse files Browse the repository at this point in the history
* feat: add googleapis-gen as a tracked source

* fix java prefix (always cloud)

* chore: fix lint

* inline constants as they are only referenced once

* chore: fix lint - remove unused import
  • Loading branch information
chingor13 authored Feb 11, 2021
1 parent 2414b81 commit b32c87d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
37 changes: 37 additions & 0 deletions synthtool/gcp/pregenerated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from pathlib import Path
import os

from synthtool.log import logger
from synthtool.sources import git


class Pregenerated:
"""A synthtool component that copies pregenerated bazel code."""

def __init__(self):
local_clone = os.environ.get("SYNTHTOOL_GOOGLEAPIS_GEN")
if local_clone:
self._googleapis_gen = Path(local_clone).expanduser()
logger.debug(f"Using local googleapis-gen at {self._googleapis_gen}")
else:
logger.debug("Cloning googleapis-gen.")
self._googleapis_gen = git.clone(
git.make_repo_clone_url("googleapis/googleapis-gen")
)

def generate(self, path: str) -> Path:
return self._googleapis_gen / path
45 changes: 42 additions & 3 deletions synthtool/languages/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import synthtool as s
import synthtool.gcp as gcp
from synthtool import cache, shell
from synthtool.gcp import common, partials, samples, snippets
from synthtool.gcp import common, partials, pregenerated, samples, snippets
from synthtool.log import logger
from pathlib import Path
from typing import Any, Optional, Dict, List
Expand Down Expand Up @@ -306,11 +306,10 @@ def bazel_library(
service=service, version=version, diregapic=diregapic, **kwargs
)

cloud_prefix = "cloud-" if cloud_api else ""
_common_generation(
service=service,
version=version,
library=library / f"google-{cloud_prefix}{service}-{version}-java",
library=library / f"google-cloud-{service}-{version}-java",
package_pattern=package_pattern,
suffix="-java",
destination_name=destination_name,
Expand All @@ -321,6 +320,46 @@ def bazel_library(
return library


def pregenerated_library(
path: str,
service: str,
version: str,
destination_name: str = None,
cloud_api: bool = True,
) -> Path:
"""Generate a Java library using the gapic-generator via bazel.
Generates code into a temp directory, fixes missing header fields, and
copies into the expected locations.
Args:
path (str): Path in googleapis-gen to un-versioned generated code.
service (str): Name of the service.
version (str): Service API version.
destination_name (str, optional): Override the service name for the
destination of the output code. Defaults to the service name.
cloud_api (bool, optional): Whether or not this is a cloud API (for naming)
Returns:
The path to the temp directory containing the generated client.
"""
generator = pregenerated.Pregenerated()
library = generator.generate(path)

cloud_prefix = "cloud-" if cloud_api else ""
_common_generation(
service=service,
version=version,
library=library / f"google-{cloud_prefix}{service}-{version}-java",
package_pattern="unused",
suffix="-java",
destination_name=destination_name,
cloud_api=cloud_api,
)

return library


def _merge_common_templates(
source_text: str, destination_text: str, file_path: Path
) -> str:
Expand Down

0 comments on commit b32c87d

Please sign in to comment.