From 4673144026ae4ac0324ebac154706a487cf77edb Mon Sep 17 00:00:00 2001 From: Dillon DuPont Date: Wed, 11 Jun 2025 14:28:35 -0400 Subject: [PATCH] workspace setup --- .devcontainer/devcontainer.json | 3 +- .devcontainer/post-install.sh | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 .devcontainer/post-install.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 918ac9ab..b3e80233 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -45,5 +45,6 @@ } } }, - "workspaceFolder": "/workspaces/cua/.vscode/py.code-workspace" + // Automatically run post-install.sh after container is created + "postCreateCommand": "/bin/bash .devcontainer/post-install.sh" } diff --git a/.devcontainer/post-install.sh b/.devcontainer/post-install.sh new file mode 100644 index 00000000..ba16a25c --- /dev/null +++ b/.devcontainer/post-install.sh @@ -0,0 +1,73 @@ +#!/usr/bin/env bash + +if [ "$1" == "--help" ]; then + + cat << EOF +Run from project folder, auto opens vscode in some mode depending on folder contents: + +* Folder contains .devcontainer/devcontainer.json and .code-workspace file: vscode opens in devcontainer, workspace file is loaded +* Folder contains .devcontainer/devcontainer.json: vscode opens in devcontainer +* Folder contains .code-workspace file: Workspace is opened in vscode +* Folder contains no .code-workspace and no devcontainer: vscode is opened, loading contents of the current folder + +This script was created for WSL2, probably works the same way for native Linux, but untested + +Assumes the following filestructure: + + +| +| -- .code-workspace +| -- ./devcontainer/devcontainer.json +| -- ... + +Note: If you set workspaceFolder or workspaceMount in devcontainer.json this may cause issues + Also, if .devcontainer/devcontainer.json is not in the root of your repository, you may get in trouble + refer to https://code.visualstudio.com/remote/advancedcontainers/change-default-source-mount + +EOF + exit 0 +fi + +# check for dependencies +if ! command -v xxd &> /dev/null; then + echo "xxd command not found, install with" + echo "sudo apt install xxd" + exit 1 +fi + +DEVCONTAINER_JSON="$PWD/.devcontainer/devcontainer.json" +CODE_WS_FILE=$(ls $PWD/*.code-workspace 2>/dev/null) + +if [ ! -f "$DEVCONTAINER_JSON" ];then + + # open code without container + + if [ -f "$CODE_WS_FILE" ]; then + echo "Opening vscode workspace from $CODE_WS_FILE" + code $CODE_WS_FILE + else + echo "Opening vscode in current directory" + code . + fi + exit 0 +fi + +# open devcontainer +HOST_PATH=$(echo $(wslpath -w $PWD) | sed -e 's,\\,\\\\,g') +WORKSPACE="/workspaces/$(basename $PWD)" + +URI_SUFFIX= +if [ -f "$CODE_WS_FILE" ]; then + # open workspace file + URI_TYPE="--file-uri" + URI_SUFFIX="$WORKSPACE/$(basename $CODE_WS_FILE)" + echo "Opening vscode workspace file within devcontainer" +else + URI_TYPE="--folder-uri" + URI_SUFFIX="$WORKSPACE" + echo "Opening vscode within devcontainer" +fi + +URI="{\"hostPath\":\"$HOST_PATH\",\"configFile\":{\"\$mid\":1,\"path\":\"$DEVCONTAINER_JSON\",\"scheme\":\"vscode-fileHost\"}}" +URI_HEX=$(echo "${URI}" | xxd -c 0 -p) +code ${URI_TYPE}="vscode-remote://dev-container%2B${URI_HEX}${URI_SUFFIX}" & \ No newline at end of file