-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-devcontainer.sh
More file actions
executable file
·243 lines (215 loc) · 7.41 KB
/
create-devcontainer.sh
File metadata and controls
executable file
·243 lines (215 loc) · 7.41 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
#!/bin/bash
#
# DevContainer Template Copy Script
#
# This script copies the .devcontainer/ directory from this template
# to a target project directory and applies customization.
#
# Usage:
# ./create-devcontainer.sh [OPTIONS] TARGET_DIRECTORY
#
# Options:
# -t, --timezone TZ Set timezone (default: America/Los_Angeles)
# -n, --name NAME Set container name (default: derived from directory)
# --dry-run Show what would be done without making changes
# -h, --help Show this help message
#
# Arguments:
# TARGET_DIRECTORY Path to target project (must exist)
#
# Examples:
# ./create-devcontainer.sh ~/projects/my-app
# ./create-devcontainer.sh --timezone "UTC" ~/projects/my-app
# ./create-devcontainer.sh --name "MyApp Dev" ~/projects/my-app
#
set -euo pipefail
# Default values
DEFAULT_TZ="America/Los_Angeles"
TIMEZONE="${DEFAULT_TZ}"
CONTAINER_NAME=""
DRY_RUN=false
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Help message
show_help() {
cat << EOF
DevContainer Template Copy Script
Usage: $0 [OPTIONS] TARGET_DIRECTORY
Options:
-t, --timezone TZ Set timezone (default: $DEFAULT_TZ)
-n, --name NAME Set container name (default: derived from directory)
--dry-run Show what would be done without making changes
-h, --help Show this help message
Arguments:
TARGET_DIRECTORY Path to target project (must exist)
Examples:
$0 ~/projects/my-app
$0 --timezone "UTC" ~/projects/my-app
$0 --name "MyApp Dev" ~/projects/my-app
$0 --dry-run ~/projects/my-app
EOF
}
# Parse arguments
TARGET_DIR=""
while [[ $# -gt 0 ]]; do
case $1 in
-t|--timezone)
TIMEZONE="$2"
shift 2
;;
-n|--name)
CONTAINER_NAME="$2"
shift 2
;;
--dry-run)
DRY_RUN=true
shift
;;
-h|--help)
show_help
exit 0
;;
-*)
echo -e "${RED}Error: Unknown option: $1${NC}" >&2
echo "Run '$0 --help' for usage information." >&2
exit 1
;;
*)
TARGET_DIR="$1"
shift
;;
esac
done
# Validate TARGET_DIR is provided
if [ -z "$TARGET_DIR" ]; then
echo -e "${RED}Error: TARGET_DIRECTORY is required${NC}" >&2
echo "Run '$0 --help' for usage information." >&2
exit 1
fi
# Convert to absolute path
TARGET_DIR="$(cd "$TARGET_DIR" 2>/dev/null && pwd)" || {
echo -e "${RED}Error: Target directory does not exist: $TARGET_DIR${NC}" >&2
exit 1
}
# Validate TARGET_DIR is not the template directory itself
if [ "$TARGET_DIR" = "$SCRIPT_DIR" ]; then
echo -e "${RED}Error: Cannot copy devcontainer to itself${NC}" >&2
echo "Target directory must be different from template directory." >&2
exit 1
fi
# Derive container name from directory if not provided
if [ -z "$CONTAINER_NAME" ]; then
CONTAINER_NAME="$(basename "$TARGET_DIR")"
fi
# Derive workspace folder name
WORKSPACE_NAME="$(basename "$TARGET_DIR")"
echo "========================================="
echo "DevContainer Template Copy"
echo "========================================="
echo ""
echo "Template: $SCRIPT_DIR"
echo "Target: $TARGET_DIR"
echo "Name: $CONTAINER_NAME"
echo "Timezone: $TIMEZONE"
echo "Workspace: /workspaces/$WORKSPACE_NAME"
if [ "$DRY_RUN" = true ]; then
echo -e "${YELLOW}Mode: DRY RUN (no changes will be made)${NC}"
fi
echo ""
# Check if .devcontainer already exists
if [ -d "$TARGET_DIR/.devcontainer" ]; then
echo -e "${YELLOW}Warning: .devcontainer/ directory already exists in target${NC}"
if [ "$DRY_RUN" = false ]; then
read -p "Overwrite existing .devcontainer/? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 0
fi
rm -rf "$TARGET_DIR/.devcontainer"
fi
fi
# Copy .devcontainer directory
echo "📁 Copying .devcontainer/ directory..."
if [ "$DRY_RUN" = false ]; then
cp -r "$SCRIPT_DIR/.devcontainer" "$TARGET_DIR/"
echo -e "${GREEN}✓${NC} Copied .devcontainer/ directory"
else
echo " Would copy: $SCRIPT_DIR/.devcontainer -> $TARGET_DIR/.devcontainer"
fi
echo ""
# Apply templating to devcontainer.json
echo "🔧 Customizing devcontainer.json..."
if [ "$DRY_RUN" = false ]; then
DEVCONTAINER_JSON="$TARGET_DIR/.devcontainer/devcontainer.json"
# Create temporary file for modifications
TMP_FILE=$(mktemp)
# Update name field
sed "s/\"name\": \".*\"/\"name\": \"$CONTAINER_NAME\"/" "$DEVCONTAINER_JSON" > "$TMP_FILE"
cp "$TMP_FILE" "$DEVCONTAINER_JSON"
# Update workspaceFolder
sed "s|\"workspaceFolder\": \"/workspaces/[^\"]*\"|\"workspaceFolder\": \"/workspaces/$WORKSPACE_NAME\"|" "$DEVCONTAINER_JSON" > "$TMP_FILE"
cp "$TMP_FILE" "$DEVCONTAINER_JSON"
# Update mount volume name (python3-devcontainer-commandhistory -> workspace_name-commandhistory)
sed "s/python3-devcontainer-commandhistory/${WORKSPACE_NAME}-commandhistory/" "$DEVCONTAINER_JSON" > "$TMP_FILE"
cp "$TMP_FILE" "$DEVCONTAINER_JSON"
# Update git safe.directory in postStartCommand
sed "s|git config --global --add safe.directory /workspaces/[^\"]*|git config --global --add safe.directory /workspaces/$WORKSPACE_NAME|" "$DEVCONTAINER_JSON" > "$TMP_FILE"
cp "$TMP_FILE" "$DEVCONTAINER_JSON"
# Update timezone if different from default
if [ "$TIMEZONE" != "$DEFAULT_TZ" ]; then
sed "s/\"TZ\": \"[^\"]*\"/\"TZ\": \"$TIMEZONE\"/" "$DEVCONTAINER_JSON" > "$TMP_FILE"
cp "$TMP_FILE" "$DEVCONTAINER_JSON"
fi
rm "$TMP_FILE"
echo -e "${GREEN}✓${NC} Updated devcontainer.json"
else
echo " Would update: name -> $CONTAINER_NAME"
echo " Would update: workspaceFolder -> /workspaces/$WORKSPACE_NAME"
echo " Would update: volume name -> ${WORKSPACE_NAME}-commandhistory"
echo " Would update: safe.directory -> /workspaces/$WORKSPACE_NAME"
if [ "$TIMEZONE" != "$DEFAULT_TZ" ]; then
echo " Would update: timezone -> $TIMEZONE"
fi
fi
echo ""
# Success message
echo "========================================="
if [ "$DRY_RUN" = false ]; then
echo -e "${GREEN}✓ DevContainer successfully copied!${NC}"
else
echo -e "${BLUE}ℹ Dry run complete${NC}"
fi
echo "========================================="
echo ""
if [ "$DRY_RUN" = false ]; then
echo "Next steps:"
echo " 1. Open $TARGET_DIR in VS Code"
echo " 2. Install the 'Dev Containers' extension if not already installed"
echo " 3. Command Palette -> 'Dev Containers: Reopen in Container'"
echo ""
echo "The container will build automatically with:"
echo " • Python 3.13"
echo " • Claude Code CLI"
echo " • Playwright (Chromium, Firefox, WebKit)"
echo " • Node.js 20.x and npm"
echo " • uv (fast Python package manager)"
echo " • ZSH with Powerline10k"
echo ""
echo "Optional: Enable network firewall (inside container):"
echo " sudo /usr/local/bin/init-firewall.sh"
echo ""
echo "Copied files:"
echo " $TARGET_DIR/.devcontainer/devcontainer.json"
echo " $TARGET_DIR/.devcontainer/Dockerfile"
echo " $TARGET_DIR/.devcontainer/init-firewall.sh"
echo " $TARGET_DIR/.devcontainer/post-create.sh"
else
echo "Run without --dry-run to apply changes."
fi
echo ""