Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 134 additions & 14 deletions src/elixir-asdf/install.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,144 @@
#!/usr/bin/env bash
# This is part of devcontainers-extra script library
# source: https://github.com/devcontainers-extra/features

set -e
set -ex

source ./library_scripts.sh
VERSION=${VERSION:-"latest"}

# nanolayer is a cli utility which keeps container layers as small as possible
# source code: https://github.com/devcontainers-extra/nanolayer
# `ensure_nanolayer` is a bash function that will find any existing nanolayer installations,
# and if missing - will download a temporary copy that automatically get deleted at the end
# of the script
ensure_nanolayer nanolayer_location "v0.4.45"
# Clean up
rm -rf /var/lib/apt/lists/*

if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as
root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi

$nanolayer_location \
install \
devcontainer-feature \
"ghcr.io/devcontainers-extra/features/asdf-package:1.0.8" \
--option plugin='elixir' --option version="$VERSION"
check_alpine_packages() {
apk add -v --no-cache "$@"
}

check_packages() {
if ! dpkg -s "$@" >/dev/null 2>&1; then
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
apt-get -y install --no-install-recommends "$@"
fi
}

updaterc() {
if cat /etc/os-release | grep "ID_LIKE=.*alpine.*\|ID=.*alpine.*" ; then
echo "Updating /etc/profile"
echo -e "$1" >>/etc/profile
fi
if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then
echo "Updating /etc/bash.bashrc"
echo -e "$1" >>/etc/bash.bashrc
fi
if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then
echo "Updating /etc/zsh/zshrc"
echo -e "$1" >>/etc/zsh/zshrc
fi
}

echo 'Done!'
install_via_asdf() {
PLUGIN=$1
VERSION=$2
REPO=$3

# install git and curl if does not exists
if cat /etc/os-release | grep "ID_LIKE=.*alpine.*\|ID=.*alpine.*" ; then
check_alpine_packages curl git ca-certificates
elif cat /etc/os-release | grep "ID_LIKE=.*debian.*\|ID=.*debian.*"; then
check_packages curl git ca-certificates
fi


# asdf may be installed somewhere on the machine, but we need it to be accessible to the remote user
# the code bellow will return 2 only when asdf is available, and 1 otherwise
set +e
su "$_REMOTE_USER" <<EOF
export HOME="$_REMOTE_USER_HOME"
export PATH="\$HOME/.asdf/bin:\$HOME/.asdf/shims:\$PATH"

if type asdf >/dev/null 2>&1; then
exit 2
fi
exit 1
EOF
exit_code=$?
set -e

if [ "${exit_code}" -eq 2 ]; then
# asdf already available to remote user, use it
su "$_REMOTE_USER" <<EOF
export HOME="$_REMOTE_USER_HOME"
. "\$HOME/.asdf/asdf.sh"

if asdf list "$PLUGIN" >/dev/null 2>&1; then
echo "$PLUGIN already exists - skipping adding it"
else
asdf plugin add "$PLUGIN" "$REPO"
fi

if [ "${VERSION}" = "latest" ] ; then
resolved_version=\$(asdf latest "$PLUGIN" "$LATESTVERSIONPATTERN")
else
resolved_version=$VERSION
fi

asdf install "$PLUGIN" "\$resolved_version"
asdf global "$PLUGIN" "\$resolved_version"
EOF
else
# asdf is not available to remote user, install it, then update rc files

su "$_REMOTE_USER" <<EOF
export HOME="$_REMOTE_USER_HOME"

git clone --depth=1 \
-c core.eol=lf \
-c core.autocrlf=false \
-c fsck.zeroPaddedFilemode=ignore \
-c fetch.fsck.zeroPaddedFilemode=ignore \
-c receive.fsck.zeroPaddedFilemode=ignore \
"https://github.com/asdf-vm/asdf.git" --branch v0.12.0 "\$HOME/.asdf" 2>&1

. "\$HOME/.asdf/asdf.sh"
if asdf list "$PLUGIN" >/dev/null 2>&1; then
echo "$PLUGIN already exists - skipping adding it"
else
asdf plugin add "$PLUGIN" "$REPO"
fi
EOF


# I resolve the version like this because in bash resolving
# a subshell take prevedent to su, so we must resolve variables
# pre using them in final su clause.
# I hate bash.
resolved_version=$(su "$_REMOTE_USER" <<EOF
export HOME="$_REMOTE_USER_HOME"
. "\$HOME/.asdf/asdf.sh" > /dev/null 2>&1

if [ "${VERSION}" = "latest" ] ; then
asdf latest "$PLUGIN" "$LATESTVERSIONPATTERN"
else
echo $VERSION
fi
EOF
)
su "$_REMOTE_USER" <<EOF
export HOME="$_REMOTE_USER_HOME"
. "\$HOME/.asdf/asdf.sh"
asdf install "$PLUGIN" "$resolved_version"
asdf global "$PLUGIN" "$resolved_version"
EOF
updaterc ". $_REMOTE_USER_HOME/.asdf/asdf.sh"
fi
}

install_via_asdf elixir "$VERSION" "https://github.com/asdf-vm/asdf-elixir.git"
179 changes: 0 additions & 179 deletions src/elixir-asdf/library_scripts.sh

This file was deleted.

Loading
Loading