From e82d4a380412fa0fecad0a0e1c08778613ac9b79 Mon Sep 17 00:00:00 2001 From: Daisuke NISHISAKA Date: Fri, 24 Jul 2020 18:30:42 +0900 Subject: [PATCH] first commit --- bin/install | 41 +++++++++++++++++++++++++++++++++++++ bin/list-all | 10 +++++++++ bin/utils.sh | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100755 bin/install create mode 100755 bin/list-all create mode 100755 bin/utils.sh diff --git a/bin/install b/bin/install new file mode 100755 index 0000000..ebcfcb0 --- /dev/null +++ b/bin/install @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +set -e + +source "$(dirname "$0")/utils.sh" + +install_perl() { + local install_type=$1 + local version=$2 + local install_path=$3 + + if [ "$install_type" != "version" ]; then + echoerr "Cannot install specific ref from source, sorry." + echoerr "For a list of available versions, see \`asdf list-all perl\`." + exit 1 + fi + install_or_update_perl_build + + echo "perl-build $version $install_path" + $(perl_build_path) "$version" "$install_path" +} + +install_default_perl_packages() { + local default_perl_packages="${HOME}/.default-perl-packages" + + if [ ! -f $default_perl_packages ]; then return; fi + + cat "$default_perl_packages" | while read -r name; do + echo -ne "\nInstalling \033[33m${name}\033[39m perl package... " + PATH="$ASDF_INSTALL_PATH/bin:$PATH" pip install "$name" > /dev/null 2>&1 && rc=$? || rc=$? + if [[ $rc -eq 0 ]]; then + echo -e "\033[32mSUCCESS\033[39m" + else + echo -e "\033[31mFAIL\033[39m" + fi + done +} + +ensure_perl_build_installed +install_perl "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH" +install_default_perl_packages diff --git a/bin/list-all b/bin/list-all new file mode 100755 index 0000000..65e4c7c --- /dev/null +++ b/bin/list-all @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +source "$(dirname "$0")/utils.sh" + +list_all() { + install_or_update_perl_build + $(perl_build_path) --definitions | tr '\n' ' ' +} + +list_all diff --git a/bin/utils.sh b/bin/utils.sh new file mode 100755 index 0000000..b4170a0 --- /dev/null +++ b/bin/utils.sh @@ -0,0 +1,57 @@ +echoerr() { + printf "\033[0;31m%s\033[0m" "$1" >&2 +} + +ensure_perl_build_installed() { + if [ ! -f "$(perl_build_path)" ]; then + download_perl_build + fi +} + +download_perl_build() { + echo "Downloading perl-build..." >&2 + local plenv_url="https://github.com/tokuhirom/plenv.git" + local perl_build_url="https://github.com/tokuhirom/Perl-Build.git" + git clone $plenv_url "$(plenv_path)" + git clone $perl_build_url "$(plenv_path)/plugins/perl-build/" +} + +perl_build_path() { + echo "$(plenv_path)/plugins/perl-build/bin/perl-build" +} + +update_perl_build() { + cd "$(plenv_path)" && git fetch && git reset --hard origin/master > /dev/null 2>&1 +} + +plenv_path() { + echo "$(dirname $(dirname $0))/plenv" +} + +plenv_update_timestamp_path() { + echo "$(dirname $(dirname "$0"))/plenv_last_update" +} + +plenv_should_update() { + update_timeout=3600 + update_timestamp_path=$(plenv_update_timestamp_path) + + if [ ! -f "$update_timestamp_path" ]; then + return 0 + fi + + last_update=$(cat "$update_timestamp_path") + current_timestamp=$(date +%s) + invalidated_at=$(($last_update + $update_timeout)) + + [ $invalidated_at -lt $current_timestamp ] +} + +install_or_update_perl_build() { + if [ ! -f "$(perl_build_path)" ]; then + download_perl_build + elif plenv_should_update; then + update_perl_build + date +%s > "$(plenv_update_timestamp_path)" + fi +}