返回 CodeWhale
codewhale.nsi
根目录 / scripts / installer / codewhale.nsi
1 ; codewhale.nsi — NSIS installer for CodeWhale (Windows)
2 ;
3 ; Requirements (see https://github.com/codewhale-hq/CodeWhale/issues/1983):
4 ; - Install codewhale.exe and codew.exe side-by-side (single binary, no codewhale-tui.exe)
5 ; - Default to %LOCALAPPDATA%\Programs\CodeWhale\bin
6 ; - Add install dir to current-user PATH
7 ; - Uninstaller removes the PATH entry
8 ; - Install codewhale.bat and a current-user Start Menu shortcut (#1854)
9 ; - Uninstaller removes the launcher and shortcut
10 ;
11 ; Usage:
12 ; 1. Place the binaries next to this script (codewhale.bat is already here):
13 ; codewhale.exe
14 ; codew.exe
15 ; 2. Build:
16 ; makensis /DVERSION=1.2.3 codewhale.nsi
17 ; 3. Output: CodeWhaleSetup.exe (in current directory)
18
19 ;--------------------------------
20 ; Includes
21 ;--------------------------------
22 !include "MUI2.nsh"
23 !include "FileFunc.nsh"
24
25 ;--------------------------------
26 ; General
27 ;--------------------------------
28 !ifndef VERSION
29 !define VERSION "0.0.0"
30 !endif
31
32 !define PRODUCT_NAME "CodeWhale"
33 !define PRODUCT_PUBLISHER "Hmbown"
34 !define PRODUCT_WEB_SITE "https://github.com/codewhale-hq/CodeWhale"
35
36 Name "${PRODUCT_NAME} ${VERSION}"
37 OutFile "CodeWhaleSetup.exe"
38 InstallDir "$LOCALAPPDATA\Programs\CodeWhale"
39 RequestExecutionLevel user
40 BrandingText "${PRODUCT_NAME} Installer"
41
42 ;--------------------------------
43 ; Interface Settings
44 ;--------------------------------
45 !define MUI_ABORTWARNING
46 !define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
47 !define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"
48
49 ;--------------------------------
50 ; Pages
51 ;--------------------------------
52 !insertmacro MUI_PAGE_WELCOME
53 !insertmacro MUI_PAGE_LICENSE "..\..\LICENSE"
54 !ifdef COMPILED_HOST
55 !insertmacro MUI_PAGE_COMPONENTS
56 !endif
57 !insertmacro MUI_PAGE_DIRECTORY
58 !insertmacro MUI_PAGE_INSTFILES
59 !insertmacro MUI_PAGE_FINISH
60
61 !insertmacro MUI_UNPAGE_CONFIRM
62 !insertmacro MUI_UNPAGE_INSTFILES
63
64 ;--------------------------------
65 ; Languages
66 ;--------------------------------
67 !insertmacro MUI_LANGUAGE "English"
68 !insertmacro MUI_LANGUAGE "SimpChinese"
69
70 ;--------------------------------
71 ; Installer Sections
72 ;--------------------------------
73 !macro ReadInstallOwner
74 StrCpy $1 ""
75 ClearErrors
76 FileOpen $0 "$INSTDIR\.codewhale-install" r
77 ${IfNot} ${Errors}
78 FileRead $0 $1
79 FileClose $0
80 ${EndIf}
81 !macroend
82
83 Section "Install" SecInstall
84 ${If} ${FileExists} "$INSTDIR\codewhale-notices"
85 !insertmacro ReadInstallOwner
86 ReadRegStr $0 HKCU "Software\${PRODUCT_NAME}" "NoticesDir"
87 ${If} $0 != "$INSTDIR\codewhale-notices"
88 ${OrIf} $1 != "CodeWhale"
89 IfSilent +2
90 MessageBox MB_ICONSTOP|MB_OK "The release notices destination already exists. Choose another installation directory."
91 SetErrorLevel 1
92 Abort
93 ${EndIf}
94 ${EndIf}
95 SetOutPath "$INSTDIR"
96 ClearErrors
97 FileOpen $0 "$INSTDIR\.codewhale-install" w
98 ${IfNot} ${Errors}
99 FileWrite $0 "CodeWhale"
100 FileClose $0
101 ${EndIf}
102 ${If} ${Errors}
103 DetailPrint "Could not write the CodeWhale install ownership marker to $INSTDIR."
104 IfSilent +2
105 MessageBox MB_ICONSTOP|MB_OK "CodeWhale could not record ownership of $INSTDIR. Installation has stopped."
106 SetErrorLevel 1
107 Abort
108 ${EndIf}
109 File "update-user-path.ps1"
110 ClearErrors
111 WriteRegStr HKCU "Software\${PRODUCT_NAME}" "NoticesDir" "$INSTDIR\codewhale-notices"
112 ${If} ${Errors}
113 SetErrorLevel 1
114 Abort
115 ${EndIf}
116 SetOutPath "$INSTDIR\codewhale-notices"
117 File "..\..\LICENSE"
118 File "..\..\THIRD_PARTY_NOTICES.md"
119 SetOutPath "$INSTDIR\codewhale-notices\docs"
120 File "..\..\docs\THIRD_PARTY_NOTICES.md"
121
122 SetOutPath "$INSTDIR\bin"
123
124 ; Copy binaries (single binary) and the Windows Terminal launcher (#1854)
125 File "codewhale.exe"
126 File "codew.exe"
127 File "codewhale.bat"
128
129 ; Write uninstaller
130 WriteUninstaller "$INSTDIR\Uninstall.exe"
131
132 CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
133 CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" "$INSTDIR\bin\codewhale.bat" "" "$INSTDIR\bin\codewhale.exe" 0
134
135 ; NSIS strings default to 1024 characters. ReadRegStr returns an empty string
136 ; when a registry value exceeds that limit, which used to make a long user
137 ; PATH look absent and overwrite it. Use PowerShell/.NET registry APIs so the
138 ; complete raw value and its REG_SZ/REG_EXPAND_SZ kind are preserved.
139 Call AddToUserPath
140
141 ; Store install directory for uninstaller
142 WriteRegStr HKCU "Software\${PRODUCT_NAME}" "InstallDir" "$INSTDIR"
143 WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "DisplayName" "${PRODUCT_NAME}"
144 WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "UninstallString" "$\"$INSTDIR\Uninstall.exe$\""
145 WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "QuietUninstallString" "$\"$INSTDIR\Uninstall.exe$\" /S"
146 WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "Publisher" "${PRODUCT_PUBLISHER}"
147 WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
148 WriteRegStr HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "DisplayVersion" "${VERSION}"
149 WriteRegDWORD HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "NoModify" 1
150 WriteRegDWORD HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "NoRepair" 1
151
152 ; Calculate and store installed size
153 ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
154 IntFmt $0 "0x%08X" $0
155 WriteRegDWORD HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "EstimatedSize" "$0"
156 SectionEnd
157
158 Function AddToUserPath
159 ExecWait '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$INSTDIR\update-user-path.ps1" -Operation Add -Entry "$INSTDIR\bin"' $0
160 IntCmp $0 0 add_path_done
161 DetailPrint "Failed to add CodeWhale to the current-user PATH (exit code $0)."
162 IfSilent +2
163 MessageBox MB_ICONSTOP|MB_OK "CodeWhale could not safely update your user PATH. Installation has stopped without replacing the existing PATH."
164 SetErrorLevel 1
165 Abort
166
167 add_path_done:
168 SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
169 FunctionEnd
170
171 ;--------------------------------
172 ; Uninstaller Section
173 ;--------------------------------
174 ; Only a reviewed release build defines COMPILED_HOST. The component is
175 ; explicitly opt-in and never changes [extension_host] runtime = "node".
176 !ifdef COMPILED_HOST
177 Section /o "Compiled plugin host (Bun, opt-in)" SecCompiledHost
178 SetOutPath "$INSTDIR\bin"
179 File "codewhale-extension-host.exe"
180 File "codewhale-extension-host.LICENSES.txt"
181 File "codewhale-extension-host.relink-source.tar.gz"
182 File "codewhale-extension-host.release.json"
183 WriteRegDWORD HKCU "Software\${PRODUCT_NAME}" "CompiledHostInstalled" 1
184 SectionEnd
185 !endif
186
187 !macro RemoveOwnedFile FILE
188 ClearErrors
189 Delete "${FILE}"
190 ${If} ${Errors}
191 DetailPrint "Could not remove ${FILE}."
192 IntOp $R9 $R9 + 1
193 ${EndIf}
194 !macroend
195
196 !macro RemoveOwnedDir DIR
197 ClearErrors
198 RMDir "${DIR}"
199 ${If} ${Errors}
200 ${IfNot} ${FileExists} "${DIR}\*.*"
201 ${If} ${FileExists} "${DIR}"
202 DetailPrint "Could not remove empty directory ${DIR}."
203 IntOp $R9 $R9 + 1
204 ${EndIf}
205 ${EndIf}
206 ${EndIf}
207 !macroend
208
209 Section "Uninstall"
210 StrCpy $R9 0
211 Call un.RequireOwnership
212
213 ; Remove only CodeWhale's exact entry before deleting the helper. The helper
214 ; handles PATH values longer than NSIS_MAX_STRLEN without truncation.
215 Call un.RemoveFromUserPath
216
217 ; Remove binaries, launcher, and Start Menu shortcut (single binary)
218 !insertmacro RemoveOwnedFile "$INSTDIR\bin\codewhale.exe"
219 !insertmacro RemoveOwnedFile "$INSTDIR\bin\codew.exe"
220 !insertmacro RemoveOwnedFile "$INSTDIR\bin\codewhale.bat"
221 ReadRegStr $0 HKCU "Software\${PRODUCT_NAME}" "NoticesDir"
222 ${If} $0 == "$INSTDIR\codewhale-notices"
223 ${If} ${FileExists} "$INSTDIR\codewhale-notices\LICENSE"
224 !insertmacro RemoveOwnedFile "$INSTDIR\codewhale-notices\LICENSE"
225 ${EndIf}
226 ${If} ${FileExists} "$INSTDIR\codewhale-notices\THIRD_PARTY_NOTICES.md"
227 !insertmacro RemoveOwnedFile "$INSTDIR\codewhale-notices\THIRD_PARTY_NOTICES.md"
228 ${EndIf}
229 ${If} ${FileExists} "$INSTDIR\codewhale-notices\docs\THIRD_PARTY_NOTICES.md"
230 !insertmacro RemoveOwnedFile "$INSTDIR\codewhale-notices\docs\THIRD_PARTY_NOTICES.md"
231 ${EndIf}
232 !insertmacro RemoveOwnedDir "$INSTDIR\codewhale-notices\docs"
233 !insertmacro RemoveOwnedDir "$INSTDIR\codewhale-notices"
234 ${If} $R9 == 0
235 ClearErrors
236 DeleteRegValue HKCU "Software\${PRODUCT_NAME}" "NoticesDir"
237 ${If} ${Errors}
238 IntOp $R9 $R9 + 1
239 ${EndIf}
240 ${EndIf}
241 ${EndIf}
242 Delete "$INSTDIR\bin\.codewhale-install.sha256"
243 Delete "$INSTDIR\bin\codewhale.old-*"
244 Delete "$INSTDIR\bin\codew.old-*"
245 ReadRegDWORD $R0 HKCU "Software\${PRODUCT_NAME}" "CompiledHostInstalled"
246 ${If} $R0 == 1
247 !insertmacro RemoveOwnedFile "$INSTDIR\bin\codewhale-extension-host.exe"
248 !insertmacro RemoveOwnedFile "$INSTDIR\bin\codewhale-extension-host.LICENSES.txt"
249 !insertmacro RemoveOwnedFile "$INSTDIR\bin\codewhale-extension-host.relink-source.tar.gz"
250 !insertmacro RemoveOwnedFile "$INSTDIR\bin\codewhale-extension-host.release.json"
251 ${EndIf}
252 !insertmacro RemoveOwnedFile "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
253 !insertmacro RemoveOwnedDir "$SMPROGRAMS\${PRODUCT_NAME}"
254
255 ${If} $R9 == 0
256 ClearErrors
257 ReadRegStr $0 HKCU "Software\${PRODUCT_NAME}" "InstallDir"
258 ${IfNot} ${Errors}
259 ClearErrors
260 DeleteRegValue HKCU "Software\${PRODUCT_NAME}" "InstallDir"
261 ${If} ${Errors}
262 DetailPrint "Could not remove the CodeWhale InstallDir registry value."
263 IntOp $R9 $R9 + 1
264 ${EndIf}
265 ${EndIf}
266 ClearErrors
267 ReadRegDWORD $0 HKCU "Software\${PRODUCT_NAME}" "CompiledHostInstalled"
268 ${IfNot} ${Errors}
269 ClearErrors
270 DeleteRegValue HKCU "Software\${PRODUCT_NAME}" "CompiledHostInstalled"
271 ${If} ${Errors}
272 DetailPrint "Could not remove the CodeWhale CompiledHostInstalled registry value."
273 IntOp $R9 $R9 + 1
274 ${EndIf}
275 ${EndIf}
276 ClearErrors
277 DeleteRegKey /ifempty HKCU "Software\${PRODUCT_NAME}"
278 ClearErrors
279 ReadRegStr $0 HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "DisplayName"
280 ${IfNot} ${Errors}
281 ClearErrors
282 DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
283 ${If} ${Errors}
284 DetailPrint "Could not remove the CodeWhale uninstall registration."
285 IntOp $R9 $R9 + 1
286 ${EndIf}
287 ${EndIf}
288 ${EndIf}
289
290 ${If} $R9 == 0
291 !insertmacro RemoveOwnedFile "$INSTDIR\Uninstall.exe"
292 ${If} $R9 == 0
293 !insertmacro RemoveOwnedFile "$INSTDIR\.codewhale-install"
294 ${EndIf}
295 ${If} $R9 == 0
296 !insertmacro RemoveOwnedFile "$INSTDIR\update-user-path.ps1"
297 ${EndIf}
298 ${EndIf}
299
300 !insertmacro RemoveOwnedDir "$INSTDIR\bin"
301 !insertmacro RemoveOwnedDir "$INSTDIR"
302
303 ${If} $R9 > 0
304 DetailPrint "CodeWhale uninstall did not remove $R9 item(s)."
305 IfSilent +2
306 MessageBox MB_ICONEXCLAMATION|MB_OK "CodeWhale uninstall did not remove $R9 item(s). Check the uninstall details; if Uninstall.exe is still present, run it again, otherwise remove the remaining CodeWhale files manually."
307 SetErrorLevel 1
308 ${EndIf}
309 SectionEnd
310
311 Function un.RequireOwnership
312 !insertmacro ReadInstallOwner
313 StrCmp $1 "CodeWhale" un_ownership_ok
314
315 DetailPrint "Refusing to uninstall: $INSTDIR is not a CodeWhale-owned install root."
316 IfSilent +2
317 MessageBox MB_ICONSTOP|MB_OK "CodeWhale could not verify that $INSTDIR is its installation. No files were removed."
318 SetErrorLevel 1
319 Abort
320
321 un_ownership_ok:
322 FunctionEnd
323
324 Function un.RemoveFromUserPath
325 ExecWait '"$SYSDIR\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "$INSTDIR\update-user-path.ps1" -Operation Remove -Entry "$INSTDIR\bin"' $0
326 IntCmp $0 0 remove_path_done
327 DetailPrint "Failed to remove CodeWhale from the current-user PATH (exit code $0)."
328 IfSilent +2
329 MessageBox MB_ICONSTOP|MB_OK "CodeWhale could not safely update your user PATH. Uninstallation has stopped without removing CodeWhale files or replacing the existing PATH."
330 SetErrorLevel 1
331 Abort
332
333 remove_path_done:
334 SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
335 FunctionEnd
336
336 lines Plain Text