Skip to content

Instantly share code, notes, and snippets.

@GGG-KILLER
Last active December 8, 2024 05:29
Show Gist options
  • Save GGG-KILLER/3a52e6244bbd14dd38e27f115e0ad2d6 to your computer and use it in GitHub Desktop.
Save GGG-KILLER/3a52e6244bbd14dd38e27f115e0ad2d6 to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
set -euo pipefail
grep -oRIP --exclude='*.md' '(?<=nugetDeps = (?:import )?)(\.\.?/[\w\-_\/]+\.nix)' . | while read -r LINE; do
echo "Line: $LINE"
IFS=':' read -ra PARTS <<<"$LINE"
echo "${LINE[@]}"
DRV_FILE_PATH="${PARTS[0]}"
NIX_DEPS_FILE_NAME="${PARTS[1]}"
NIX_DEPS_FILE_PATH="$(realpath "$(dirname "$DRV_FILE_PATH")/${PARTS[1]}")"
JSON_DEPS_FILE_NAME="${NIX_DEPS_FILE_NAME%.nix}.json"
JSON_DEPS_FILE_PATH="${NIX_DEPS_FILE_PATH%.nix}.json"
# Convert file if it hasn't been already.
if [ ! -e "$JSON_DEPS_FILE_PATH" ]; then
if ! [ -e "$NIX_DEPS_FILE_PATH" ]; then
echo "Nix-based dependency file $NIX_DEPS_FILE_PATH does not exist and converted version was not found at $JSON_DEPS_FILE_PATH!"
exit 1
fi
echo "Migrating $NIX_DEPS_FILE_PATH to $JSON_DEPS_FILE_PATH.json..."
nix eval --json --file "$NIX_DEPS_FILE_PATH" --apply 'f: f { fetchNuGet = id: id; }' | jq 'map({pname, version, sha256, hash, md5, url}|with_entries(select(.value != null)))' >"$JSON_DEPS_FILE_PATH"
rm "$NIX_DEPS_FILE_PATH"
fi
echo "Updating file name in $DRV_FILE_PATH to rename $NIX_DEPS_FILE_NAME to $JSON_DEPS_FILE_NAME..."
if IFS='' read -r -d '' DRV_FILE_CONTENT <"$DRV_FILE_PATH"; then
echo "$DRV_FILE_PATH: null bytes found." >&2
exit 1
fi
DRV_FILE_CONTENT="${DRV_FILE_CONTENT//import "$NIX_DEPS_FILE_NAME"/"$JSON_DEPS_FILE_NAME"}"
printf '%s' "${DRV_FILE_CONTENT//"$NIX_DEPS_FILE_NAME"/"$JSON_DEPS_FILE_NAME"}" >"$DRV_FILE_PATH"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment