File: update_system_path.erl

package info (click to toggle)
elixir-lang 1.19.5.dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,524 kB
  • sloc: erlang: 12,234; sh: 321; makefile: 288
file content (45 lines) | stat: -rw-r--r-- 1,238 bytes parent folder | download
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
44
45
#!/usr/bin/env escript
%% SPDX-License-Identifier: Apache-2.0
%% SPDX-FileCopyrightText: 2021 The Elixir Team

%%! -noinput

%% This file is used by the Elixir installer and uninstaller.
main(["add", ";" ++ PathsToAdd]) ->
  {ok, Reg} = win32reg:open([read, write]),
  ok = win32reg:change_key(Reg, "\\hkey_current_user\\environment"),
  {ok, SystemPath} = win32reg:value(Reg, "path"),

  NewSystemPath =
    lists:foldl(
      fun(Elem, Acc) ->
        Elem ++ ";" ++
          binary_to_list(
            iolist_to_binary(
              string:replace(Acc, Elem ++ ";", "", all)))
      end,
      SystemPath,
      string:split(PathsToAdd, ";", all)
    ),

  ok = win32reg:set_value(Reg, "Path", NewSystemPath),
  ok;

main(["remove", ";" ++ PathsToRemove]) ->
  {ok, Reg} = win32reg:open([read, write]),
  ok = win32reg:change_key(Reg, "\\hkey_current_user\\environment"),
  {ok, SystemPath} = win32reg:value(Reg, "path"),

  NewSystemPath =
    lists:foldl(
      fun(Elem, Acc) ->
        binary_to_list(
          iolist_to_binary(
            string:replace(Acc, Elem ++ ";", "", all)))
      end,
      SystemPath,
      string:split(PathsToRemove, ";", all)
    ),

  ok = win32reg:set_value(Reg, "Path", NewSystemPath),
  ok.