Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting :scope via maven coords too #646

Merged
merged 7 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion private/rules/java_export.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
70 changes: 38 additions & 32 deletions private/rules/pom_file.bzl
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
load(":has_maven_deps.bzl", "MavenInfo", "has_maven_deps")

_PLAIN_DEP = """ <dependency>
<groupId>{0}</groupId>
<artifactId>{1}</artifactId>
<version>{2}</version>
</dependency>"""
def _format_dep(unpacked):
shs96c marked this conversation as resolved.
Show resolved Hide resolved
return "".join([
" <dependency>\n",
" <groupId>%s</groupId>\n" % unpacked.groupId,
" <artifactId>%s</artifactId>\n" % unpacked.artifactId,
" <version>%s</version>\n" % unpacked.version,
(" <type>%s</type>\n" % unpacked.type) if unpacked.type and unpacked.type != "jar" else "",
(" <scope>%s</scope>\n" % unpacked.scope) if unpacked.scope and unpacked.scope != "compile" else "",
" </dependency>",
])

_TYPED_DEP = """ <dependency>
<groupId>{0}</groupId>
<artifactId>{1}</artifactId>
<version>{2}</version>
<type>{3}</type>
</dependency>"""
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.

def _explode_coordinates(coords):
"""Takes a maven coordinate and explodes it into a tuple of
(groupId, artifactId, version, type)
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")
if len(parts) == 4:
# Assume a buildr coordinate: groupId:artifactId:type:version
return (parts[0], parts[1], parts[3], parts[2])
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]))
shs96c marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -36,22 +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],
"{groupId}": coordinates.groupId,
"{artifactId}": coordinates.artifactId,
"{version}": coordinates.version,
"{type}": coordinates.type or "jar",
"{scope}": coordinates.scope or "compile",
}

deps = []
for dep in sorted(info.maven_deps.to_list()):
exploded = _explode_coordinates(dep)
if (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)
Expand All @@ -77,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.
""",
Expand Down