|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | + |
| 4 | +### ------------------------------- ### |
| 5 | +### Helper methods for BASH scripts ### |
| 6 | +### ------------------------------- ### |
| 7 | + |
| 8 | +realpath () { |
| 9 | +( |
| 10 | + TARGET_FILE="$1" |
| 11 | + FIX_CYGPATH="$2" |
| 12 | + |
| 13 | + cd "$(dirname "$TARGET_FILE")" |
| 14 | + TARGET_FILE=$(basename "$TARGET_FILE") |
| 15 | + |
| 16 | + COUNT=0 |
| 17 | + while [ -L "$TARGET_FILE" -a $COUNT -lt 100 ] |
| 18 | + do |
| 19 | + TARGET_FILE=$(readlink "$TARGET_FILE") |
| 20 | + cd "$(dirname "$TARGET_FILE")" |
| 21 | + TARGET_FILE=$(basename "$TARGET_FILE") |
| 22 | + COUNT=$(($COUNT + 1)) |
| 23 | + done |
| 24 | + |
| 25 | + # make sure we grab the actual windows path, instead of cygwin's path. |
| 26 | + if [[ "x$FIX_CYGPATH" != "x" ]]; then |
| 27 | + echo "$(cygwinpath "$(pwd -P)/$TARGET_FILE")" |
| 28 | + else |
| 29 | + echo "$(pwd -P)/$TARGET_FILE" |
| 30 | + fi |
| 31 | +) |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +# Uses uname to detect if we're in the odd cygwin environment. |
| 36 | +is_cygwin() { |
| 37 | + local os=$(uname -s) |
| 38 | + case "$os" in |
| 39 | + CYGWIN*) return 0 ;; |
| 40 | + *) return 1 ;; |
| 41 | + esac |
| 42 | +} |
| 43 | + |
| 44 | +# TODO - Use nicer bash-isms here. |
| 45 | +CYGWIN_FLAG=$(if is_cygwin; then echo true; else echo false; fi) |
| 46 | + |
| 47 | + |
| 48 | +# This can fix cygwin style /cygdrive paths so we get the |
| 49 | +# windows style paths. |
| 50 | +cygwinpath() { |
| 51 | + local file="$1" |
| 52 | + if [[ "$CYGWIN_FLAG" == "true" ]]; then |
| 53 | + echo $(cygpath -w $file) |
| 54 | + else |
| 55 | + echo $file |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +. "$(dirname "$(realpath "$0")")/sbt-launch-lib.bash" |
| 60 | + |
| 61 | + |
| 62 | +declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy" |
| 63 | +declare -r sbt_opts_file=".sbtopts" |
| 64 | +declare -r etc_sbt_opts_file="${sbt_home}/conf/sbtopts" |
| 65 | +declare -r win_sbt_opts_file="${sbt_home}/conf/sbtconfig.txt" |
| 66 | + |
| 67 | +usage() { |
| 68 | + cat <<EOM |
| 69 | +Usage: $script_name [options] |
| 70 | +
|
| 71 | + -h | -help print this message |
| 72 | + -v | -verbose this runner is chattier |
| 73 | + -d | -debug set sbt log level to debug |
| 74 | + -no-colors disable ANSI color codes |
| 75 | + -sbt-create start sbt even if current directory contains no sbt project |
| 76 | + -sbt-dir <path> path to global settings/plugins directory (default: ~/.sbt) |
| 77 | + -sbt-boot <path> path to shared boot directory (default: ~/.sbt/boot in 0.11 series) |
| 78 | + -ivy <path> path to local Ivy repository (default: ~/.ivy2) |
| 79 | + -mem <integer> set memory options (default: $sbt_mem, which is $(get_mem_opts $sbt_mem)) |
| 80 | + -no-share use all local caches; no sharing |
| 81 | + -no-global uses global caches, but does not use global ~/.sbt directory. |
| 82 | + -jvm-debug <port> Turn on JVM debugging, open at the given port. |
| 83 | + -batch Disable interactive mode |
| 84 | +
|
| 85 | + # sbt version (default: from project/build.properties if present, else latest release) |
| 86 | + -sbt-version <version> use the specified version of sbt |
| 87 | + -sbt-jar <path> use the specified jar as the sbt launcher |
| 88 | + -sbt-rc use an RC version of sbt |
| 89 | + -sbt-snapshot use a snapshot version of sbt |
| 90 | +
|
| 91 | + # java version (default: java from PATH, currently $(java -version 2>&1 | grep version)) |
| 92 | + -java-home <path> alternate JAVA_HOME |
| 93 | +
|
| 94 | + # jvm options and output control |
| 95 | + JAVA_OPTS environment variable, if unset uses "$java_opts" |
| 96 | + SBT_OPTS environment variable, if unset uses "$default_sbt_opts" |
| 97 | + .sbtopts if this file exists in the current directory, it is |
| 98 | + prepended to the runner args |
| 99 | + /etc/sbt/sbtopts if this file exists, it is prepended to the runner args |
| 100 | + -Dkey=val pass -Dkey=val directly to the java runtime |
| 101 | + -J-X pass option -X directly to the java runtime |
| 102 | + (-J is stripped) |
| 103 | + -S-X add -X to sbt's scalacOptions (-S is stripped) |
| 104 | +
|
| 105 | +In the case of duplicated or conflicting options, the order above |
| 106 | +shows precedence: JAVA_OPTS lowest, command line options highest. |
| 107 | +EOM |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +process_my_args () { |
| 113 | + while [[ $# -gt 0 ]]; do |
| 114 | + case "$1" in |
| 115 | + -no-colors) addJava "-Dsbt.log.noformat=true" && shift ;; |
| 116 | + -no-share) addJava "$noshare_opts" && shift ;; |
| 117 | + -no-global) addJava "-Dsbt.global.base=$(pwd)/project/.sbtboot" && shift ;; |
| 118 | + -sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;; |
| 119 | + -sbt-dir) require_arg path "$1" "$2" && addJava "-Dsbt.global.base=$2" && shift 2 ;; |
| 120 | + -debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;; |
| 121 | + -batch) exec </dev/null && shift ;; |
| 122 | + |
| 123 | + -sbt-create) sbt_create=true && shift ;; |
| 124 | + |
| 125 | + *) addResidual "$1" && shift ;; |
| 126 | + esac |
| 127 | + done |
| 128 | + |
| 129 | + # Now, ensure sbt version is used. |
| 130 | + [[ "${sbt_version}XXX" != "XXX" ]] && addJava "-Dsbt.version=$sbt_version" |
| 131 | +} |
| 132 | + |
| 133 | +loadConfigFile() { |
| 134 | + cat "$1" | sed '/^\#/d' | while read line; do |
| 135 | + eval echo $line |
| 136 | + done |
| 137 | +} |
| 138 | + |
| 139 | +# TODO - Pull in config based on operating system... (MSYS + cygwin should pull in txt file). |
| 140 | +# Here we pull in the global settings configuration. |
| 141 | +[[ -f "$etc_sbt_opts_file" ]] && set -- $(loadConfigFile "$etc_sbt_opts_file") "$@" |
| 142 | +# -- Windows behavior stub'd |
| 143 | +# JAVA_OPTS=$(cat "$WDIR/sbtconfig.txt" | sed -e 's/\r//g' -e 's/^#.*$//g' | sed ':a;N;$!ba;s/\n/ /g') |
| 144 | + |
| 145 | + |
| 146 | +# Pull in the project-level config file, if it exists. |
| 147 | +[[ -f "$sbt_opts_file" ]] && set -- $(loadConfigFile "$sbt_opts_file") "$@" |
| 148 | + |
| 149 | + |
| 150 | +run "$@" |
| 151 | + |
0 commit comments