Last active
April 24, 2021 13:46
-
-
Save MBetters/ba1e8c65c3d9a65e27ebe72b5d99a36b to your computer and use it in GitHub Desktop.
Script to manually set the environmentVariables of all the kits in VSCode's CMake Tools' .vscode/cmake-kits.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: source ./set_kits_env_vars.sh /path/to/your/environment/setup/script | |
# Prerequisites: jq (sudo apt-get install -y jq) | |
# This script sources the environment script provided by the given argument | |
# and sets all the "environmentVariables" fields in .vscode/cmake-kits.json accordingly. | |
# TODO: | |
# This script will be deprecated once the feature requested | |
# at https://github.com/vector-of-bool/vscode-cmake-tools/issues/288 is added. | |
# TODO: | |
# This script gives all the kits the same "environmentVariables" object, | |
# which is wrong. It should instead just do this for one kit. | |
# Make it so that you can provide the "name" of the one kit that should be changed. | |
# TODO: | |
# If your running this script for a toolchain kit, then you'll still need | |
# to edit the resulting .vscode/cmake-kits.json file so the "toolchainFile" field | |
# comes after the "environmentVariables" object, so that the toolchain file can access | |
# the environment variables. | |
source "$1" | |
env > .env | |
# Loop through the lines in .env, | |
# as seen at https://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash | |
INPUT_JSON_FILE=".vscode/cmake-kits.json" | |
TMP_INDEX=1 | |
TMP_JSON_FILE="tmp$TMP_INDEX.json" | |
while IFS="" read -r line || [ -n "$line" ] | |
do | |
KEY=$(printf '%s\n' "$line" | cut -d "=" -f 1) | |
VAL=$(printf '%s\n' "$line" | cut -d "=" -f 2-) | |
# echo $KEY | |
# echo $VAL | |
# The first time this command runs, it uses .vscode/cmake-kits.json as the input JSON file. | |
# All subsequent times it runs, it uses tmp$TMP_INDEX.json as the input file, | |
# where TMP_INDEX is incremented every loop iteration. | |
# This is necessary because jq needs a new input file every time, or else the output file will be empty. | |
cat "$INPUT_JSON_FILE" | jq --arg KEY "$KEY" --arg VAL "$VAL" '.[].environmentVariables[$KEY]=$VAL' -M > $TMP_JSON_FILE | |
INPUT_JSON_FILE="$TMP_JSON_FILE" | |
# Increment the index. | |
TMP_INDEX=$((TMP_INDEX+1)) | |
TMP_JSON_FILE="tmp$TMP_INDEX.json" | |
done < .env | |
# Get the name of the last created tmp JSON file. | |
# That file will have all the "environmentVariables" fields set. | |
TMP_INDEX=$((TMP_INDEX-1)) | |
TMP_JSON_FILE="tmp$TMP_INDEX.json" | |
# Overwrite .vscode/cmake-kits.json | |
mv $TMP_JSON_FILE .vscode/cmake-kits.json | |
# Remove all the temporary files that this script used | |
rm .env | |
rm tmp*.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment