2020-05-07 10:21:39 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
#
|
|
|
|
# This script takes a deps.list file and builds a Nix expression
|
|
|
|
# that can be used by maven-repo-builder.nix to produce a path to
|
|
|
|
# a local Maven repository.
|
|
|
|
#
|
|
|
|
|
2020-05-22 18:24:47 +00:00
|
|
|
# This defines URLs of Maven repos we know about and use.
|
|
|
|
declare -a REPOS=(
|
|
|
|
"https://repo.maven.apache.org/maven2"
|
|
|
|
"https://dl.google.com/dl/android/maven2"
|
2021-06-30 13:43:05 +00:00
|
|
|
"https://plugins.gradle.org/m2"
|
|
|
|
"https://jitpack.io"
|
2020-05-22 18:24:47 +00:00
|
|
|
)
|
2020-05-07 10:21:39 +00:00
|
|
|
|
2023-08-31 13:54:41 +00:00
|
|
|
# These files are not necessary for the build process.
|
|
|
|
FILENAMES_BLACKLIST='-(javadoc|runtime|gwt|headers|sources|src|tests|adapters|modular|site|bin)\.'
|
|
|
|
FILETYPES_BLACKLIST='(pom|json|zip|module|xml|md5|sha1|sha256|sha512)$'
|
|
|
|
SIGNATURE_BLACKLIST='(pom|jar|json|zip|module|xml|md5|asc).asc$'
|
|
|
|
|
2023-05-10 13:12:36 +00:00
|
|
|
function nix_prefetch() {
|
|
|
|
nix store prefetch-file --json "${1}" 2>/dev/null
|
2020-05-07 10:21:39 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 13:12:36 +00:00
|
|
|
function get_nix_path() { echo "${1}" | jq -r .storePath; }
|
|
|
|
function get_nix_sha() { echo "${1}" | jq -r .hash; }
|
2020-05-14 09:13:06 +00:00
|
|
|
function get_sha1() { sha1sum "${1}" | cut -d' ' -f1; }
|
2020-05-07 10:21:39 +00:00
|
|
|
|
|
|
|
# Assumes REPOS from repos.sh is available
|
|
|
|
function match_repo_url() {
|
|
|
|
for REPO_URL in "${REPOS[@]}"; do
|
|
|
|
if [[ "$1" = ${REPO_URL}* ]]; then
|
|
|
|
echo "${REPO_URL}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
echo " ! Failed to match a repo for: ${1}" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2023-02-28 11:43:44 +00:00
|
|
|
function pom_has_nodeps_jar() {
|
2023-02-28 17:22:17 +00:00
|
|
|
grep '<shadedClassifierName>nodeps</shadedClassifierName>' "${1}" \
|
2023-08-31 13:54:41 +00:00
|
|
|
>/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
function guess_pkg_files() {
|
|
|
|
# Some deps are just POMs, in which case there is no JAR to fetch.
|
|
|
|
[[ "${OBJ_TYPE}" == "" ]] && echo "${PKG_NAME}.jar"
|
|
|
|
[[ "${OBJ_TYPE}" == "jar" ]] && echo "${PKG_NAME}.jar"
|
|
|
|
[[ "${OBJ_TYPE}" == "bundle" ]] && echo "${PKG_NAME}.jar"
|
|
|
|
[[ "${OBJ_TYPE}" =~ aar* ]] && echo "${PKG_NAME}.aar"
|
|
|
|
[[ "${OBJ_TYPE}" == "aar.asc" ]] && echo "${PKG_NAME}.${OBJ_TYPE}"
|
|
|
|
pom_has_nodeps_jar "${POM_PATH}" && echo "${PKG_NAME}-nodeps.jar"
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_pkg_files() {
|
|
|
|
REPO_URL="${1}"
|
|
|
|
PKG_PATH="${2}"
|
|
|
|
PKG_NAME="${3}"
|
|
|
|
# Google Maven repo doesn't have normal HTML directory listing.
|
|
|
|
if [[ "${REPO_URL}" == "https://dl.google.com/dl/android/maven2" ]]; then
|
|
|
|
FOUND=$(curl --fail -s "${REPO_URL}/${PKG_PATH}/artifact-metadata.json")
|
|
|
|
# Some older packages do not have artifacts-metadata.json.
|
|
|
|
if [[ "$?" -eq 0 ]]; then
|
|
|
|
FOUND=$(echo "${FOUND}" | jq -r '.artifacts[].name')
|
|
|
|
else
|
|
|
|
FOUND=''
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
FOUND=$(
|
|
|
|
curl -s "${REPO_URL}/${PKG_PATH}/" \
|
|
|
|
| htmlq a -a href \
|
|
|
|
| grep -e "^${PKG_NAME}"
|
|
|
|
)
|
|
|
|
fi
|
|
|
|
if [[ "${FOUND}" == '' ]]; then
|
|
|
|
guess_pkg_files
|
|
|
|
else
|
|
|
|
# Filter out files we don't actually need for builds.
|
|
|
|
echo "${FOUND}" \
|
|
|
|
| grep -v -E \
|
|
|
|
-e "${FILENAMES_BLACKLIST}" \
|
|
|
|
-e "${FILETYPES_BLACKLIST}" \
|
|
|
|
-e "${SIGNATURE_BLACKLIST}"
|
|
|
|
fi
|
2023-02-28 17:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fetch_and_template_file() {
|
|
|
|
local FILENAME="${1}"
|
|
|
|
local OBJ_URL OBJ_NIX_FETCH_OUT OBJ_NAME OBJ_PATH
|
|
|
|
|
|
|
|
OBJ_URL="${REPO_URL}/${PKG_PATH}/${FILENAME}"
|
2023-05-10 13:12:36 +00:00
|
|
|
if ! OBJ_NIX_FETCH_OUT=$(nix_prefetch "${OBJ_URL}"); then
|
2023-02-28 17:22:17 +00:00
|
|
|
echo " ! Failed to fetch: ${OBJ_URL}" >&2
|
2023-05-05 09:36:49 +00:00
|
|
|
return 1
|
2023-02-28 17:22:17 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
OBJ_NAME="${FILENAME}"
|
|
|
|
OBJ_PATH=$(get_nix_path "${OBJ_NIX_FETCH_OUT}")
|
|
|
|
echo -n ",
|
|
|
|
\"${OBJ_NAME}\": {
|
|
|
|
\"sha1\": \"$(get_sha1 "${OBJ_PATH}")\",
|
|
|
|
\"sha256\": \"$(get_nix_sha "${OBJ_NIX_FETCH_OUT}")\"
|
|
|
|
}"
|
2023-02-28 11:43:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:21:39 +00:00
|
|
|
if [[ -z "${1}" ]]; then
|
2023-02-28 17:22:17 +00:00
|
|
|
echo "Required POM URL argument not given!" >&2
|
2020-05-07 10:21:39 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2020-05-24 13:44:15 +00:00
|
|
|
POM_URL=${1}
|
2023-02-28 17:22:17 +00:00
|
|
|
# Drop the POM extension.
|
|
|
|
PKG_URL_NO_EXT="${POM_URL%.pom}"
|
|
|
|
# Name of package without extension.
|
|
|
|
PKG_NAME="$(basename "${PKG_URL_NO_EXT}")"
|
2020-05-07 10:21:39 +00:00
|
|
|
|
2020-05-24 13:44:15 +00:00
|
|
|
echo -en "${CLR} - Nix entry for: ${1##*/}\r" >&2
|
2020-05-07 10:21:39 +00:00
|
|
|
|
2023-02-28 17:22:17 +00:00
|
|
|
REPO_URL=$(match_repo_url "${PKG_URL_NO_EXT}")
|
2020-05-07 10:21:39 +00:00
|
|
|
|
|
|
|
if [[ -z "${REPO_URL}" ]]; then
|
2023-08-31 13:54:41 +00:00
|
|
|
echo " ! Repo URL not found for: ${POM_URL}" >&2
|
2023-02-28 17:22:17 +00:00
|
|
|
exit 1
|
2020-05-07 10:21:39 +00:00
|
|
|
fi
|
|
|
|
# Get the relative path without full URL
|
2023-02-28 17:22:17 +00:00
|
|
|
PKG_PATH="${PKG_URL_NO_EXT#"${REPO_URL}/"}"
|
|
|
|
PKG_PATH="$(dirname "${PKG_PATH}")"
|
2020-05-07 10:21:39 +00:00
|
|
|
|
2020-05-14 09:13:06 +00:00
|
|
|
# Both JARs and AARs have a POM
|
2023-05-10 13:12:36 +00:00
|
|
|
POM_NIX_FETCH_OUT=$(nix_prefetch "${PKG_URL_NO_EXT}.pom")
|
2020-05-14 09:13:06 +00:00
|
|
|
POM_PATH=$(get_nix_path "${POM_NIX_FETCH_OUT}")
|
2023-02-28 17:22:17 +00:00
|
|
|
POM_NAME=$(basename "${PKG_URL_NO_EXT}.pom")
|
2020-05-07 10:21:39 +00:00
|
|
|
if [[ -z "${POM_PATH}" ]]; then
|
2023-02-28 17:22:17 +00:00
|
|
|
echo " ! Failed to fetch: ${PKG_URL_NO_EXT}.pom" >&2
|
2020-05-07 10:21:39 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2020-05-14 09:13:06 +00:00
|
|
|
POM_SHA256=$(get_nix_sha "${POM_NIX_FETCH_OUT}")
|
2020-05-07 10:21:39 +00:00
|
|
|
POM_SHA1=$(get_sha1 "${POM_PATH}")
|
|
|
|
|
2023-02-28 17:22:17 +00:00
|
|
|
# Identify packaging type, JAR, AAR, bundle, or just POM.
|
|
|
|
OBJ_TYPE=$(grep -oP '<packaging>\K[^<]+' "${POM_PATH}")
|
2022-05-17 16:13:51 +00:00
|
|
|
|
|
|
|
# Some deps are Eclipse plugins, and we don't need those.
|
|
|
|
if [[ "${OBJ_TYPE}" == "eclipse-plugin" ]]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2020-05-07 10:21:39 +00:00
|
|
|
# Format into a Nix attrset entry
|
2020-05-14 09:13:06 +00:00
|
|
|
echo -ne "
|
2020-05-07 10:21:39 +00:00
|
|
|
{
|
2023-02-28 17:22:17 +00:00
|
|
|
\"path\": \"${PKG_PATH}\",
|
|
|
|
\"repo\": \"${REPO_URL}\",
|
|
|
|
\"files\": {
|
|
|
|
\"${POM_NAME}\": {
|
|
|
|
\"sha1\": \"${POM_SHA1}\",
|
|
|
|
\"sha256\": \"${POM_SHA256}\"
|
|
|
|
}"
|
|
|
|
|
2023-08-31 13:54:41 +00:00
|
|
|
for FILE in $(get_pkg_files "${REPO_URL}" "${PKG_PATH}" "${PKG_NAME}"); do
|
|
|
|
fetch_and_template_file "${FILE}"
|
|
|
|
done
|
2023-02-28 17:22:17 +00:00
|
|
|
|
|
|
|
echo -e '\n }\n },'
|