1
0
Fork 0
dotfiles/home/programs/nvim/update-plugins.sh

136 lines
3.5 KiB
Bash
Raw Permalink Normal View History

2021-12-06 22:35:29 +00:00
#!/usr/bin/env nix-shell
#!nix-shell update-shell.nix -i bash
2023-10-01 15:59:45 +00:00
# shellcheck shell=bash disable=SC2154
2021-12-06 22:35:29 +00:00
2021-12-15 23:27:07 +00:00
set -eEuo pipefail
current_command='none'
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
trap 'code=$?; if [ "$code" -ne "0" ]; then echo "\"${last_command}\" command ended with exit code $code."; fi' EXIT
2021-12-15 19:43:52 +00:00
2021-12-06 22:35:29 +00:00
script_dir="$(dirname "$(realpath "$0")")"
2022-02-14 14:22:40 +00:00
plugins="${script_dir}/plugins.yaml"
2021-12-06 22:35:29 +00:00
nix_new_file="${script_dir}/plugins_new.nix"
nix_file="${script_dir}/plugins.nix"
2022-02-14 14:22:40 +00:00
plugins_json="$(dasel -r yaml -w json . <"$plugins")"
readarray -t plugin_array <<<"$(echo "$plugins_json" | jq -c '.[]')"
2022-02-13 20:58:18 +00:00
2021-12-15 19:43:52 +00:00
rm -f "$nix_new_file"
2023-04-09 20:27:57 +00:00
{
echo '# This file has been auto-generated'
echo '{ pkgs, ... }:'
echo 'let'
echo 'inherit (pkgs) fetchFromGitHub;'
# echo 'inherit (pkgs) fetchFromSourcehut;'
2023-10-01 15:59:45 +00:00
echo 'inherit (pkgs.vimUtils) buildVimPlugin;'
2023-04-09 20:27:57 +00:00
echo 'in'
echo '{'
} >>"$nix_new_file"
2021-12-06 22:35:29 +00:00
2022-02-13 20:58:18 +00:00
for plugin in "${plugin_array[@]}"; do
2022-12-16 20:51:35 +00:00
raw_src="$(echo "$plugin" | dasel -r json -w - '.src')"
2022-04-21 16:55:35 +00:00
owner="$(echo "$raw_src" | awk -F'/' '{ print $(NF-1) }')"
repo="$(echo "$raw_src" | awk -F'/' '{ print $(NF) }')"
2023-10-01 15:59:45 +00:00
name="$(echo "$repo" | tr '.' '-')"
2021-12-15 19:43:52 +00:00
echo "Updating ${owner}/${repo} ..."
2023-07-13 17:57:12 +00:00
if [[ $raw_src == http* ]]; then
2022-04-21 16:55:35 +00:00
clone_src="$raw_src"
else
clone_src="https://github.com/${owner}/${repo}.git"
fi
2022-04-23 22:16:45 +00:00
nix_prefetch_flags="--quiet"
2022-04-27 20:17:14 +00:00
fetch_submodules="$(echo "$plugin" | jq -r '.fetchSubmodules // empty')"
2022-04-20 20:47:01 +00:00
if [ "$fetch_submodules" == "true" ]; then
2022-04-23 22:16:45 +00:00
nix_prefetch_flags+=" --fetch-submodules"
2022-04-20 20:47:01 +00:00
fi
2022-01-05 21:28:17 +00:00
2024-05-02 14:34:50 +00:00
branch="$(echo "$plugin" | jq -r '.branch // empty')"
if [ -n "$branch" ]; then
nix_prefetch_flags+=" --rev refs/heads/$branch"
fi
2022-04-27 20:17:14 +00:00
rev="$(echo "$plugin" | jq -r '.rev // empty')"
if [ -n "$rev" ]; then
nix_prefetch_flags+=" --rev $rev"
2022-01-05 21:28:17 +00:00
fi
2023-10-01 15:59:45 +00:00
# shellcheck disable=SC2086
2022-04-23 22:16:45 +00:00
src_json="$(nix-prefetch-git $nix_prefetch_flags "$clone_src")"
2022-04-21 16:55:35 +00:00
src="{
owner = \"${owner}\";
repo = \"${repo}\";
2022-12-16 20:51:35 +00:00
rev = \"$(echo "$src_json" | dasel -r json -w - '.rev')\";
sha256 = \"$(echo "$src_json" | dasel -r json -w - '.sha256')\";
fetchSubmodules = $(echo "$src_json" | dasel -r json -w - '.fetchSubmodules');
2022-04-21 16:55:35 +00:00
}"
2021-12-15 19:43:52 +00:00
2022-12-16 20:51:35 +00:00
commit_date="$(echo "$src_json" | dasel -r json -w - '.date')"
2022-04-20 20:47:01 +00:00
version="$(date -d "$commit_date" "+%Y-%m-%d")"
2021-12-06 22:35:29 +00:00
2022-04-21 16:55:35 +00:00
case "$clone_src" in
https://github.com*)
2023-04-09 20:27:57 +00:00
fetcher="fetchFromGitHub"
2022-04-21 16:55:35 +00:00
;;
https://git.sr.ht*)
2023-04-09 20:27:57 +00:00
fetcher="fetchFromSourcehut"
2022-04-21 16:55:35 +00:00
;;
*)
echo "Unsupported URL: $clone_src"
exit 1
;;
esac
2022-11-27 16:12:49 +00:00
case "$name" in
*)
{
2023-10-01 15:59:45 +00:00
echo "${name} = buildVimPlugin {"
2022-11-27 16:12:49 +00:00
echo "pname = \"${repo}\";"
} >>"$nix_new_file"
close_block="};"
;;
esac
{
echo "version = \"${version}\";"
2022-04-21 16:55:35 +00:00
echo "src = ${fetcher} ${src};"
} >>"$nix_new_file"
2021-12-06 22:35:29 +00:00
2022-02-13 20:58:18 +00:00
build_inputs="$(echo "$plugin" | jq -r '.nativeBuildInputs // empty' | jq -r @sh)"
if [ -n "$build_inputs" ]; then
{
echo -n "nativeBuildInputs = with pkgs; ["
echo -n "$build_inputs" | tr -d "'"
echo '];'
} >>"$nix_new_file"
2022-02-13 20:58:18 +00:00
fi
build_phase="$(echo "$plugin" | jq -r '.buildPhase // empty')"
if [ -n "$build_phase" ]; then
printf "buildPhase = ''\n%s\n'';\n" "$build_phase" >>"$nix_new_file"
2021-12-06 22:35:29 +00:00
fi
2022-11-27 16:12:49 +00:00
echo "$close_block" >>"$nix_new_file"
2022-02-13 20:58:18 +00:00
done
2021-12-07 08:05:54 +00:00
echo "}" >>"$nix_new_file"
2021-12-06 22:35:29 +00:00
nixpkgs-fmt "$nix_new_file"
if test -f "$nix_file"; then
2021-12-15 23:27:07 +00:00
set +eo pipefail
difft "$nix_file" "$nix_new_file"
2021-12-15 23:27:07 +00:00
set -eo pipefail
2021-12-06 22:35:29 +00:00
mv "$nix_new_file" "$nix_file"
else
mv "$nix_new_file" "$nix_file"
2021-12-15 23:27:07 +00:00
set +eo pipefail
2021-12-06 22:35:29 +00:00
bat --paging=never "$nix_file"
2021-12-15 23:27:07 +00:00
set -eo pipefail
2021-12-06 22:35:29 +00:00
fi