Skip to content

Commit ad20022

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 2e63ee8 commit ad20022

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

nix/deps/gradle/url2json.sh

+50-13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ 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|asc|xml|md5|sha1|sha256|sha512)$'
20+
1721
function nix_prefetch() {
1822
nix store prefetch-file --json "${1}" 2>/dev/null
1923
}
@@ -36,7 +40,48 @@ function match_repo_url() {
3640

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

4287
function fetch_and_template_file() {
@@ -58,10 +103,6 @@ function fetch_and_template_file() {
58103
}"
59104
}
60105

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

79120
if [[ -z "${REPO_URL}" ]]; then
80-
echo " ! Repo URL not found: %s" "${REPO_URL}" >&2
121+
echo " ! Repo URL not found for: ${POM_URL}" >&2
81122
exit 1
82123
fi
83124
# Get the relative path without full URL
@@ -114,12 +155,8 @@ echo -ne "
114155
\"sha256\": \"${POM_SHA256}\"
115156
}"
116157

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"
158+
for FILE in $(get_pkg_files "${REPO_URL}" "${PKG_PATH}" "${PKG_NAME}"); do
159+
fetch_and_template_file "${FILE}"
160+
done
124161

125162
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)