Skip to content

Sbt:Example:StartupScript

sbt 시작을 위한 bash 스크립트.

Source code

#!/usr/bin/env bash

SCRIPT_DIR=`cd "$(dirname "${BASH_SOURCE[0]}")" ; echo "$PWD"`

VERBOSE_FLAG=1
PLATFORM_NAME=`uname -s`

case "$PLATFORM_NAME" in
Darwin)
    JDK_VERSION=11.0.2
    JDK_PLATFORM_NAME=osx
    ARCHITECTURE=x64
    JDK_SRC_URL_PREFIX="https://download.java.net/java/GA/jdk11/9/GPL"
    JDK_SHA256=f365750d4be6111be8a62feda24e265d97536712bc51783162982b8ad96a70ee
    ;;
Linux)
    JDK_VERSION=11.0.2
    JDK_PLATFORM_NAME=linux
    ARCHITECTURE=x64
    JDK_SRC_URL_PREFIX="https://download.java.net/java/GA/jdk11/9/GPL"
    JDK_SHA256=99be79935354f5c0df1ad293620ea36d13f48ec3ea870c838f20c504c9668b57
    ;;
*)
    print_error "This platform is not supported: $PLATFORM_NAME"
    exit 1
    ;;
esac

JDK_FILE_NAME="openjdk-${JDK_VERSION}_${JDK_PLATFORM_NAME}-${ARCHITECTURE}_bin.tar.gz"
JDK_SRC_URL="$JDK_SRC_URL_PREFIX/$JDK_FILE_NAME"
JDK_DEST=$SCRIPT_DIR/var/$JDK_FILE_NAME
JDK_INSTALL_DIR=$SCRIPT_DIR/var/jdk-$JDK_VERSION
JDK_HOME=$JDK_INSTALL_DIR/jdk-$JDK_VERSION.jdk/Contents/Home
JAVA_CMD=$JDK_HOME/bin/java

SBT_VERSION=1.2.8
SBT_FILE_NAME=sbt-${SBT_VERSION}.tgz
SBT_SRC_URL="https://sbt-downloads.cdnedge.bluemix.net/releases/v${SBT_VERSION}/${SBT_FILE_NAME}"
SBT_SHA256=9bb9212541176d6fcce7bd12e4cf8a9c9649f5b63f88b3aff474e0b02c7cfe58
SBT_DEST=$SCRIPT_DIR/var/$SBT_FILE_NAME
SBT_INSTALL_DIR=$SCRIPT_DIR/var/sbt-$SBT_VERSION
SBT_HOME=$SBT_INSTALL_DIR/sbt
SBT_CMD=$SBT_HOME/bin/sbt

function print_message
{
    echo "$@"
}

function print_verbose
{
    if [[ $VERBOSE_FLAG -ne 0 ]]; then
        echo "$@"
    fi
}

function print_information
{
    echo -e "\033[32m$@\033[0m"
}

function print_warning
{
    echo -e "\033[33m$@\033[0m"
}

function print_error
{
    echo -e "\033[31m$@\033[0m" 1>&2
}

function mkdirs
{
    if [[ ! -d "$1" ]]; then
        print_verbose "Create directory: $1"
        mkdir -p "$1"
    fi
}

function curl_download
{
    local url=$1
    local dest=$2

    if [[ -f "$dest" ]]; then
        print_verbose "Exists file: $dest"
        return 0
    fi

    local which_curl=`which curl 2> /dev/null`

    if [[ ! -x "$which_curl" ]]; then
        print_error "Not found curl executable"
        return 1
    fi

    print_information "Download URL: $url"
    print_information "Destination path: $dest"

    "$which_curl" -L "$url" -o "$dest"
    local retval=$?

    if [[ $retval -ne 0 ]]; then
        print_error "curl program error: $retval"
        return $retval
    fi

    if [[ ! -f "$dest" ]]; then
        print_error "Not found downloaded file: $dest"
        return 1
    fi

    return 0
}

