2020-06-02 18:00:15 +00:00
|
|
|
# Updates the vscode setting file base on a nix expression
|
|
|
|
# should run from the workspace root.
|
|
|
|
{ writeShellScriptBin
|
|
|
|
, lib
|
|
|
|
, jq
|
|
|
|
}:
|
|
|
|
##User Input
|
|
|
|
{ settings ? {}
|
2021-09-18 10:52:07 +00:00
|
|
|
# if marked as true will create an empty json file if does not exist
|
2020-06-02 18:00:15 +00:00
|
|
|
, createIfDoesNotExists ? true
|
|
|
|
, vscodeSettingsFile ? ".vscode/settings.json"
|
|
|
|
, userSettingsFolder ? ""
|
|
|
|
, symlinkFromUserSetting ? false
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
|
|
|
|
updateVSCodeSettingsCmd = ''
|
|
|
|
(
|
2020-09-25 04:45:31 +00:00
|
|
|
echo 'updateSettings.nix: Updating ${vscodeSettingsFile}...'
|
2020-06-02 18:00:15 +00:00
|
|
|
oldSettings=$(cat ${vscodeSettingsFile})
|
|
|
|
echo $oldSettings' ${builtins.toJSON settings}' | ${jq}/bin/jq -s add > ${vscodeSettingsFile}
|
|
|
|
)'';
|
|
|
|
|
|
|
|
createEmptySettingsCmd = ''mkdir -p .vscode && echo "{}" > ${vscodeSettingsFile}'';
|
|
|
|
fileName = builtins.baseNameOf vscodeSettingsFile;
|
|
|
|
symlinkFromUserSettingCmd = lib.optionalString symlinkFromUserSetting
|
|
|
|
'' && mkdir -p "${userSettingsFolder}" && ln -sfv "$(pwd)/${vscodeSettingsFile}" "${userSettingsFolder}/" '';
|
2020-09-25 04:45:31 +00:00
|
|
|
in
|
2020-06-02 18:00:15 +00:00
|
|
|
|
|
|
|
writeShellScriptBin ''vscodeNixUpdate-${lib.removeSuffix ".json" (fileName)}''
|
2020-09-25 04:45:31 +00:00
|
|
|
(lib.optionalString (settings != {})
|
2020-06-02 18:00:15 +00:00
|
|
|
(if createIfDoesNotExists then ''
|
|
|
|
[ ! -f "${vscodeSettingsFile}" ] && ${createEmptySettingsCmd}
|
|
|
|
${updateVSCodeSettingsCmd} ${symlinkFromUserSettingCmd}
|
|
|
|
''
|
|
|
|
else ''[ -f "${vscodeSettingsFile}" ] && ${updateVSCodeSettingsCmd} ${symlinkFromUserSettingCmd}
|
|
|
|
''
|
|
|
|
)
|
|
|
|
)
|