···
2
+
# shellcheck shell=bash
···
# used for glob ordering of package names
12
+
>&2 echo "WARNING: nuget-to-nix has deprecated in favor of nuget-to-json."
12
-
>&2 echo "Usage: $0 <packages directory> [path to a file with a list of excluded packages] > deps.nix"
16
+
>&2 echo " $0 <packages directory> [path to a file with a list of excluded packages] > deps.nix"
17
+
>&2 echo " $0 --convert deps.json > deps.nix"
17
-
tmp=$(realpath "$(mktemp -td nuget-to-nix.XXXXXX)")
18
-
trap 'rm -r "$tmp"' EXIT
20
-
excluded_list=$(realpath "${2:-/dev/null}")
22
-
export DOTNET_NOLOGO=1
23
-
export DOTNET_CLI_TELEMETRY_OPTOUT=1
25
-
mapfile -t sources < <(dotnet nuget list source --format short | awk '/^E / { print $2 }')
28
-
declare -a remote_sources
29
-
declare -A base_addresses
31
-
for index in "${sources[@]}"; do
32
-
if [[ -d "$index" ]]; then
22
+
if [ "$1" == "--convert" ]; then
23
+
if [ ! -e "$2" ]; then
24
+
echo "File not found."
36
-
remote_sources+=($index)
39
-
curl --compressed --netrc -fsL "$index" | \
40
-
jq -r '.resources[] | select(."@type" == "PackageBaseAddress/3.0.0")."@id"')
41
-
if [[ ! "$base_address" == */ ]]; then
42
-
base_address="$base_address/"
28
+
if IFS='' read -r -d '' jsonDeps <"$2"; then
29
+
echo "Null bytes found in file." >&2
44
-
base_addresses[$index]="$base_address"
47
-
echo "{ fetchNuGet }: ["
50
-
for package in *; do
51
-
[[ -d "$package" ]] || continue
53
-
for version in *; do
54
-
id=$(xmlstarlet sel -t -v /_:package/_:metadata/_:id "$version"/*.nuspec)
56
-
if grep -qxF "$id.$version.nupkg" "$excluded_list"; then
33
+
if ! jsonDeps=$(nuget-to-json "$@"); then
34
+
echo "nuget-to-json failed."
60
-
# packages in the nix store should have an empty metadata file
61
-
used_source="$(jq -r 'if has("source") then .source else "" end' "$version"/.nupkg.metadata)"
39
+
# pkgs/by-name/az/azure-functions-core-tools/deps.json
40
+
IFS='' readarray -d '' depsParts < <(jq 'map([.pname, .version, .sha256, .hash, .url]) | flatten[]' --raw-output0 <<<"$jsonDeps")
64
-
if [[ -z "$used_source" || -d "$used_source" ]]; then
43
+
echo '{ fetchNuGet }: ['
68
-
for source in "${remote_sources[@]}"; do
69
-
url="${base_addresses[$source]}$package/$version/$package.$version.nupkg"
70
-
if [[ "$source" == "$used_source" ]]; then
71
-
hash="$(nix-hash --type sha256 --flat --sri "$version/$package.$version".nupkg)"
75
-
if hash=$(nix-prefetch-url "$url" 2>"$tmp"/error); then
76
-
hash="$(nix-hash --to-sri --type sha256 "$hash")"
77
-
# If multiple remote sources are enabled, nuget will try them all
78
-
# concurrently and use the one that responds first. We always use the
79
-
# first source that has the package.
80
-
echo "$package $version is available at $url, but was restored from $used_source" 1>&2
84
-
if ! grep -q 'HTTP error 404' "$tmp/error"; then
85
-
cat "$tmp/error" 1>&2
45
+
while [ "$index" -lt "${#depsParts[@]}" ]; do
46
+
pname="${depsParts[index]}"
47
+
version="${depsParts[++index]}"
48
+
sha256="${depsParts[++index]}"
49
+
hash="${depsParts[++index]}"
50
+
url="${depsParts[++index]}"
51
+
((++index)) # Go to next pname
92
-
if [[ $found = false ]]; then
93
-
echo "couldn't find $package $version" >&2
97
-
if [[ "$source" != https://api.nuget.org/v3/index.json ]]; then
98
-
echo " (fetchNuGet { pname = \"$id\"; version = \"$version\"; hash = \"$hash\"; url = \"$url\"; })"
53
+
echo -n " (fetchNuGet { pname = \"$pname\"; version = \"$version\"; "
54
+
if [ "$sha256" != null ]; then
55
+
echo -n "sha256 = \"$sha256\"; "
100
-
echo " (fetchNuGet { pname = \"$id\"; version = \"$version\"; hash = \"$hash\"; })"
57
+
echo -n "hash = \"$hash\"; "
59
+
if [ "$url" != "null" ]; then
60
+
echo -n "url = \"$url\"; "