about summary refs log tree commit diff
path: root/pkgs/applications/editors/vscode/extensions/updateSettings.nix
blob: fe23e099b536da93600de7730b0cdb2d31c3ec22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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 exist
  createIfDoesNotExists ? true,
  vscodeSettingsFile ? ".vscode/settings.json",
  userSettingsFolder ? "",
  symlinkFromUserSetting ? false,
}:
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}'';
  fileName = builtins.baseNameOf vscodeSettingsFile;
  symlinkFromUserSettingCmd = lib.optionalString symlinkFromUserSetting ''&& mkdir -p "${userSettingsFolder}" && ln -sfv "$(pwd)/${vscodeSettingsFile}" "${userSettingsFolder}/" '';
in

writeShellScriptBin ''vscodeNixUpdate-${lib.removeSuffix ".json" (fileName)}'' (
  lib.optionalString (settings != { }) (
    if createIfDoesNotExists then
      ''
        [ ! -f "${vscodeSettingsFile}" ] && ${createEmptySettingsCmd}
        ${updateVSCodeSettingsCmd} ${symlinkFromUserSettingCmd}
      ''
    else
      ''
        [ -f "${vscodeSettingsFile}" ] && ${updateVSCodeSettingsCmd} ${symlinkFromUserSettingCmd}
      ''
  )
)