Skip to content

Commit

Permalink
Fix Optimized ANTLR dependency loading
Browse files Browse the repository at this point in the history
  • Loading branch information
marcohu committed Jan 5, 2020
1 parent 29f9bbe commit 54110ac
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
45 changes: 29 additions & 16 deletions antlr/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ v2 = [2, "2.7.7"]
def rules_antlr_dependencies(*versionsAndLanguages):
"""Loads the dependencies for the specified ANTLR releases.
You have to provide at least the version number of the ANTLR release you want to use. To
load the dependencies for languages besides Java, you have to indicate the languages as well.
```python
load("@rules_antlr//antlr:lang.bzl", "CPP", "PYTHON")
load("@rules_antlr//antlr:repositories.bzl", "rules_antlr_dependencies")
rules_antlr_dependencies("4.7.2", CPP, PYTHON)
```
Args:
*versionsAndLanguages: the ANTLR release versions to make available for the provided target languages.
"""
Expand Down Expand Up @@ -41,34 +51,37 @@ def rules_antlr_dependencies(*versionsAndLanguages):
languages = [JAVA]

for version in sorted(versions, key = _toString):
if (version == 4 or version == "4.7.2"):
if version == 4 or version == "4.7.2":
_antlr472_dependencies(languages)
elif (version == "4.7.1"):
elif version == "4.7.1":
_antlr471_dependencies(languages)
elif (version == 3 or version == "3.5.2"):
elif version == 3 or version == "3.5.2":
_antlr352_dependencies(languages)
elif (version == 2 or version == "2.7.7"):
elif version == 2 or version == "2.7.7":
_antlr277_dependencies(languages)
else:
fail("Missing ANTLR version", attr = "versionsAndLanguages")

def rules_antlr_optimized_dependencies(*versions):
def rules_antlr_optimized_dependencies(version):
"""Loads the dependencies for the "optimized" fork of ANTLR 4 maintained by Sam Harwell.
```python
load("@rules_antlr//antlr:repositories.bzl", "rules_antlr_optimized_dependencies")
rules_antlr_optimized_dependencies("4.7.2")
```
Args:
*versions: the ANTLR releases versions to make available.
version: the ANTLR release version to make available.
"""
if versions:
versions = sorted(versions)
for version in versions:
if (version == 4 or version == 472):
_antlr472_optimized_dependencies()
elif (version == 471):
_antlr471_optimized_dependencies()
else:
fail("Invalid ANTLR version provided: {0}. Currently supported are: {1}".format(version, v4), attr = "versions")
else:
if version == 4 or version == "4.7.2":
_antlr472_optimized_dependencies()
elif version == "4.7.1":
_antlr471_optimized_dependencies()
elif type(version) == "int" or str(version).isdigit():
fail('Integer version \'{}\' no longer valid. Use semantic version "{}" instead.'.format(version, ".".join(str(version).elems())), attr = "version")
else:
fail('Unsupported ANTLR version provided: "{0}". Currently supported are: {1}'.format(version, v4), attr = "version")

def _antlr472_dependencies(languages):
_antlr4_dependencies(
Expand Down
42 changes: 34 additions & 8 deletions src/it/java/org/antlr/bazel/RepositoriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void missingVersion() throws Exception
+ "rules_antlr_dependencies()";

TestWorkspace workspace = new TestWorkspace(true);
Path file = workspace.file("WORKSPACE", contents);
workspace.file("WORKSPACE", contents);
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
assertEquals(c.output(), 1, c.exitValue());
assertTrue(c.output().contains("attribute versionsAndLanguages: Missing ANTLR version"));
Expand All @@ -51,7 +51,7 @@ public void languageAndMissingVersion() throws Exception
+ "rules_antlr_dependencies(\"Java\")";

TestWorkspace workspace = new TestWorkspace(true);
Path file = workspace.file("WORKSPACE", contents);
workspace.file("WORKSPACE", contents);
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
assertEquals(c.output(), 1, c.exitValue());
assertTrue(c.output().contains("attribute versionsAndLanguages: Missing ANTLR version"));
Expand All @@ -69,10 +69,23 @@ public void unsupportedVersion() throws Exception
+ "rules_antlr_dependencies(\"4.0\")";

TestWorkspace workspace = new TestWorkspace(true);
Path file = workspace.file("WORKSPACE", contents);
workspace.file("WORKSPACE", contents);
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
assertEquals(c.output(), 1, c.exitValue());
assertTrue(c.output().contains("attribute versionsAndLanguages: Unsupported ANTLR version provided: \"4.0\"."));

contents = "workspace(name=\"examples\")\n"
+ "local_repository(\n"
+ " name = \"rules_antlr\",\n"
+ " path = \"../../../rules_antlr\",\n"
+ ")\n"
+ "load(\"@rules_antlr//antlr:repositories.bzl\", \"rules_antlr_optimized_dependencies\")\n"
+ "rules_antlr_optimized_dependencies(\"4.0\")";

workspace.file("WORKSPACE", contents);
c = new Command(workspace.root, "//antlr4/HelloWorld/...").build();
assertEquals(c.output(), 1, c.exitValue());
assertTrue(c.output().contains("attribute version: Unsupported ANTLR version provided: \"4.0\"."));
}

