Skip to content

Instantly share code, notes, and snippets.

@tavinus
Forked from noplanman/upgrade-nghttp2
Created April 3, 2020 23:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tavinus/52364cfafad73cd9ef5a942266a916ab to your computer and use it in GitHub Desktop.
Save tavinus/52364cfafad73cd9ef5a942266a916ab to your computer and use it in GitHub Desktop.
Upgrade nghttp2 for brew with a fix for older MacOS versions
#!/usr/bin/env bash
# Update brew repo.
printf "%s" "Updating brew repo..."
brew update &> /dev/null
echo "✅"
# Get the currently installed and new version of nghttp2.
printf "%s" "Fetch installed and stable nghttp2 versions..."
VERSION_INSTALLED="$(brew list --versions | grep nghttp2 | awk '{print $2}')"
VERSION_STABLE="$(brew info --json=v1 nghttp2 | sed -E -e 's/.*"stable":"([^"]*).*/\1/')"
echo "✅"
if [ "$VERSION_STABLE" = "$VERSION_INSTALLED" ]; then
echo "Latest version (${VERSION_STABLE}) seems to be installed already!";
read -r -p "Continue anyway? (Y/n): " REPLY
case "${REPLY:-y}" in
[yY] | [yY][Ee][Ss] ) ;;
*) echo "Cancelled ❌"; exit; ;;
esac
fi
if [ -z "$VERSION_INSTALLED" ]; then
echo "nghttp2 doesn't seem to be installed. Installing version ${VERSION_STABLE} now.";
fi
FILENAME="nghttp2--${VERSION_STABLE}.tar.xz"
FOLDERNAME="nghttp2-${VERSION_STABLE}"
FORMULAFILE="$(brew formula nghttp2)"
# Download the package.
printf "%s" "Downloading latest version of nghttp2 (${VERSION_STABLE})..."
brew fetch nghttp2 &> /dev/null
echo "✅"
# Change into the Homebrew cache folder.
cd "$(brew --cache)"
# Extract the archive.
printf "%s" "Extract the archive..."
tar xJf "${FILENAME}"
echo "✅"
# Apply the code changes from https://github.com/nghttp2/nghttp2/pull/1319
printf "%s" "Apply code fix..."
sed -ibkp 's/return dconn;/return std::move(dconn);/g' \
"${FOLDERNAME}/src/shrpx_client_handler.cc"
echo "✅"
# Compress the files to an archive and replace the original one.
printf "%s" "Create new archive..."
tar cJf ${FILENAME} ${FOLDERNAME} &> /dev/null
echo "✅"
# Get the checksum of the new archive.
SHASUM="$(sha256sum ${FILENAME} | awk '{print $1}')"
# Enter the new checksum in /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/nghttp2.rb
printf "%s" "Inject new checksum..."
sed -ibkp "s/sha256 \".*\"$/sha256 \"${SHASUM}\"/g" \
"${FORMULAFILE}"
echo "✅"
# Install / Upgrade nghttp2.
ACTION=upgrade
if [ -z "$VERSION_INSTALLED" ]; then
ACTION=install
fi
printf "%s" "Starting nghttp2 ${VERSION_STABLE} ${ACTION}..."
brew $ACTION nghttp2 &> /dev/null
echo "✅"
# Reset the formula to prevent any repo update issues.
printf "%s" "Reset formula file..."
(cd "$(dirname $(brew formula nghttp2))" && git checkout "${FORMULAFILE}") &> /dev/null
echo "✅"
echo "All done 🎉"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment