Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/env bash
0002 #
0003 
0004 # A library to simplify using the SBT launcher from other packages.
0005 # Note: This should be used by tools like giter8/conscript etc.
0006 
0007 # TODO - Should we merge the main SBT script with this library?
0008 
0009 if test -z "$HOME"; then
0010   declare -r script_dir="$(dirname "$script_path")"
0011 else
0012   declare -r script_dir="$HOME/.sbt"
0013 fi
0014 
0015 declare -a residual_args
0016 declare -a java_args
0017 declare -a scalac_args
0018 declare -a sbt_commands
0019 declare -a maven_profiles
0020 declare sbt_default_mem=2048
0021 
0022 if test -x "$JAVA_HOME/bin/java"; then
0023     echo -e "Using $JAVA_HOME as default JAVA_HOME."
0024     echo "Note, this will be overridden by -java-home if it is set."
0025     declare java_cmd="$JAVA_HOME/bin/java"
0026 else
0027     declare java_cmd=java
0028 fi
0029 
0030 echoerr () {
0031   echo 1>&2 "$@"
0032 }
0033 vlog () {
0034   [[ $verbose || $debug ]] && echoerr "$@"
0035 }
0036 dlog () {
0037   [[ $debug ]] && echoerr "$@"
0038 }
0039 
0040 acquire_sbt_jar () {
0041   SBT_VERSION=`awk -F "=" '/sbt\.version/ {print $2}' ./project/build.properties`
0042   URL1=https://dl.bintray.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/${SBT_VERSION}/sbt-launch.jar
0043   JAR=build/sbt-launch-${SBT_VERSION}.jar
0044 
0045   sbt_jar=$JAR
0046 
0047   if [[ ! -f "$sbt_jar" ]]; then
0048     # Download sbt launch jar if it hasn't been downloaded yet
0049     if [ ! -f "${JAR}" ]; then
0050     # Download
0051     printf "Attempting to fetch sbt\n"
0052     JAR_DL="${JAR}.part"
0053     if [ $(command -v curl) ]; then
0054       curl --fail --location --silent ${URL1} > "${JAR_DL}" &&\
0055         mv "${JAR_DL}" "${JAR}"
0056     elif [ $(command -v wget) ]; then
0057       wget --quiet ${URL1} -O "${JAR_DL}" &&\
0058         mv "${JAR_DL}" "${JAR}"
0059     else
0060       printf "You do not have curl or wget installed, please install sbt manually from https://www.scala-sbt.org/\n"
0061       exit -1
0062     fi
0063     fi
0064     if [ ! -f "${JAR}" ]; then
0065     # We failed to download
0066     printf "Our attempt to download sbt locally to ${JAR} failed. Please install sbt manually from https://www.scala-sbt.org/\n"
0067     exit -1
0068     fi
0069     printf "Launching sbt from ${JAR}\n"
0070   fi
0071 }
0072 
0073 execRunner () {
0074   # print the arguments one to a line, quoting any containing spaces
0075   [[ $verbose || $debug ]] && echo "# Executing command line:" && {
0076     for arg; do
0077       if printf "%s\n" "$arg" | grep -q ' '; then
0078         printf "\"%s\"\n" "$arg"
0079       else
0080         printf "%s\n" "$arg"
0081       fi
0082     done
0083     echo ""
0084   }
0085 
0086   "$@"
0087 }
0088 
0089 addJava () {
0090   dlog "[addJava] arg = '$1'"
0091   java_args=( "${java_args[@]}" "$1" )
0092 }
0093 
0094 enableProfile () {
0095   dlog "[enableProfile] arg = '$1'"
0096   maven_profiles=( "${maven_profiles[@]}" "$1" )
0097   export SBT_MAVEN_PROFILES="${maven_profiles[@]}"
0098 }
0099 
0100 addSbt () {
0101   dlog "[addSbt] arg = '$1'"
0102   sbt_commands=( "${sbt_commands[@]}" "$1" )
0103 }
0104 addResidual () {
0105   dlog "[residual] arg = '$1'"
0106   residual_args=( "${residual_args[@]}" "$1" )
0107 }
0108 addDebugger () {
0109   addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$1"
0110 }
0111 
0112 # a ham-fisted attempt to move some memory settings in concert
0113 # so they need not be dicked around with individually.
0114 get_mem_opts () {
0115   local mem=${1:-$sbt_default_mem}
0116   local codecache=$(( $mem / 8 ))
0117   (( $codecache > 128 )) || codecache=128
0118   (( $codecache < 2048 )) || codecache=2048
0119 
0120   echo "-Xms${mem}m -Xmx${mem}m -XX:ReservedCodeCacheSize=${codecache}m"
0121 }
0122 
0123 require_arg () {
0124   local type="$1"
0125   local opt="$2"
0126   local arg="$3"
0127   if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then
0128     echo "$opt requires <$type> argument" 1>&2
0129     exit 1
0130   fi
0131 }
0132 
0133 is_function_defined() {
0134   declare -f "$1" > /dev/null
0135 }
0136 
0137 process_args () {
0138   while [[ $# -gt 0 ]]; do
0139     case "$1" in
0140        -h|-help) usage; exit 1 ;;
0141     -v|-verbose) verbose=1 && shift ;;
0142       -d|-debug) debug=1 && shift ;;
0143 
0144            -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;;
0145            -mem) require_arg integer "$1" "$2" && sbt_mem="$2" && shift 2 ;;
0146      -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;;
0147          -batch) exec </dev/null && shift ;;
0148 
0149        -sbt-jar) require_arg path "$1" "$2" && sbt_jar="$2" && shift 2 ;;
0150    -sbt-version) require_arg version "$1" "$2" && sbt_version="$2" && shift 2 ;;
0151      -java-home) require_arg path "$1" "$2" && java_cmd="$2/bin/java" && export JAVA_HOME=$2 && shift 2 ;;
0152 
0153             -D*) addJava "$1" && shift ;;
0154             -J*) addJava "${1:2}" && shift ;;
0155             -P*) enableProfile "$1" && shift ;;
0156               *) addResidual "$1" && shift ;;
0157     esac
0158   done
0159 
0160   is_function_defined process_my_args && {
0161     myargs=("${residual_args[@]}")
0162     residual_args=()
0163     process_my_args "${myargs[@]}"
0164   }
0165 }
0166 
0167 run() {
0168   # no jar? download it.
0169   [[ -f "$sbt_jar" ]] || acquire_sbt_jar "$sbt_version" || {
0170     # still no jar? uh-oh.
0171     echo "Download failed. Obtain the sbt-launch.jar manually and place it at $sbt_jar"
0172     exit 1
0173   }
0174 
0175   # process the combined args, then reset "$@" to the residuals
0176   process_args "$@"
0177   set -- "${residual_args[@]}"
0178   argumentCount=$#
0179 
0180   # run sbt
0181   execRunner "$java_cmd" \
0182     ${SBT_OPTS:-$default_sbt_opts} \
0183     $(get_mem_opts $sbt_mem) \
0184     ${java_opts} \
0185     ${java_args[@]} \
0186     -jar "$sbt_jar" \
0187     "${sbt_commands[@]}" \
0188     "${residual_args[@]}"
0189 }