update fixshebang for AIX's sed
[james-home.git] / bin / lein
blob01909f981ceb43cc0fb666d8627774cb1ce7a259
1 #!/usr/bin/env bash
3 # Ensure this file is executable via `chmod a+x lein`, then place it
4 # somewhere on your $PATH, like ~/bin. The rest of Leiningen will be
5 # installed upon first run into the ~/.lein/self-installs directory.
7 export LEIN_VERSION="2.7.1"
9 case $LEIN_VERSION in
10 *SNAPSHOT) SNAPSHOT="YES" ;;
11 *) SNAPSHOT="NO" ;;
12 esac
14 if [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "msys" ]]; then
15 delimiter=";"
16 else
17 delimiter=":"
20 if [[ "$OSTYPE" == "cygwin" ]]; then
21 cygwin=true
22 else
23 cygwin=false
26 function command_not_found {
27 >&2 echo "Leiningen coundn't find $1 in your \$PATH ($PATH), which is required."
28 exit 1
31 function make_native_path {
32 # ensure we have native paths
33 if $cygwin && [[ "$1" == /* ]]; then
34 echo -n "$(cygpath -wp "$1")"
35 elif [[ "$OSTYPE" == "msys" && "$1" == /?/* ]]; then
36 echo -n "$(sh -c "(cd $1 2</dev/null && pwd -W) || echo $1 | sed 's/^\\/\([a-z]\)/\\1:/g'")"
37 else
38 echo -n "$1"
42 # usage : add_path PATH_VAR [PATH]...
43 function add_path {
44 local path_var="$1"
45 shift
46 while [ -n "$1" ];do
47 # http://bashify.com/?Useful_Techniques:Indirect_Variables:Indirect_Assignment
48 if [[ -z ${!path_var} ]]; then
49 export ${path_var}="$(make_native_path "$1")"
50 else
51 export ${path_var}="${!path_var}${delimiter}$(make_native_path "$1")"
53 shift
54 done
57 function download_failed_message {
58 echo "Failed to download $1 (exit code $2)"
59 echo "It's possible your HTTP client's certificate store does not have the"
60 echo "correct certificate authority needed. This is often caused by an"
61 echo "out-of-date version of libssl. It's also possible that you're behind a"
62 echo "firewall and haven't set HTTP_PROXY and HTTPS_PROXY."
65 function self_install {
66 if [ -r "$LEIN_JAR" ]; then
67 echo "The self-install jar already exists at $LEIN_JAR."
68 echo "If you wish to re-download, delete it and rerun \"$0 self-install\"."
69 exit 1
71 echo "Downloading Leiningen to $LEIN_JAR now..."
72 mkdir -p "$(dirname "$LEIN_JAR")"
73 LEIN_URL="https://github.com/technomancy/leiningen/releases/download/$LEIN_VERSION/leiningen-$LEIN_VERSION-standalone.zip"
74 $HTTP_CLIENT "$LEIN_JAR.pending" "$LEIN_URL"
75 local exit_code=$?
76 if [ $exit_code == 0 ]; then
77 # TODO: checksum
78 mv -f "$LEIN_JAR.pending" "$LEIN_JAR"
79 else
80 rm "$LEIN_JAR.pending" 2> /dev/null
81 download_failed_message "$LEIN_URL" "$exit_code"
82 exit 1
86 function check_root {
87 local -i user_id
88 # Thank you for the complexity, Solaris
89 if [ `uname` = "SunOS" -a -x /usr/xpg4/bin/id ]; then
90 user_id=$(/usr/xpg4/bin/id -u 2>/dev/null || echo 0)
91 else
92 user_id=$(id -u 2>/dev/null || echo 0)
94 [ $user_id -eq 0 -a "$LEIN_ROOT" = "" ] && return 0
95 return 1
98 if check_root; then
99 echo "WARNING: You're currently running as root; probably by accident."
100 echo "Press control-C to abort or Enter to continue as root."
101 echo "Set LEIN_ROOT to disable this warning."
102 read _
105 NOT_FOUND=1
106 ORIGINAL_PWD="$PWD"
107 while [ ! -r "$PWD/project.clj" ] && [ "$PWD" != "/" ] && [ $NOT_FOUND -ne 0 ]
109 cd ..
110 if [ "$(dirname "$PWD")" = "/" ]; then
111 NOT_FOUND=0
112 cd "$ORIGINAL_PWD"
114 done
116 export LEIN_HOME="${LEIN_HOME:-"$HOME/.lein"}"
118 for f in "/etc/leinrc" "$LEIN_HOME/leinrc" ".leinrc"; do
119 if [ -e "$f" ]; then
120 source "$f"
122 done
124 if $cygwin; then
125 export LEIN_HOME=$(cygpath -w "$LEIN_HOME")
128 LEIN_JAR="$LEIN_HOME/self-installs/leiningen-$LEIN_VERSION-standalone.jar"
130 # normalize $0 on certain BSDs
131 if [ "$(dirname "$0")" = "." ]; then
132 SCRIPT="$(which "$(basename "$0")")"
133 if [ -z "$SCRIPT" ]; then
134 SCRIPT="$0"
136 else
137 SCRIPT="$0"
140 # resolve symlinks to the script itself portably
141 while [ -h "$SCRIPT" ] ; do
142 ls=$(ls -ld "$SCRIPT")
143 link=$(expr "$ls" : '.*-> \(.*\)$')
144 if expr "$link" : '/.*' > /dev/null; then
145 SCRIPT="$link"
146 else
147 SCRIPT="$(dirname "$SCRIPT"$)/$link"
149 done
151 BIN_DIR="$(dirname "$SCRIPT")"
153 export LEIN_JVM_OPTS="${LEIN_JVM_OPTS-"-XX:+TieredCompilation -XX:TieredStopAtLevel=1"}"
155 # This needs to be defined before we call HTTP_CLIENT below
156 if [ "$HTTP_CLIENT" = "" ]; then
157 if type -p curl >/dev/null 2>&1; then
158 if [ "$https_proxy" != "" ]; then
159 CURL_PROXY="-x $https_proxy"
161 HTTP_CLIENT="curl $CURL_PROXY -f -L -o"
162 else
163 HTTP_CLIENT="wget -O"
168 # When :eval-in :classloader we need more memory
169 grep -E -q '^\s*:eval-in\s+:classloader\s*$' project.clj 2> /dev/null && \
170 export LEIN_JVM_OPTS="$LEIN_JVM_OPTS -Xms64m -Xmx512m"
172 if [ -r "$BIN_DIR/../src/leiningen/version.clj" ]; then
173 # Running from source checkout
174 LEIN_DIR="$(dirname "$BIN_DIR")"
176 # Need to use lein release to bootstrap the leiningen-core library (for aether)
177 if [ ! -r "$LEIN_DIR/leiningen-core/.lein-bootstrap" ]; then
178 echo "Leiningen is missing its dependencies."
179 echo "Please run \"lein bootstrap\" in the leiningen-core/ directory"
180 echo "with a stable release of Leiningen. See CONTRIBUTING.md for details."
181 exit 1
184 # If project.clj for lein or leiningen-core changes, we must recalculate
185 LAST_PROJECT_CHECKSUM=$(cat "$LEIN_DIR/.lein-project-checksum" 2> /dev/null)
186 PROJECT_CHECKSUM=$(sum "$LEIN_DIR/project.clj" "$LEIN_DIR/leiningen-core/project.clj")
187 if [ "$PROJECT_CHECKSUM" != "$LAST_PROJECT_CHECKSUM" ]; then
188 if [ -r "$LEIN_DIR/.lein-classpath" ]; then
189 rm "$LEIN_DIR/.lein-classpath"
193 # Use bin/lein to calculate its own classpath.
194 if [ ! -r "$LEIN_DIR/.lein-classpath" ] && [ "$1" != "classpath" ]; then
195 echo "Recalculating Leiningen's classpath."
196 ORIG_PWD="$PWD"
197 cd "$LEIN_DIR"
199 LEIN_NO_USER_PROFILES=1 $0 classpath .lein-classpath
200 sum "$LEIN_DIR/project.clj" "$LEIN_DIR/leiningen-core/project.clj" > \
201 .lein-project-checksum
202 cd "$ORIG_PWD"
205 mkdir -p "$LEIN_DIR/target/classes"
206 export LEIN_JVM_OPTS="$LEIN_JVM_OPTS -Dclojure.compile.path=$LEIN_DIR/target/classes"
207 add_path CLASSPATH "$LEIN_DIR/leiningen-core/src/" "$LEIN_DIR/leiningen-core/resources/" \
208 "$LEIN_DIR/test:$LEIN_DIR/target/classes" "$LEIN_DIR/src" ":$LEIN_DIR/resources"
210 if [ -r "$LEIN_DIR/.lein-classpath" ]; then
211 add_path CLASSPATH "$(cat "$LEIN_DIR/.lein-classpath" 2> /dev/null)"
212 else
213 add_path CLASSPATH "$(cat "$LEIN_DIR/leiningen-core/.lein-bootstrap" 2> /dev/null)"
215 else # Not running from a checkout
216 add_path CLASSPATH "$LEIN_JAR"
218 BOOTCLASSPATH="-Xbootclasspath/a:$LEIN_JAR"
220 if [ ! -r "$LEIN_JAR" -a "$1" != "self-install" ]; then
221 self_install
225 if [ ! -x "$JAVA_CMD" ] && ! type -f java >/dev/null
226 then
227 >&2 echo "Leiningen coundn't find 'java' executable, which is required."
228 >&2 echo "Please either set JAVA_CMD or put java (>=1.6) in your \$PATH ($PATH)."
229 exit 1
232 export LEIN_JAVA_CMD="${LEIN_JAVA_CMD:-${JAVA_CMD:-java}}"
234 if [[ -z "${DRIP_INIT+x}" && "$(basename "$LEIN_JAVA_CMD")" == *drip* ]]; then
235 export DRIP_INIT="$(printf -- '-e\n(require (quote leiningen.repl))')"
236 export DRIP_INIT_CLASS="clojure.main"
239 # Support $JAVA_OPTS for backwards-compatibility.
240 export JVM_OPTS="${JVM_OPTS:-"$JAVA_OPTS"}"
242 # Handle jline issue with cygwin not propagating OSTYPE through java subprocesses: https://github.com/jline/jline2/issues/62
243 cygterm=false
244 if $cygwin; then
245 case "$TERM" in
246 rxvt* | xterm* | vt*) cygterm=true ;;
247 esac
250 if $cygterm; then
251 LEIN_JVM_OPTS="$LEIN_JVM_OPTS -Djline.terminal=jline.UnixTerminal"
252 stty -icanon min 1 -echo > /dev/null 2>&1
255 # TODO: investigate http://skife.org/java/unix/2011/06/20/really_executable_jars.html
256 # If you're packaging this for a package manager (.deb, homebrew, etc)
257 # you need to remove the self-install and upgrade functionality or see lein-pkg.
258 if [ "$1" = "self-install" ]; then
259 if [ -r "$BIN_DIR/../src/leiningen/version.clj" ]; then
260 echo "Running self-install from a checkout is not supported."
261 echo "See CONTRIBUTING.md for SNAPSHOT-specific build instructions."
262 exit 1
264 echo "Manual self-install is deprecated; it will run automatically when necessary."
265 self_install
266 elif [ "$1" = "upgrade" ] || [ "$1" = "downgrade" ]; then
267 if [ "$LEIN_DIR" != "" ]; then
268 echo "The upgrade task is not meant to be run from a checkout."
269 exit 1
271 if [ $SNAPSHOT = "YES" ]; then
272 echo "The upgrade task is only meant for stable releases."
273 echo "See the \"Bootstrapping\" section of CONTRIBUTING.md."
274 exit 1
276 if [ ! -w "$SCRIPT" ]; then
277 echo "You do not have permission to upgrade the installation in $SCRIPT"
278 exit 1
279 else
280 TARGET_VERSION="${2:-stable}"
281 echo "The script at $SCRIPT will be upgraded to the latest $TARGET_VERSION version."
282 echo -n "Do you want to continue [Y/n]? "
283 read RESP
284 case "$RESP" in
285 y|Y|"")
286 echo
287 echo "Upgrading..."
288 TARGET="/tmp/lein-$$-upgrade"
289 if $cygwin; then
290 TARGET=$(cygpath -w "$TARGET")
292 LEIN_SCRIPT_URL="https://github.com/technomancy/leiningen/raw/$TARGET_VERSION/bin/lein"
293 $HTTP_CLIENT "$TARGET" "$LEIN_SCRIPT_URL"
294 if [ $? == 0 ]; then
295 cmp -s "$TARGET" "$SCRIPT"
296 if [ $? == 0 ]; then
297 echo "Leiningen is already up-to-date."
299 mv "$TARGET" "$SCRIPT" && chmod +x "$SCRIPT"
300 exec "$SCRIPT" version
301 else
302 download_failed_message "$LEIN_SCRIPT_URL"
303 fi;;
305 echo "Aborted."
306 exit 1;;
307 esac
309 else
310 if $cygwin; then
311 # When running on Cygwin, use Windows-style paths for java
312 ORIGINAL_PWD=$(cygpath -w "$ORIGINAL_PWD")
315 # apply context specific CLASSPATH entries
316 if [ -f .lein-classpath ]; then
317 add_path CLASSPATH "$(cat .lein-classpath)"
320 if [ -n "$DEBUG" ]; then
321 echo "Leiningen's classpath: $CLASSPATH"
324 if [ -r .lein-fast-trampoline ]; then
325 export LEIN_FAST_TRAMPOLINE='y'
328 if [ "$LEIN_FAST_TRAMPOLINE" != "" ] && [ -r project.clj ]; then
329 INPUTS="$* $(cat project.clj) $LEIN_VERSION $(test -f "$LEIN_HOME/profiles.clj" && cat "$LEIN_HOME/profiles.clj")"
331 if command -v shasum >/dev/null 2>&1; then
332 SUM="shasum"
333 elif command -v sha1sum >/dev/null 2>&1; then
334 SUM="sha1sum"
335 else
336 command_not_found "sha1sum or shasum"
339 export INPUT_CHECKSUM=$(echo "$INPUTS" | $SUM | cut -f 1 -d " ")
340 # Just don't change :target-path in project.clj, mkay?
341 TRAMPOLINE_FILE="target/trampolines/$INPUT_CHECKSUM"
342 else
343 if hash mktemp 2>/dev/null; then
344 # Check if mktemp is available before using it
345 TRAMPOLINE_FILE="$(mktemp /tmp/lein-trampoline-XXXXXXXXXXXXX)"
346 else
347 TRAMPOLINE_FILE="/tmp/lein-trampoline-$$"
349 trap "rm -f $TRAMPOLINE_FILE" EXIT
352 if $cygwin; then
353 TRAMPOLINE_FILE=$(cygpath -w "$TRAMPOLINE_FILE")
356 if [ "$INPUT_CHECKSUM" != "" ] && [ -r "$TRAMPOLINE_FILE" ]; then
357 if [ -n "$DEBUG" ]; then
358 echo "Fast trampoline with $TRAMPOLINE_FILE."
360 exec sh -c "exec $(cat "$TRAMPOLINE_FILE")"
361 else
362 export TRAMPOLINE_FILE
363 "$LEIN_JAVA_CMD" \
364 "${BOOTCLASSPATH[@]}" \
365 -Dfile.encoding=UTF-8 \
366 -Dmaven.wagon.http.ssl.easy=false \
367 -Dmaven.wagon.rto=10000 \
368 $LEIN_JVM_OPTS \
369 -Dleiningen.original.pwd="$ORIGINAL_PWD" \
370 -Dleiningen.script="$SCRIPT" \
371 -classpath "$CLASSPATH" \
372 clojure.main -m leiningen.core.main "$@"
374 EXIT_CODE=$?
376 if $cygterm ; then
377 stty icanon echo > /dev/null 2>&1
380 ## TODO: [ -r "$TRAMPOLINE_FILE" ] may be redundant? A trampoline file
381 ## is always generated these days.
382 if [ -r "$TRAMPOLINE_FILE" ] && [ "$LEIN_TRAMPOLINE_WARMUP" = "" ]; then
383 TRAMPOLINE="$(cat "$TRAMPOLINE_FILE")"
384 if [ "$INPUT_CHECKSUM" = "" ]; then
385 rm "$TRAMPOLINE_FILE"
387 if [ "$TRAMPOLINE" = "" ]; then
388 exit $EXIT_CODE
389 else
390 exec sh -c "exec $TRAMPOLINE"
392 else
393 exit $EXIT_CODE