Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JCash
Created July 29, 2019 08:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JCash/6bb50e1613aac311700f3d6d82a561fb to your computer and use it in GitHub Desktop.
Save JCash/6bb50e1613aac311700f3d6d82a561fb to your computer and use it in GitHub Desktop.
Create a Visual Studio Code "project" on the fly, given an executable, then launching the project
#! /usr/bin/env bash
# DISCLAIMER:
# This code IS WITHOUT ANY WARRANTY!
# Use at own risk
FOLDER='.vscode'
LAUNCH_FILE='launch.json'
MAGIC_FILE='vscode_script'
function usage {
echo "Usage: ./vscode.sh <executable> [options]"
echo " -a/--arg: arguments to the executable"
echo " -e/--env: environment variables for the executable"
echo " -c/--cwd: current directory for the executable"
exit 1
}
if [ "$1" == "" ]; then
usage
fi
PROGRAM=$1
if [ ! -e $PROGRAM ]; then
echo "File does not exist" $PROGRAM
exit 1
fi
shift
ARGS=
ENV=
CWD="\${workspaceFolder}"
for i in "$@"
do
case $i in
-a=*|--arg=*)
ARGPREFIX=""
if [ "$ARGS" != "" ]; then
ARGPREFIX=","
fi
ARGS="${ARGS}${ARGPREFIX}\"${i#*=}\""
;;
-e=*|--env=*)
ARGPREFIX=""
if [ "$ENV" != "" ]; then
ARGPREFIX=","
fi
ENV="${ENV}${ARGPREFIX}\"${i#*=}\""
;;
-c=*|--cwd=*)
CWD="${i#*=}"
;;
*)
# unknown option
;;
esac
done
# In order to easily substitute variables, and also keeping the double quoted keys/values
read -r -d '' LAUNCH_JSON << EOM
{\n
\t// Use IntelliSense to learn about possible attributes.\n
\t// Hover to view descriptions of existing attributes.\n
\t// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387\n
\t"version": "0.2.0",\n
\t"configurations": [\n
\t\t{\n
\t\t\t"name": "${PROGRAM}",\n
\t\t\t"type": "cppdbg",\n
\t\t\t"request": "launch",\n
\t\t\t"program": "\${workspaceFolder}/${PROGRAM}",\n
\t\t\t"args": [${ARGS}],\n
\t\t\t"stopAtEntry": false,\n
\t\t\t"cwd": "${CWD}",\n
\t\t\t"environment": [${ENV}],\n
\t\t\t"externalConsole": false,\n
\t\t\t"MIMode": "lldb"\n
\t\t}\n
\t]\n
}\n
EOM
# In order to not overwrite an existing folder
if [ ! -d $FOLDER ]; then
mkdir $FOLDER
echo "magic" > $FOLDER/$MAGIC_FILE
echo "Created $FOLDER directory"
fi
if [ ! -e $FOLDER/$MAGIC_FILE ]; then
echo "Won't overwrite existing folder $FOLDER"
exit 1
else
echo -e $LAUNCH_JSON > ${FOLDER}/${LAUNCH_FILE}
echo "Wrote ${FOLDER}/${LAUNCH_FILE}"
fi
if [ `uname` == "Darwin" ]; then
open -a "/Applications/Visual Studio Code.app" .
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment