From 34173260732d62e215bff44df254d728afe442e7 Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Wed, 27 Mar 2019 10:01:13 -0700 Subject: [PATCH 1/5] feat: skip README rendering if .repo-metadata.json does not exist --- synthtool/gcp/common.py | 11 ++++++++--- synthtool/sources/templates.py | 8 ++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index 80300b003..b5eeccdd0 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -35,9 +35,12 @@ def __init__(self): self._templates = templates.Templates(_TEMPLATES_DIR) def _generic_library(self, directory: str, **kwargs) -> Path: - if "metadata" in kwargs: - self._load_generic_metadata(kwargs["metadata"]) t = templates.TemplateGroup(_TEMPLATES_DIR / directory) + + # load common repo meta information (metadata that's not language specific). + if "metadata" in kwargs: + self._load_generic_metadata(kwargs["metadata"], t) + result = t.render(**kwargs) _tracked_paths.add(result) metadata.add_template_source( @@ -62,7 +65,7 @@ def render(self, template_name: str, **kwargs) -> Path: # # loads additional meta information from .repo-metadata.json. # - def _load_generic_metadata(self, metadata): + def _load_generic_metadata(self, metadata, template_group): self._load_samples(metadata) self._load_partials(metadata) @@ -70,6 +73,8 @@ def _load_generic_metadata(self, metadata): if os.path.exists("./.repo-metadata.json"): with open("./.repo-metadata.json") as f: metadata["repo"] = json.load(f) + else: + template_group.excludes.append("README.md") # # walks samples directory and builds up samples data-structure: diff --git a/synthtool/sources/templates.py b/synthtool/sources/templates.py index a9a94cb07..0f8c01f26 100644 --- a/synthtool/sources/templates.py +++ b/synthtool/sources/templates.py @@ -72,11 +72,15 @@ class TemplateGroup: def __init__(self, location: PathOrStr) -> None: self.env = _make_env(location) self.dir = tmp.tmpdir() + self.excludes = [] def render(self, **kwargs) -> Path: for template_name in self.env.list_templates(): - print(template_name) - _render_to_path(self.env, template_name, self.dir, kwargs) + if template_name not in self.excludes: + print(f"Rendering: {template_name}") + _render_to_path(self.env, template_name, self.dir, kwargs) + else: + print(f"Skipping: {template_name}") return self.dir From 67e2b1df643a5c4efbe0af061a32a0b2698712be Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Wed, 27 Mar 2019 10:13:51 -0700 Subject: [PATCH 2/5] fix: don't want to change PR format --- synthtool/sources/templates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synthtool/sources/templates.py b/synthtool/sources/templates.py index 0f8c01f26..4ef8e427e 100644 --- a/synthtool/sources/templates.py +++ b/synthtool/sources/templates.py @@ -77,7 +77,7 @@ def __init__(self, location: PathOrStr) -> None: def render(self, **kwargs) -> Path: for template_name in self.env.list_templates(): if template_name not in self.excludes: - print(f"Rendering: {template_name}") + print(template_name) _render_to_path(self.env, template_name, self.dir, kwargs) else: print(f"Skipping: {template_name}") From 63277ba6c58107477cda7a88aa258f98b451d691 Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Wed, 27 Mar 2019 10:29:35 -0700 Subject: [PATCH 3/5] fix: address linting issue --- synthtool/sources/templates.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synthtool/sources/templates.py b/synthtool/sources/templates.py index 4ef8e427e..2342c1229 100644 --- a/synthtool/sources/templates.py +++ b/synthtool/sources/templates.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Union +from typing import Union, List from pathlib import Path import jinja2 @@ -72,7 +72,7 @@ class TemplateGroup: def __init__(self, location: PathOrStr) -> None: self.env = _make_env(location) self.dir = tmp.tmpdir() - self.excludes = [] + self.excludes = [] # type: List[str] def render(self, **kwargs) -> Path: for template_name in self.env.list_templates(): From 33b283e3c3bd787a1d1d4efd4b3f8e5c6280f236 Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Wed, 27 Mar 2019 10:58:23 -0700 Subject: [PATCH 4/5] chore: fix linting --- synthtool/sources/templates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synthtool/sources/templates.py b/synthtool/sources/templates.py index 2342c1229..606b53d9e 100644 --- a/synthtool/sources/templates.py +++ b/synthtool/sources/templates.py @@ -72,7 +72,7 @@ class TemplateGroup: def __init__(self, location: PathOrStr) -> None: self.env = _make_env(location) self.dir = tmp.tmpdir() - self.excludes = [] # type: List[str] + self.excludes = [] # type: List[str] def render(self, **kwargs) -> Path: for template_name in self.env.list_templates(): From ece4e4bdb99ebc3460f68622668e76065b2cd132 Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Wed, 27 Mar 2019 14:16:57 -0700 Subject: [PATCH 5/5] chore: address code review --- synthtool/gcp/common.py | 16 ++++++++++------ synthtool/sources/templates.py | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/synthtool/gcp/common.py b/synthtool/gcp/common.py index b5eeccdd0..9b3fb893a 100644 --- a/synthtool/gcp/common.py +++ b/synthtool/gcp/common.py @@ -17,6 +17,7 @@ import re import yaml from pathlib import Path +from typing import List from synthtool.languages import node from synthtool.sources import templates @@ -33,14 +34,14 @@ class CommonTemplates: def __init__(self): self._templates = templates.Templates(_TEMPLATES_DIR) + self.excludes = [] # type: List[str] def _generic_library(self, directory: str, **kwargs) -> Path: - t = templates.TemplateGroup(_TEMPLATES_DIR / directory) - # load common repo meta information (metadata that's not language specific). if "metadata" in kwargs: - self._load_generic_metadata(kwargs["metadata"], t) + self._load_generic_metadata(kwargs["metadata"]) + t = templates.TemplateGroup(_TEMPLATES_DIR / directory, self.excludes) result = t.render(**kwargs) _tracked_paths.add(result) metadata.add_template_source( @@ -52,6 +53,11 @@ def py_library(self, **kwargs) -> Path: return self._generic_library("python_library", **kwargs) def node_library(self, **kwargs) -> Path: + # TODO: once we've migrated all Node.js repos to either having + # .repo-metadata.json, or excluding README.md, we can remove this. + if not os.path.exists("./.repo-metadata.json"): + self.excludes.append("README.md") + kwargs["metadata"] = node.read_metadata() kwargs["publish_token"] = node.get_publish_token(kwargs["metadata"]["name"]) return self._generic_library("node_library", **kwargs) @@ -65,7 +71,7 @@ def render(self, template_name: str, **kwargs) -> Path: # # loads additional meta information from .repo-metadata.json. # - def _load_generic_metadata(self, metadata, template_group): + def _load_generic_metadata(self, metadata): self._load_samples(metadata) self._load_partials(metadata) @@ -73,8 +79,6 @@ def _load_generic_metadata(self, metadata, template_group): if os.path.exists("./.repo-metadata.json"): with open("./.repo-metadata.json") as f: metadata["repo"] = json.load(f) - else: - template_group.excludes.append("README.md") # # walks samples directory and builds up samples data-structure: diff --git a/synthtool/sources/templates.py b/synthtool/sources/templates.py index 606b53d9e..e9ee5a0a5 100644 --- a/synthtool/sources/templates.py +++ b/synthtool/sources/templates.py @@ -69,10 +69,10 @@ def render(self, template_name: str, **kwargs) -> Path: class TemplateGroup: - def __init__(self, location: PathOrStr) -> None: + def __init__(self, location: PathOrStr, excludes: List[str] = []) -> None: self.env = _make_env(location) self.dir = tmp.tmpdir() - self.excludes = [] # type: List[str] + self.excludes = excludes def render(self, **kwargs) -> Path: for template_name in self.env.list_templates():