Skip to content

Commit 88bed92

Browse files
committed
Use gradle module metadata when appropriate
1 parent a1f6989 commit 88bed92

File tree

4 files changed

+66
-42
lines changed

4 files changed

+66
-42
lines changed

buildGradleApplication/mkM2Repository.nix

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,38 @@
2323
"python ${./parse.py} ${filteredSrc}/${verificationFile} ${builtins.toString (builtins.map lib.escapeShellArg repositories)}> $out"
2424
))
2525
);
26-
mkDep = depSpec: {
27-
inherit (depSpec) url_prefixes path name hash component;
26+
mkDep = depSpec: let
27+
# Gradle module metadata may specify a different url suffix than the artifact name
28+
urlSuffixFallback = depSpec.name;
29+
urlSuffix =
30+
if (depSpec.module == null)
31+
then urlSuffixFallback
32+
else let
33+
module = fetchArtifact {
34+
urls = lib.map (prefix: "${prefix}/${depSpec.module.name}") depSpec.url_prefixes;
35+
inherit (depSpec.module) hash name;
36+
};
37+
moduleMetadata = builtins.fromJSON (builtins.readFile module);
38+
variantFiles = lib.flatten (lib.map (variant: variant.files or []) (moduleMetadata.variants or []));
39+
matchingVariantFiles =
40+
builtins.filter (
41+
file: (file.${depSpec.hash_algo} or null) == depSpec.hash_value && file.name == depSpec.name
42+
)
43+
variantFiles;
44+
firstMatchingVariantFile = builtins.head matchingVariantFiles;
45+
in
46+
if matchingVariantFiles == []
47+
then urlSuffixFallback
48+
else if builtins.all (f: f.url == firstMatchingVariantFile.url) matchingVariantFiles
49+
then firstMatchingVariantFile.url
50+
else throw "Found multiple matching urls with name ${depSpec.name} and hash ${depSpec.hash} in ${module}: ${builtins.toString (lib.map (f: f.url) matchingVariantFiles)}";
51+
urls = lib.map (prefix: "${prefix}/${urlSuffix}") depSpec.url_prefixes;
52+
in {
53+
inherit urls urlSuffix;
54+
inherit (depSpec) path name hash component;
2855
jar = fetchArtifact {
29-
inherit (depSpec) url_prefixes hash name hash_algo hash_value path;
30-
module = if (depSpec.module_name == null) then null else fetchArtifact {
31-
inherit (depSpec) url_prefixes hash_algo hash_value path;
32-
hash = depSpec.module_hash;
33-
name = depSpec.module_name;
34-
};
56+
inherit urls;
57+
inherit (depSpec) hash name;
3558
};
3659
};
3760
dependencies = builtins.map (depSpec: mkDep depSpec) depSpecs;
@@ -42,6 +65,6 @@
4265
{src = filteredSrc;}
4366
(
4467
"mkdir $out"
45-
+ lib.concatMapStringsSep "\n" (dep: "mkdir -p $out/${dep.path}\nln -s ${builtins.toString dep.jar} $out/${dep.path}/${dep.name}") dependencies
68+
+ lib.concatMapStringsSep "\n" (dep: "mkdir -p $out/${dep.path}\nln -s ${builtins.toString dep.jar} $out/${dep.path}/${dep.urlSuffix}") dependencies
4669
);
4770
in {inherit dependencies m2Repository;}

buildGradleApplication/parse.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ class Artifact:
1515
name: str
1616
hash: object
1717
component: object
18-
module_name: str
19-
module_hash: str
18+
module: object
19+
20+
@dataclass
21+
class Module:
22+
name: str
23+
hash: object
24+
2025

2126
@dataclass
2227
class Hash:
@@ -37,8 +42,12 @@ def main():
3742
"url_prefixes": [f"{maven_repo}/{path}" for maven_repo in maven_repos],
3843
"path": path,
3944
"name": artifact.name,
40-
"module_name": artifact.module_name,
41-
"module_hash": artifact.module_hash,
45+
"module": {
46+
"name": artifact.module.name,
47+
"hash": toSri(artifact.module.hash.algo, artifact.module.hash.value),
48+
"hash_algo": artifact.module.hash.algo,
49+
"hash_value": artifact.module.hash.value,
50+
} if artifact.module is not None else None,
4251
"component": {
4352
"group": artifact.component.group,
4453
"name": artifact.component.name,
@@ -71,9 +80,8 @@ def parse(xml_file):
7180
name = component_elem.get("name")
7281
version = component_elem.get("version")
7382
component_obj = Component(group=group, name=name, version=version)
74-
module_name = None
75-
module_hash = None
7683

84+
component_artifacts = []
7785
for artifact_elem in component_elem.findall("default:artifact", namespaces):
7886
artifact_name = artifact_elem.get("name")
7987
hash_obj=None
@@ -83,13 +91,22 @@ def parse(xml_file):
8391
value = elem.get("value")
8492
hash_obj = Hash(algo=algo, value=value)
8593

86-
artifact_obj = Artifact(name=artifact_name, hash=hash_obj, component=component_obj, module_name=module_name, module_hash=module_hash)
87-
artifacts.append(artifact_obj)
88-
89-
if artifact_name.endswith(".module"):
90-
module_name = artifact_name
91-
module_hash = toSri(hash_obj.algo, hash_obj.value)
92-
94+
artifact_obj = Artifact(name=artifact_name, hash=hash_obj, component=component_obj, module=None)
95+
component_artifacts.append(artifact_obj)
96+
97+
# keep reference to Gradle module metadata if it exist
98+
module_name = f"{name}-{version}.module"
99+
module_artifact = next(
100+
(artifact for artifact in component_artifacts if artifact.name == module_name),
101+
None,
102+
)
103+
if module_artifact is not None:
104+
module = Module(name=module_artifact.name, hash=module_artifact.hash)
105+
for artifact in component_artifacts:
106+
if artifact is not module_artifact:
107+
artifact.module = module
108+
109+
artifacts.extend(component_artifacts)
93110
return artifacts
94111

95112

fetchArtefact/builder.bash

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,10 @@ check_hash() {
2929
# expected variables to be set:
3030
name="${name:?}"
3131
out="${out:?}"
32-
url_prefixes="${url_prefixes:?}"
32+
urls="${urls:?}"
3333
hash="${hash:?}"
3434

35-
url_suffix=$name
36-
if [[ -n $module ]]; then
37-
url_suffix="$(< $module $jq -r '.variants | map(.files) | flatten | map(select(.'$hash_algo' == "'$hash_value'")) | (. + [{ url: "'$name'"}])[0].url')"
38-
fi
39-
40-
for url_prefix in $url_prefixes; do
41-
url="$url_prefix/$url_suffix"
35+
for url in $urls; do
4236
echo "Downloading $name from $url"
4337

4438
if "${curl[@]}" --retry 0 --connect-timeout "${NIX_CONNECT_TIMEOUT:-15}" \

fetchArtefact/default.nix

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,13 @@
33
curl,
44
nix,
55
cacert,
6-
jq,
7-
fetchurl
86
}: {
97
# A list of URLs specifying alternative download locations. They are tried in order.
10-
url_prefixes,
8+
urls,
119
# SRI hash.
1210
hash,
1311
# Name of the file.
1412
name,
15-
# path
16-
path,
17-
# hash in undecoded form
18-
hash_algo,
19-
hash_value,
20-
# module json file that can contain content with url/name mappings
21-
module ? null,
2213
}:
2314
stdenvNoCC.mkDerivation {
2415
inherit name hash;
@@ -28,8 +19,7 @@ stdenvNoCC.mkDerivation {
2819
builder = ./builder.bash;
2920
nativeBuildInputs = [curl nix];
3021
SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt";
31-
inherit url_prefixes module hash_algo hash_value;
32-
jq = "${jq}/bin/jq";
22+
inherit urls;
3323

3424
# Doing the download on a remote machine just duplicates network
3525
# traffic, so don't do that

0 commit comments

Comments
 (0)