function checksum_sha256
{
    local file=$1
    local checksum=$2

    if [[ ! -f "$file" ]]; then
        print_error "Not exists file: $file"
        return 1
    fi

    local which_shasum=`which shasum 2> /dev/null`
    local which_cut=`which cut 2> /dev/null`

    if [[ ! -x "$which_shasum" ]]; then
        print_error "Not found shasum executable"
        return 1
    fi
    if [[ ! -x "$which_cut" ]]; then
        print_error "Not found cut executable"
        return 1
    fi

    local actual=`"$which_shasum" -a 256 "$file" | "$which_cut" -d ' ' -f1`

    if [[ "$checksum" == "$actual" ]]; then
        print_verbose "The checksum values match."
        return 0
    else
        print_error "Checksum values do not match."
        print_error " - Test file: $file"
        print_error " - Expect value: $checksum"
        print_error " - Actual value: $actual"
        return 1
    fi
}

function get_file_extension
{
    local name=$1
    echo "${name#${name%.*}}"
}

function get_file_name
{
    local name=$1
    local ext=`get_file_extension $name`
    echo "${name%${ext}}"
}

function unarchive
{
    local file=$1
    local dest=$2

    if [[ ! -f "$file" ]]; then
        print_error "Not exists file: $file"
        return 1
    fi

    if [[ ! -d "$dest" ]]; then
        mkdir -p "$dest"
    fi

    local which_unzip=`which unzip 2> /dev/null`
    local which_tar=`which tar 2> /dev/null`

    if [[ ! -x "$which_unzip" ]]; then
        print_error "Not found unzip executable"
        return 1
    fi
    if [[ ! -x "$which_tar" ]]; then
        print_error "Not found tar executable"
        return 1
    fi

    local file_ext=`get_file_extension "$file"`
    local file_name=`get_file_name $file`

    print_information "Proceed with unarchive: $file"
    print_information "Output directory: $dest"

    case $file_ext in
    .[zZ][iI][pP])
        "$which_unzip" -qo "$file" -d "$dest"
        ;;
    *)
        "$which_tar" -xf "$file" -C "$dest"
        ;;
    esac

    local retval=$?
    if [[ $retval -ne 0 ]]; then
        print_warning "An error occurred while unarchive was in progress: $retval"
    fi

    return $retval
}

function import_package
{
    local url=$1
    local file=$2
    local checksum=$3
    local unarchive_dir=$4
    local test_cmd=$5

    if [[ -x "$test_cmd" ]]; then
        print_verbose "Exists command: $test_cmd"
        return 0
    fi

    mkdirs "$unarchive_dir"

    if [[ ! -d "$unarchive_dir" ]]; then
        print_error "Not exists installation directory: $unarchive_dir"
        return 1
    fi

    if [[ -f "$file" ]]; then
        print_verbose "Exists downloaded file: $file"
    else
        print_verbose "Not found downloaded file: $file"
        curl_download "$url" "$file"
    fi

    checksum_sha256 "$file" "$checksum"
    local retval=$?

    if [[ $retval -ne 0 ]]; then
        print_error "Checksum failed."
        print_error "Please make sure it is the correct file."
        return 1
    fi

    unarchive "$file" "$unarchive_dir"

    if [[ ! -x "$test_cmd" ]]; then
        print_error "Not found executable command: $test_cmd"
        return 1
    fi

    return 0
}

function check_exitcode
{
    local code=$?
    if [[ $code -ne 0 ]]; then
        exit $code
    fi
}

import_package "$JDK_SRC_URL" "$JDK_DEST" "$JDK_SHA256" "$JDK_HOME" "$JAVA_CMD" && check_exitcode
import_package "$SBT_SRC_URL" "$SBT_DEST" "$SBT_SHA256" "$SBT_HOME" "$SBT_CMD"  && check_exitcode

if [[ ! -x "$SBT_CMD" ]]; then
    print_error "Not found sbt executable: $SBT_CMD"
    exit 1
fi

JAVA_HOME="$JDK_HOME" PATH="$JAVA_HOME/bin:$PATH" "$SBT_CMD" "$@"

See also

Favorite site