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