|
Scala example source code file (sbt)
The sbt Scala example source code#!/usr/bin/env bash # # A more capable sbt runner, coincidentally also called sbt. # Author: Paul Phillips <paulp@improving.org> # todo - make this dynamic declare -r sbt_release_version="0.13.8" declare -r sbt_unreleased_version="0.13.9-M1" declare -r buildProps="project/build.properties" declare sbt_jar sbt_dir sbt_create sbt_version declare scala_version sbt_explicit_version declare verbose noshare batch trace_level log_level declare sbt_saved_stty debugUs echoerr () { echo >&2 "$@"; } vlog () { [[ -n "$verbose" ]] && echoerr "$@"; } # spaces are possible, e.g. sbt.version = 0.13.0 build_props_sbt () { [[ -r "$buildProps" ]] && \ grep '^sbt\.version' "$buildProps" | tr '=\r' ' ' | awk '{ print $2; }' } update_build_props_sbt () { local ver="$1" local old="$(build_props_sbt)" [[ -r "$buildProps" ]] && [[ "$ver" != "$old" ]] && { perl -pi -e "s/^sbt\.version\b.*\$/sbt.version=${ver}/" "$buildProps" grep -q '^sbt.version[ =]' "$buildProps" || printf "\nsbt.version=%s\n" "$ver" >> "$buildProps" vlog "!!!" vlog "!!! Updated file $buildProps setting sbt.version to: $ver" vlog "!!! Previous value was: $old" vlog "!!!" } } set_sbt_version () { sbt_version="${sbt_explicit_version:-$(build_props_sbt)}" [[ -n "$sbt_version" ]] || sbt_version=$sbt_release_version export sbt_version } # restore stty settings (echo in particular) onSbtRunnerExit() { [[ -n "$sbt_saved_stty" ]] || return vlog "" vlog "restoring stty: $sbt_saved_stty" stty "$sbt_saved_stty" unset sbt_saved_stty } # save stty and trap exit, to ensure echo is reenabled if we are interrupted. trap onSbtRunnerExit EXIT sbt_saved_stty="$(stty -g 2>/dev/null)" vlog "Saved stty: $sbt_saved_stty" # this seems to cover the bases on OSX, and someone will # have to tell me about the others. get_script_path () { local path="$1" [[ -L "$path" ]] || { echo "$path" ; return; } local target="$(readlink "$path")" if [[ "${target:0:1}" == "/" ]]; then echo "$target" else echo "${path%/*}/$target" fi } die() { echo "Aborting: $@" exit 1 } make_url () { version="$1" case "$version" in 0.7.*) echo "http://simple-build-tool.googlecode.com/files/sbt-launch-0.7.7.jar" ;; 0.10.* ) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;; 0.11.[12]) echo "$sbt_launch_repo/org.scala-tools.sbt/sbt-launch/$version/sbt-launch.jar" ;; *) echo "$sbt_launch_repo/org.scala-sbt/sbt-launch/$version/sbt-launch.jar" ;; esac } init_default_option_file () { local overriding_var="${!1}" local default_file="$2" if [[ ! -r "$default_file" && "$overriding_var" =~ ^@(.*)$ ]]; then local envvar_file="${BASH_REMATCH[1]}" if [[ -r "$envvar_file" ]]; then default_file="$envvar_file" fi fi echo "$default_file" } declare -r cms_opts="-XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC" declare -r jit_opts="-XX:ReservedCodeCacheSize=256m -XX:+TieredCompilation" declare -r default_jvm_opts_common="-Xms512m -Xmx1536m -Xss2m $jit_opts $cms_opts" declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy" declare -r latest_28="2.8.2" declare -r latest_29="2.9.3" declare -r latest_210="2.10.5" declare -r latest_211="2.11.7" declare -r script_path="$(get_script_path "$BASH_SOURCE")" declare -r script_name="${script_path##*/}" # some non-read-onlies set with defaults declare java_cmd="java" declare sbt_opts_file="$(init_default_option_file SBT_OPTS .sbtopts)" declare jvm_opts_file="$(init_default_option_file JVM_OPTS .jvmopts)" declare sbt_launch_repo="http://repo.typesafe.com/typesafe/ivy-releases" # pull -J and -D options to give to java. declare -a residual_args declare -a java_args declare -a scalac_args declare -a sbt_commands # args to jvm/sbt via files or environment variables declare -a extra_jvm_opts extra_sbt_opts addJava () { vlog "[addJava] arg = '$1'" java_args+=("$1") } addSbt () { vlog "[addSbt] arg = '$1'" sbt_commands+=("$1") } setThisBuild () { vlog "[addBuild] args = '$@'" local key="$1" && shift addSbt "set $key in ThisBuild := $@" } addScalac () { vlog "[addScalac] arg = '$1'" scalac_args+=("$1") } addResidual () { vlog "[residual] arg = '$1'" residual_args+=("$1") } addResolver () { addSbt "set resolvers += $1" } addDebugger () { addJava "-Xdebug" addJava "-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$1" } setScalaVersion () { [[ "$1" == *"-SNAPSHOT" ]] && addResolver 'Resolver.sonatypeRepo("snapshots")' addSbt "++ $1" } setJavaHome () { java_cmd="$1/bin/java" setThisBuild javaHome "Some(file(\"$1\"))" export JAVA_HOME="$1" export JDK_HOME="$1" export PATH="$JAVA_HOME/bin:$PATH" } setJavaHomeQuietly () { addSbt warn setJavaHome "$1" addSbt info } # if set, use JDK_HOME/JAVA_HOME over java found in path if [[ -e "$JDK_HOME/lib/tools.jar" ]]; then setJavaHomeQuietly "$JDK_HOME" elif [[ -e "$JAVA_HOME/bin/java" ]]; then setJavaHomeQuietly "$JAVA_HOME" fi # directory to store sbt launchers declare sbt_launch_dir="$HOME/.sbt/launchers" [[ -d "$sbt_launch_dir" ]] || mkdir -p "$sbt_launch_dir" [[ -w "$sbt_launch_dir" ]] || sbt_launch_dir="$(mktemp -d -t sbt_extras_launchers.XXXXXX)" java_version () { local version=$("$java_cmd" -version 2>&1 | grep -E -e '(java|openjdk) version' | awk '{ print $3 }' | tr -d \") vlog "Detected Java version: $version" echo "${version:2:1}" } # MaxPermSize critical on pre-8 jvms but incurs noisy warning on 8+ default_jvm_opts () { local v="$(java_version)" if [[ $v -ge 8 ]]; then echo "$default_jvm_opts_common" else echo "-XX:MaxPermSize=384m $default_jvm_opts_common" fi } build_props_scala () { if [[ -r "$buildProps" ]]; then versionLine="$(grep '^build.scala.versions' "$buildProps")" versionString="${versionLine##build.scala.versions=}" echo "${versionString%% .*}" fi } execRunner () { # print the arguments one to a line, quoting any containing spaces vlog "# Executing command line:" && { for arg; do if [[ -n "$arg" ]]; then if printf "%s\n" "$arg" | grep -q ' '; then printf >&2 "\"%s\"\n" "$arg" else printf >&2 "%s\n" "$arg" fi fi done vlog "" } [[ -n "$batch" ]] && exec </dev/null exec "$@" } jar_url () { make_url "$1" } jar_file () { echo "$sbt_launch_dir/$1/sbt-launch.jar" } download_url () { local url="$1" local jar="$2" echoerr "Downloading sbt launcher for $sbt_version:" echoerr " From $url" echoerr " To $jar" mkdir -p "${jar%/*}" && { if which curl >/dev/null; then curl --fail --silent --location "$url" --output "$jar" elif which wget >/dev/null; then wget --quiet -O "$jar" "$url" fi } && [[ -r "$jar" ]] } acquire_sbt_jar () { sbt_url="$(jar_url "$sbt_version")" sbt_jar="$(jar_file "$sbt_version")" [[ -r "$sbt_jar" ]] || download_url "$sbt_url" "$sbt_jar" } usage () { cat < |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.