Skip to content

Commit

Permalink
For Kotlin skip generating GleanBuildInfo.kt when requested (with `…
Browse files Browse the repository at this point in the history
…with_buildinfo=false`)

This defaults to generating the build info, but that can be skipped,
e.g. for libraries, where that build info is not used anyway (and might
just not exist)
  • Loading branch information
badboy committed May 28, 2021
1 parent bae988e commit a07ce32
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- For Kotlin only generate `GleanBuildInfo.kt` when explicitly requested (with `with_buildinfo=true`)

## 3.3.2 (2021-05-18)

- Fix another bug in the Swift code generation when generating extra keys ([#334](https://github.com/mozilla/glean_parser/pull/334))
Expand Down
25 changes: 15 additions & 10 deletions glean_parser/kotlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,30 +236,35 @@ def output_kotlin(
- `glean_namespace`: The package namespace of the glean library itself.
This is where glean objects will be imported from in the generated
code.
- `with_buildinfo`: If "true" a `GleanBuildInfo.kt` file is generated.
Otherwise generation of that file is skipped.
Defaults to "true".
"""
if options is None:
options = {}

namespace = options.get("namespace", "GleanMetrics")
glean_namespace = options.get("glean_namespace", "mozilla.components.service.glean")
namespace_package = namespace[: namespace.rfind(".")]
with_buildinfo = options.get("with_buildinfo", "true").lower() == "true"

# Write out the special "build info" object
template = util.get_jinja2_template(
"kotlin.buildinfo.jinja2",
)

# This filename needs to start with "Glean" so it can never clash with a
# metric category
with (output_dir / "GleanBuildInfo.kt").open("w", encoding="utf-8") as fd:
fd.write(
template.render(
namespace=namespace,
namespace_package=namespace_package,
glean_namespace=glean_namespace,
if with_buildinfo:
# This filename needs to start with "Glean" so it can never clash with a
# metric category
with (output_dir / "GleanBuildInfo.kt").open("w", encoding="utf-8") as fd:
fd.write(
template.render(
namespace=namespace,
namespace_package=namespace_package,
glean_namespace=glean_namespace,
)
)
)
fd.write("\n")
fd.write("\n")

template = util.get_jinja2_template(
"kotlin.jinja2",
Expand Down
36 changes: 36 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ def test_translate(tmpdir):
assert "package Foo" in content


def test_translate_no_buildinfo(tmpdir):
"""Test the 'translate' command."""
runner = CliRunner()
result = runner.invoke(
__main__.main,
[
"translate",
str(ROOT / "data" / "core.yaml"),
"-o",
str(tmpdir),
"-f",
"kotlin",
"-s",
"namespace=Foo",
"-s",
"with_buildinfo=false",
"--allow-reserved",
],
)
assert result.exit_code == 0
assert set(os.listdir(str(tmpdir))) == set(
[
"CorePing.kt",
"Telemetry.kt",
"Environment.kt",
"DottedCategory.kt",
"GleanInternalMetrics.kt",
]
)
for filename in os.listdir(str(tmpdir)):
path = Path(str(tmpdir)) / filename
with path.open(encoding="utf-8") as fd:
content = fd.read()
assert "package Foo" in content


def test_translate_errors(tmpdir):
"""Test the 'translate' command."""
runner = CliRunner()
Expand Down

0 comments on commit a07ce32

Please sign in to comment.