-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·112 lines (95 loc) · 2.23 KB
/
setup
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
#!/bin/sh -e
# Supports methods such as native or docker.
# See <setup>_setup helpers.
USE_SETUP="${PIKACONFIG_SETUP:-native}"
dispatch_setup_helper() {
log Using "${USE_SETUP?}" setup
"${USE_SETUP?}_setup" "$@"
}
src_root() {
cd "$(dirname "$0")"
pwd
}
BOOTSTRAP=pikaconfig-bootstrap
BINARY=pikaconfig
ROOT="$(src_root)"
SRC="${ROOT?}"
BUILD="$SRC/target/release"
CACHED_BOOTSTRAP="$BUILD/$BOOTSTRAP"
CACHED_BINARY="$BUILD/$BINARY"
# Environment used by pikaconfig binary.
export PIKACONFIG_CONFIG_ROOT="$ROOT"
log() {
echo "$@" >&2
}
log_eval() {
log '$' "$@"
"$@"
}
fatal() {
log "$@"
exit 1
}
load_cargo_env() {
CARGO_ENV="${CARGO_HOME:-$HOME/.cargo}/env"
if [ -f "$CARGO_ENV" ]; then
source "$CARGO_ENV"
fi
}
get_rust() {
if cargo --version >/dev/null 2>&1; then
log "Using existing cargo"
return
fi
# Opportunistically try to find existing Cargo installation.
load_cargo_env
if cargo --version >/dev/null 2>&1; then
log "Using existing cargo"
return
fi
# Use https://rustup.rs/ as package managers often don't have new enough
# rust toolchain.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s - --no-modify-path
# Load environment from the newly installed Rust.
load_cargo_env
}
native_setup() {
get_rust
cargo run \
--manifest-path="$SRC/Cargo.toml" \
--package="$BOOTSTRAP" \
--release \
-- "$@"
cargo run \
--manifest-path="$SRC/Cargo.toml" \
--package="$BINARY" \
--release \
-- "$@"
}
cached_setup() {
"$CACHED_BINARY" "$@"
}
docker_build() {
log_eval $OPTIONAL_SUDO docker run \
--pull=always \
--volume="${SRC?}/cargo-registry:/usr/local/cargo/registry" \
--volume="${SRC?}:/app" \
--rm \
--workdir=/app \
rust:latest \
cargo build "$@"
}
docker_setup_base() {
docker_build --package="$BOOTSTRAP" --release
"$CACHED_BOOTSTRAP" "$@"
docker_build --package="$BINARY" --release
"$CACHED_BINARY" "$@"
}
docker_setup() {
docker_setup_base "$@"
}
sudo_docker_setup() {
OPTIONAL_SUDO=sudo
docker_setup_base "$@"
}
dispatch_setup_helper "$@"