-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsetup.sh
More file actions
364 lines (317 loc) · 14.7 KB
/
Copy pathsetup.sh
File metadata and controls
364 lines (317 loc) · 14.7 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env bash
# ---------------------------------------------------------------------------
# Healthcare App (Angular) + Nia CLI Dev Container Setup
#
# This script prepares the dev container so a user can:
# 1. Run the Angular application (npm start / ng serve on port 4200)
# 2. Use AI coding agents: GitHub Copilot CLI, OpenCode, Claude Code
# 3. Use the Nia CLI installed from the public telerik/project-nia repo
# (latest GitHub release, falling back to the latest pre-release)
#
# Design goals: idempotent, defensive, and self-reporting.
# ---------------------------------------------------------------------------
set -euo pipefail
# Resolve the workspace root (parent of this script's .devcontainer directory)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORKSPACE_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
# --- Nia release source -----------------------------------------------------
NIA_REPO="telerik/project-nia"
# Track non-fatal installation failures for the final summary
INSTALL_FAILURES=""
mark_failure() {
INSTALL_FAILURES="${INSTALL_FAILURES} $1"
}
verify_command() {
local cmd="$1"
local version_flag="${2:---version}"
if command -v "$cmd" &> /dev/null && "$cmd" "$version_flag" &> /dev/null; then
return 0
fi
return 1
}
# ---------------------------------------------------------------------------
# Step 1: Shell profile
# ---------------------------------------------------------------------------
if [ -f "${SCRIPT_DIR}/.bashrc" ]; then
echo "→ Configuring custom .bashrc..."
cp "${SCRIPT_DIR}/.bashrc" "${HOME}/.bashrc"
echo "✓ .bashrc configured"
fi
# Place a custom npm configuration (e.g. private registry / auth token) into
# the container user's home so every npm command — the agent installs below and
# the project's `npm install` — respects it. The file is gitignored and copied
# from the host into .devcontainer/.npmrc.
if [ -f "${SCRIPT_DIR}/.npmrc" ]; then
echo "→ Configuring custom .npmrc..."
cp "${SCRIPT_DIR}/.npmrc" "${HOME}/.npmrc"
chmod 600 "${HOME}/.npmrc"
echo "✓ .npmrc configured"
else
echo "ℹ No custom .devcontainer/.npmrc found — using default npm configuration"
fi
# ---------------------------------------------------------------------------
# Step 2: Base system dependencies
# ---------------------------------------------------------------------------
echo "→ Updating package manager cache..."
if sudo apt-get update -y; then
echo "✓ Package cache updated"
else
echo "✗ Error: Failed to update package cache"
exit 1
fi
echo "→ Installing base dependencies (curl, jq, ca-certificates, gnupg)..."
if sudo apt-get install -y curl jq ca-certificates gnupg; then
echo "✓ Base dependencies installed"
else
echo "✗ Error: Failed to install base dependencies"
exit 1
fi
# ---------------------------------------------------------------------------
# Step 3: AI coding agents (installed globally via npm, always latest)
# ---------------------------------------------------------------------------
# Generic idempotent npm agent installer with retry + verification.
# Args: <display name> <command to verify> <npm package>
install_npm_agent() {
local name="$1"
local cmd="$2"
local pkg="$3"
echo "→ Installing ${name} (latest)..."
if ! command -v npm &> /dev/null; then
echo "✗ Error: npm is required but not available"
return 1
fi
if command -v "$cmd" &> /dev/null; then
echo "✓ ${name} already installed ($("$cmd" --version 2>&1 | head -1))"
return 0
fi
local max_attempts=3
local attempt=1
while [ "$attempt" -le "$max_attempts" ]; do
echo " Installing ${pkg}@latest (attempt ${attempt}/${max_attempts})..."
if npm install -g "${pkg}@latest"; then
if command -v "$cmd" &> /dev/null; then
echo "✓ ${name} installed ($("$cmd" --version 2>&1 | head -1))"
return 0
fi
echo " Warning: package installed but '${cmd}' command not found on PATH"
else
echo " Error: npm install failed (attempt ${attempt}/${max_attempts})"
fi
if [ "$attempt" -lt "$max_attempts" ]; then
echo " Retrying in 5 seconds..."
sleep 5
fi
attempt=$((attempt + 1))
done
echo "✗ Error: Failed to install ${name} after ${max_attempts} attempts"
return 1
}
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Installing AI Coding Agents"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
install_npm_agent "GitHub Copilot CLI" "copilot" "@github/copilot" || mark_failure "copilot"
install_npm_agent "OpenCode" "opencode" "opencode-ai" || mark_failure "opencode"
install_npm_agent "Claude Code" "claude" "@anthropic-ai/claude-code" || mark_failure "claude"
# ---------------------------------------------------------------------------
# Step 4: Nia CLI (from telerik/project-nia)
# ---------------------------------------------------------------------------
# Detect operating system and CPU architecture for the correct release asset.
detect_os() {
case "$(uname -s)" in
Linux*) echo "linux" ;;
Darwin*) echo "darwin" ;;
*) echo "unsupported" ;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) echo "x86_64" ;;
aarch64|arm64) echo "aarch64" ;;
*) echo "unsupported" ;;
esac
}
# Resolve the target release tag:
# - Prefer the latest stable (non-draft, non-prerelease) release.
# - Fall back to the latest pre-release when no stable release exists.
# Emits: "<tag>|<is_prerelease>" on stdout, or empty string on failure.
resolve_nia_release() {
local api="https://api.github.com/repos/${NIA_REPO}/releases?per_page=100"
local auth_header=()
# Use GITHUB_TOKEN if present to avoid low unauthenticated rate limits.
if [ -n "${GITHUB_TOKEN:-}" ]; then
auth_header=(-H "Authorization: Bearer ${GITHUB_TOKEN}")
fi
local releases_json
if ! releases_json=$(curl -fsSL "${auth_header[@]}" \
-H "Accept: application/vnd.github+json" "${api}" 2>/dev/null); then
return 1
fi
local stable_tag
stable_tag=$(echo "${releases_json}" \
| jq -r 'map(select(.draft == false and .prerelease == false)) | .[0].tag_name // empty')
if [ -n "${stable_tag}" ]; then
echo "${stable_tag}|false"
return 0
fi
local prerelease_tag
prerelease_tag=$(echo "${releases_json}" \
| jq -r 'map(select(.draft == false)) | .[0].tag_name // empty')
if [ -n "${prerelease_tag}" ]; then
echo "${prerelease_tag}|true"
return 0
fi
return 1
}
install_nia_cli() {
echo "→ Installing Nia CLI from ${NIA_REPO}..."
if command -v nia &> /dev/null; then
echo "✓ Nia CLI already installed ($(nia --version 2>&1 | head -1))"
return 0
fi
local os arch
os="$(detect_os)"
arch="$(detect_arch)"
if [ "${os}" = "unsupported" ] || [ "${arch}" = "unsupported" ]; then
echo "✗ Error: Unsupported platform ($(uname -s)/$(uname -m)) for Nia CLI"
return 1
fi
echo " Detected platform: ${arch}-${os}"
local resolved tag is_prerelease
if ! resolved="$(resolve_nia_release)"; then
echo "✗ Error: Could not resolve a Nia release from GitHub"
return 1
fi
tag="${resolved%%|*}"
is_prerelease="${resolved##*|}"
local version="${tag#v}"
if [ "${is_prerelease}" = "true" ]; then
echo " No stable release found — using latest pre-release: ${tag}"
else
echo " Using latest stable release: ${tag}"
fi
# Primary path: the vendor install.sh from the resolved release tag.
local installer_url="https://github.com/${NIA_REPO}/releases/download/${tag}/install.sh"
local tmp_installer
tmp_installer="$(mktemp)"
if curl -fsSL "${installer_url}" -o "${tmp_installer}" 2>/dev/null && [ -s "${tmp_installer}" ]; then
echo " Running vendor installer (version ${version})..."
local installer_args=(--version "${version}")
if [ "${is_prerelease}" = "true" ]; then
installer_args=(--pre-release)
fi
if sh "${tmp_installer}" "${installer_args[@]}"; then
rm -f "${tmp_installer}"
if command -v nia &> /dev/null; then
echo "✓ Nia CLI installed ($(nia --version 2>&1 | head -1))"
return 0
fi
fi
echo " Vendor installer did not complete — falling back to direct binary download"
fi
rm -f "${tmp_installer}"
# Fallback path: download the platform binary directly and verify checksum.
local asset="nia-${version}-${arch}-${os}"
local binary_url="https://github.com/${NIA_REPO}/releases/download/${tag}/${asset}"
local sums_url="https://github.com/${NIA_REPO}/releases/download/${tag}/SHA256SUMS.asc"
local install_dir="${HOME}/.local/bin"
mkdir -p "${install_dir}"
local tmp_bin
tmp_bin="$(mktemp)"
echo " Downloading ${asset}..."
if ! curl -fsSL "${binary_url}" -o "${tmp_bin}"; then
echo "✗ Error: Failed to download Nia binary (${asset})"
rm -f "${tmp_bin}"
return 1
fi
# Best-effort checksum verification against the (clear-signed) SHA256SUMS.
local expected_sum
if expected_sum=$(curl -fsSL "${sums_url}" 2>/dev/null | grep -F "${asset}" | awk '{print $1}' | head -1) \
&& [ -n "${expected_sum}" ]; then
local actual_sum
actual_sum=$(sha256sum "${tmp_bin}" | awk '{print $1}')
if [ "${expected_sum}" != "${actual_sum}" ]; then
echo "✗ Error: Checksum verification failed for ${asset}"
rm -f "${tmp_bin}"
return 1
fi
echo " Checksum verified"
else
echo " Warning: could not verify checksum (SHA256SUMS unavailable)"
fi
install -m 755 "${tmp_bin}" "${install_dir}/nia"
rm -f "${tmp_bin}"
if "${install_dir}/nia" --version &> /dev/null; then
echo "✓ Nia CLI installed to ${install_dir}/nia ($("${install_dir}/nia" --version 2>&1 | head -1))"
return 0
fi
echo "✗ Error: Nia CLI was downloaded but is not runnable"
return 1
}
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Installing Nia CLI"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
install_nia_cli || mark_failure "nia"
# ---------------------------------------------------------------------------
# Step 5: Project bootstrap (install Angular dependencies)
# ---------------------------------------------------------------------------
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Installing Angular project dependencies"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
if [ -f "${WORKSPACE_ROOT}/package.json" ]; then
echo "→ Running npm install..."
if (cd "${WORKSPACE_ROOT}" && npm install); then
echo "✓ Angular dependencies installed"
else
echo "✗ Error: npm install failed"
mark_failure "npm-install"
fi
else
echo "✗ Warning: package.json not found at ${WORKSPACE_ROOT}"
fi
# ---------------------------------------------------------------------------
# Step 6: Summary and next steps
# ---------------------------------------------------------------------------
echo ""
echo "═══════════════════════════════════════════════"
echo " Dev Container Setup Summary"
echo "═══════════════════════════════════════════════"
verify_command "node" && echo " ✓ Node.js: $(node --version)" || echo " ✗ Node.js: not installed"
verify_command "npm" && echo " ✓ npm: $(npm --version)" || echo " ✗ npm: not installed"
verify_command "copilot" && echo " ✓ GitHub Copilot CLI: $(copilot --version 2>&1 | head -1)" || echo " ✗ GitHub Copilot CLI: not installed"
verify_command "opencode" && echo " ✓ OpenCode: $(opencode --version 2>&1 | head -1)" || echo " ✗ OpenCode: not installed"
verify_command "claude" && echo " ✓ Claude Code: $(claude --version 2>&1 | head -1)" || echo " ✗ Claude Code: not installed"
verify_command "nia" && echo " ✓ Nia CLI: $(nia --version 2>&1 | head -1)" || echo " ✗ Nia CLI: not installed"
echo "═══════════════════════════════════════════════"
echo ""
echo "───────────────────────────────────────────────"
echo " NEXT STEPS TO FINALIZE YOUR SETUP (manual)"
echo "───────────────────────────────────────────────"
echo " Authentication is required before the agents and Nia CLI can run."
echo ""
echo " 1. Authenticate the GitHub CLI (used by Nia workflows):"
echo " gh auth login"
echo ""
echo " 2. Authenticate the AI coding agents you plan to use:"
echo " copilot # GitHub Copilot CLI — sign in when prompted"
echo " claude # Claude Code — set ANTHROPIC_API_KEY or sign in"
echo " opencode auth login"
echo ""
echo " 3. Explore the Project Nia CLI commands and onboarding tutorials:"
echo " nia --help # explore available commands"
echo " nia learn # guided, hands-on onboarding tutorials"
echo ""
echo " 4. Run the Angular application:"
echo " npm start # serves on http://localhost:4200"
echo "───────────────────────────────────────────────"
if [ -n "${INSTALL_FAILURES}" ]; then
failures_trimmed="${INSTALL_FAILURES# }"
echo ""
echo "✗ Some components failed to install: ${failures_trimmed// /, }"
echo " Review the logs above; you can re-run: bash .devcontainer/setup.sh"
exit 1
fi
echo ""
echo "✓ Dev container setup complete."