Skip to content

Commit

Permalink
feat(node-mono-repo): make years in node mono-repo templates variable (
Browse files Browse the repository at this point in the history
…#1512)

* feat(node-mono-repo): make mono-repo templates with years variable
  • Loading branch information
sofisl authored Jul 29, 2022
1 parent ccccac3 commit 653c514
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
2 changes: 2 additions & 0 deletions synthtool/gcp/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pathlib import Path
from typing import Dict, List, Optional
import jinja2
from datetime import date

from synthtool import shell, _tracked_paths
from synthtool.gcp import partials
Expand Down Expand Up @@ -349,6 +350,7 @@ def node_mono_repo_library(self, relative_dir, **kwargs) -> Path:
versions=kwargs["versions"],
default_version=kwargs["default_version"],
relative_dir=relative_dir,
year=str(date.today().year),
)

return self._generic_library("node_mono_repo_library", **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion synthtool/gcp/templates/node_mono_repo_library/.mocharc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Google LLC
// Copyright {{ metadata['year'] }} Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Google LLC
// Copyright {{ metadata['year'] }} Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
25 changes: 25 additions & 0 deletions synthtool/gcp/templates/node_mono_repo_split_library/index.ts.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright {{year}} 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.
//
// ** This file is automatically generated by synthtool. **
// ** https://github.com/googleapis/synthtool **
// ** All changes to this file may be overwritten. **

{% for version in versions %}import * as {{ version }} from './{{ version}}';{{ "\n" }}{% endfor %}
{% for client in clients %}const {{ client }} = {{ default_version }}.{{ client }};
type {{ client }} = {{ default_version }}.{{ client }};{{ "\n" }}{% endfor %}
export {{ "{" }}{{ versions|join(', ')}}, {{ clients|join(', ')}}{{ "}" }};
export default {{ "{" }}{{ versions|join(', ')}}, {{ clients|join(', ')}}{{ "}" }};
import * as protos from '../protos/protos';
export {protos};
11 changes: 8 additions & 3 deletions synthtool/languages/node_mono_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import logging
import shutil
from synthtool.languages import common
from datetime import date


_REQUIRED_FIELDS = ["name", "repository", "engines"]
Expand Down Expand Up @@ -90,6 +91,7 @@ def template_metadata(relative_dir: str) -> Dict[str, Any]:
all_samples,
)
)
metadata["year"] = date.today().year
return metadata


Expand All @@ -108,7 +110,7 @@ def extract_clients(filePath: Path) -> List[str]:


def generate_index_ts(
versions: List[str], default_version: str, relative_dir: str
versions: List[str], default_version: str, relative_dir: str, year: str
) -> None:
"""
generate src/index.ts to export the client name and versions in the client library.
Expand Down Expand Up @@ -146,15 +148,18 @@ def generate_index_ts(

# compose template directory
template_path = (
Path(__file__).parent.parent / "gcp" / "templates" / "node_split_library"
Path(__file__).parent.parent
/ "gcp"
/ "templates"
/ "node_mono_repo_split_library"
)
template_loader = FileSystemLoader(searchpath=str(template_path))
template_env = Environment(loader=template_loader, keep_trailing_newline=True)
TEMPLATE_FILE = "index.ts.j2"
index_template = template_env.get_template(TEMPLATE_FILE)
# render index.ts content
output_text = index_template.render(
versions=versions, default_version=default_version, clients=clients
versions=versions, default_version=default_version, clients=clients, year=year
)
with open(Path(relative_dir, "src/index.ts").resolve(), "w") as fh:
fh.write(output_text)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_node_mono_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from pathlib import Path
from unittest import TestCase
from unittest.mock import patch
from datetime import date

import pytest

Expand Down Expand Up @@ -127,6 +128,7 @@ def test_generate_index_ts():
["v1", "v1beta1"],
"v1",
relative_dir=(FIXTURES / "node_templates" / "index_samples"),
year="2020",
)
generated_index_path = pathlib.Path(
FIXTURES / "node_templates" / "index_samples" / "src" / "index.ts"
Expand All @@ -142,7 +144,10 @@ def test_generate_index_ts_empty_versions():
with util.chdir(FIXTURES / "node_templates" / "index_samples"):
with pytest.raises(AttributeError) as err:
node_mono_repo.generate_index_ts(
[], "v1", relative_dir=(FIXTURES / "node_templates" / "index_samples")
[],
"v1",
relative_dir=(FIXTURES / "node_templates" / "index_samples",),
year=date.today().year,
)
assert "can't be empty" in err.args

Expand All @@ -158,6 +163,7 @@ def test_generate_index_ts_invalid_default_version():
versions,
default_version,
relative_dir=(FIXTURES / "node_templates" / "index_samples"),
year=date.today().year,
)
assert f"must contain default version {default_version}" in err.args

Expand All @@ -173,6 +179,7 @@ def test_generate_index_ts_no_clients():
versions,
default_version,
relative_dir=(FIXTURES / "node_templates" / "index_samples"),
year=date.today().year,
)
assert (
f"No client is exported in the default version's({default_version}) index.ts ."
Expand Down

0 comments on commit 653c514

Please sign in to comment.