Skip to content

Instantly share code, notes, and snippets.

@ecosse3
Last active December 21, 2023 19:02
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ecosse3/ab58a8577d8c69dccdf03304b720e1c8 to your computer and use it in GitHub Desktop.
Save ecosse3/ab58a8577d8c69dccdf03304b720e1c8 to your computer and use it in GitHub Desktop.
Update Neovim Nightly from latest github release
#!/bin/bash
# Colors definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
BOLD=$(tput bold)
NORMAL=$(tput sgr0)
# Check if necessary applications are installed
if ! [ -x "$(command -v wget)" ]; then
printf "\n${RED}wget not found in path. Please install it to continue!${NC}\n"
exit
fi
if ! [ -x "$(command -v curl)" ]; then
printf "\n${RED}curl not found in path. Please install it to continue!${NC}\n"
exit
fi
if ! [ -x "$(command -v xmllint)" ]; then
printf "\n${RED}xmllint not found in path. Please install it to continue!${NC}\n"
exit
fi
if ! [ -x "$(command -v datediff)" ]; then
printf "\n${RED}dateutils not found in path. Please install it to continue!${NC}\n"
exit
fi
# Get newest Neovim Nightly info
wget https://github.com/neovim/neovim/releases/tag/nightly -q -O - > /tmp/nvim28dce75c-4317-4006-a103-8069d573e2b2
RESPONSE=$(wget https://github.com/neovim/neovim/releases/tag/nightly --save-headers -O - 2>&1)
if [[ "$RESPONSE" =~ 404\ Not\ Found ]]; then
printf "${RED}Couldn't fetch newest Neovim Nighly info. Aborting...${NC}\n"
exit
fi
# Variables
SHOW_PROMPT=0
CURR_NVIM_VER=$(nvim --version | head -n 1)
NEW_NVIM_VER=$(xmllint --html --xpath "//pre//code/node()" /tmp/nvim28dce75c-4317-4006-a103-8069d573e2b2 2>/dev/null | grep NVIM)
CURR_DATETIME_ISO=$(date --iso-8601=ns)
NEW_NVIM_VER_RELEASE_DATETIME_ISO=$(xmllint --html --xpath "string(//relative-time/@datetime)" /tmp/nvim28dce75c-4317-4006-a103-8069d573e2b2 2>/dev/null)
LAST_RELEASE_DATETIME_DIFF=$(datediff ${NEW_NVIM_VER_RELEASE_DATETIME_ISO} ${CURR_DATETIME_ISO} -f "%H hours %M minutes ago")
# Check if Neovim Nightly exists in repo
if [[ "$NEW_NVIM_VER" == "" ]]; then
printf "\n${RED}Couldn't fetch latest Neovim Nightly version from github repo! Check if it exists. Aborting...${NC}\n"
exit
fi
# Check if the current neovim version is the latest
if [[ "$CURR_NVIM_VER" == "$NEW_NVIM_VER" ]]; then
printf "\n${RED}No new version of ${BOLD}Neovim Nightly${NORMAL}${RED} found!\n${NC}Last release: ${LAST_RELEASE_DATETIME_DIFF}\nAborting...\n"
exit
fi
# If a newer version of Neovim Nightly found show prompt
if [[ CURR_NVIM_VER != NEW_NVIM_VER ]]; then
printf "\n${GREEN}New ${BOLD}Neovim Nightly${NORMAL}${GREEN} version found!${NC}\n${CURR_NVIM_VER} -> ${BOLD}${NEW_NVIM_VER}${NORMAL}\nReleased: ${LAST_RELEASE_DATETIME_DIFF}\n\n"
SHOW_PROMPT=1
fi
# Update function
update_neovim() {
printf "${RED}Updating Neovim Nightly...${NC}\n"
HTTPS_URL="https://github.com/neovim/neovim/releases/download/nightly/nvim.appimage"
CURL_CMD="curl -L -w http_code=%{http_code}"
CURL_OUTPUT=`${CURL_CMD} ${HTTPS_URL} -o /tmp/nvim`
HTTP_CODE=$(echo "${CURL_OUTPUT}" | sed -e 's/.*\http_code=//')
ERROR_MESSAGE=$(echo "${CURL_OUTPUT}" | sed -e 's/http_code.*//')
if [[ ${HTTP_CODE} == 200 ]]; then
chmod +x /tmp/nvim;
sudo cp /tmp/nvim /usr/local/bin;
sudo mv /tmp/nvim /usr/bin;
printf "${GREEN}Neovim Nightly has been updated successfully!${NC}\n"
else
printf "${RED}Neovim Nightly has NOT been updated! ERROR: ${ERROR_MESSAGE}${NC}\n"
fi
}
rm /tmp/nvim28dce75c-4317-4006-a103-8069d573e2b2
while [ $SHOW_PROMPT -gt 0 ]; do
read -p "Do you wish to update neovim? [yes/no] " yn
case $yn in
[Yy]* ) update_neovim; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
@luisvgs
Copy link

luisvgs commented May 12, 2021

Does this script overwrites my current init.vim config?

@ecosse3
Copy link
Author

ecosse3 commented May 12, 2021

Does this script overwrites my current init.vim config?

No, it doesn't. It just updates neovim in /usr/local/bin.

@qqhgs
Copy link

qqhgs commented Jun 3, 2021

Can i get neovim nightly with this? Or this for update only?

@itsfarseen
Copy link

itsfarseen commented Jun 20, 2021

  1. Doesn't follow github redirects
  2. Doesn't save curl output to /tmp/nvim
  3. http_status check is wrong

Fix - change these lines as below:

Line 56-57:

  CURL_CMD="curl -L -w http_code=%{http_code}"
  CURL_OUTPUT=`${CURL_CMD} ${HTTPS_URL} -o /tmp/nvim`

Line 59:

  if [ ${HTTP_CODE} -eq 200 ]; then

@itsfarseen
Copy link

@qqhgs yes

@itsfarseen
Copy link

@qqhgs yes

@ecosse3
Copy link
Author

ecosse3 commented Jun 22, 2021

@itsfarseen, thank you! Updated gist and added few more checks.

@ericraio
Copy link

What version dateuqils do you have, I keep getting this error.

date: illegal option -- -
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
ddiff: Error: reference DATE must be specified

@ecosse3
Copy link
Author

ecosse3 commented Nov 16, 2021

@ericraio

~ ❯ date --version
date (GNU coreutils) 9.0

Also updated gist.

@jodojodo
Copy link

FYI I had to apply the following link in order to use this:
sudo ln -s /usr/bin/dateutils.ddiff /usr/local/bin/datediff

@rkk1995
Copy link

rkk1995 commented Dec 21, 2023

For mac use

CURR_DATETIME_ISO=$(date -u +"%Y-%m-%dT%H:%M:%SZ")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment