Skip to content

Commit 39ab21b

Browse files
committed
Support included builds for in update-verification-metadata
1 parent d004d73 commit 39ab21b

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

update-verification-metadata/default.nix

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
python3,
44
git,
55
writeShellApplication,
6-
gradle,
76
updateAction ? "dependencies",
87
cmd ? "gradle --refresh-dependencies --write-verification-metadata sha256 ${updateAction}",
98
verificationFile ? "gradle/verification-metadata.xml",
@@ -12,17 +11,34 @@
1211
writeShellApplication {
1312
name = "update-verification-metadata";
1413

15-
runtimeInputs = [python3 gradle git];
14+
runtimeInputs = [python3 git];
1615
text = ''
1716
verificationFile=''${1:-${verificationFile}}
1817
if [ ! -f "$verificationFile" ]
1918
then
20-
echo "Error: $verificationFile does not (yet) exist."
21-
exit 1
19+
echo "WARNING: $verificationFile does not (yet) exist." 1>&2
2220
fi
23-
echo "Removing all component entries from $verificationFile ..."
24-
python ${./update-verification-metadata.py} "$verificationFile" ${builtins.toString (builtins.map lib.escapeShellArg whitelist)}
25-
echo "Regenerating gradle verification data ..."
26-
${cmd}
21+
22+
update_metadata() {
23+
if [ -f "$verificationFile" ]; then
24+
echo "Removing all component entries from $build/$verificationFile for build $build"
25+
fi
26+
python ${./update-verification-metadata.py} "$verificationFile" ${builtins.toString (builtins.map lib.escapeShellArg whitelist)}
27+
echo "(Re)generating gradle verification data for build $build"
28+
${cmd}
29+
}
30+
31+
32+
echo "Locating included builds"
33+
includedBuilds=$(gradle -q --init-script ${./listIncludedBuildsRelative.init.gradle.kts} listIncludedBuilds)
34+
for build in $includedBuilds; do
35+
pushd "$build" > /dev/null
36+
update_metadata
37+
popd > /dev/null
38+
done
39+
40+
build=. update_metadata
41+
42+
python ${./merge-verification-metadata.py} "$verificationFile" "''${includedBuilds[@]}"
2743
'';
2844
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
gradle.rootProject {
2+
tasks.register("listIncludedBuilds") {
3+
doLast {
4+
val rootPath = rootDir.toPath().toAbsolutePath().normalize()
5+
gradle.includedBuilds.forEach { build ->
6+
val includedPath = build.projectDir.toPath().toAbsolutePath().normalize()
7+
println(rootPath.relativize(includedPath))
8+
}
9+
}
10+
}
11+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import sys
2+
import os
3+
import xml.etree.ElementTree as ET
4+
5+
main_file = sys.argv[1]
6+
included_dirs = sys.argv[2:]
7+
8+
namespaces = {'': "https://schema.gradle.org/dependency-verification"}
9+
ET.register_namespace("", namespaces[''])
10+
11+
def parse_components(path):
12+
if not os.path.isfile(path):
13+
return []
14+
tree = ET.parse(path)
15+
root = tree.getroot()
16+
components = root.find('components', namespaces)
17+
return components if components is not None else []
18+
19+
20+
# Load main file
21+
tree = ET.parse(main_file)
22+
root = tree.getroot()
23+
main_components = root.find('components', namespaces)
24+
if main_components is None:
25+
main_components = ET.SubElement(root, 'components')
26+
27+
# Track seen component triples and artifact names
28+
seen_components = {}
29+
def component_key(c): return (c.get('group'), c.get('name'), c.get('version'))
30+
def artifact_key(a): return a.get('name')
31+
32+
# Index existing components
33+
for component in list(main_components):
34+
key = component_key(component)
35+
seen_components[key] = component
36+
37+
# Merge each included build's verification-metadata.xml
38+
for build_dir in included_dirs:
39+
included_file = os.path.join(build_dir, main_file)
40+
included_components = parse_components(included_file)
41+
for component in included_components:
42+
key = component_key(component)
43+
if key in seen_components:
44+
existing = seen_components[key]
45+
existing_artifacts = {artifact_key(a): a for a in existing.findall('artifact', namespaces)}
46+
for artifact in component.findall('artifact', namespaces):
47+
a_key = artifact_key(artifact)
48+
if a_key not in existing_artifacts:
49+
existing.append(artifact)
50+
else:
51+
seen_components[key] = component
52+
main_components.append(component)
53+
54+
# Write back merged result
55+
tree.write(main_file, encoding='UTF-8', xml_declaration=True)

0 commit comments

Comments
 (0)