From 5bae9217daf87a477ae296f598e5ab65b0b4cade Mon Sep 17 00:00:00 2001 From: Jon Massey Date: Fri, 19 Jul 2024 11:28:41 +0100 Subject: [PATCH] Fix setting of Rstudio preferences Move the preference setting logic to a function for readability. Use jq rather than grep to check for existing keys for readability. Perform modifications on a string copy of the preference file rather than piping straight back to the file. Should anything go awry, this should help prevent damaging the existing preferences file. If, for whatever reason, the preferences file is blank at the time of running this script, replace blank string with an empty dictionary `{}` to allow the following `jq` processes to succeed. Only write back to prefs file once modifications complete to avoid writing blank/invalid data to file. --- devcontainer/postCreate.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/devcontainer/postCreate.sh b/devcontainer/postCreate.sh index b61f685..576d8f9 100755 --- a/devcontainer/postCreate.sh +++ b/devcontainer/postCreate.sh @@ -7,7 +7,16 @@ set -euo pipefail #set R working directory ! grep -q "$1" $R_HOME/etc/Rprofile.site && sudo tee -a $R_HOME/etc/Rprofile.site <<< "setwd(\"$1\")" #set RStudio working directory -! grep -q "$1" ~/.config/rstudio/rstudio-prefs.json && cat ~/.config/rstudio/rstudio-prefs.json | jq ". + {\"initial_working_directory\":\"$1\"}" > ~/.config/rstudio/rstudio-prefs.json +set_rstudio_pref() { + key="$1" + value="$2" + orig_prefs=$(jq -ne "input ? // {}" < ~/.config/rstudio/rstudio-prefs.json) + new_prefs=$(jq "if has(\"$key\") then . else . + {\"$key\": $value} end" <<< "$orig_prefs") + echo "$new_prefs" > ~/.config/rstudio/rstudio-prefs.json +} + +#set RStudio working directory +set_rstudio_pref "initial_working_directory" "\"$1\"" #download and extract latest ehrql source wget https://github.com/opensafely-core/ehrql/archive/main.zip -P .devcontainer