-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·312 lines (270 loc) · 9.6 KB
/
install.sh
File metadata and controls
executable file
·312 lines (270 loc) · 9.6 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
set -euo pipefail
SCRIPT_NAME="install.sh"
REPO_URL_SSH_DEFAULT="git@github.com:forepath/tsef.git"
REPO_URL_HTTPS_DEFAULT="https://github.com/forepath/tsef.git"
NVM_VERSION="v0.40.3"
NODE_VERSION=""
CLONE_DIR="devkit"
BRANCH=""
RUN_UPDATE=1
ASSUME_YES=0
# Colors
if [ -t 1 ]; then
RED="\033[0;31m"; GREEN="\033[0;32m"; YELLOW="\033[1;33m"; BLUE="\033[0;34m"; NC="\033[0m"
else
RED=""; GREEN=""; YELLOW=""; BLUE=""; NC=""
fi
timestamp() { date +"%Y-%m-%dT%H:%M:%S%z"; }
log_info() { echo -e "$(timestamp) ${BLUE}[INFO]${NC} $*"; }
log_warn() { echo -e "$(timestamp) ${YELLOW}[WARN]${NC} $*"; }
log_error() { echo -e "$(timestamp) ${RED}[ERROR]${NC} $*" 1>&2; }
die() { log_error "$*"; exit 1; }
usage() {
cat <<EOF
${SCRIPT_NAME} - Multi-distro installer and workspace scaffolder
Options:
--repo <url> Git repository URL to clone (default: SSH ${REPO_URL_SSH_DEFAULT})
--dir <path> Directory to clone into (default: ${CLONE_DIR})
--branch <name> Optional branch to checkout after clone
--node <ver> Node version for nvm (default: workspace .nvmrc or --lts)
--no-update Skip system package index update/upgrade
-y, --yes Assume "yes" for package manager prompts
-h, --help Show this help message
EOF
}
REPO_URL=""
while [ "${1-}" != "" ]; do
case "$1" in
--repo)
REPO_URL="$2"; shift 2 ;;
--dir)
CLONE_DIR="$2"; shift 2 ;;
--branch)
BRANCH="$2"; shift 2 ;;
--node)
NODE_VERSION="$2"; shift 2 ;;
--no-update)
RUN_UPDATE=0; shift ;;
-y|--yes)
ASSUME_YES=1; shift ;;
-h|--help)
usage; exit 0 ;;
*)
usage; die "Unknown option: $1" ;;
esac
done
require_cmd() {
command -v "$1" >/dev/null 2>&1 || return 1
}
ensure_sudo() {
if [ "${EUID}" -ne 0 ]; then
if require_cmd sudo; then
SUDO="sudo"
else
die "This script requires root privileges or 'sudo' installed."
fi
else
SUDO=""
fi
}
detect_distro() {
if [ -r /etc/os-release ]; then
. /etc/os-release
echo "${ID}"
else
uname -s | tr '[:upper:]' '[:lower:]'
fi
}
detect_pkg_manager() {
if require_cmd apt-get; then echo apt; return; fi
if require_cmd dnf; then echo dnf; return; fi
if require_cmd yum; then echo yum; return; fi
if require_cmd zypper; then echo zypper; return; fi
if require_cmd pacman; then echo pacman; return; fi
die "No supported package manager found (apt, dnf, yum, zypper, pacman)."
}
pkg_install() {
local pkgs=("$@")
local pm="$(detect_pkg_manager)"
local yes_flag=""
[ "$ASSUME_YES" -eq 1 ] && yes_flag="-y"
case "$pm" in
apt)
[ "$RUN_UPDATE" -eq 1 ] && { $SUDO apt-get update -y || true; $SUDO DEBIAN_FRONTEND=noninteractive apt-get upgrade -y || true; }
$SUDO DEBIAN_FRONTEND=noninteractive apt-get install -y "${pkgs[@]}" ;;
dnf)
[ "$RUN_UPDATE" -eq 1 ] && { $SUDO dnf -y update || true; }
$SUDO dnf -y install "${pkgs[@]}" ;;
yum)
[ "$RUN_UPDATE" -eq 1 ] && { $SUDO yum -y update || true; }
$SUDO yum -y install "${pkgs[@]}" ;;
zypper)
[ "$RUN_UPDATE" -eq 1 ] && { $SUDO zypper -n refresh || true; $SUDO zypper -n update || true; }
$SUDO zypper -n install "${pkgs[@]}" ;;
pacman)
[ "$RUN_UPDATE" -eq 1 ] && { $SUDO pacman -Sy --noconfirm || true; }
$SUDO pacman -S --needed --noconfirm "${pkgs[@]}" ;;
*) die "Unsupported package manager: $pm" ;;
esac
}
install_prereqs() {
log_info "Detecting distribution and package manager..."
local distro pm
distro="$(detect_distro)"
pm="$(detect_pkg_manager)"
log_info "Distro: ${distro}, Package manager: ${pm}"
case "$pm" in
apt)
pkg_install ca-certificates curl git bash build-essential openssh-client
;;
dnf|yum)
pkg_install ca-certificates curl git bash @"Development Tools" gcc-c++ make openssh-clients
;;
zypper)
pkg_install ca-certificates curl git bash -t pattern devel_basis || pkg_install ca-certificates curl git bash gcc gcc-c++ make openssh
;;
pacman)
pkg_install ca-certificates curl git bash base-devel openssh
;;
esac
}
install_nvm_and_node() {
if [ ! -d "$HOME/.nvm" ]; then
log_info "Installing nvm ${NVM_VERSION}..."
curl -fsSL "https://raw.githubusercontent.com/nvm-sh/nvm/${NVM_VERSION}/install.sh" | bash
else
log_info "nvm already installed."
fi
# shellcheck disable=SC1090
[ -s "$HOME/.nvm/nvm.sh" ] && . "$HOME/.nvm/nvm.sh"
# shellcheck disable=SC1091
[ -s "/usr/share/nvm/init-nvm.sh" ] && . "/usr/share/nvm/init-nvm.sh" || true
command -v nvm >/dev/null 2>&1 || die "nvm not found after installation."
local resolved_node="${NODE_VERSION}"
if [ -z "${resolved_node}" ]; then
if [ -f "${CLONE_DIR}/.nvmrc" ]; then
resolved_node="$(tr -d '\n' < "${CLONE_DIR}/.nvmrc")"
log_info "Using Node version from ${CLONE_DIR}/.nvmrc: ${resolved_node}"
else
resolved_node="--lts"
log_info "No .nvmrc found, defaulting to Node ${resolved_node}"
fi
else
log_info "Using Node version from --node flag: ${resolved_node}"
fi
nvm install ${resolved_node}
nvm use ${resolved_node}
local nodev
nodev="$(node -v)"
log_info "Using Node ${nodev}"
}
install_nx_and_deps() {
log_info "Installing Nx CLI globally..."
npm install -g nx || die "Failed to install Nx globally"
}
clone_or_update_repo() {
if [ -d "${CLONE_DIR}/.git" ]; then
log_info "Repository exists at ${CLONE_DIR}. Fetching latest..."
git -C "${CLONE_DIR}" fetch --all --prune
if [ -n "${BRANCH}" ]; then
git -C "${CLONE_DIR}" checkout "${BRANCH}"
git -C "${CLONE_DIR}" pull --ff-only origin "${BRANCH}" || true
else
git -C "${CLONE_DIR}" pull --ff-only || true
fi
else
local chosen_url
if [ -n "${REPO_URL}" ]; then
chosen_url="${REPO_URL}"
else
chosen_url="${REPO_URL_SSH_DEFAULT}"
fi
log_info "Preparing to clone ${chosen_url} into ${CLONE_DIR}..."
# Add host to known_hosts to avoid interactive prompt if possible
if command -v ssh-keyscan >/dev/null 2>&1; then
local host
if echo "${chosen_url}" | grep -q '^git@'; then
host="$(echo "${chosen_url}" | sed -E 's#^git@([^:]+):.*#\1#')"
elif echo "${chosen_url}" | grep -q '^https?://'; then
host="$(echo "${chosen_url}" | sed -E 's#^https?://([^/]+)/.*#\1#')"
else
host="github.com"
fi
mkdir -p "$HOME/.ssh"
touch "$HOME/.ssh/known_hosts"
ssh-keyscan -H "$host" 2>/dev/null >> "$HOME/.ssh/known_hosts" || true
fi
log_info "Cloning via ${chosen_url}..."
if ! git clone "${chosen_url}" "${CLONE_DIR}"; then
if echo "${chosen_url}" | grep -q '^git@'; then
# Fallback to HTTPS
local https_url
if [ "${chosen_url}" = "${REPO_URL_SSH_DEFAULT}" ]; then
https_url="${REPO_URL_HTTPS_DEFAULT}"
else
https_url="$(echo "${chosen_url}" | sed -E 's#^git@([^:]+):(.+)#https://\1/\2#')"
fi
log_warn "SSH clone failed. Falling back to HTTPS: ${https_url}"
git clone "${https_url}" "${CLONE_DIR}" || die "Failed to clone repository via HTTPS as well."
else
die "Failed to clone repository: ${chosen_url}"
fi
fi
if [ -n "${BRANCH}" ]; then
git -C "${CLONE_DIR}" checkout "${BRANCH}"
fi
fi
}
init_new_repo() {
log_info "Initializing new repository..."
# Remove existing .git folder
if [ -d .git ]; then
log_info "Removing existing .git folder..."
rm -rf .git
fi
# Initialize new repository with main branch
git init -b main
git add .
# Create initial commit with conventional commit format
local commit_msg="feat: initial workspace setup
- Install system prerequisites and build tools
- Setup Node.js via nvm with workspace .nvmrc
- Install Nx CLI and workspace dependencies
- Initialize new git repository
Co-authored-by: install.sh <install@devkit>"
git commit -m "${commit_msg}" || {
log_warn "Failed to create initial commit. Repository initialized but uncommitted."
return 0
}
log_info "New repository initialized with initial commit."
}
setup_workspace() {
log_info "Setting up workspace dependencies..."
cd "${CLONE_DIR}"
# Ensure nvm in subshell
[ -s "$HOME/.nvm/nvm.sh" ] && . "$HOME/.nvm/nvm.sh"
# Respect project's .nvmrc if available
if [ -f .nvmrc ]; then
nvm install >/dev/null 2>&1 || true
nvm use >/dev/null 2>&1 || true
fi
if [ -f package.json ]; then
npm ci || npm install
else
log_warn "No package.json found; skipping npm install."
fi
if command -v nx >/dev/null 2>&1; then
npx nx reset || true
fi
}
trap 'log_error "An error occurred. See logs above."' ERR
log_info "Starting installation and setup..."
ensure_sudo
install_prereqs
clone_or_update_repo
install_nvm_and_node
install_nx_and_deps
setup_workspace
init_new_repo
log_info "TSEF installed and workspace is ready in '${CLONE_DIR}'."