Skip to content

Instantly share code, notes, and snippets.

@andreaferretti
Last active November 4, 2019 21:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreaferretti/d40af6a276fb3275d97d0f9585d8197e to your computer and use it in GitHub Desktop.
Save andreaferretti/d40af6a276fb3275d97d0f9585d8197e to your computer and use it in GitHub Desktop.
mynim - manage nim versions

MyNim

MyNim is a tool to install and manage various versions of Nim and related tools.

Since it requires a bash and symlinks, it works fine on Unix derived systems (e.g. MacOSX or Linux) but not on Windows.

Installing MyNim

Just download the mynim file, make it executable and put it somewhere on your $PATH. Then, add the folders ~/.mynim/bin and ~/.nimble/bin to your $PATH. The first one contains the binaries for nim and related tools, such as the nimble package manager. The second one contains binaries installed using Nimble.

For instance, I use ~/.bin as a generic directory where to put local executables. With this convention you can download mynim from this Gist, and then make it executable with the following commands:

mkdir -p ~/.bin
mv mynim ~/.bin
chmod +x ~/.bin/mynim

Then, edit your .bash_profile (on MacOSX) or .bashrc (on Linux) file and add something like the following lines:

BIN="$HOME/.bin"
MYNIM="$HOME/.mynim/bin"
NIMBLE="$HOME/.nimble/bin"
PATH="$BIN:$MYNIM:$NIMBLE:$PATH"

New shells will automatically take these settings, but you can refresh them now with source ~/.bash_profile or source ~/.bashrc respectively.

Using MyNim

If you have installed MyNim following the previous commands, you should be able to launch the mynim command. By default it displays a help like

mynim
Manage nim versions

Usage:
$ mynim install VERSION
$ mynim uninstall VERSION
$ mynim use VERSION
$ mynim list
$ mynim active
$ mynim update

You can install a version of Nim with mynim install, then switch to using it with mynim use. For instance, to install v0.19.4 (the latest stable version at the moment of writing), just run

mynim install v0.19.4
mynim use v0.19.4

To install the development version of Nim, use

mynim install devel

Multiple versions can coexist, and you can switch between them with mynim use $VERSION. If you are on devel, you can keep up with the latest development by mynim update.

#! /bin/bash
set -e
set -u
BASE_DIR="$HOME/.mynim"
BIN_DIR="$BASE_DIR/bin"
NIM_REPO="https://github.com/nim-lang/Nim.git"
OS_NAME="$(uname -s)"
function install_version {
VERSION="$1"
DIR="$BASE_DIR/$VERSION"
if [ ! -d "$DIR" ]; then
mkdir -p "$BASE_DIR"
git clone "$NIM_REPO" "$DIR"
cd $DIR
git checkout $VERSION
git clone https://github.com/nim-lang/csources.git # --depth 1
cd csources
if [ "$VERSION" != "devel" ]; then
# $VERSION may not exist for csources
# we thus temporarily disable exit on error
set +e
git checkout $VERSION
set -e
fi
sh build.sh
cd ..
bin/nim c koch
./koch boot -d:release
./koch tools
echo "version $VERSION installed"
fi
}
function uninstall_version {
TARGET_VERSION="$1"
read_current_version
if [ "$VERSION" == "$TARGET_VERSION" ]; then
echo "you cannot uninstall $TARGET_VERSION because it is currently in use"
else
DIR="$BASE_DIR/$TARGET_VERSION"
if [ -d "$DIR" ]; then
rm -rf "$DIR"
else
echo "version $TARGET_VERSION not installed"
fi
fi
}
function use_version {
VERSION="$1"
DIR="$BASE_DIR/$VERSION"
if [ ! -d "$DIR" ]; then
echo "version $VERSION not installed"
else
if [ -d "$BIN_DIR" ]; then
rm "$BIN_DIR"
fi
ln -s "$DIR/bin" "$BIN_DIR"
echo "version $VERSION in use"
fi
}
function read_current_version {
case "${OS_NAME}" in
Darwin*) TARGET_DIR=$( readlink $BIN_DIR );;
*) TARGET_DIR=$( readlink -f $BIN_DIR )
esac
VERSION_DIR=$( dirname $TARGET_DIR )
VERSION=$( basename $VERSION_DIR )
}
function update {
read_current_version
if [ "$VERSION" != "devel" ]; then
echo "you must be on devel to update"
else
DIR="$BASE_DIR/devel"
cd $DIR
git pull
nim c koch
./koch boot -d:release
./koch tools
fi
}
function list {
BASE_DIR="$HOME/.mynim"
for i in $( ls $BASE_DIR ); do
if [ "$i" != "bin" ]; then
echo $i
fi
done
}
function print_help {
echo "mynim"
echo "Manage nim versions"
echo ""
echo "Usage:"
echo "$ mynim install VERSION"
echo "$ mynim uninstall VERSION"
echo "$ mynim use VERSION"
echo "$ mynim list"
echo "$ mynim active"
echo "$ mynim update"
}
if [ $# -eq 0 ]; then
COMMAND="help"
else
COMMAND="$1"
fi
case $COMMAND in
"update")
update
;;
"use")
use_version "$2"
;;
"install")
install_version "$2"
;;
"uninstall")
uninstall_version "$2"
;;
"list")
list
;;
"active")
read_current_version
echo "$VERSION"
;;
"help")
print_help
;;
*)
echo "don't know command $COMMAND"
print_help
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment