2022-04-27 09:35:20 +00:00
|
|
|
#!@runtimeShell@
|
2021-08-18 13:19:15 +00:00
|
|
|
|
|
|
|
set -euo pipefail
|
|
|
|
|
2022-04-27 09:35:20 +00:00
|
|
|
export PATH="@binPath@"
|
2022-10-06 18:32:54 +00:00
|
|
|
# used for glob ordering of package names
|
|
|
|
export LC_ALL=C
|
2022-04-27 09:35:20 +00:00
|
|
|
|
2021-08-18 13:19:15 +00:00
|
|
|
if [ $# -eq 0 ]; then
|
2022-10-06 18:32:54 +00:00
|
|
|
>&2 echo "Usage: $0 <packages directory> [path to excluded package source] > deps.nix"
|
2021-08-18 13:19:15 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
pkgs=$1
|
2022-10-06 18:32:54 +00:00
|
|
|
tmp=$(realpath "$(mktemp -td nuget-to-nix.XXXXXX)")
|
|
|
|
trap 'rm -r "$tmp"' EXIT
|
|
|
|
excluded_source=$(realpath "${2:-$tmp/empty}")
|
2021-08-18 13:19:15 +00:00
|
|
|
|
2022-10-06 18:32:54 +00:00
|
|
|
export DOTNET_NOLOGO=1
|
|
|
|
export DOTNET_CLI_TELEMETRY_OPTOUT=1
|
2022-06-26 10:26:21 +00:00
|
|
|
|
2022-10-06 18:32:54 +00:00
|
|
|
mapfile -t sources < <(dotnet nuget list source --format short | awk '/^E / { print $2 }')
|
|
|
|
|
|
|
|
declare -A base_addresses
|
2021-08-18 13:19:15 +00:00
|
|
|
|
2022-10-06 18:32:54 +00:00
|
|
|
for index in "${sources[@]}"; do
|
|
|
|
base_addresses[$index]=$(
|
|
|
|
curl --compressed --netrc -fsL "$index" | \
|
|
|
|
jq -r '.resources[] | select(."@type" == "PackageBaseAddress/3.0.0")."@id"')
|
|
|
|
done
|
2022-08-21 13:32:41 +00:00
|
|
|
|
2022-10-06 18:32:54 +00:00
|
|
|
echo "{ fetchNuGet }: ["
|
2022-08-21 13:32:41 +00:00
|
|
|
|
2022-10-06 18:32:54 +00:00
|
|
|
cd "$pkgs"
|
|
|
|
for package in *; do
|
|
|
|
cd "$package"
|
|
|
|
for version in *; do
|
|
|
|
id=$(xq -r .package.metadata.id "$version/$package".nuspec)
|
2021-08-18 13:19:15 +00:00
|
|
|
|
2022-10-06 18:32:54 +00:00
|
|
|
if [[ -e "$excluded_source/$id.$version".nupkg ]]; then
|
2022-09-22 12:36:57 +00:00
|
|
|
continue
|
2022-10-06 18:32:54 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
used_source="$(jq -r '.source' "$version"/.nupkg.metadata)"
|
|
|
|
for source in "${sources[@]}"; do
|
|
|
|
url="${base_addresses[$source]}$package/$version/$package.$version.nupkg"
|
|
|
|
if [[ "$source" == "$used_source" ]]; then
|
|
|
|
sha256="$(nix-hash --type sha256 --flat --base32 "$version/$package.$version".nupkg)"
|
|
|
|
found=true
|
|
|
|
break
|
|
|
|
else
|
|
|
|
if sha256=$(nix-prefetch-url "$url" 2>"$tmp"/error); then
|
|
|
|
# If multiple remote sources are enabled, nuget will try them all
|
|
|
|
# concurrently and use the one that responds first. We always use the
|
|
|
|
# first source that has the package.
|
2022-10-30 15:09:59 +00:00
|
|
|
echo "$package $version is available at $url, but was restored from $used_source" 1>&2
|
2022-10-06 18:32:54 +00:00
|
|
|
found=true
|
|
|
|
break
|
|
|
|
else
|
|
|
|
if ! grep -q 'HTTP error 404' "$tmp/error"; then
|
|
|
|
cat "$tmp/error" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
if ! ${found-false}; then
|
|
|
|
echo "couldn't find $package $version" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ "$source" != https://api.nuget.org/v3/index.json ]]; then
|
|
|
|
echo " (fetchNuGet { pname = \"$id\"; version = \"$version\"; sha256 = \"$sha256\"; url = \"$url\"; })"
|
|
|
|
else
|
|
|
|
echo " (fetchNuGet { pname = \"$id\"; version = \"$version\"; sha256 = \"$sha256\"; })"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
cd ..
|
|
|
|
done
|
|
|
|
|
|
|
|
cat << EOL
|
|
|
|
]
|
|
|
|
EOL
|