-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·195 lines (176 loc) · 5.28 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·195 lines (176 loc) · 5.28 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env bash
#
# build.sh — build/install script for the Cx compiler
#
# Usage:
# ./build.sh # checks deps, builds, installs binary and updates std/
# ./build.sh check # only checks if ldc2 and dub are installed
# ./build.sh install-deps # shows how to install ldc2/dub on your distro
# ./build.sh build # only builds (dub build --compiler=ldc2 --build=release)
# ./build.sh install-bin # only copies the already-built `cx` binary to ~/.local/bin/
# ./build.sh update-std # only updates ~/.cx/std/ with the project's std/
# ./build.sh path # shows how to add ~/.local/bin to PATH (zsh/bash/fish)
# ./build.sh help # shows this help
#
set -euo pipefail
# ---------- colors ----------
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
BOLD='\033[1m'
NC='\033[0m'
info() { echo -e "${BLUE}[*]${NC} $1"; }
ok() { echo -e "${GREEN}[ok]${NC} $1"; }
warn() { echo -e "${YELLOW}[!]${NC} $1"; }
err() { echo -e "${RED}[error]${NC} $1" >&2; }
# ---------- paths ----------
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BIN_NAME="cx"
INSTALL_DIR="$HOME/.local/bin"
CX_HOME="$HOME/.cx"
CX_STD_DEST="$CX_HOME/std"
CX_STD_SRC="$PROJECT_DIR/std"
# ---------- distro-specific install instructions ----------
show_install_instructions() {
echo
echo -e "${BOLD}Install ldc2 + dub:${NC}"
echo
echo -e "${YELLOW}Debian / Ubuntu${NC} (and derivatives):"
echo " sudo apt update && sudo apt install -y ldc dub"
echo
echo -e "${YELLOW}RedHat / Fedora / CentOS / RHEL${NC}:"
echo " sudo dnf install -y ldc dub"
echo " # older systems using yum:"
echo " sudo yum install -y ldc dub"
echo
echo -e "${YELLOW}Arch / Manjaro${NC}:"
echo " sudo pacman -S --needed ldc dub"
echo
echo "If a package isn't available in your distro's repos, install via the"
echo "official D installer (works on any distro):"
echo ' curl -fsS https://dlang.org/install.sh | bash -s ldc'
echo " see also: https://dlang.org/download.html"
echo
}
# ---------- dependency check ----------
check_deps() {
local missing=0
if command -v ldc2 >/dev/null 2>&1; then
ok "ldc2 found: $(ldc2 --version | head -n1)"
else
err "ldc2 not found in PATH"
missing=1
fi
if command -v dub >/dev/null 2>&1; then
ok "dub found: $(dub --version | head -n1)"
else
err "dub not found in PATH"
missing=1
fi
if [[ $missing -eq 1 ]]; then
echo
warn "Install the missing dependencies before continuing."
show_install_instructions
return 1
fi
ok "All dependencies are installed."
return 0
}
# ---------- build ----------
do_build() {
cd "$PROJECT_DIR"
info "Building with dub (ldc2, release)..."
dub build --compiler=ldc2 --build=release
if [[ -f "$PROJECT_DIR/$BIN_NAME" ]]; then
ok "Build finished: $PROJECT_DIR/$BIN_NAME"
else
err "Build finished but binary '$BIN_NAME' was not found in $PROJECT_DIR"
return 1
fi
}
# ---------- install binary ----------
install_bin() {
if [[ ! -f "$PROJECT_DIR/$BIN_NAME" ]]; then
err "Binary '$BIN_NAME' not found in $PROJECT_DIR. Run './build.sh build' first."
return 1
fi
mkdir -p "$INSTALL_DIR"
cp "$PROJECT_DIR/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME"
chmod +x "$INSTALL_DIR/$BIN_NAME"
ok "Binary copied to $INSTALL_DIR/$BIN_NAME"
}
# ---------- update std ----------
update_std() {
if [[ ! -d "$CX_STD_SRC" ]]; then
err "std/ directory not found in $PROJECT_DIR"
return 1
fi
mkdir -p "$CX_HOME"
rm -rf "$CX_STD_DEST"
cp -r "$CX_STD_SRC" "$CX_STD_DEST"
ok "std/ updated at $CX_STD_DEST"
}
# ---------- PATH instructions ----------
show_path_instructions() {
echo
echo -e "${BOLD}Add ~/.local/bin to PATH:${NC}"
echo
echo -e "${YELLOW}zsh${NC} (~/.zshrc):"
echo ' echo '"'"'export PATH="$HOME/.local/bin:$PATH"'"'"' >> ~/.zshrc && source ~/.zshrc'
echo
echo -e "${YELLOW}bash${NC} (~/.bashrc):"
echo ' echo '"'"'export PATH="$HOME/.local/bin:$PATH"'"'"' >> ~/.bashrc && source ~/.bashrc'
echo
echo -e "${YELLOW}fish${NC}:"
echo ' fish_add_path $HOME/.local/bin'
echo
echo "Then verify with: which cx"
echo
}
show_help() {
sed -n '2,15p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
}
# ---------- main ----------
main() {
local cmd="${1:-all}"
case "$cmd" in
check)
check_deps
;;
install-deps)
show_install_instructions
;;
build)
check_deps
do_build
;;
install-bin)
install_bin
;;
update-std)
update_std
;;
path)
show_path_instructions
;;
help|-h|--help)
show_help
;;
all)
check_deps
do_build
install_bin
update_std
echo
ok "All done! '$BIN_NAME' installed at $INSTALL_DIR and std/ at $CX_STD_DEST"
show_path_instructions
;;
*)
err "Unknown command: $cmd"
show_help
exit 1
;;
esac
}
main "$@"