Skip to content

Commit 20a0cc0

Browse files
committed
nix: refactor Gradle deps to be more generic
Refactoring the derivation that fetches all the POMs, JARs, and AARs in order to make it more generic and easier to extend. The main change is adding `files` key in `deps.json` which contains a dict of all the files reletad to given package. This way we can more easily include other files that might be available for download, like AARs with ASC suffix, or `nodeps` JARs. This is also necessary for the React Native upgrade: #15203 Signed-off-by: Jakub Sokołowski <[email protected]>
1 parent ff3ba6c commit 20a0cc0

File tree

10 files changed

+8527
-7748
lines changed

10 files changed

+8527
-7748
lines changed

nix/deps/gradle/README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Generating scripts:
2424
- `get_deps.sh` - Calls Gradle to get all the dependencies of sub-projects.
2525
- `gradle_parser.awk` - An AWK script that parses above Gradle output.
2626
- `url2json.sh` - Converts the list of URLs into a format consumable by Nix.
27+
- `add_package.sh` - Allows for adding a missing package manually.
2728

2829
Finally we have the Nix derivation in `default.nix` which produces a derivation with all of the Gradle project dependencies:
2930
```
@@ -66,9 +67,14 @@ You can use the `add_package.sh` script to add missing Gradle dependencies:
6667
> make shell TARGET=gradle
6768
Configuring Nix shell for target 'gradle'...
6869
69-
[nix-shell:~/status-mobile]$ nix/deps/gradle/add_package.sh com.android.tools.build:gradle:3.4.0
70+
[nix-shell:~/work/status-mobile]$ nix/deps/gradle/add_package.sh com.android.tools.build:gradle:3.5.3
71+
Changes made:
72+
nix/deps/gradle/deps.urls | 24 ++++++++++++++++++++++++
73+
1 file changed, 24 insertions(+)
74+
7075
Regenerating Nix files...
71-
Found 548 direct dependencies...
72-
Successfully added: com.android.tools.build:gradle:3.4.0
76+
Successfully added: com.android.tools.build:gradle:3.5.3
77+
78+
NOTICE: Running 'make nix-update-gradle' in a new shell is recommended.
7379
```
74-
This may take a bit longer than normal `make nix-update-gradle` but should add the missing package.
80+
Keep in mind that the changes made by this script do not affect the already spawned shell.

nix/deps/gradle/add_package.sh

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env bash
22

3-
set -Eeu
3+
# This script allows for adding a package by providing a Maven full name.
4+
# Such name consists of 3 sections separated by the colon character.
5+
# Example: com.android.tools.build:gradle:3.5.3
6+
7+
set -Eeuo pipefail
48

