Skip to content

Instantly share code, notes, and snippets.

@Kaptensanders
Last active July 23, 2024 03:33
Show Gist options
  • Save Kaptensanders/79da7c1547751fb43c75904e3110bbf9 to your computer and use it in GitHub Desktop.
Save Kaptensanders/79da7c1547751fb43c75904e3110bbf9 to your computer and use it in GitHub Desktop.
Open VSCode .code-workspace file from within devcontainer - from terminal
#!/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 <name>.code-workspace file: vscode opens in devcontainer, workspace file is loaded
* Folder contains .devcontainer/devcontainer.json: vscode opens in devcontainer
* Folder contains <name>.code-workspace file: Workspace is opened in vscode
* Folder contains no <name>.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:
<some folder>
|
| -- <name>.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}" &
@Kaptensanders
Copy link
Author

Kaptensanders commented Sep 13, 2023

Removed the devcontainer.json parsing in this rev. If you use non-standard settings for workspaceFolder and workspaceMount you can modify the script to suit your situation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment