Skip to content

Commit

Permalink
fix(java-templates): custom templates needs to load partial metadata (#…
Browse files Browse the repository at this point in the history
…788)

* refactor: split partial yaml loading into a separate module for re-use

* fix: java custom_templates needs to load partials data

* chore: newline

* chore: fix lint
  • Loading branch information
chingor13 authored Oct 8, 2020
1 parent 487eba7 commit b6164c2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 42 deletions.
26 changes: 2 additions & 24 deletions synthtool/gcp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import os
import re
import shutil
import yaml
from copy import deepcopy
from pathlib import Path
from typing import Dict, List, Optional
import jinja2

from synthtool import shell, _tracked_paths
from synthtool.gcp import partials
from synthtool.languages import node
from synthtool.log import logger
from synthtool.sources import git, templates
Expand Down Expand Up @@ -304,7 +304,7 @@ def _load_generic_metadata(self, metadata: Dict):
"""
loads additional meta information from .repo-metadata.json.
"""
self._load_partials(metadata)
metadata["partials"] = partials.load_partials()

# Loads repo metadata information from the default location if it
# hasn't already been set. Some callers may have already loaded repo
Expand All @@ -313,28 +313,6 @@ def _load_generic_metadata(self, metadata: Dict):
if "repo" not in metadata:
metadata["repo"] = _load_repo_metadata()

def _load_partials(self, metadata: Dict):
"""
hand-crafted artisinal markdown can be provided in a .readme-partials.yml.
The following fields are currently supported:
body: custom body to include in the usage section of the document.
samples_body: an optional body to place below the table of contents
in samples/README.md.
introduction: a more thorough introduction than metadata["description"].
title: provide markdown to use as a custom title.
"""
cwd_path = Path(os.getcwd())
partials_file = None
for file in [".readme-partials.yml", ".readme-partials.yaml"]:
if os.path.exists(cwd_path / file):
partials_file = cwd_path / file
break
if not partials_file:
return
with open(partials_file) as f:
metadata["partials"] = yaml.load(f, Loader=yaml.SafeLoader)


def decamelize(value: str):
""" parser to convert fooBar.js to Foo Bar. """
Expand Down
44 changes: 44 additions & 0 deletions synthtool/gcp/partials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2020 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.

import os
import yaml
from pathlib import Path
from typing import Dict, List

# these are the default locations to look up
_DEFAULT_PARTIAL_FILES = [".readme-partials.yml", ".readme-partials.yaml"]


def load_partials(files: List[str] = _DEFAULT_PARTIAL_FILES) -> Dict:
"""
hand-crafted artisinal markdown can be provided in a .readme-partials.yml.
The following fields are currently supported:
body: custom body to include in the usage section of the document.
samples_body: an optional body to place below the table of contents
in samples/README.md.
introduction: a more thorough introduction than metadata["description"].
title: provide markdown to use as a custom title.
"""
cwd_path = Path(os.getcwd())
partials_file = None
for file in files:
if os.path.exists(cwd_path / file):
partials_file = cwd_path / file
break
if not partials_file:
return {}
with open(partials_file) as f:
return yaml.load(f, Loader=yaml.SafeLoader)
3 changes: 2 additions & 1 deletion synthtool/languages/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import synthtool as s
import synthtool.gcp as gcp
from synthtool import cache, shell
from synthtool.gcp import common, samples, snippets
from synthtool.gcp import common, partials, samples, snippets
from synthtool.log import logger
from pathlib import Path
from typing import Any, Optional, Dict, List
Expand Down Expand Up @@ -385,6 +385,7 @@ def custom_templates(files: List[str], **kwargs) -> None:
**kwargs: Additional options for CommonTemplates.render()
"""
kwargs["metadata"] = _common_template_metadata()
kwargs["metadata"]["partials"] = partials.load_partials()
for file in files:
template = gcp.CommonTemplates().render(file, **kwargs)
s.copy([template])
36 changes: 36 additions & 0 deletions tests/test_partials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2020 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.

import os
from pathlib import Path

from synthtool.gcp import partials

FIXTURES = Path(__file__).parent / "fixtures" / "node_templates" / "standard"


def test_readme_partials():
cwd = os.getcwd()
os.chdir(FIXTURES)

data = partials.load_partials()
# should have populated introduction from partial.
assert "objects to users via direct download" in data["introduction"]

os.chdir(cwd)


def test_readme_partials_not_found():
data = partials.load_partials(["non-existent.yaml"])
assert len(data) == 0
17 changes: 0 additions & 17 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import stat
import sys
from pathlib import Path

from synthtool.gcp import common
from synthtool.sources import templates


Expand Down Expand Up @@ -108,21 +106,6 @@ def test_hide_billing():
assert "Enable billing for your project" not in result


def test_readme_partials():
cwd = os.getcwd()
os.chdir(FIXTURES)

common_templates = common.CommonTemplates()
metadata = {}
common_templates._load_partials(metadata)
# should have populated introduction from partial.
assert (
"objects to users via direct download" in metadata["partials"]["introduction"]
)

os.chdir(cwd)


def test_ruby_authentication():
t = templates.Templates(RUBY_TEMPLATES)
# .repo-metadata.json in google-cloud-ruby package directories
Expand Down

0 comments on commit b6164c2

Please sign in to comment.