@Test
Expand All @@ -87,10 +100,23 @@ public void invalidVersion() throws Exception
+ "rules_antlr_dependencies(471)";

TestWorkspace workspace = new TestWorkspace(true);
Path file = workspace.file("WORKSPACE", contents);
workspace.file("WORKSPACE", contents);
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
assertEquals(c.output(), 1, c.exitValue());
assertTrue(c.output().contains("attribute versionsAndLanguages: Integer version '471' no longer valid. Use semantic version \"4.7.1\" instead."));

contents = "workspace(name=\"examples\")\n"
+ "local_repository(\n"
+ " name = \"rules_antlr\",\n"
+ " path = \"../../../rules_antlr\",\n"
+ ")\n"
+ "load(\"@rules_antlr//antlr:repositories.bzl\", \"rules_antlr_optimized_dependencies\")\n"
+ "rules_antlr_optimized_dependencies(471)";

workspace.file("WORKSPACE", contents);
c = new Command(workspace.root, "//antlr4/HelloWorld/...").build();
assertEquals(c.output(), 1, c.exitValue());
assertTrue(c.output().contains("attribute version: Integer version '471' no longer valid. Use semantic version \"4.7.1\" instead."));
}

@Test
Expand All @@ -105,7 +131,7 @@ public void invalidLanguage() throws Exception
+ "rules_antlr_dependencies(4, \"Haskell\")";

TestWorkspace workspace = new TestWorkspace(true);
Path file = workspace.file("WORKSPACE", contents);
workspace.file("WORKSPACE", contents);
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
assertEquals(c.output(), 1, c.exitValue());
assertTrue(c.output().contains("attribute versionsAndLanguages: Invalid language provided: \"Haskell\"."));
Expand All @@ -123,7 +149,7 @@ public void severalVersions() throws Exception
+ "rules_antlr_dependencies(\"4.7.1\", \"4.7.2\")";

TestWorkspace workspace = new TestWorkspace(true);
Path file = workspace.file("WORKSPACE", contents);
workspace.file("WORKSPACE", contents);
Command c = new Command(workspace.root, "//antlr2/Calc/...").build();
assertEquals(c.output(), 1, c.exitValue());
assertTrue(c.output().contains("attribute versionsAndLanguages: You can only load one version from ANTLR 4. You specified both \"4.7.1\" and \"4.7.2\"."));
Expand All @@ -141,7 +167,7 @@ public void missingLanguage() throws Exception
+ "rules_antlr_dependencies(\"2.7.7\")";

TestWorkspace workspace = new TestWorkspace(true);
Path file = workspace.file("WORKSPACE", contents);
workspace.file("WORKSPACE", contents);
Command c = new Command(workspace.root, "//antlr2/Cpp/...").build();
assertEquals(c.output(), 1, c.exitValue());
}
Expand All @@ -158,7 +184,7 @@ public void alwaysLoadJavaDependencies() throws Exception
+ "rules_antlr_dependencies(\"2.7.7\", \"Cpp\")";

TestWorkspace workspace = new TestWorkspace(true);
Path file = workspace.file("WORKSPACE", contents);
workspace.file("WORKSPACE", contents);
Command c = new Command(workspace.root, "//antlr2/Cpp/...").build();
assertEquals(c.output(), 0, c.exitValue());
}
Expand Down

0 comments on commit 54110ac

Please sign in to comment.