11use std:: {
22 net:: { SocketAddr , TcpListener } ,
3- process:: Command ,
43 sync:: { Arc , Mutex } ,
54 time:: { Duration , Instant } ,
65} ;
76#[ cfg( target_os = "macos" ) ]
87use tauri:: TitleBarStyle ;
9- use tauri:: { AppHandle , LogicalSize , Manager , Monitor , RunEvent , WebviewUrl , WebviewWindow } ;
10- use tauri_plugin_dialog:: { DialogExt , MessageDialogButtons , MessageDialogResult } ;
8+ use tauri:: { AppHandle , LogicalSize , Manager , RunEvent , WebviewUrl , WebviewWindow } ;
119use tauri_plugin_shell:: process:: { CommandChild , CommandEvent } ;
1210use tauri_plugin_shell:: ShellExt ;
1311use tokio:: net:: TcpSocket ;
1412
1513#[ derive( Clone ) ]
1614struct ServerState ( Arc < Mutex < Option < CommandChild > > > ) ;
1715
16+ #[ tauri:: command]
17+ fn kill_sidecar ( app : AppHandle ) {
18+ let Some ( server_state) = app. try_state :: < ServerState > ( ) else {
19+ println ! ( "Server not running" ) ;
20+ return ;
21+ } ;
22+
23+ let Some ( server_state) = server_state
24+ . 0
25+ . lock ( )
26+ . expect ( "Failed to acquire mutex lock" )
27+ . take ( )
28+ else {
29+ println ! ( "Server state missing" ) ;
30+ return ;
31+ } ;
32+
33+ let _ = server_state. kill ( ) ;
34+
35+ println ! ( "Killed server" ) ;
36+ }
37+
1838fn get_sidecar_port ( ) -> u16 {
1939 option_env ! ( "OPENCODE_PORT" )
2040 . map ( |s| s. to_string ( ) )
@@ -29,40 +49,6 @@ fn get_sidecar_port() -> u16 {
2949 } )
3050}
3151
32- fn find_and_kill_process_on_port ( port : u16 ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
33- // Find all listeners on the specified port
34- let listeners = listeners:: get_processes_by_port ( port) ?;
35-
36- if listeners. is_empty ( ) {
37- println ! ( "No processes found listening on port {}" , port) ;
38- return Ok ( ( ) ) ;
39- }
40-
41- for listener in listeners {
42- let pid = listener. pid ;
43- println ! ( "Found process {} listening on port {}" , pid, port) ;
44-
45- // Kill the process using platform-appropriate command
46- #[ cfg( target_os = "windows" ) ]
47- {
48- Command :: new ( "taskkill" )
49- . args ( [ "/F" , "/PID" , & pid. to_string ( ) ] )
50- . output ( ) ?;
51- }
52-
53- #[ cfg( not( target_os = "windows" ) ) ]
54- {
55- Command :: new ( "kill" )
56- . args ( [ "-9" , & pid. to_string ( ) ] )
57- . output ( ) ?;
58- }
59-
60- println ! ( "Killed process {}" , pid) ;
61- }
62-
63- Ok ( ( ) )
64- }
65-
6652fn spawn_sidecar ( app : & AppHandle , port : u16 ) -> CommandChild {
6753 let ( mut rx, child) = app
6854 . shell ( )
@@ -116,6 +102,7 @@ pub fn run() {
116102 . plugin ( tauri_plugin_shell:: init ( ) )
117103 . plugin ( tauri_plugin_process:: init ( ) )
118104 . plugin ( tauri_plugin_opener:: init ( ) )
105+ . invoke_handler ( tauri:: generate_handler![ kill_sidecar] )
119106 . setup ( move |app| {
120107 let app = app. handle ( ) . clone ( ) ;
121108
@@ -124,28 +111,6 @@ pub fn run() {
124111
125112 let should_spawn_sidecar = !is_server_running ( port) . await ;
126113
127- // if server_running {
128- // let res = app
129- // .dialog()
130- // .message(
131- // "OpenCode Server is already running, would you like to restart it?",
132- // )
133- // .buttons(MessageDialogButtons::YesNo)
134- // .blocking_show_with_result();
135-
136- // match res {
137- // MessageDialogResult::Yes => {
138- // if let Err(e) = find_and_kill_process_on_port(port) {
139- // eprintln!("Failed to kill process on port {}: {}", port, e);
140- // }
141- // true
142- // }
143- // _ => false,
144- // }
145- // } else {
146- // true
147- // };
148-
149114 let child = if should_spawn_sidecar {
150115 let child = spawn_sidecar ( & app, port) ;
151116
@@ -218,24 +183,7 @@ pub fn run() {
218183 if let RunEvent :: Exit = event {
219184 println ! ( "Received Exit" ) ;
220185
221- let Some ( server_state) = app. try_state :: < ServerState > ( ) else {
222- println ! ( "Server not running" ) ;
223- return ;
224- } ;
225-
226- let Some ( server_state) = server_state
227- . 0
228- . lock ( )
229- . expect ( "Failed to acquire mutex lock" )
230- . take ( )
231- else {
232- println ! ( "Server state missing" ) ;
233- return ;
234- } ;
235-
236- let _ = server_state. kill ( ) ;
237-
238- println ! ( "Killed server" ) ;
186+ kill_sidecar ( app. clone ( ) ) ;
239187 }
240188 } ) ;
241189}
0 commit comments