Skip to content

Instantly share code, notes, and snippets.

@pelletencate
Last active January 8, 2023 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pelletencate/cf4972f128fdea726ff2f4c2f103291b to your computer and use it in GitHub Desktop.
Save pelletencate/cf4972f128fdea726ff2f4c2f103291b to your computer and use it in GitHub Desktop.
Install latest asdf stable and asdf-direnv, from scratch, and make it all work
#!/bin/sh
shell=$(basename $SHELL)
# Exit if we don't support your shell
if [ $shell != "zsh" -a $shell != "bash" ]; then
printf "This script only works if you're using zsh or bash\n"
exit 1
fi
# Crash if the asdf or direnv commands already exist, or if ~/.asdf, ~/.envrc or ~/.tool-versions already exist
if [ \
"$(command -v asdf)" -o \
"$(command -v direnv)" -o \
-e $HOME/.asdf -o \
-e $HOME/.envrc -o \
-e $HOME/.tool-versions \
]; then
printf "This script assumes asdf and direnv are not already installed. Please make sure this is the case, and remove .asdf, .envrc and .tool-versions from your home directory if you want to use this script.\n"
exit 1
fi
printf "\
This command will do the following:
- Replace/remove custom asdf and direnv configuration you may have in $HOME/.config
- Install asdf in $HOME/.asdf
- Install the direnv plugin and set up asdf-direnv which installs the latest version of direnv in asdf
- Make direnv available everywhere by adding it to your global tools in $HOME/.tool-versions
- Write 4 lines (including a comment) to the bottom of your $HOME/.${shell}rc that do the following:
- Add the asdf binary to your \$PATH
- Hook direnv in your shell
- Silence direnv's logging
To uninstall, remove $HOME/.asdf, $HOME/.envrc and $HOME/.tool-versions, and remove the 4 lines from your $HOME/.${shell}rc
"
# Abort unless the user's answer begins with Y or y
read -p "Do you want to continue? [y/N] " -n 1 -r
printf "\n"
if ! [[ $REPLY =~ ^[Yy]$ ]]; then
printf "Aborting.\n"
exit 1
fi
cd
printf "\n*****\n* Installing asdf\n*****\n\n"
# Install asdf from git and update to latest stable tag.
git clone -q https://github.com/asdf-vm/asdf.git .asdf
.asdf/bin/asdf update 2>/dev/null
# Don't load it in rc (direnv will take care of that). Instead, add it to $PATH.
printf '\n# asdf-direnv: add asdf to your path and hook direnv into your shell\n' >> $HOME/.${shell}rc
printf 'export PATH="$HOME/.asdf/bin:$PATH"\n' >> $HOME/.${shell}rc
printf "\n*****\n* Installing direnv\n*****\n\n"
# Hook asdf into our current session, we do need it to get direnv up and running
source .asdf/asdf.sh
# Install the direnv plugin and setup asdf-direnv
asdf plugin add direnv
asdf direnv setup --version latest
printf 'export DIRENV_LOG_FORMAT=""\n' >> $HOME/.${shell}rc
# Make sure we can use direnv everywhere by creating a .tool-versions and creating and allowing a .envrc file
asdf direnv local direnv latest
printf "\n*****\n* All done. Please exit and start a new ${shell} session for it to work. \n*****\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment