-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvscode.sh
More file actions
executable file
Β·273 lines (238 loc) Β· 9.77 KB
/
vscode.sh
File metadata and controls
executable file
Β·273 lines (238 loc) Β· 9.77 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
#!/usr/bin/env bash
# ----------------------------------------------
# User-adjustable parameters
# ----------------------------------------------
VSCODE_PROFILE="django_project"
EXT_DIR=".vscode-profile/extensions"
VSCODE_DIR=".vscode-profile"
LOG_FILE=".vscode-profile/.log"
CODING_EXTENSIONS=(
ms-vscode-remote.remote-containers@0.429.0
charliermarsh.ruff@2025.26.0
)
DOCUMENTATION_EXTENSIONS=(
bpruitt-goddard.mermaid-markdown-syntax-highlighting@1.7.1
bierner.markdown-mermaid@1.28.0
DavidAnson.vscode-markdownlint@0.60.0
yzhang.markdown-all-in-one@3.6.3
)
REQUIRED_EXTENSIONS=(
"${CODING_EXTENSIONS[@]}"
"${DOCUMENTATION_EXTENSIONS[@]}"
)
# ----------------------------------------------
# Functions
# ----------------------------------------------
launch_vscode() {
code --user-data-dir="$VSCODE_DIR" \
--profile="${VSCODE_PROFILE}" \
--extensions-dir="$EXT_DIR" "$@"
}
list_installed_extensions() {
find "$EXT_DIR" -maxdepth 1 -mindepth 1 -type d | while read -r dir; do
pkg="$dir/package.json"
if [[ -f "$pkg" ]]; then
name=$(jq -r '.name' <"$pkg")
publisher=$(jq -r '.publisher' <"$pkg")
version=$(jq -r '.version' <"$pkg")
echo "${publisher}.${name}@${version}"
fi
done
}
print_help() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
This script sets up and launches VSCode with a custom profile and extensions for the Kartoza Django Project.
Actions performed:
- Checks for required files and directories (deployment/.env, redis volume, etc.)
- Ensures VSCode and Docker are installed
- Initializes VSCode user and extension directories if needed
- Updates VSCode settings for commit signing, formatters, and linters (Markdown, Shell, Python)
- Installs all required VSCode extensions
- Launches VSCode with the specified profile and directories
Options:
--help Show this help message and exit
--verbose Print final settings.json contents before launching VSCode
--list-extensions List installed VSCode extensions in the custom extensions directory
EOF
}
# Parameter handler
for arg in "$@"; do
case "$arg" in
--help)
print_help
exit 0
;;
--verbose)
# Handled later in the script
;;
--list-extensions)
echo "Installed extensions:"
list_installed_extensions
exit 0
;;
*) ;;
esac
done
# ----------------------------------------------
# Script starts here
# ----------------------------------------------
# Truncate the log file at the start
echo "π¨οΈ Truncating $LOG_FILE..."
true >"$LOG_FILE"
echo "π¨οΈ Checking deployment .env exists ..."
if [ ! -f deployment/.env ]; then
echo "β deployment/.env file not found."
echo " Please run ./setup.sh to create it."
exit 1
else
echo " β
deployment/.env found ok."
fi
echo "π¨οΈ Checking VSCode is installed ..."
if ! command -v code &>/dev/null; then
echo " β 'code' CLI not found. Please install VSCode and add 'code' to your PATH."
exit 1
else
echo " β
VSCode found ok."
fi
# Ensure .vscode directory exists
echo "π¨οΈ Checking if VSCode has been run before..."
if [ ! -d .vscode ]; then
echo " π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»"
echo " βοΈ It appears you have not run vscode in this project before."
echo " After it opens, please close vscode and then rerun this script"
echo " so that the extensions directory initialises properly."
echo " πΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊ"
mkdir -p .vscode
mkdir -p .vscode-extensions
# Launch VSCode with the sandboxed environment
launch_vscode .
exit 1
else
echo " β
VSCode directory found from previous runs of vscode."
fi
# Ensure profiles directory exists
echo "π¨οΈ Checking if VSCode has profiles..."
if [ ! -d ".vscode-profile/User/profiles" ]; then
echo " π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»π»"
echo " βοΈ It appears you have have not profiles yet."
echo " After it opens, please close vscode and then rerun this script"
echo " so that the extensions directory initialises properly."
echo " πΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊπΊ"
# Launch VSCode with the sandboxed environment
launch_vscode .
exit 1
else
echo " β
${VSCODE_PROFILE} profile has been created."
fi
echo "π¨οΈ Checking docker is installed ..."
if ! command -v docker &>/dev/null; then
echo " β 'docker' CLI not found. Please install docker and ensure you have permissions to use it."
exit 1
else
echo " β
Docker found ok."
fi
echo "π¨οΈ Checking redis directory permissions..."
if [ ! -d deployment/volumes/tmp_data/redis ]; then
echo " π Creating deployment/volumes/tmp_data/redis..."
mkdir -p deployment/volumes/tmp_data/redis
sudo chown -R 1001:1001 deployment/volumes/tmp_data/redis
else
echo " β
Redis directory already exists."
fi
echo "π¨οΈ Checking if VSCode has been run before..."
if [ ! -d "$VSCODE_DIR" ]; then
echo " βοΈ First-time VSCode run detected. Opening VSCode to initialize..."
mkdir -p "$VSCODE_DIR"
mkdir -p "$EXT_DIR"
launch_vscode .
exit 1
else
echo " β
VSCode directory detected."
fi
SETTINGS_FILE="$VSCODE_DIR/settings.json"
echo "π¨οΈ Checking if settings.json exists..."
if [[ ! -f "$SETTINGS_FILE" ]]; then
echo "{}" >"$SETTINGS_FILE"
echo " π§ Created new settings.json"
else
echo " β
settings.json exists"
fi
echo "π¨οΈ Updating git commit signing setting..."
jq '.["git.enableCommitSigning"] = true' "$SETTINGS_FILE" >"$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE"
echo " π§ git.enableCommitSigning enabled"
echo "π¨οΈ Ensuring markdown formatter is set..."
if ! jq -e '."[markdown]".editor.defaultFormatter' "$SETTINGS_FILE" >/dev/null; then
jq '."[markdown]" += {"editor.defaultFormatter": "DavidAnson.vscode-markdownlint"}' "$SETTINGS_FILE" >"$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE"
echo " π§ Markdown formatter set"
else
echo " β
Markdown formatter already configured"
fi
echo "π¨οΈ Ensuring shell script formatter and linter are set..."
if ! jq -e '."[shellscript]".editor.defaultFormatter' "$SETTINGS_FILE" >/dev/null; then
jq '."[shellscript]" += {"editor.defaultFormatter": "foxundermoon.shell-format", "editor.formatOnSave": true}' "$SETTINGS_FILE" >"$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE"
echo " π§ Shell script formatter set to foxundermoon.shell-format, formatOnSave enabled"
else
echo " β
Shell script formatter already configured"
fi
if ! jq -e '.["shellcheck.enable"]' "$SETTINGS_FILE" >/dev/null; then
jq '. + {"shellcheck.enable": true}' "$SETTINGS_FILE" >"$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE"
echo " π§ ShellCheck linting enabled"
else
echo " β
ShellCheck linting already configured"
fi
if ! jq -e '.["shellformat.flag"]' "$SETTINGS_FILE" >/dev/null; then
jq '. + {"shellformat.flag": "-i 4 -bn -ci"}' "$SETTINGS_FILE" >"$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE"
echo " π§ Shell format flags set (-i 4 -bn -ci)"
else
echo " β
Shell format flags already configured"
fi
echo "π¨οΈ Ensuring global format-on-save is enabled..."
if ! jq -e '.["editor.formatOnSave"]' "$SETTINGS_FILE" >/dev/null; then
jq '. + {"editor.formatOnSave": true}' "$SETTINGS_FILE" >"$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE"
echo " π§ Global formatOnSave enabled"
else
echo " β
Global formatOnSave already configured"
fi
# Python formatter and linter
echo "π¨οΈ Ensuring Python formatter and linter are set..."
if ! jq -e '."[python]".editor.defaultFormatter' "$SETTINGS_FILE" >/dev/null; then
jq '."[python]" += {"editor.defaultFormatter": "charliermarsh.ruff"}' "$SETTINGS_FILE" >"$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE"
echo " π§ Python formatter set to Black"
else
echo " β
Python formatter already configured"
fi
if ! jq -e '.["python.linting.enabled"]' "$SETTINGS_FILE" >/dev/null; then
jq '. + {"python.linting.enabled": true, "python.linting.pylintEnabled": true}' "$SETTINGS_FILE" >"$SETTINGS_FILE.tmp" && mv "$SETTINGS_FILE.tmp" "$SETTINGS_FILE"
echo " π§ Python linting enabled (pylint)"
else
echo " β
Python linting already configured"
fi
if [[ " $* " == *" --verbose "* ]]; then
echo "π¨οΈ Final settings.json contents:"
cat "$SETTINGS_FILE"
fi
echo "π¨οΈ Installing required extensions..."
for ext in "${REQUIRED_EXTENSIONS[@]}"; do
if echo "$installed_exts" | grep -q "^${ext}$"; then
echo " β
Extension ${ext} already installed."
else
echo " π¦ Installing ${ext}..."
# Capture both stdout and stderr to log file
if launch_vscode --install-extension "${ext}" >>"$LOG_FILE" 2>&1; then
# Refresh installed_exts after install
installed_exts=$(list_installed_extensions)
if echo "$installed_exts" | grep -q "^${ext}$"; then
echo " β
Successfully installed ${ext}."
else
echo " β Failed to install ${ext} (not found after install)."
exit 1
fi
else
echo " β Failed to install ${ext} (error during install). Check $LOG_FILE for details."
exit 1
fi
fi
done
echo "π¨οΈ Launching VSCode..."
launch_vscode .