1616
1717package com .tang .intellij .lua .debugger .attach ;
1818
19+ import com .intellij .execution .configurations .GeneralCommandLine ;
20+ import com .intellij .execution .process .OSProcessHandler ;
21+ import com .intellij .execution .process .ProcessEvent ;
22+ import com .intellij .execution .process .ProcessListener ;
23+ import com .intellij .execution .process .ProcessOutputTypes ;
1924import com .intellij .execution .ui .ConsoleViewContentType ;
25+ import com .intellij .openapi .util .Key ;
2026import com .intellij .openapi .vfs .VirtualFile ;
2127import com .intellij .xdebugger .XDebugSession ;
2228import com .intellij .xdebugger .XExpression ;
3339
3440import javax .xml .parsers .DocumentBuilder ;
3541import javax .xml .parsers .DocumentBuilderFactory ;
36- import java .io .*;
42+ import java .io .BufferedWriter ;
43+ import java .io .ByteArrayInputStream ;
44+ import java .io .IOException ;
45+ import java .io .OutputStreamWriter ;
3746import java .nio .charset .Charset ;
38- import java .util .ArrayList ;
3947import java .util .HashMap ;
40- import java .util .List ;
4148import java .util .Map ;
4249
4350/**
4451 * debug bridge
4552 * Created by tangzx on 2017/3/26.
4653 */
4754public class LuaAttachBridge {
55+ private OSProcessHandler handler ;
4856 private XDebugSession session ;
49- private Process process ;
5057 private BufferedWriter writer ;
51- private BufferedReader reader ;
52- private Thread writerThread ;
53- private Thread readerThread ;
54- private boolean isRunning ;
5558 private ProtoHandler protoHandler ;
5659 private ProtoFactory protoFactory ;
5760 private int evalIdCounter = 0 ;
@@ -85,38 +88,46 @@ public LuaAttachBridge(XDebugSession session) {
8588 this .session = session ;
8689 }
8790
88- private Runnable readProcess = new Runnable () {
91+ private ProcessListener processListener = new ProcessListener () {
92+ private boolean readProto = false ;
93+ private StringBuilder sb ;
94+
8995 @ Override
90- public void run () {
91- boolean readProto = false ;
92- StringBuilder sb = null ;
93- while (isRunning ) {
94- try {
95- String line = reader .readLine ();
96- if (line == null )
97- break ;
98- if (readProto ) {
99- if (line .startsWith ("[end]" )) {
100- readProto = false ;
101- String data = sb .toString ();
102- LuaAttachProto proto = parse (data );
103- if (proto != null )
104- handleProto (proto );
105- } else {
106- sb .append (line );
107- }
96+ public void startNotified (ProcessEvent processEvent ) {
97+
98+ }
99+
100+ @ Override
101+ public void processTerminated (ProcessEvent processEvent ) {
102+ stop (false );
103+ }
104+
105+ @ Override
106+ public void processWillTerminate (ProcessEvent processEvent , boolean b ) {
107+
108+ }
109+
110+ @ Override
111+ public void onTextAvailable (ProcessEvent processEvent , Key key ) {
112+ if (key == ProcessOutputTypes .STDOUT ) {
113+ String line = processEvent .getText ();
114+ if (readProto ) {
115+ if (line .startsWith ("[end]" )) {
116+ readProto = false ;
117+ String data = sb .toString ();
118+ LuaAttachProto proto = parse (data );
119+ if (proto != null )
120+ handleProto (proto );
108121 } else {
109- readProto = line .startsWith ("[start]" );
110- if (readProto ) {
111- sb = new StringBuilder ();
112- }
122+ sb .append (line );
123+ }
124+ } else {
125+ readProto = line .startsWith ("[start]" );
126+ if (readProto ) {
127+ sb = new StringBuilder ();
113128 }
114- } catch (IOException e ) {
115- e .printStackTrace ();
116- break ;
117129 }
118130 }
119- stop (false );
120131 }
121132 };
122133
@@ -137,10 +148,6 @@ private void handleEvalCallback(LuaAttachEvalResultProto proto) {
137148 }
138149 }
139150
140- private Runnable writeProcess = () -> {
141-
142- };
143-
144151 private String getEmmyLua () {
145152 return LuaFileUtil .getPluginVirtualFile ("debugger/Emmy.lua" );
146153 }
@@ -164,26 +171,17 @@ public void attach(int processId) {
164171 // attach debugger
165172 String exe = LuaFileUtil .getPluginVirtualFile (String .format ("debugger/windows/%s/Debugger.exe" , archType ));
166173
167- processBuilder = new ProcessBuilder (exe , "-m" , "attach" , "-p" , pid , "-e" , getEmmyLua ());
168-
169- process = processBuilder .start ();
170- writer = new BufferedWriter (new OutputStreamWriter (process .getOutputStream ()));
171- reader = new BufferedReader (new InputStreamReader (process .getInputStream (), Charset .forName ("UTF-8" )));
172-
173- readerThread = new Thread (readProcess );
174- readerThread .start ();
175- writerThread = new Thread (writeProcess );
176- writerThread .start ();
177- isRunning = true ;
174+ GeneralCommandLine commandLine = new GeneralCommandLine (exe );
175+ commandLine .addParameters ("-m" , "attach" , "-p" , pid , "-e" , getEmmyLua ());
176+ commandLine .setCharset ( Charset .forName ("UTF-8" ));
177+ handler = new OSProcessHandler (commandLine );
178+ handler .addProcessListener (processListener );
179+ handler .startNotify ();
180+ writer = new BufferedWriter (new OutputStreamWriter (handler .getProcess ().getOutputStream ()));
178181 }
179182 } catch (Exception e ) {
180- /*ByteArrayOutputStream stream = new ByteArrayOutputStream();
181- PrintStream ps = new PrintStream(stream);
182- e.printStackTrace(ps);
183- session.getConsoleView().print(stream.toString(), ConsoleViewContentType.ERROR_OUTPUT);*/
184183 session .getConsoleView ().print (e .getMessage (), ConsoleViewContentType .ERROR_OUTPUT );
185184 session .stop ();
186- isRunning = false ;
187185 }
188186 }
189187
@@ -212,43 +210,25 @@ public void launch(@NotNull String program, String workingDir, String[] args) {
212210 // attach debugger
213211 String exe = LuaFileUtil .getPluginVirtualFile (String .format ("debugger/windows/%s/Debugger.exe" , archType ));
214212
215- List <String > argList = new ArrayList <>();
216- argList .add (exe );
217- argList .add ("-m" ); argList .add ("run" );
218- argList .add ("-c" ); argList .add (program );
219- argList .add ("-e" ); argList .add (getEmmyLua ());
220- if (!workingDir .isEmpty ()) {
221- argList .add ("-w" );
222- argList .add (workingDir );
223- }
213+ GeneralCommandLine commandLine = new GeneralCommandLine (exe );
214+ commandLine .setCharset ( Charset .forName ("UTF-8" ));
215+ commandLine .addParameters ("-m" , "run" , "-c" , program , "-e" , getEmmyLua (), "-w" , workingDir );
224216 if (args != null ) {
225217 String argString = String .join (" " , args );
226218 if (!argString .isEmpty ()) {
227- argList .add ("-a" );
228- argList .add (argString );
219+ commandLine .addParameters ("-a" , argString );
229220 }
230221 }
231222
232- processBuilder = new ProcessBuilder (argList );
223+ handler = new OSProcessHandler (commandLine );
224+ handler .addProcessListener (processListener );
225+ handler .startNotify ();
233226
234- process = processBuilder .start ();
235- writer = new BufferedWriter (new OutputStreamWriter (process .getOutputStream ()));
236- reader = new BufferedReader (new InputStreamReader (process .getInputStream (), Charset .forName ("UTF-8" )));
237-
238- readerThread = new Thread (readProcess );
239- readerThread .start ();
240- writerThread = new Thread (writeProcess );
241- writerThread .start ();
242- isRunning = true ;
227+ writer = new BufferedWriter (new OutputStreamWriter (handler .getProcess ().getOutputStream ()));
243228 }
244229 } catch (Exception e ) {
245- /*ByteArrayOutputStream stream = new ByteArrayOutputStream();
246- PrintStream ps = new PrintStream(stream);
247- e.printStackTrace(ps);
248- session.getConsoleView().print(stream.toString(), ConsoleViewContentType.ERROR_OUTPUT);*/
249230 session .getConsoleView ().print (e .getMessage (), ConsoleViewContentType .ERROR_OUTPUT );
250231 session .stop ();
251- isRunning = false ;
252232 }
253233 }
254234
@@ -260,18 +240,9 @@ void stop(boolean detach) {
260240 if (detach )
261241 send ("detach" );
262242 writer = null ;
263- reader = null ;
264- isRunning = false ;
265- if (process != null ) {
266- process .destroy ();
267- process = null ;
268- isRunning = false ;
269- }
270- if (writerThread != null ) {
271- writerThread .interrupt ();
272- }
273- if (readerThread != null ) {
274- readerThread .interrupt ();
243+ if (handler != null ) {
244+ handler .destroyProcess ();
245+ handler = null ;
275246 }
276247 }
277248
0 commit comments