From e4c0b81f926faa91a14dff2b736939c257983bfe Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Tue, 4 Jan 2022 13:38:46 +0100 Subject: [PATCH 1/7] Allow setting :scope via maven coords too Default scope is "compile" but it could be useful to depend on other scopes like "tests" --- private/rules/pom_file.bzl | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index b5492750e..42ee26ba8 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl @@ -13,19 +13,30 @@ _TYPED_DEP = """ {3} """ +_SCOPED_DEP = """ + {0} + {1} + {2} + {3} + {4} + """ + def _explode_coordinates(coords): """Takes a maven coordinate and explodes it into a tuple of - (groupId, artifactId, version, type) + (groupId, artifactId, version, type, scope) """ if not coords: return None parts = coords.split(":") if len(parts) == 3: - return (parts[0], parts[1], parts[2], "jar") + return (parts[0], parts[1], parts[2], "jar", "compile") if len(parts) == 4: # Assume a buildr coordinate: groupId:artifactId:type:version - return (parts[0], parts[1], parts[3], parts[2]) + return (parts[0], parts[1], parts[3], parts[2], "compile") + if len(parts) == 5: + # Assume a buildr coordinate: groupId:artifactId:type:scope:version + return (parts[0], parts[1], parts[4], parts[2], parts[3]) fail("Unparsed: %s" % coords) @@ -42,12 +53,15 @@ def _pom_file_impl(ctx): "{artifactId}": coordinates[1], "{version}": coordinates[2], "{type}": coordinates[3], + "{scope}": coordinates[4], } deps = [] for dep in sorted(info.maven_deps.to_list()): exploded = _explode_coordinates(dep) - if (exploded[3] == "jar"): + if (exploded[4] != "compile"): + template = _SCOPED_DEP + elif (exploded[3] == "jar"): template = _PLAIN_DEP else: template = _TYPED_DEP From f0c7c0a365a5116dda88793274cced8e49d85262 Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Wed, 5 Jan 2022 17:18:37 +0100 Subject: [PATCH 2/7] Replace tuple with struct --- private/rules/pom_file.bzl | 81 +++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index 42ee26ba8..b967c7dd6 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl @@ -1,44 +1,41 @@ load(":has_maven_deps.bzl", "MavenInfo", "has_maven_deps") -_PLAIN_DEP = """ - {0} - {1} - {2} - """ +def _format_dep(unpacked): + return "".join([ + " \n", + " %s\n" % unpacked.groupId, + " %s\n" % unpacked.artifactId, + " %s\n" % unpacked.version, +(" %s\n" % unpacked.type) if unpacked.type and unpacked.type != "jar" else "", +(" %s\n" % unpacked.scope) if unpacked.scope and unpacked.scope != "compile" else "", + " ", + ]) -_TYPED_DEP = """ - {0} - {1} - {2} - {3} - """ +def _unpack_coordinates(coords): + """Takes a maven coordinate and unpacks it into a struct with fields + groupId, artifactId, version, type_, scope + where type and scope are optional. -_SCOPED_DEP = """ - {0} - {1} - {2} - {3} - {4} - """ - -def _explode_coordinates(coords): - """Takes a maven coordinate and explodes it into a tuple of - (groupId, artifactId, version, type, scope) + Assumes following maven coordinate syntax: + groupId:artifactId[:type[:scope]]:version """ if not coords: return None parts = coords.split(":") - if len(parts) == 3: - return (parts[0], parts[1], parts[2], "jar", "compile") - if len(parts) == 4: - # Assume a buildr coordinate: groupId:artifactId:type:version - return (parts[0], parts[1], parts[3], parts[2], "compile") - if len(parts) == 5: - # Assume a buildr coordinate: groupId:artifactId:type:scope:version - return (parts[0], parts[1], parts[4], parts[2], parts[3]) + nparts = len(parts) + if nparts < 3 or nparts > 5: + fail("Unparsed: %s" % coords) - fail("Unparsed: %s" % coords) + version = parts[-1] + parts = dict(enumerate(parts[:-1])) + return struct( + groupId = parts.get(0), + artifactId = parts.get(1), + type_ = parts.get(2), + scope = parts.get(3), + version = version, + ) def _pom_file_impl(ctx): # Ensure the target has coordinates @@ -47,25 +44,19 @@ def _pom_file_impl(ctx): info = ctx.attr.target[MavenInfo] - coordinates = _explode_coordinates(info.coordinates) + coordinates = _unpack_coordinates(info.coordinates) substitutions = { - "{groupId}": coordinates[0], - "{artifactId}": coordinates[1], - "{version}": coordinates[2], - "{type}": coordinates[3], - "{scope}": coordinates[4], + "{groupId}": coordinates.groupId, + "{artifactId}": coordinates.artifactId, + "{version}": coordinates.version, + "{type}": coordinates.type_, + "{scope}": coordinates.scope, } deps = [] for dep in sorted(info.maven_deps.to_list()): - exploded = _explode_coordinates(dep) - if (exploded[4] != "compile"): - template = _SCOPED_DEP - elif (exploded[3] == "jar"): - template = _PLAIN_DEP - else: - template = _TYPED_DEP - deps.append(template.format(*exploded)) + unpacked = _unpack_coordinates(dep) + deps.append(_format_dep(unpacked)) substitutions.update({"{dependencies}": "\n".join(deps)}) out = ctx.actions.declare_file("%s.xml" % ctx.label.name) From 8521164ed79da9b23b7e2aaa02d529d0b13d72bc Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Mon, 17 Jan 2022 18:51:12 +0100 Subject: [PATCH 3/7] backtick-quote fields for docs --- private/rules/pom_file.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index b967c7dd6..0638250f8 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl @@ -13,7 +13,7 @@ def _format_dep(unpacked): def _unpack_coordinates(coords): """Takes a maven coordinate and unpacks it into a struct with fields - groupId, artifactId, version, type_, scope + `groupId`, `artifactId`, `version`, `type_`, `scope` where type and scope are optional. Assumes following maven coordinate syntax: From 37007b3e72fb67c420427fa87a260fc334cab4f1 Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Mon, 17 Jan 2022 18:51:26 +0100 Subject: [PATCH 4/7] type_ -> type in the struct --- private/rules/pom_file.bzl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index 0638250f8..24e8b55c3 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl @@ -13,7 +13,7 @@ def _format_dep(unpacked): def _unpack_coordinates(coords): """Takes a maven coordinate and unpacks it into a struct with fields - `groupId`, `artifactId`, `version`, `type_`, `scope` + `groupId`, `artifactId`, `version`, `type`, `scope` where type and scope are optional. Assumes following maven coordinate syntax: @@ -32,7 +32,7 @@ def _unpack_coordinates(coords): return struct( groupId = parts.get(0), artifactId = parts.get(1), - type_ = parts.get(2), + type = parts.get(2), scope = parts.get(3), version = version, ) @@ -49,7 +49,7 @@ def _pom_file_impl(ctx): "{groupId}": coordinates.groupId, "{artifactId}": coordinates.artifactId, "{version}": coordinates.version, - "{type}": coordinates.type_, + "{type}": coordinates.type, "{scope}": coordinates.scope, } From 19bd64b477cc8b605e61b50396ba53ead0015270 Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Mon, 17 Jan 2022 18:52:54 +0100 Subject: [PATCH 5/7] run "buildifier -r lint fix" for private/rules/pom_file.bzl --- private/rules/pom_file.bzl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index 24e8b55c3..1eb2fdbcf 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl @@ -2,13 +2,13 @@ load(":has_maven_deps.bzl", "MavenInfo", "has_maven_deps") def _format_dep(unpacked): return "".join([ - " \n", - " %s\n" % unpacked.groupId, - " %s\n" % unpacked.artifactId, - " %s\n" % unpacked.version, -(" %s\n" % unpacked.type) if unpacked.type and unpacked.type != "jar" else "", -(" %s\n" % unpacked.scope) if unpacked.scope and unpacked.scope != "compile" else "", - " ", + " \n", + " %s\n" % unpacked.groupId, + " %s\n" % unpacked.artifactId, + " %s\n" % unpacked.version, + (" %s\n" % unpacked.type) if unpacked.type and unpacked.type != "jar" else "", + (" %s\n" % unpacked.scope) if unpacked.scope and unpacked.scope != "compile" else "", + " ", ]) def _unpack_coordinates(coords): From dcafc4822dcd8d9fa373a18e8da3b0cb20a0db64 Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Tue, 18 Jan 2022 12:44:48 +0100 Subject: [PATCH 6/7] Fix substitutions fallback --- private/rules/pom_file.bzl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index 1eb2fdbcf..d6d1fd984 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl @@ -49,8 +49,8 @@ def _pom_file_impl(ctx): "{groupId}": coordinates.groupId, "{artifactId}": coordinates.artifactId, "{version}": coordinates.version, - "{type}": coordinates.type, - "{scope}": coordinates.scope, + "{type}": coordinates.type or "jar", + "{scope}": coordinates.scope or "compile", } deps = [] From eba04dd16120da128f2666cb0dbde73a5663d166 Mon Sep 17 00:00:00 2001 From: Dmitry Ivankov Date: Tue, 18 Jan 2022 12:48:36 +0100 Subject: [PATCH 7/7] Update docs --- docs/api.md | 3 ++- private/rules/java_export.bzl | 3 ++- private/rules/pom_file.bzl | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/api.md b/docs/api.md index 2c4512e86..3d20075f0 100644 --- a/docs/api.md +++ b/docs/api.md @@ -72,7 +72,8 @@ the template file: * `{groupId}`: Replaced with the maven coordinates group ID. * `{artifactId}`: Replaced with the maven coordinates artifact ID. * `{version}`: Replaced by the maven coordinates version. - * `{type}`: Replaced by the maven coordintes type, if present (defaults to "jar") + * `{type}`: Replaced by the maven coordinates type, if present (defaults to "jar") + * `{scope}`: Replaced by the maven coordinates scope, if present (defaults to "compile") * `{dependencies}`: Replaced by a list of maven dependencies directly relied upon by java_library targets within the artifact. diff --git a/private/rules/java_export.bzl b/private/rules/java_export.bzl index 2dac89c03..6b797afc4 100644 --- a/private/rules/java_export.bzl +++ b/private/rules/java_export.bzl @@ -31,7 +31,8 @@ def java_export( * `{groupId}`: Replaced with the maven coordinates group ID. * `{artifactId}`: Replaced with the maven coordinates artifact ID. * `{version}`: Replaced by the maven coordinates version. - * `{type}`: Replaced by the maven coordintes type, if present (defaults to "jar") + * `{type}`: Replaced by the maven coordinates type, if present (defaults to "jar") + * `{scope}`: Replaced by the maven coordinates type, if present (defaults to "compile") * `{dependencies}`: Replaced by a list of maven dependencies directly relied upon by java_library targets within the artifact. diff --git a/private/rules/pom_file.bzl b/private/rules/pom_file.bzl index d6d1fd984..7c7300248 100644 --- a/private/rules/pom_file.bzl +++ b/private/rules/pom_file.bzl @@ -82,7 +82,8 @@ The following substitutions are performed on the template file: {groupId}: Replaced with the maven coordinates group ID. {artifactId}: Replaced with the maven coordinates artifact ID. {version}: Replaced by the maven coordinates version. - {type}: Replaced by the maven coordintes type, if present (defaults to "jar") + {type}: Replaced by the maven coordinates type, if present (defaults to "jar") + {scope}: Replaced by the maven coordinates type, if present (defaults to "compile") {dependencies}: Replaced by a list of maven dependencies directly relied upon by java_library targets within the artifact. """,