Skip to content

Commit 119580f

Browse files
wojtekmachjosevalim
authored andcommitted
Fix PATH handling on Windows Installer (#12900)
1 parent 9fd97c4 commit 119580f

File tree

2 files changed

+89
-45
lines changed

2 files changed

+89
-45
lines changed

lib/elixir/scripts/windows_installer/installer.nsi

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
!include "MUI2.nsh"
22
!include "StrFunc.nsh"
3-
${Using:StrFunc} StrStr
43
${Using:StrFunc} UnStrStr
5-
${Using:StrFunc} UnStrRep
64

75
Name "Elixir"
86
ManifestDPIAware true
@@ -16,7 +14,6 @@ Page custom CheckOTPPageShow CheckOTPPageLeave
1614

1715
var Dialog
1816
var DownloadOTPLink
19-
var InstalledERTSVersion
2017
var InstalledOTPRelease
2118
var OTPPath
2219
Function CheckOTPPageShow
@@ -30,11 +27,10 @@ Function CheckOTPPageShow
3027
${EndIf}
3128

3229
EnumRegKey $0 HKLM "SOFTWARE\WOW6432NODE\Ericsson\Erlang" 0
33-
ReadRegStr $1 HKLM "SOFTWARE\WOW6432NODE\Ericsson\Erlang\$0" ""
34-
StrCpy $InstalledERTSVersion $0
35-
StrCpy $OTPPath $1
30+
ReadRegStr $0 HKLM "SOFTWARE\WOW6432NODE\Ericsson\Erlang\$0" ""
31+
StrCpy $OTPPath $0
3632

37-
${If} $1 == ""
33+
${If} $OTPPath == ""
3834
${NSD_CreateLabel} 0 0 100% 20u "Couldn't find existing Erlang/OTP installation. Click the link below to download and install it before proceeding."
3935
${NSD_CreateLink} 0 25u 100% 20u "Download Erlang/OTP ${OTP_RELEASE}"
4036
Pop $DownloadOTPLink
@@ -103,62 +99,55 @@ Function FinishPageShow
10399

104100
EnumRegKey $0 HKLM "SOFTWARE\WOW6432NODE\Ericsson\Erlang" 0
105101
ReadRegStr $0 HKLM "SOFTWARE\WOW6432NODE\Ericsson\Erlang\$0" ""
106-
StrCpy $OTPPath $0
107-
${If} $0 != ""
108-
${NSD_CreateCheckbox} 0 20u 195u 10u "&Add $0\bin to %PATH%"
109-
Pop $AddOTPToPathCheckbox
110-
SendMessage $AddOTPToPathCheckbox ${BM_SETCHECK} ${BST_CHECKED} 0
111-
${EndIf}
102+
${NSD_CreateCheckbox} 0 20u 195u 10u "&Add $0\bin to %PATH%"
103+
Pop $AddOTPToPathCheckbox
104+
SendMessage $AddOTPToPathCheckbox ${BM_SETCHECK} ${BST_CHECKED} 0
112105

113106
nsDialogs::Show
114107
FunctionEnd
115108

109+
var PathsToAdd
116110
Function FinishPageLeave
117111
${NSD_GetState} $AddOTPToPathCheckbox $0
118112
${If} $0 <> ${BST_UNCHECKED}
119-
ReadRegStr $0 HKCU "Environment" "Path"
120-
${StrStr} $1 "$0" "$OTPPath\bin"
121-
${If} $1 == ""
122-
WriteRegExpandStr HKCU "Environment" "Path" "$OTPPath\bin;$0"
123-
${Else}
124-
MessageBox MB_OK "$OTPPath\bin already in %PATH%"
125-
${EndIf}
113+
StrCpy $PathsToAdd ";$OTPPath\bin"
126114
${EndIf}
127115

128116
${NSD_GetState} $AddElixirToPathCheckbox $0
129117
${If} $0 <> ${BST_UNCHECKED}
130-
ReadRegStr $0 HKCU "Environment" "Path"
131-
${StrStr} $1 "$0" "$INSTDIR\bin"
132-
${If} $1 == ""
133-
WriteRegExpandStr HKCU "Environment" "Path" "$INSTDIR\bin;$0"
134-
${Else}
135-
MessageBox MB_OK "$INSTDIR\bin already in %PATH%"
118+
StrCpy $PathsToAdd "$PathsToAdd;$INSTDIR\bin"
119+
${EndIf}
120+
121+
${If} "$PathsToAdd" != ""
122+
nsExec::ExecToStack `"$OTPPath\bin\escript.exe" "$INSTDIR\update_system_path.erl" add "$PathsToAdd"`
123+
Pop $0
124+
Pop $1
125+
${If} $0 != 0
126+
SetErrorlevel 5
127+
MessageBox MB_ICONSTOP "adding paths failed with $0: $1"
128+
Quit
136129
${EndIf}
137130
${EndIf}
138131
FunctionEnd
139132

140133
Section "Install Elixir" SectionElixir
141134
SetOutPath "$INSTDIR"
142135
File /r "${ELIXIR_DIR}\"
136+
File "update_system_path.erl"
143137

144138
WriteUninstaller "Uninstall.exe"
145139
SectionEnd
146140

147-
; Uninstall Page: Files
148-
149-
!insertmacro MUI_UNPAGE_DIRECTORY
150-
!insertmacro MUI_UNPAGE_INSTFILES
151-
152-
Section "Uninstall"
153-
RMDir /r "$INSTDIR"
154-
SectionEnd
155-
156-
; Uninstall Page: Finish
141+
; Uninstall Page: Remove from %PATH%
157142

158143
var RemoveOTPFromPathCheckbox
159144
var RemoveElixirFromPathCheckbox
160145
Function un.FinishPageShow
161-
!insertmacro MUI_HEADER_TEXT "Finish Setup" ""
146+
!insertmacro MUI_HEADER_TEXT "Remove from %PATH%" ""
147+
148+
EnumRegKey $0 HKLM "SOFTWARE\WOW6432NODE\Ericsson\Erlang" 0
149+
ReadRegStr $0 HKLM "SOFTWARE\WOW6432NODE\Ericsson\Erlang\$0" ""
150+
StrCpy $OTPPath $0
162151

163152
nsDialogs::Create 1018
164153
Pop $Dialog
@@ -168,16 +157,14 @@ Function un.FinishPageShow
168157
${EndIf}
169158

170159
ReadRegStr $0 HKCU "Environment" "Path"
160+
171161
${UnStrStr} $1 "$0" "$INSTDIR\bin"
172162
${If} $1 != ""
173163
${NSD_CreateCheckbox} 0 0 195u 10u "&Remove $INSTDIR\bin from %PATH%"
174164
Pop $RemoveElixirFromPathCheckbox
175165
SendMessage $RemoveElixirFromPathCheckbox ${BM_SETCHECK} ${BST_CHECKED} 0
176166
${EndIf}
177167

178-
EnumRegKey $1 HKLM "SOFTWARE\WOW6432NODE\Ericsson\Erlang" 0
179-
ReadRegStr $1 HKLM "SOFTWARE\WOW6432NODE\Ericsson\Erlang\$1" ""
180-
StrCpy $OTPPath $1
181168
${UnStrStr} $1 "$0" "$OTPPath\bin"
182169
${If} $1 != ""
183170
${NSD_CreateCheckbox} 0 20u 195u 10u "&Remove $OTPPath\bin from %PATH%"
@@ -188,22 +175,37 @@ Function un.FinishPageShow
188175
nsDialogs::Show
189176
FunctionEnd
190177

178+
var PathsToRemove
191179
Function un.FinishPageLeave
192-
ReadRegStr $0 HKCU "Environment" "Path"
193-
194180
${NSD_GetState} $RemoveOTPFromPathCheckbox $1
195181
${If} $1 <> ${BST_UNCHECKED}
196-
${UnStrRep} $0 "$0" "$OTPPath\bin;" ""
182+
StrCpy $PathsToRemove ";$OTPPath\bin"
197183
${EndIf}
198184

199185
${NSD_GetState} $RemoveElixirFromPathCheckbox $1
200186
${If} $1 <> ${BST_UNCHECKED}
201-
${UnStrRep} $0 "$0" "$INSTDIR\bin;" ""
187+
StrCpy $PathsToRemove "$PathsToRemove;$INSTDIR\bin"
202188
${EndIf}
203189

204-
WriteRegExpandStr HKCU "Environment" "Path" "$0"
190+
${If} "$PathsToRemove" != ""
191+
nsExec::ExecToStack `"$OTPPath\bin\escript.exe" "$INSTDIR\update_system_path.erl" remove "$PathsToRemove"`
192+
Pop $0
193+
Pop $1
194+
${If} $0 != 0
195+
SetErrorlevel 5
196+
MessageBox MB_ICONSTOP "removing paths failed with $0: $1"
197+
Quit
198+
${EndIf}
199+
${EndIf}
205200
FunctionEnd
206201

207202
UninstPage custom un.FinishPageShow un.FinishPageLeave
208203

204+
!insertmacro MUI_UNPAGE_DIRECTORY
205+
!insertmacro MUI_UNPAGE_INSTFILES
206+
207+
Section "Uninstall"
208+
RMDir /r "$INSTDIR"
209+
SectionEnd
210+
209211
!insertmacro MUI_LANGUAGE "English"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env escript
2+
%%! -noinput
3+
4+
%% This file is used by the Elixir installer and uninstaller.
5+
main(["add", ";" ++ PathsToAdd]) ->
6+
{ok, Reg} = win32reg:open([read, write]),
7+
ok = win32reg:change_key(Reg, "\\hkey_current_user\\environment"),
8+
{ok, SystemPath} = win32reg:value(Reg, "path"),
9+
10+
NewSystemPath =
11+
lists:foldl(
12+
fun(Elem, Acc) ->
13+
Elem ++ ";" ++
14+
binary_to_list(
15+
iolist_to_binary(
16+
string:replace(Acc, Elem ++ ";", "", all)))
17+
end,
18+
SystemPath,
19+
string:split(PathsToAdd, ";", all)
20+
),
21+
22+
ok = win32reg:set_value(Reg, "Path", NewSystemPath),
23+
ok;
24+
25+
main(["remove", ";" ++ PathsToRemove]) ->
26+
{ok, Reg} = win32reg:open([read, write]),
27+
ok = win32reg:change_key(Reg, "\\hkey_current_user\\environment"),
28+
{ok, SystemPath} = win32reg:value(Reg, "path"),
29+
30+
NewSystemPath =
31+
lists:foldl(
32+
fun(Elem, Acc) ->
33+
binary_to_list(
34+
iolist_to_binary(
35+
string:replace(Acc, Elem ++ ";", "", all)))
36+
end,
37+
SystemPath,
38+
string:split(PathsToRemove, ";", all)
39+
),
40+
41+
ok = win32reg:set_value(Reg, "Path", NewSystemPath),
42+
ok.

0 commit comments

Comments
 (0)