Skip to content

Commit

Permalink
Revert "build(nodejs): make default_version optional (#1368)" (#1371)
Browse files Browse the repository at this point in the history
This reverts commit 1f06210.
  • Loading branch information
bcoe authored Mar 17, 2022
1 parent 1f06210 commit 02193e4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 31 deletions.
8 changes: 4 additions & 4 deletions synthtool/gcp/templates/node_split_library/index.ts.j2
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
// ** All changes to this file may be overwritten. **

{% for version in versions %}import * as {{ version }} from './{{ version}}';{{ "\n" }}{% endfor %}
{% if default_version is defined %}{% for client in clients %}const {{ client }} = {{ default_version }}.{{ client }};
type {{ client }} = {{ default_version }}.{{ client }};{{ "\n" }}{% endfor %}{% endif %}
export {{ "{" }}{{ versions|join(', ')}}{% if clients|length > 0 %}, {{ clients|join(', ')}}{% endif %}{{ "}" }};
export default {{ "{" }}{{ versions|join(', ')}}{% if clients|length > 0 %}, {{ clients|join(', ')}}{% endif %}{{ "}" }};
{% 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};
31 changes: 14 additions & 17 deletions synthtool/languages/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,21 @@ def generate_index_ts(versions: List[str], default_version: str) -> None:
)
logger.error(err_msg)
raise AttributeError(err_msg)
if default_version and default_version not in versions:
if default_version not in versions:
err_msg = f"Version {versions} must contain default version {default_version}."
logger.error(err_msg)
raise AttributeError(err_msg)

# To make sure the output is always deterministic.
versions = sorted(versions)

# compose default version's index.ts file
clients = []
if default_version:
versioned_index_ts_path = Path("src") / default_version / "index.ts"
clients = extract_clients(versioned_index_ts_path)
if not clients:
err_msg = f"No client is exported in the default version's({default_version}) index.ts ."
logger.error(err_msg)
raise AttributeError(err_msg)
# compose default version's index.ts file path
versioned_index_ts_path = Path("src") / default_version / "index.ts"
clients = extract_clients(versioned_index_ts_path)
if not clients:
err_msg = f"No client is exported in the default version's({default_version}) index.ts ."
logger.error(err_msg)
raise AttributeError(err_msg)

# compose template directory
template_path = (
Expand Down Expand Up @@ -299,14 +297,14 @@ def owlbot_main(
)
staging = Path("owl-bot-staging")
s_copy = transforms.move
versions = []
if staging.is_dir():
if default_version is None:
logger.info("No default version found in .repo-metadata.json. Ok.")
elif staging.is_dir():
logger.info(f"Copying files from staging directory ${staging}.")
# Collect the subdirectories of the staging directory.
versions = [v.name for v in staging.iterdir() if v.is_dir()]
# Reorder the versions so the default version always comes last.
if default_version:
versions = [v for v in versions if v != default_version] + [default_version]
versions = [v for v in versions if v != default_version] + [default_version]
logger.info(f"Collected versions ${versions} from ${staging}")

# Copy each version directory into the root.
Expand All @@ -322,13 +320,12 @@ def owlbot_main(
src = Path("src")
versions = [v.name for v in src.iterdir() if v.is_dir()]
# Reorder the versions so the default version always comes last.
if default_version:
versions = [v for v in versions if v != default_version] + [default_version]
versions = [v for v in versions if v != default_version] + [default_version]
logger.info(f"Collected versions ${versions} from ${src}")

common_templates = gcp.CommonTemplates(template_path)
common_templates.excludes.extend(templates_excludes)
if len(versions) > 0:
if default_version:
templates = common_templates.node_library(
source_location="build/src",
versions=versions,
Expand Down
10 changes: 0 additions & 10 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,6 @@ def test_extract_multiple_clients():
assert clients[1] == "VideoIntelligenceServiceClient"


def test_generate_index_ts_no_default_version():
# use a non-nodejs template directory
with util.chdir(FIXTURES / "node_templates" / "index_samples"):
versions = ["v1beta1"]
node.generate_index_ts(versions, None)
text = open("./src/index.ts", "rt").read()
assert "export {v1beta1};" in text
assert "export default {v1beta1};" in text


def test_generate_index_ts():
# use a non-nodejs template directory
with util.chdir(FIXTURES / "node_templates" / "index_samples"):
Expand Down

0 comments on commit 02193e4

Please sign in to comment.