Skip to content

Commit

Permalink
Fix setting of Rstudio preferences
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Jongmassey committed Jul 19, 2024
1 parent 078208a commit 5bae921
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion devcontainer/postCreate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5bae921

Please sign in to comment.