|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Wrapper script for babashka to be dropped into projects, will run `bb` from |
| 4 | +# the PATH if it exists, or otherwise download it and store it inside the |
| 5 | +# project. When using the system `bb` it will do a version check and warn if the |
| 6 | +# version is older than what is requested. |
| 7 | +# |
| 8 | +# Will look for a `bb_deps.edn` and run that through `clojure` to compute a |
| 9 | +# classpath. |
| 10 | +# |
| 11 | +# Will use rlwrap if it is available. |
| 12 | + |
| 13 | +name=babashka |
| 14 | +babashka_version="0.1.3" |
| 15 | +store_dir="$(pwd)/.store" |
| 16 | +install_dir="${store_dir}/$name-$babashka_version" |
| 17 | + |
| 18 | +system_bb="$(which bb)" |
| 19 | +set -e |
| 20 | + |
| 21 | +# https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash |
| 22 | +vercomp () { |
| 23 | + if [[ $1 == $2 ]] |
| 24 | + then |
| 25 | + return 0 |
| 26 | + fi |
| 27 | + local IFS=. |
| 28 | + local i ver1=($1) ver2=($2) |
| 29 | + # fill empty fields in ver1 with zeros |
| 30 | + for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) |
| 31 | + do |
| 32 | + ver1[i]=0 |
| 33 | + done |
| 34 | + for ((i=0; i<${#ver1[@]}; i++)) |
| 35 | + do |
| 36 | + if [[ -z ${ver2[i]} ]] |
| 37 | + then |
| 38 | + # fill empty fields in ver2 with zeros |
| 39 | + ver2[i]=0 |
| 40 | + fi |
| 41 | + if ((10#${ver1[i]} > 10#${ver2[i]})) |
| 42 | + then |
| 43 | + return 1 |
| 44 | + fi |
| 45 | + if ((10#${ver1[i]} < 10#${ver2[i]})) |
| 46 | + then |
| 47 | + return 2 |
| 48 | + fi |
| 49 | + done |
| 50 | + return 0 |
| 51 | +} |
| 52 | + |
| 53 | +if [[ -f "$system_bb" ]]; then |
| 54 | + bb_path="$system_bb" |
| 55 | +elif [[ -f "$install_dir/bb" ]]; then |
| 56 | + bb_path="$install_dir/bb" |
| 57 | +else |
| 58 | + case "$(uname -s)" in |
| 59 | + Linux*) platform=linux;; |
| 60 | + Darwin*) platform=macos;; |
| 61 | + esac |
| 62 | + |
| 63 | + echo "$name $babashka_version not found, installing to $install_dir..." |
| 64 | + download_url="https://github.com/borkdude/babashka/releases/download/v$babashka_version/babashka-$babashka_version-$platform-amd64.zip" |
| 65 | + |
| 66 | + mkdir -p $install_dir |
| 67 | + echo -e "Downloading $download_url." |
| 68 | + curl -o bb.zip -sL "$download_url" |
| 69 | + unzip -qqo "bb.zip" -d $install_dir |
| 70 | + rm "bb.zip" |
| 71 | + bb_path="$install_dir/bb" |
| 72 | +fi |
| 73 | + |
| 74 | +set +e |
| 75 | +actual_version="$($bb_path --version | sed 's/babashka v//')" |
| 76 | + |
| 77 | +vercomp $actual_version $babashka_version |
| 78 | +case $? in |
| 79 | + 0) ;; # = |
| 80 | + 1) ;; # > |
| 81 | + 2) echo "WARNING: babashka version is $actual_version, expected $babashka_version" ;; # < |
| 82 | +esac |
| 83 | +set -e |
| 84 | + |
| 85 | +try_exec_rlwrap() { |
| 86 | + if [ -x "$(command -v rlwrap)" ]; then |
| 87 | + exec "rlwrap" "$@" |
| 88 | + else |
| 89 | + exec "$@" |
| 90 | + fi |
| 91 | +} |
| 92 | + |
| 93 | +deps_clj="$(pwd)/.store/deps.clj" |
| 94 | + |
| 95 | +ensure_deps_clj() { |
| 96 | + if [[ ! -f "${deps_clj}" ]]; then |
| 97 | + mkdir -p "${store_dir}" |
| 98 | + curl -sL https://raw.githubusercontent.com/borkdude/deps.clj/master/deps.clj -o "${deps_clj}" |
| 99 | + fi |
| 100 | +} |
| 101 | + |
| 102 | +if [[ -f bb_deps.edn ]]; then |
| 103 | + ensure_deps_clj |
| 104 | + # Note this will install clojure-tools in ~/.deps.clj/ClojureTools |
| 105 | + cp="$($bb_path $deps_clj -Srepro -Sdeps-file bb_deps.edn -Spath)" |
| 106 | + try_exec_rlwrap "$bb_path" "-cp" "${cp}" "$@" |
| 107 | +else |
| 108 | + try_exec_rlwrap "$bb_path" "$@" |
| 109 | +fi |
0 commit comments