59
if [[ $# -ne 1 ]]; then
610
echo "Usage: add_package.sh <package>" >&2
@@ -18,12 +22,15 @@ source "${GIT_ROOT}/scripts/colors.sh"
1822
echo "${1}" | go-maven-resolver >> nix/deps/gradle/deps.urls
1923

2024
# Remove duplicates and sort.
21-
sort -uo nix/deps/gradle/deps.urls nix/deps/gradle/deps.urls
25+
sort -uVo nix/deps/gradle/deps.urls nix/deps/gradle/deps.urls
26+
27+
echo -e "${GRN}Changes made:${RST}" >&2
28+
git diff --stat nix/deps/gradle/deps.urls
29+
echo
2230

2331
# Re-generate dependencies JSON.
2432
"${GIT_ROOT}/nix/deps/gradle/generate.sh" gen_deps_json
2533

26-
# Re-generate dependencies list.
27-
"${GIT_ROOT}/nix/deps/gradle/generate.sh" gen_deps_list
28-
2934
echo -e "${GRN}Successfully added:${RST} ${BLD}${1}${RST}" >&2
35+
echo
36+
echo -e "${YLW}NOTICE:${RST} Running '${BLD}make nix-update-gradle${RST}' in a new shell is recommended."

nix/deps/gradle/default.nix

+25-63
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,44 @@
11
{ stdenv, lib, pkgs, fetchurl, writeShellScriptBin }:
22

33
let
4-
inherit (lib)
5-
removeSuffix optionalString splitString concatMapStrings
6-
attrByPath attrValues last makeOverridable importJSON;
4+
inherit (lib) concatStrings concatMapStrings mapAttrsToList makeOverridable;
75

8-
inherit (pkgs) aapt2;
9-
10-
deps = importJSON ./deps.json;
11-
12-
# some .jar files have an `-aot` suffix that doesn't work for .pom files
13-
getPOM = jarUrl: "${removeSuffix "-aot" jarUrl}.pom";
6+
deps = lib.importJSON ./deps.json;
147

158
script = writeShellScriptBin "create-local-maven-repo" (''
169
mkdir -p $out
1710
cd $out
1811
'' +
19-
# TODO: Generalize this section to not repeat the same code.
20-
(concatMapStrings (dep:
21-
let
22-
url = "${dep.host}/${dep.path}";
23-
pom = {
24-
sha1 = attrByPath [ "pom" "sha1" ] "" dep;
25-
sha256 = attrByPath [ "pom" "sha256" ] "" dep;
26-
};
27-
pom-download = optionalString (pom.sha256 != "") (
28-
fetchurl { url = getPOM url; inherit (pom) sha256; }
29-
);
30-
jar = {
31-
sha1 = attrByPath [ "jar" "sha1" ] "" dep;
32-
sha256 = attrByPath [ "jar" "sha256" ] "" dep;
33-
};
34-
jar-download = optionalString (jar.sha256 != "") (
35-
fetchurl { url = "${url}.${dep.type}"; inherit (jar) sha256; }
36-
);
37-
nodeps = {
38-
sha1 = attrByPath [ "nodeps" "sha1" ] "" dep;
39-
sha256 = attrByPath [ "nodeps" "sha256" ] "" dep;
40-
};
41-
nodeps-download = optionalString (nodeps.sha256 != "") (
42-
fetchurl { url = "${url}-nodeps.jar"; inherit (nodeps) sha256; }
43-
);
44-
fileName = last (splitString "/" dep.path);
45-
directory = removeSuffix fileName dep.path;
46-
in
47-
''
48-
mkdir -p ${directory}
49-
50-
${optionalString (pom-download != "") ''
51-
ln -s "${pom-download}" "${getPOM dep.path}"
52-
''}
53-
${optionalString (pom.sha1 != "") ''
54-
echo "${pom.sha1}" > "${getPOM dep.path}.sha1"
55-
''}
56-
${optionalString (jar-download != "") ''
57-
ln -s "${jar-download}" "${dep.path}.${dep.type}"
58-
''}
59-
${optionalString (jar.sha1 != "") ''
60-
echo "${jar.sha1}" > "${dep.path}.${dep.type}.sha1"
61-
''}
62-
${optionalString (nodeps-download != "") ''
63-
ln -s "${nodeps-download}" "${dep.path}-nodeps.jar"
64-
''}
65-
${optionalString (nodeps.sha1 != "") ''
66-
echo "${nodeps.sha1}" > "${dep.path}.${dep.type}.sha1"
67-
''}
12+
# For each dependency in deps.json.
13+
(concatMapStrings (dep: concatStrings
14+
# And for each file in the 'files' dict of given dependency.
15+
(mapAttrsToList (filename: hashes: let
16+
# Download given file(POM, JAR, nodeps JAR, or AAR).
17+
download = fetchurl {
18+
url = "${dep.repo}/${dep.path}/${filename}";
19+
inherit (hashes) sha256;
20+
};
21+
# And symlink it in the correct folder along with SHA1.
22+
in ''
23+
mkdir -p "${dep.path}"
24+
ln -s "${download}" "${dep.path}/${filename}"
25+
echo "${hashes.sha1}" > "${dep.path}/${filename}.sha1"
6826
'')
69-
deps));
27+
dep.files)
28+
) deps)
29+
);
7030

7131
in makeOverridable stdenv.mkDerivation {
7232
name = "status-mobile-maven-deps";
73-
buildInputs = [ aapt2 ];
33+
34+
buildInputs = [ pkgs.aapt2 ];
35+
7436
phases = [ "buildPhase" "patchPhase" ];
7537
buildPhase = "${script}/bin/create-local-maven-repo";
76-
# Patched AAPT2
38+
# Replace AAPT2 package only with our patched version from overlay.
7739
patchPhase = ''
78-
aapt2_dir=$out/com/android/tools/build/aapt2/${aapt2.version}
40+
aapt2_dir=$out/com/android/tools/build/aapt2/${pkgs.aapt2.version}
7941
mkdir -p $aapt2_dir
80-
ln -sf ${aapt2}/* $aapt2_dir
42+
ln -sf ${pkgs.aapt2}/* $aapt2_dir
8143
'';
8244
}

0 commit comments

Comments
 (0)