|
| 1 | +/* |
| 2 | + * (c) Copyright 2025 Palantir Technologies Inc. All rights reserved. |
| 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 com.palantir.javaformat.gradle |
| 18 | + |
| 19 | +import nebula.test.functional.internal.classpath.ClasspathAddingInitScriptBuilder |
| 20 | + |
| 21 | +import java.nio.charset.StandardCharsets |
| 22 | +import java.nio.file.Path |
| 23 | +import java.util.concurrent.TimeUnit |
| 24 | +import java.util.stream.Collectors |
| 25 | +import java.util.stream.Stream |
| 26 | + |
| 27 | +class GradlewExecutor { |
| 28 | + File projectDir |
| 29 | + |
| 30 | + GradlewExecutor(File projectDir) { |
| 31 | + this.projectDir = projectDir |
| 32 | + } |
| 33 | + |
| 34 | + private static Iterable<File> getBuildPluginClasspathInjector() { |
| 35 | + return getPluginClasspathInjector(Path.of("../gradle-palantir-java-format/build/pluginUnderTestMetadata/plugin-under-test-metadata.properties")) |
| 36 | + } |
| 37 | + |
| 38 | + private static Iterable<File> getPluginClasspathInjector(Path path) { |
| 39 | + File propertiesFile = path.toFile() |
| 40 | + Properties properties = new Properties() |
| 41 | + propertiesFile.withInputStream { inputStream -> |
| 42 | + properties.load(inputStream) |
| 43 | + } |
| 44 | + String classpath = properties.getProperty('implementation-classpath') |
| 45 | + return classpath.split(File.pathSeparator).collect { new File(it) } |
| 46 | + } |
| 47 | + |
| 48 | + GradlewExecutionResult runGradlewTasks(String... tasks) { |
| 49 | + ProcessBuilder processBuilder = getProcessBuilder(tasks) |
| 50 | + Process process = processBuilder.start() |
| 51 | + String output = readAllInput(process.getInputStream()) |
| 52 | + process.waitFor(1, TimeUnit.MINUTES) |
| 53 | + GradlewExecutionResult result = new GradlewExecutionResult(process.exitValue(), output) |
| 54 | + return result |
| 55 | + } |
| 56 | + |
| 57 | + final class GradlewExecutionResult { |
| 58 | + |
| 59 | + final Boolean success |
| 60 | + final String standardOutput |
| 61 | + final Throwable failure |
| 62 | + |
| 63 | + GradlewExecutionResult(int exitValue, String output) { |
| 64 | + this.success = exitValue == 0 |
| 65 | + this.standardOutput = output |
| 66 | + this.failure = exitValue != 0 ? new RuntimeException(String.format("Build failed with exitCode %s", exitValue)) : null |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static String readAllInput(InputStream inputStream) { |
| 71 | + try (Stream<String> lines = |
| 72 | + new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)).lines()) { |
| 73 | + return lines.collect(Collectors.joining("\n")); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private ProcessBuilder getProcessBuilder(String... tasks) { |
| 78 | + File initScript = new File(projectDir, "init.gradle") |
| 79 | + ClasspathAddingInitScriptBuilder.build(initScript, getBuildPluginClasspathInjector().toList()) |
| 80 | + List<String> arguments = ["./gradlew", "--init-script", initScript.toPath().toString()] |
| 81 | + Arrays.asList(tasks).forEach(arguments::add) |
| 82 | + ProcessBuilder processBuilder = new ProcessBuilder() |
| 83 | + .command(arguments) |
| 84 | + .directory(projectDir) |
| 85 | + .redirectErrorStream(true) |
| 86 | + return processBuilder |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments