-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(java-templates): custom templates needs to load partial metadata (#…
…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
Showing
5 changed files
with
84 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters