1 /*
<lambda>null2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.benchmark.darwin.gradle
18 
19 import java.io.ByteArrayOutputStream
20 import org.gradle.api.GradleException
21 import org.gradle.process.ExecOperations
22 
23 /** Executes the [ExecOperations] quietly. */
24 fun ExecOperations.executeQuietly(args: List<String>) {
25     val output = ByteArrayOutputStream()
26     // Combine stdout and stderr here. So when we need to surface exceptions
27     // the timeline is consistent.
28     output.use {
29         val result = exec { spec ->
30             spec.commandLine = args
31             spec.standardOutput = output
32             spec.errorOutput = output
33             // Throw a better exception
34             spec.isIgnoreExitValue = true
35         }
36         if (result.exitValue != 0) {
37             // Throw the exception with the full context.
38             throw GradleException(
39                 """
40                     ${output.toString(Charsets.UTF_8)}
41                 """
42                     .trimIndent()
43             )
44         }
45     }
46 }
47