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-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}" \
|
|
|
|
>/dev/null 2>&1
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-05-05 09:36:49 +00:00
|
|
|
function fetch_and_template_file_no_fail() {
|
|
|
|
fetch_and_template_file "${1}" 2>/dev/null || true
|
|
|
|
}
|
|
|
|
|
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-02-28 17:22:17 +00:00
|
|
|
echo " ! Repo URL not found: %s" "${REPO_URL}" >&2
|
|
|
|
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}\"
|
|
|
|
}"
|
|
|
|
|
|
|
|
# Some deps are just POMs, in which case there is no JAR to fetch.
|
2023-05-05 09:36:49 +00:00
|
|
|
[[ "${OBJ_TYPE}" == "" ]] && fetch_and_template_file_no_fail "${PKG_NAME}.jar"
|
2023-02-28 17:22:17 +00:00
|
|
|
[[ "${OBJ_TYPE}" == "jar" ]] && fetch_and_template_file "${PKG_NAME}.jar"
|
|
|
|
[[ "${OBJ_TYPE}" == "bundle" ]] && fetch_and_template_file "${PKG_NAME}.jar"
|
|
|
|
[[ "${OBJ_TYPE}" =~ aar* ]] && fetch_and_template_file "${PKG_NAME}.aar"
|
|
|
|
[[ "${OBJ_TYPE}" == "aar.asc" ]] && fetch_and_template_file "${PKG_NAME}.${OBJ_TYPE}"
|
|
|
|
pom_has_nodeps_jar "${POM_PATH}" && fetch_and_template_file "${PKG_NAME}-nodeps.jar"
|
|
|
|
|
|
|
|
echo -e '\n }\n },'
|