forked from norwegianredcross/devcontainer-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·187 lines (160 loc) · 6.35 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·187 lines (160 loc) · 6.35 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
#!/bin/bash
# install.sh - First-time install of devcontainer-toolbox (image mode)
# Run with: curl -fsSL https://raw.githubusercontent.com/helpers-no/devcontainer-toolbox/main/install.sh | bash
set -e
REPO="helpers-no/devcontainer-toolbox"
IMAGE="ghcr.io/$REPO:latest"
TEMPLATE_URL="https://raw.githubusercontent.com/$REPO/main/devcontainer-user-template.json"
echo "Installing devcontainer-toolbox from $REPO..."
echo ""
# ─── 1. Check Docker is available ────────────────────────────────────────────
if ! command -v docker &> /dev/null; then
echo "Error: Docker is not installed or not in PATH."
echo ""
echo "Install Docker Desktop from: https://www.docker.com/products/docker-desktop"
echo "Then run this script again."
exit 1
fi
if ! docker info >/dev/null 2>&1; then
echo "Error: Docker is installed but not running."
echo ""
echo "Please start Docker Desktop (or Rancher Desktop / Colima) and try again."
exit 1
fi
# ─── 2. Backup existing .devcontainer/ ───────────────────────────────────────
if [ -d ".devcontainer.backup" ]; then
echo "Error: .devcontainer.backup/ already exists."
echo ""
echo "Remove or rename it before re-running this script, e.g.:"
echo " mv .devcontainer.backup .devcontainer.backup.old"
exit 1
fi
if [ -d ".devcontainer" ]; then
echo "Found existing .devcontainer/ directory."
echo "Creating backup at .devcontainer.backup/..."
mv .devcontainer .devcontainer.backup
echo "Backup created."
echo ""
fi
# ─── 3. Download devcontainer-user-template.json ─────────────────────────────
mkdir -p .devcontainer
echo "Downloading devcontainer.json from $TEMPLATE_URL..."
if command -v curl >/dev/null 2>&1; then
if ! curl -fsSL "$TEMPLATE_URL" -o .devcontainer/devcontainer.json; then
echo "Error: Failed to download devcontainer-user-template.json"
exit 1
fi
elif command -v wget >/dev/null 2>&1; then
if ! wget -qO .devcontainer/devcontainer.json "$TEMPLATE_URL"; then
echo "Error: Failed to download devcontainer-user-template.json"
exit 1
fi
else
echo "Error: Neither 'curl' nor 'wget' is available"
exit 1
fi
if [ ! -s .devcontainer/devcontainer.json ]; then
echo "Error: Downloaded file is empty"
exit 1
fi
echo "Created .devcontainer/devcontainer.json"
# ─── 4. Ensure .vscode/extensions.json recommends Dev Containers ─────────────
EXT_ID="ms-vscode-remote.remote-containers"
EXT_FILE=".vscode/extensions.json"
mkdir -p .vscode
if [ -f "$EXT_FILE" ]; then
if grep -q "$EXT_ID" "$EXT_FILE" 2>/dev/null; then
echo "Dev Containers extension already in $EXT_FILE"
elif command -v python3 >/dev/null 2>&1; then
python3 -c "
import json, sys
path = '$EXT_FILE'
ext_id = '$EXT_ID'
with open(path) as f:
data = json.load(f)
recs = data.setdefault('recommendations', [])
if ext_id not in recs:
recs.append(ext_id)
with open(path, 'w') as f:
json.dump(data, f, indent=2)
f.write('\n')
"
echo "Added Dev Containers extension to $EXT_FILE"
else
echo "Warning: Could not update existing $EXT_FILE (python3 not available)"
fi
else
cat > "$EXT_FILE" << 'EXTENSIONS_EOF'
{
"recommendations": [
"ms-vscode-remote.remote-containers"
]
}
EXTENSIONS_EOF
echo "Created $EXT_FILE with Dev Containers extension recommendation"
fi
# ─── 5. Pull the Docker image ────────────────────────────────────────────────
echo ""
echo "Pulling container image: $IMAGE"
echo "(This may take a few minutes on first install...)"
if ! docker pull "$IMAGE"; then
echo ""
echo "Error: Failed to pull $IMAGE"
echo ""
echo "This image is public and does not require a Docker login. If you saw \"denied\","
echo "try clearing any stale ghcr.io credentials:"
echo " docker logout ghcr.io"
echo "then re-run this script."
exit 1
fi
# ─── 6. Install dct-exec / dct-find-container host helpers ──────────────────
# These run on the HOST (not inside the devcontainer) and let host-side
# scripts find/exec into this project's own devcontainer without hardcoding
# a container name. macOS/Linux only (bash required) — skipped on Windows.
BIN_DIR="$HOME/.local/bin"
if [ "$(uname -s 2>/dev/null)" = "Linux" ] || [ "$(uname -s 2>/dev/null)" = "Darwin" ]; then
mkdir -p "$BIN_DIR"
echo ""
echo "Installing dct-exec and dct-find-container to $BIN_DIR..."
for name in dct-exec dct-find-container; do
url="https://raw.githubusercontent.com/$REPO/main/host-tools/$name.sh"
dest="$BIN_DIR/$name"
if command -v curl >/dev/null 2>&1; then
ok=1; curl -fsSL "$url" -o "$dest" || ok=0
elif command -v wget >/dev/null 2>&1; then
ok=1; wget -qO "$dest" "$url" || ok=0
else
ok=0
fi
if [ "$ok" = "1" ] && [ -s "$dest" ]; then
chmod +x "$dest"
echo " Installed $dest"
else
echo " Warning: Failed to install $name (skipping — this doesn't affect the devcontainer itself)"
rm -f "$dest"
fi
done
case ":$PATH:" in
*":$BIN_DIR:"*) ;;
*)
echo ""
echo "Note: $BIN_DIR is not on your PATH."
echo "Add this to your shell profile (~/.bashrc, ~/.zshrc, etc.) to use dct-exec:"
echo " export PATH=\"\$HOME/.local/bin:\$PATH\""
;;
esac
fi
# ─── 7. Print next steps ─────────────────────────────────────────────────────
echo ""
echo "✅ devcontainer-toolbox installed!"
echo ""
echo "Next steps:"
echo " 1. Open this folder in VS Code"
echo " 2. When prompted, click 'Reopen in Container'"
echo " (or run: Cmd/Ctrl+Shift+P > 'Dev Containers: Reopen in Container')"
echo " 3. Inside the container, run: dev-help"
echo ""
if [ -d ".devcontainer.backup" ]; then
echo "Note: Your previous .devcontainer/ was backed up to .devcontainer.backup/"
echo ""
fi