11package com .github .itechbear .clion .cpplint ;
22
3- import com .github .itechbear .util .CygwinUtil ;
4- import com .github .itechbear .util .MinGWUtil ;
5- import com .intellij .openapi .project .Project ;
6- import com .intellij .openapi .wm .StatusBar ;
7-
83import java .io .BufferedReader ;
94import java .io .File ;
105import java .io .IOException ;
138import java .util .Collections ;
149import java .util .List ;
1510
11+ import com .github .itechbear .util .CygwinUtil ;
12+ import com .github .itechbear .util .MinGWUtil ;
13+ import com .google .common .base .Strings ;
14+ import com .intellij .openapi .project .Project ;
15+ import com .intellij .openapi .vfs .VirtualFile ;
16+ import com .intellij .openapi .wm .StatusBar ;
17+
1618/**
1719 * Created by HD on 2015/1/1.
1820 */
1921public class CpplintCommand {
20- public static String execute (Project project , String ... arg ) throws IOException {
21- List <String > args = new ArrayList <String >();
22+ public static String execute (Project project , String ... arg ) throws IOException {
23+ List <String > args = new ArrayList <String >();
2224
23- String python = Settings .get (Option .OPTION_KEY_PYTHON );
24- String cpplint = Settings .get (Option .OPTION_KEY_CPPLINT );
25- String cpplintOptions = Settings .get (Option .OPTION_KEY_CPPLINT_OPTIONS );
25+ String python = Settings .get (Option .OPTION_KEY_PYTHON );
26+ String cpplint = Settings .get (Option .OPTION_KEY_CPPLINT );
27+ String cpplintOptions = Settings .get (Option .OPTION_KEY_CPPLINT_OPTIONS );
2628
27- if (null == cpplint || cpplint .isEmpty ()) {
28- StatusBar .Info .set ("Please set path of cpplint.py first!" , project );
29- return "" ;
30- }
29+ if (null == cpplint || cpplint .isEmpty ()) {
30+ StatusBar .Info .set ("Please set path of cpplint.py first!" , project );
31+ return "" ;
32+ }
3133
32- // First time users will not have this Option set if they do not open the Settings
33- // UI yet.
34- if (null == cpplintOptions ) {
35- cpplintOptions = "" ;
36- }
34+ // First time users will not have this Option set if they do not open the Settings
35+ // UI yet.
36+ if (null == cpplintOptions ) {
37+ cpplintOptions = "" ;
38+ }
3739
38- if (MinGWUtil .isMinGWEnvironment ()) {
39- args .add (python );
40- args .add (cpplint );
41- Collections .addAll (args , cpplintOptions .split ("\\ s+" ));
42- Collections .addAll (args , arg );
43- }
44- else
45- {
46- args .add (CygwinUtil .getBashPath ());
47- args .add ("-c" );
48- String joinedArgs ;
49- if (CygwinUtil .isCygwinEnvironment ()){
50- joinedArgs = "\" \\ \" " + python + "\\ \" \\ \" " + cpplint + "\\ \" " + cpplintOptions + " " ;
51- for (String oneArg : arg )
52- joinedArgs += "\\ \" " + oneArg + "\\ \" " ;
53- joinedArgs += '\"' ;
54- }
55- else {
56- joinedArgs = "\" " + python + "\" \" " + cpplint + "\" " + cpplintOptions + " " ;
57- for (String oneArg : arg )
58- joinedArgs += "\" " + oneArg + "\" " ;
59- }
60- args .add (joinedArgs );
61- }
40+ if (MinGWUtil .isMinGWEnvironment ()) {
41+ args .add (python );
42+ args .add (cpplint );
43+ Collections .addAll (args , cpplintOptions .split ("\\ s+" ));
44+ Collections .addAll (args , arg );
45+ } else {
46+ args .add (CygwinUtil .getBashPath ());
47+ args .add ("-c" );
48+ String joinedArgs ;
49+ if (CygwinUtil .isCygwinEnvironment ()) {
50+ joinedArgs = "\" \\ \" " + python + "\\ \" \\ \" " + cpplint + "\\ \" " + cpplintOptions + " " ;
51+ for (String oneArg : arg )
52+ joinedArgs += "\\ \" " + oneArg + "\\ \" " ;
53+ joinedArgs += '\"' ;
54+ } else {
55+ joinedArgs = "\" " + python + "\" \" " + cpplint + "\" " + cpplintOptions + " " ;
56+ for (String oneArg : arg )
57+ joinedArgs += "\" " + oneArg + "\" " ;
58+ }
59+ args .add (joinedArgs );
60+ }
61+
62+ final VirtualFile baseDir = project .getBaseDir ();
63+ if (null == baseDir ) {
64+ return "" ;
65+ }
66+ final String canonicalPath = baseDir .getCanonicalPath ();
67+ if (Strings .isNullOrEmpty (canonicalPath )) {
68+ return "" ;
69+ }
70+ File cpplintWorkingDirectory = new File (canonicalPath );
71+ final Process process = Runtime .getRuntime ().exec (
72+ args .toArray (new String [args .size ()]), null , cpplintWorkingDirectory );
73+
74+ final StringBuilder outString = new StringBuilder ();
75+ Thread outThread = new Thread (new Runnable () {
76+ @ Override
77+ public void run () {
78+ BufferedReader outStream = new BufferedReader (
79+ new InputStreamReader (process .getInputStream ()));
80+ String line ;
81+ try {
82+ while ((line = outStream .readLine ()) != null ) {
83+ outString .append (line + "\n " );
84+ }
85+ } catch (IOException ex ) {
86+ ex .printStackTrace ();
87+ } finally {
88+ try {
89+ outStream .close ();
90+ } catch (IOException e ) {
91+ e .printStackTrace ();
92+ }
93+ }
94+ }
95+ });
96+ outThread .start ();
6297
63- File cpplintWorkingDirectory = new File (project .getBaseDir ().getCanonicalPath ());
64- final Process process = Runtime .getRuntime ().exec (
65- args .toArray (new String [args .size ()]), null , cpplintWorkingDirectory );
98+ final StringBuilder errString = new StringBuilder ();
99+ Thread errorThread = new Thread (new Runnable () {
100+ @ Override
101+ public void run () {
102+ BufferedReader errStream = new BufferedReader (new
103+ InputStreamReader (process .getErrorStream ()));
104+ String line ;
105+ try {
106+ while ((line = errStream .readLine ()) != null ) {
107+ errString .append (line + "\n " );
108+ }
109+ } catch (IOException ex ) {
110+ ex .printStackTrace ();
111+ } finally {
112+ try {
113+ errStream .close ();
114+ } catch (IOException e ) {
115+ e .printStackTrace ();
116+ }
117+ }
118+ }
119+ });
120+ errorThread .start ();
66121
67- final StringBuilder outString = new StringBuilder ();
68- Thread outThread = new Thread (new Runnable () {
69- @ Override
70- public void run () {
71- BufferedReader outStream = new BufferedReader (
72- new InputStreamReader (process .getInputStream ()));
73- String line ;
74122 try {
75- while ((line = outStream .readLine ()) != null ) {
76- outString .append (line + "\n " );
77- }
78- } catch (IOException ex ) {
79- ex .printStackTrace ();
80- } finally {
81- try {
82- outStream .close ();
83- } catch (IOException e ) {
123+ outThread .join ();
124+ } catch (InterruptedException e ) {
84125 e .printStackTrace ();
85- }
86126 }
87- }
88- });
89- outThread .start ();
90-
91- final StringBuilder errString = new StringBuilder ();
92- Thread errorThread = new Thread (new Runnable () {
93- @ Override
94- public void run () {
95- BufferedReader errStream = new BufferedReader (new
96- InputStreamReader (process .getErrorStream ()));
97- String line ;
98127 try {
99- while ((line = errStream .readLine ()) != null ) {
100- errString .append (line + "\n " );
101- }
102- } catch (IOException ex ) {
103- ex .printStackTrace ();
104- } finally {
105- try {
106- errStream .close ();
107- } catch (IOException e ) {
128+ errorThread .join ();
129+ } catch (InterruptedException e ) {
108130 e .printStackTrace ();
109- }
110131 }
111- }
112- });
113- errorThread .start ();
114132
115- try {
116- outThread .join ();
117- } catch (InterruptedException e ) {
118- e .printStackTrace ();
119- }
120- try {
121- errorThread .join ();
122- } catch (InterruptedException e ) {
123- e .printStackTrace ();
133+ return errString .toString ();
124134 }
125-
126- return errString .toString ();
127- }
128135}
0 commit comments