Skip to content

Commit 8af4b70

Browse files
ranjeshjCopilot
andauthored
Add uninstall choice for local WSL gateway (#591)
* Add uninstall gateway choice Prompt users during uninstall before removing the local WSL gateway and route the uninstall through a direct WSL cleanup helper so OpenClaw binaries are not launched from the install directory. The helper unregisters OpenClawGateway, cleans setup-managed gateway artifacts, and preserves generated gateway state when the user declines removal. Co-authored-by: Copilot <[email protected]> * Guard gateway cleanup against reparse points Refuse recursive gateway directory deletion when the target is a junction or symlink, matching the SetupEngine safety guard and preventing cleanup from crossing outside the app-owned WSL directory. Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 64b650c commit 8af4b70

7 files changed

Lines changed: 802 additions & 90 deletions

‎docs/images/uninstall-complete.png‎

16.9 KB
Loading
60.5 KB
Loading
35.5 KB
Loading
17.6 KB
Loading

‎installer.iss‎

Lines changed: 148 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Name: "startupicon"; Description: "Start OpenClaw Companion when Windows starts"
6969
[Files]
7070
; WinUI Tray app - include all files (WinUI needs DLLs, not single-file)
7171
Source: "{#publish}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs
72-
; WSL gateway uninstall helper — invoked by [UninstallRun] to drive clean removal
72+
; WSL gateway uninstall helper copied to {tmp} by [Code] during uninstall.
7373
Source: "scripts\Uninstall-LocalGateway.ps1"; DestDir: "{app}"; Flags: ignoreversion
7474

7575
[Registry]
@@ -91,25 +91,150 @@ Name: "{userstartup}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: st
9191
[Run]
9292
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
9393

94-
[UninstallRun]
95-
; ORDERING NOTE: Inno Setup runs [UninstallRun] entries BEFORE deleting {app}
96-
; directory contents. This guarantees OpenClawTray.exe is still present when
97-
; the script executes. See Inno docs: "[UninstallRun] section".
98-
; Fallback: if OpenClawTray.exe is missing for any reason, Uninstall-LocalGateway.ps1
99-
; logs the error to {app}\uninstall-gateway-error.log and exits 0 so Inno continues.
100-
; *** DO NOT COMMENT OUT OR REMOVE THE Flags LINE BELOW ***
101-
; waituntilterminated is non-negotiable: without it Inno races ahead and deletes
102-
; {app} while the PowerShell hook (and the CLI engine it invokes) is still running,
103-
; leaving 279+ application files behind after unins000.exe reports exit 0.
104-
; runhidden suppresses the console window that would otherwise flash briefly.
105-
Filename: "powershell.exe"; Parameters: "-NoProfile -ExecutionPolicy Bypass -File ""{app}\Uninstall-LocalGateway.ps1"""; Flags: shellexec waituntilterminated runhidden; StatusMsg: "Removing local WSL gateway..."
106-
; The hook launches our self-contained tray executable from {app}. Windows can
107-
; keep just-loaded .NET/WinUI DLL handles around briefly after the process exits;
108-
; give those handles time to release before Inno deletes the app directory.
109-
Filename: "powershell.exe"; Parameters: "-NoProfile -ExecutionPolicy Bypass -Command ""Start-Sleep -Seconds 3"""; Flags: waituntilterminated runhidden; StatusMsg: "Preparing installed files for removal..."
110-
111-
[UninstallDelete]
112-
; The tray creates runtime state (logs, WSL files, JSON metadata) under the
113-
; dedicated {app} directory. Remove the directory after the gateway cleanup hook
114-
; has finished so uninstall leaves neither app files nor generated state behind.
115-
Type: filesandordirs; Name: "{app}"
94+
[Code]
95+
var
96+
LocalGatewayCleanupChoiceInitialized: Boolean;
97+
LocalGatewayCleanupRequested: Boolean;
98+
LocalGatewayCleanupSucceeded: Boolean;
99+
100+
procedure EnsureLocalGatewayCleanupChoice;
101+
begin
102+
if LocalGatewayCleanupChoiceInitialized then
103+
Exit;
104+
105+
LocalGatewayCleanupChoiceInitialized := True;
106+
107+
if UninstallSilent() then
108+
begin
109+
LocalGatewayCleanupRequested := True;
110+
Log('Silent uninstall: local gateway cleanup will run automatically.');
111+
end
112+
else
113+
begin
114+
LocalGatewayCleanupRequested :=
115+
MsgBox(
116+
'Do you also want to remove the OpenClaw local WSL gateway?' + #13#10#13#10 +
117+
'Choose Yes to unregister the OpenClawGateway WSL distro and remove generated local gateway state.' + #13#10 +
118+
'Choose No to leave the local gateway and generated local state on this computer.',
119+
mbConfirmation,
120+
MB_YESNO) = IDYES;
121+
122+
if LocalGatewayCleanupRequested then
123+
Log('User chose to remove the local WSL gateway.')
124+
else
125+
Log('User chose to preserve the local WSL gateway and generated state.');
126+
end;
127+
end;
128+
129+
function RunLocalGatewayCleanupOnce(var ResultCode: Integer): Boolean;
130+
var
131+
SourceScriptPath: string;
132+
TempScriptPath: string;
133+
Params: string;
134+
begin
135+
SourceScriptPath := ExpandConstant('{app}\Uninstall-LocalGateway.ps1');
136+
TempScriptPath := ExpandConstant('{tmp}\Uninstall-LocalGateway.ps1');
137+
138+
if not FileExists(SourceScriptPath) then
139+
begin
140+
ResultCode := 2;
141+
Log('Local gateway cleanup script is missing: ' + SourceScriptPath);
142+
Result := False;
143+
Exit;
144+
end;
145+
146+
if FileExists(TempScriptPath) then
147+
DeleteFile(TempScriptPath);
148+
149+
if not CopyFile(SourceScriptPath, TempScriptPath, False) then
150+
begin
151+
ResultCode := 3;
152+
Log('Failed to copy local gateway cleanup script to: ' + TempScriptPath);
153+
Result := False;
154+
Exit;
155+
end;
156+
157+
Params :=
158+
'-NoProfile -ExecutionPolicy Bypass -File ' + AddQuotes(TempScriptPath) +
159+
' -AppRoot ' + AddQuotes(ExpandConstant('{app}'));
160+
161+
Log('Running local gateway cleanup script from {tmp}.');
162+
Result :=
163+
Exec(
164+
ExpandConstant('{sys}\WindowsPowerShell\v1.0\powershell.exe'),
165+
Params,
166+
'',
167+
SW_HIDE,
168+
ewWaitUntilTerminated,
169+
ResultCode);
170+
171+
if Result then
172+
Log('Local gateway cleanup script exited with code ' + IntToStr(ResultCode) + '.')
173+
else
174+
Log('Failed to start local gateway cleanup script. System error: ' + IntToStr(ResultCode) + '.');
175+
end;
176+
177+
procedure RunLocalGatewayCleanup;
178+
var
179+
ResultCode: Integer;
180+
Retry: Boolean;
181+
Started: Boolean;
182+
begin
183+
if not LocalGatewayCleanupRequested then
184+
Exit;
185+
186+
LocalGatewayCleanupSucceeded := False;
187+
188+
repeat
189+
Retry := False;
190+
UninstallProgressForm.StatusLabel.Caption := 'Removing local WSL gateway...';
191+
Started := RunLocalGatewayCleanupOnce(ResultCode);
192+
193+
if Started and (ResultCode = 0) then
194+
begin
195+
LocalGatewayCleanupSucceeded := True;
196+
Log('Local gateway cleanup completed successfully.');
197+
Exit;
198+
end;
199+
200+
if UninstallSilent() then
201+
begin
202+
Log('Local gateway cleanup failed during silent uninstall; continuing without deleting generated state.');
203+
Exit;
204+
end;
205+
206+
Retry :=
207+
MsgBox(
208+
'OpenClaw could not remove the local WSL gateway.' + #13#10#13#10 +
209+
'Exit code: ' + IntToStr(ResultCode) + #13#10#13#10 +
210+
'Select Retry to try again, or Cancel to continue uninstalling OpenClaw and leave local gateway state on disk.',
211+
mbError,
212+
MB_RETRYCANCEL) = IDRETRY;
213+
until not Retry;
214+
215+
Log('User continued uninstall after local gateway cleanup failed; generated state will be preserved.');
216+
end;
217+
218+
procedure DeleteGeneratedAppState;
219+
begin
220+
if not LocalGatewayCleanupSucceeded then
221+
Exit;
222+
223+
if DelTree(ExpandConstant('{app}'), True, True, True) then
224+
Log('Deleted generated app state from {app}.')
225+
else
226+
Log('Generated app state in {app} could not be fully deleted; continuing uninstall.');
227+
end;
228+
229+
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
230+
begin
231+
if CurUninstallStep = usUninstall then
232+
begin
233+
EnsureLocalGatewayCleanupChoice;
234+
RunLocalGatewayCleanup;
235+
end
236+
else if CurUninstallStep = usPostUninstall then
237+
begin
238+
DeleteGeneratedAppState;
239+
end;
240+
end;

0 commit comments

Comments
 (0)