3
0
Fork 0
forked from mirrors/nixpkgs
nixpkgs/pkgs/misc/vscode-extensions/updateSettings.nix

33 lines
1 KiB
Nix

# Updates the vscode setting file base on a nix expression
# should run from the workspace root.
{ writeShellScriptBin
, lib
, jq
}:
##User Input
{ settings ? {}
# if marked as true will create an empty json file if does not exists
, createIfDoesNotExists ? true
, vscodeSettingsFile ? ".vscode/settings.json"
}:
let
updateVSCodeSettingsCmd = ''
(
echo 'updateSettings.nix: Updating ${vscodeSettingsFile}...'
oldSettings=$(cat ${vscodeSettingsFile})
echo $oldSettings' ${builtins.toJSON settings}' | ${jq}/bin/jq -s add > ${vscodeSettingsFile}
)'';
createEmptySettingsCmd = ''mkdir -p .vscode && echo "{}" > ${vscodeSettingsFile}'';
in
writeShellScriptBin ''vscodeNixUpdate-${lib.removeSuffix ".json" (builtins.baseNameOf vscodeSettingsFile)}''
(lib.optionalString (settings != {})
(if createIfDoesNotExists then ''
[ ! -f "${vscodeSettingsFile}" ] && ${createEmptySettingsCmd}
${updateVSCodeSettingsCmd}
''
else ''[ -f "${vscodeSettingsFile}" ] && ${updateVSCodeSettingsCmd}''
))