nixpkgs-automation/vim-plugin-add.sh

158 lines
4.2 KiB
Bash
Executable file

#!/bin/bash
## Trying to figure out a dumb bug
#!nix-shell --run bash -p bash
#
# exit
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p git neovim vimPluginsUpdater
set -eu
msg() { echo ' ' "$@"; }
err() { msg "$@" >&2;}
dbg() { if [ "${DEBUG:-false}" = true ]; then echo; echo " DEBUG:" "$@"; echo; fi; }
nix_build_vimplug() {
for p; do
nix build --quiet --no-link .#vimPlugins."$p"
done
}
nix_test_build() {
if nix_build_vimplug "$1"; then
msg
msg "Build of $1 succeeded!"
return 0
else
msg
msg "Build of $1 failed."
return 1
fi
}
# Error checking
if [ $# -lt 1 ]; then
err 'Please pass the plugins you want to add as arguments, like so:'
err "$0 Zeioth/compiler.nvim jmbuhr/otter.nvim"
exit 1
fi
if ! git remote get-url origin | grep --silent -P 'nixpkgs.git$'; then
err 'Please execute this from the root of the nixpkgs repository'
exit 1
fi
if [ "$(git rev-parse --abbrev-ref HEAD)" != "master" ]; then
msg 'WARNING: currently not on master branch!'
sleep 1
fi
nixpkgs_dir="$(git rev-parse --show-toplevel)"
nixpkgs_vim_plugin_dir=pkgs/applications/editors/vim/plugins
tmp_dir="$(mktemp -d)"
tmp_generated_nix="$(mktemp)"
tmp_ending_nix="$(mktemp)"
pr_urls=()
for p; do
msg "Adding $p"
# set basic variables
domain=https://github.com
if grep --silent -P '^http' <<< "$p"; then # support inputting urls
domain="$(grep -Po '^http.*://[^/]+' <<< "$p")"
p="$(grep -Po '[^/]+/[^/]+$' <<< "$p")"
fi
repo_url="$domain/$p"
plugin_name="${p##*/}"
plugin_author="${p%/*}"
nixpkgs_plugin_name="$(tr . - <<< "$plugin_name")" # the plugin name, as nixpkgs will know it
plugin_repo_tmp_dir="$tmp_dir/$plugin_name"
# get info from plugin repository
dbg "Getting plugin git repository"
git clone --quiet --depth=1 "$repo_url" "$plugin_repo_tmp_dir"
cd "$plugin_repo_tmp_dir"
git_rev_hash="$(git rev-parse HEAD)"
git_date_version="$(git show -s --format=%cs "$git_rev_hash")"
rm -rf .git
repo_hash="$(nix hash path .)"
# add the plugin with vim-plugins-updater
dbg "Running vimPluginsUpdater"
cd "$nixpkgs_dir"
rm -rf "$plugin_repo_tmp_dir"
git checkout --quiet -b "$plugin_name"
dbg "actually running the updater"
nix run nixpkgs#vimPluginsUpdater -- add "$p"
# vim-plugins-updater add "$p"
# Append the new plugin to generated.nix
dbg "Writing generated.nix"
cd ./"$nixpkgs_vim_plugin_dir"
head -n -2 generated.nix > "$tmp_generated_nix"
editor_start_line="$(wc -l "$tmp_generated_nix")"
cat <<- EOF > "$tmp_ending_nix"
$nixpkgs_plugin_name = buildVimPlugin {
pname = "$nixpkgs_plugin_name";
version = "$git_date_version";
src = fetchFromGitHub {
owner = "$plugin_author";
repo = "$plugin_name";
rev = "$git_rev_hash";
sha256 = "$repo_hash";
};
meta.homepage = "$repo_url";
};
}
EOF
cat "$tmp_generated_nix" "$tmp_ending_nix" > generated.nix
first=true
until nix_test_build "$nixpkgs_plugin_name" && ! $first; do
if [ $first = true ]; then
msg "Please make any needed changes and then exit to continue."
msg "Such as adding dependencies to $nixpkgs_vim_plugin_dir/overrides.nix"
msg "Exit with a status code of 0 to test, commit, and push your changes"
first=false
fi
until $SHELL; do
ec=$?
msg -n "Shell exited with code $ec. Do you want to stop? [y/N]"
until [ $ec = 0 ]; do
read -r
case "$REPLY" in
[y|Y])
exit $ec
;;
[n|N])
ec=0
;;
*)
echo "Please answer y or n."
ec=1
;;
esac
done
done
git commit -a --amend
git push --set-upstream origin "$plugin_name" --force
git switch -
# shellcheck disable=SC2016
pr_urls+=("https://$(git remote get-url origin | sed -r 's/^[^@]+@//g; s_:_/_g; s_\.git$__g')/pull/new/$plugin_name")
echo
done
done
echo "Pull Request URLs:"
for url in "${pr_urls[@]}"; do
echo " - $url"
done
# Cleanup
rm -rf "$tmp_dir" "$tmp_ending_nix" "$tmp_generated_nix"
# vim: sw=3 ts=3 et