Skip to content

Instantly share code, notes, and snippets.

@dragos
Last active April 3, 2022 20:01
Show Gist options
  • Save dragos/c5a3d0e82f8c75a3804ebac68352dd0d to your computer and use it in GitHub Desktop.
Save dragos/c5a3d0e82f8c75a3804ebac68352dd0d to your computer and use it in GitHub Desktop.
Simple bash functions to manage different Scala versions on the command line
#!/bin/bash -e
# where Scala tarballs are extracted
SCALA_BASE=$HOME/scala
# Usage:
#
# Install:
# . /path/to/scala-env.sh in your bash profile
#
# Switch scala version:
# set-scala 2.12.11
#
# Download Scala version:
# download-scala 2.13.2
#
# Set current Scala version for the command line.
#
# $1 scala version, like "2.12.1"
function set-scala()
{
local ver=$1
local dir="$SCALA_BASE/scala-$1"
if [ -d $dir ]; then
# attempt to not duplicate scala installations on the PATH
local regex="s|$HOME/scala/scala-2\.[0-9][0-9]\.[0-9][0-9]?/bin/?:||"
stripped=$(echo $PATH | sed -E $regex)
export PATH=$dir/bin/:$stripped
else
echo "Unknown Scala version. Download it with 'download-scala $ver'"
fi
}
# $1 scala version, like "2.12.1"
function download-scala()
{
local ver=$1
local dir="$SCALA_BASE/scala-$1"
if [ -d $dir ]; then
echo "$dir already exists. If installation is broken, delete and retry."
else
(cd $HOME/Downloads/ && curl https://downloads.lightbend.com/scala/$ver/scala-$ver.tgz -O)
tar -C$SCALA_BASE -xzf $HOME/Downloads/scala-$ver.tgz
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment