Skip to content

Commit 83f622d

Browse files
committed
nix: make fetching Gradle dependencies more generic
Before what we did was essentially guess what files might exist for any given package. This approach mostly works, but not entirely. This is especially problematic when dealing with weird edge case packages like `react-native`, which you can read about here: react-native-community/discussions-and-proposals#508 https://github.com/react-native-community/discussions-and-proposals/blob/4a06fc64/proposals/0508-out-of-npm-artifacts.md#the-react-native-android-archive In order to avoid as much the guessing aspect of fetching Gradle dependencies we are using both HTML listsings of files and `artifact-metadata.json` files that exist for more recent packages. This way we can avoid having to add special edge cases that have been found out when working on React Native 72 upgrade in: #17062 Signed-off-by: Jakub Sokołowski <[email protected]>
1 parent 6809311 commit 83f622d

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

nix/deps/gradle/url2json.sh

+52-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ declare -a REPOS=(
1414
"https://jitpack.io"
1515
)
1616

17+
# These files are not necessary for the build process.
18+
FILENAMES_BLACKLIST='-(javadoc|runtime|gwt|headers|sources|src|tests|adapters|modular|site|bin)\.'
19+
FILETYPES_BLACKLIST='(pom|json|zip|module|xml|md5|sha1|sha256|sha512)$'
20+
SIGNATURE_BLACKLIST='(pom|jar|json|zip|module|xml|md5|asc).asc$'
21+
1722
function nix_prefetch() {
1823
nix store prefetch-file --json "${1}" 2>/dev/null
1924
}
@@ -36,7 +41,49 @@ function match_repo_url() {
3641

3742
function pom_has_nodeps_jar() {
3843
grep '<shadedClassifierName>nodeps</shadedClassifierName>' "${1}" \
39-
>/dev/null 2>&1
44+
>/dev/null 2>&1
45+
}
46+
47+
function guess_pkg_files() {
48+
# Some deps are just POMs, in which case there is no JAR to fetch.
49+
[[ "${OBJ_TYPE}" == "" ]] && echo "${PKG_NAME}.jar"
50+
[[ "${OBJ_TYPE}" == "jar" ]] && echo "${PKG_NAME}.jar"
51+
[[ "${OBJ_TYPE}" == "bundle" ]] && echo "${PKG_NAME}.jar"
52+
[[ "${OBJ_TYPE}" =~ aar* ]] && echo "${PKG_NAME}.aar"
53+
[[ "${OBJ_TYPE}" == "aar.asc" ]] && echo "${PKG_NAME}.${OBJ_TYPE}"
54+
pom_has_nodeps_jar "${POM_PATH}" && echo "${PKG_NAME}-nodeps.jar"
55+
}
56+
57+
function get_pkg_files() {
58+
REPO_URL="${1}"
59+
PKG_PATH="${2}"
60+
PKG_NAME="${3}"
61+
# Google Maven repo doesn't have normal HTML directory listing.
62+
if [[ "${REPO_URL}" == "https://dl.google.com/dl/android/maven2" ]]; then
63+
FOUND=$(curl --fail -s "${REPO_URL}/${PKG_PATH}/artifact-metadata.json")
64+
# Some older packages do not have artifacts-metadata.json.
65+
if [[ "$?" -eq 0 ]]; then
66+
FOUND=$(echo "${FOUND}" | jq -r '.artifacts[].name')
67+
else
68+
FOUND=''
69+
fi
70+
else
71+
FOUND=$(
72+
curl -s "${REPO_URL}/${PKG_PATH}/" \
73+
| htmlq a -a href \
74+
| grep -e "^${PKG_NAME}"
75+
)
76+
fi
77+
if [[ "${FOUND}" == '' ]]; then
78+
guess_pkg_files
79+
else
80+
# Filter out files we don't actually need for builds.
81+
echo "${FOUND}" \
82+
| grep -v -E \
83+
-e "${FILENAMES_BLACKLIST}" \
84+
-e "${FILETYPES_BLACKLIST}" \
85+
-e "${SIGNATURE_BLACKLIST}"
86+
fi
4087
}
4188

4289
function fetch_and_template_file() {
@@ -58,10 +105,6 @@ function fetch_and_template_file() {
58105
}"
59106
}
60107

61-
function fetch_and_template_file_no_fail() {
62-
fetch_and_template_file "${1}" 2>/dev/null || true
63-
}
64-
65108
if [[ -z "${1}" ]]; then
66109
echo "Required POM URL argument not given!" >&2
67110
exit 1
@@ -77,7 +120,7 @@ echo -en "${CLR} - Nix entry for: ${1##*/}\r" >&2
77120
REPO_URL=$(match_repo_url "${PKG_URL_NO_EXT}")
78121

79122
if [[ -z "${REPO_URL}" ]]; then
80-
echo " ! Repo URL not found: %s" "${REPO_URL}" >&2
123+
echo " ! Repo URL not found for: ${POM_URL}" >&2
81124
exit 1
82125
fi
83126
# Get the relative path without full URL
@@ -114,12 +157,8 @@ echo -ne "
114157
\"sha256\": \"${POM_SHA256}\"
115158
}"
116159

117-
# Some deps are just POMs, in which case there is no JAR to fetch.
118-
[[ "${OBJ_TYPE}" == "" ]] && fetch_and_template_file_no_fail "${PKG_NAME}.jar"
119-
[[ "${OBJ_TYPE}" == "jar" ]] && fetch_and_template_file "${PKG_NAME}.jar"
120-
[[ "${OBJ_TYPE}" == "bundle" ]] && fetch_and_template_file "${PKG_NAME}.jar"
121-
[[ "${OBJ_TYPE}" =~ aar* ]] && fetch_and_template_file "${PKG_NAME}.aar"
122-
[[ "${OBJ_TYPE}" == "aar.asc" ]] && fetch_and_template_file "${PKG_NAME}.${OBJ_TYPE}"
123-
pom_has_nodeps_jar "${POM_PATH}" && fetch_and_template_file "${PKG_NAME}-nodeps.jar"
160+
for FILE in $(get_pkg_files "${REPO_URL}" "${PKG_PATH}" "${PKG_NAME}"); do
161+
fetch_and_template_file "${FILE}"
162+
done
124163

125164
echo -e '\n }\n },'

nix/shells.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ let
3838

3939
# for running gradle by hand
4040
gradle = mkShell {
41-
buildInputs = with pkgs; [ gradle maven goMavenResolver ];
41+
buildInputs = with pkgs; [ gradle maven goMavenResolver htmlq ];
4242
shellHook = ''
4343
export STATUS_GO_ANDROID_LIBDIR="DUMMY"
4444
export STATUS_NIX_MAVEN_REPO="${pkgs.deps.gradle}"

0 commit comments

Comments
 (0)