Skip to content

Commit 24bec9c

Browse files
committed
enhance exec to provide an optional error stream
1 parent 0cea368 commit 24bec9c

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/com/inet/gradle/setup/abstracts/AbstractBuilder.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,23 @@ protected String exec( boolean ignoreExitValue, String... parameters ) {
106106
return baos.toString().trim();
107107
}
108108

109+
/**
110+
* Execute an external process.
111+
* Returns the response.
112+
*
113+
* @param ignoreExitValue if the exit value should be ignored
114+
* @param parameters command line
115+
* @param error the error output stream
116+
* @return the output
117+
*/
118+
protected String exec( boolean ignoreExitValue, OutputStream error, String... parameters ) {
119+
ArrayList<String> command = new ArrayList<>();
120+
command.addAll( Arrays.asList( parameters ) );
121+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
122+
exec( command, null, baos, error, ignoreExitValue );
123+
return baos.toString().trim();
124+
}
125+
109126
/**
110127
* Execute an external process.
111128
*
@@ -116,6 +133,20 @@ protected String exec( boolean ignoreExitValue, String... parameters ) {
116133
*/
117134
@SuppressWarnings( "resource" )
118135
protected void exec( ArrayList<String> parameters, InputStream input, OutputStream output, boolean ignoreExitValue ) {
136+
exec( parameters, input, output, output, ignoreExitValue );
137+
}
138+
139+
/**
140+
* Execute an external process.
141+
*
142+
* @param parameters command line
143+
* @param input optional InputStream for the process
144+
* @param output optional OutputStream for the process
145+
* @param error optional error OutputStream for the process
146+
* @param ignoreExitValue true, does not throw an exception if the return code is not equals zero.
147+
*/
148+
@SuppressWarnings( "resource" )
149+
protected void exec( ArrayList<String> parameters, InputStream input, OutputStream output, OutputStream error, boolean ignoreExitValue ) {
119150
// print command line to the log
120151
StringBuilder log = new StringBuilder( "\tCommand: " );
121152
for( String para : parameters ) {
@@ -142,14 +173,21 @@ protected void exec( ArrayList<String> parameters, InputStream input, OutputStre
142173
action.setCommandLine( parameters );
143174
action.setIgnoreExitValue( ignoreExitValue );
144175
action.setWorkingDir( buildDir );
176+
145177
if( input != null ) {
146178
action.setStandardInput( input );
147179
}
180+
148181
if( output == null ) {
149182
output = new IndentationOutputStream( System.out );
150183
}
184+
185+
if( error == null ) {
186+
error = new IndentationOutputStream( System.err );
187+
}
188+
151189
action.setStandardOutput( output );
152-
action.setErrorOutput( output );
190+
action.setErrorOutput( error );
153191
try {
154192
action.execute();
155193
} catch( Throwable th ) {

0 commit comments

Comments
 (0)