|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# check that 'bun' is installed |
| 6 | + |
| 7 | +if ! command -v bun > /dev/null; then |
| 8 | + echo "bun is not installed. Follow the instructions at https://bun.sh/docs/installation" |
| 9 | + exit 1 |
| 10 | +fi |
| 11 | + |
| 12 | +REPO="https://github.com/wormhole-foundation/example-native-token-transfers.git" |
| 13 | + |
| 14 | +function main { |
| 15 | + branch="" |
| 16 | + |
| 17 | + while [[ $# -gt 0 ]]; do |
| 18 | + key="$1" |
| 19 | + |
| 20 | + case $key in |
| 21 | + -b|--branch) |
| 22 | + branch="$2" |
| 23 | + shift |
| 24 | + shift |
| 25 | + ;; |
| 26 | + -r|--repo) |
| 27 | + REPO="$2" |
| 28 | + shift |
| 29 | + shift |
| 30 | + ;; |
| 31 | + *) |
| 32 | + echo "Unknown option $key" |
| 33 | + exit 1 |
| 34 | + ;; |
| 35 | + esac |
| 36 | + done |
| 37 | + |
| 38 | + path="" |
| 39 | + mkdir -p "$HOME/.ntt-cli" |
| 40 | + |
| 41 | + # check if there's a package.json in the parent directory, with "name": "@wormhole-foundation/ntt-cli" |
| 42 | + if [ -f "$(dirname $0)/package.json" ] && grep -q '"name": "@wormhole-foundation/ntt-cli"' "$(dirname $0)/package.json"; then |
| 43 | + path="$(dirname $0)/.." |
| 44 | + version=$(git -C "$path" rev-parse HEAD) |
| 45 | + dirty=$(git -C "$path" diff --quiet || echo "-dirty") |
| 46 | + echo "$version$dirty" > "$HOME/.ntt-cli/version" |
| 47 | + else |
| 48 | + # if branch is set, use it. otherwise use the latest tag of the form "vX.Y.Z+cli" or the 'cli' branch |
| 49 | + if [ -z "$branch" ]; then |
| 50 | + branch="$(select_branch)" |
| 51 | + fi |
| 52 | + |
| 53 | + # clone to $HOME/.ntt-cli if it doesn't exist, otherwise update it |
| 54 | + echo "Cloning $REPO $branch" |
| 55 | + |
| 56 | + path="$HOME/.ntt-cli/.checkout" |
| 57 | + |
| 58 | + if [ ! -d "$path" ]; then |
| 59 | + git clone --branch "$branch" "$REPO" "$path" |
| 60 | + else |
| 61 | + pushd "$path" |
| 62 | + # update origin url to REPO |
| 63 | + git remote set-url origin "$REPO" |
| 64 | + git fetch origin |
| 65 | + # reset hard |
| 66 | + git reset --hard "origin/$branch" |
| 67 | + version=$(git rev-parse HEAD) |
| 68 | + dirty=$(git diff --quiet || echo "-dirty") |
| 69 | + echo "$version$dirty" > "$HOME/.ntt-cli/version" |
| 70 | + popd |
| 71 | + fi |
| 72 | + fi |
| 73 | + |
| 74 | + absolute_path="$(cd $path && pwd)" |
| 75 | + echo $absolute_path >> "$HOME/.ntt-cli/version" |
| 76 | + |
| 77 | + # jq would be nicer but it's not portable |
| 78 | + # here we make the assumption that the file uses 2 spaces for indentation. |
| 79 | + # this is a bit fragile, but we don't want to catch further nested objects |
| 80 | + # (there might be a "version" in the scripts section, for example) |
| 81 | + version=$(cat "$path/cli/package.json" | grep '^ "version":' | cut -d '"' -f 4) |
| 82 | + echo "$version" >> "$HOME/.ntt-cli/version" |
| 83 | + |
| 84 | + remote_url=$(git -C "$path" remote get-url origin) |
| 85 | + echo "$remote_url" >> "$HOME/.ntt-cli/version" |
| 86 | + |
| 87 | + echo "Installing ntt CLI version $version" |
| 88 | + install_cli "$path" |
| 89 | +} |
| 90 | + |
| 91 | +# function that determines which branch/tag to clone |
| 92 | +function select_branch { |
| 93 | + # if the repo has a tag of the form "vX.Y.Z+cli", use that (the latest one) |
| 94 | + # otherwise we'll use the 'cli' branch |
| 95 | + branch="" |
| 96 | + regex="refs/tags/v[0-9]*\.[0-9]*\.[0-9]*+cli" |
| 97 | + if git ls-remote --tags "$REPO" | grep -q "$regex"; then |
| 98 | + branch="$(git ls-remote --tags "$REPO" | grep "$regex" | sort -V | tail -n 1 | awk '{print $2}')" |
| 99 | + else |
| 100 | + branch="cli" |
| 101 | + fi |
| 102 | + |
| 103 | + echo "$branch" |
| 104 | +} |
| 105 | + |
| 106 | +function install_cli { |
| 107 | + cd "$1" |
| 108 | + |
| 109 | + # if 'ntt' is already installed, uninstall it |
| 110 | + # just check with 'which' |
| 111 | + if which ntt > /dev/null; then |
| 112 | + echo "Removing existing ntt CLI" |
| 113 | + rm $(which ntt) |
| 114 | + fi |
| 115 | + |
| 116 | + # swallow the output of the first install |
| 117 | + # TODO: figure out why it fails the first time. |
| 118 | + bun install > /dev/null 2>&1 || true |
| 119 | + bun install |
| 120 | + |
| 121 | + # make a temporary directory |
| 122 | + |
| 123 | + tmpdir="$(mktemp -d)" |
| 124 | + |
| 125 | + # create a temporary symlink 'npm' to 'bun' |
| 126 | + |
| 127 | + ln -s "$(command -v bun)" "$tmpdir/npm" |
| 128 | + |
| 129 | + # add the temporary directory to the PATH |
| 130 | + |
| 131 | + export PATH="$tmpdir:$PATH" |
| 132 | + |
| 133 | + # swallow the output of the first build |
| 134 | + # TODO: figure out why it fails the first time. |
| 135 | + bun --bun run --filter '*' build > /dev/null 2>&1 || true |
| 136 | + bun --bun run --filter '*' build |
| 137 | + |
| 138 | + # remove the temporary directory |
| 139 | + |
| 140 | + rm -r "$tmpdir" |
| 141 | + |
| 142 | + # now link the CLI |
| 143 | + |
| 144 | + cd cli |
| 145 | + |
| 146 | + bun link |
| 147 | + |
| 148 | + bun link @wormhole-foundation/ntt-cli |
| 149 | +} |
| 150 | + |
| 151 | +main "$@" |
0 commit comments