|
| 1 | +/* |
| 2 | + * Copyright 2020 HiveMQ and the HiveMQ Community |
| 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 | +package com.hivemq.testcontainer.core; |
| 17 | + |
| 18 | +import net.lingala.zip4j.ZipFile; |
| 19 | +import org.apache.commons.lang3.SystemUtils; |
| 20 | +import org.jetbrains.annotations.NotNull; |
| 21 | + |
| 22 | +import java.io.BufferedReader; |
| 23 | +import java.io.File; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.util.AbstractMap; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.function.Supplier; |
| 29 | +import java.util.regex.Matcher; |
| 30 | +import java.util.regex.Pattern; |
| 31 | +import java.util.stream.Collectors; |
| 32 | + |
| 33 | +/** |
| 34 | + * This class automates the process of packaging a HiveMQ extension from a gradle project. |
| 35 | + * It uses ./gradlew on MacOS and Linux and uses gradlew.bat on Windows to execute the gradle task. |
| 36 | + * |
| 37 | + * @author Yannick Weber |
| 38 | + * @since 1.3.0 |
| 39 | + */ |
| 40 | +public class GradleHiveMQExtensionSupplier implements Supplier<File> { |
| 41 | + |
| 42 | + private static final @NotNull Pattern ROOT_PROJECT_PATTERN = Pattern.compile(".*'(.*)'"); |
| 43 | + private static final @NotNull String PROPERTY_REGEX = "(.*): (.*)"; |
| 44 | + |
| 45 | + private static final @NotNull String BUILD_STARTED = |
| 46 | + "=================================================================\n" + |
| 47 | + "=== Embedded Gradle build started: %s ===\n" + |
| 48 | + "=================================================================\n"; |
| 49 | + |
| 50 | + private static final @NotNull String BUILD_STOPPED = |
| 51 | + "=================================================================\n" + |
| 52 | + "=== Embedded Gradle build stopped: %s ===\n" + |
| 53 | + "================================================================="; |
| 54 | + |
| 55 | + private static final @NotNull String TASK = "hivemqExtensionZip"; |
| 56 | + |
| 57 | + private final @NotNull File gradleProjectDirectory; |
| 58 | + private boolean quiet = false; |
| 59 | + |
| 60 | + /** |
| 61 | + * This {@link Supplier} can be used if the current gradle project is the HiveMQ Extension to supply. |
| 62 | + * It uses the build.gradle file and the gradle wrapper of the current gradle project. |
| 63 | + * |
| 64 | + * @return a {@link GradleHiveMQExtensionSupplier} for the current gradle project |
| 65 | + * @since 1.3.0 |
| 66 | + */ |
| 67 | + public static @NotNull GradleHiveMQExtensionSupplier direct() { |
| 68 | + |
| 69 | + return new GradleHiveMQExtensionSupplier(new File("")); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates a Gradle HiveMQ extension {@link Supplier}. |
| 74 | + * It uses the gradle wrapper of the gradle project associated with the given It uses the build.gradle file. |
| 75 | + * |
| 76 | + * @param gradleProjectDirectory the gradle project directory of the HiveMQ extension to supply. |
| 77 | + * @since 1.3.0 |
| 78 | + */ |
| 79 | + public GradleHiveMQExtensionSupplier(final @NotNull File gradleProjectDirectory) { |
| 80 | + |
| 81 | + if (!gradleProjectDirectory.exists()) { |
| 82 | + throw new IllegalStateException(gradleProjectDirectory + " does not exist."); |
| 83 | + } |
| 84 | + if (!gradleProjectDirectory.canRead()) { |
| 85 | + throw new IllegalStateException(gradleProjectDirectory + " is not readable."); |
| 86 | + } |
| 87 | + this.gradleProjectDirectory = gradleProjectDirectory; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Packages the HiveMQ extension, copies it to a temporary directory and returns the directory as a {@link File}. |
| 92 | + * |
| 93 | + * @return the {@link File} of the packaged HiveMQ extension |
| 94 | + * @since 1.3.0 |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public @NotNull File get() { |
| 98 | + System.out.printf((BUILD_STARTED) + "%n", gradleProjectDirectory); |
| 99 | + |
| 100 | + try { |
| 101 | + final ProcessBuilder extensionZipProcessBuilder = new ProcessBuilder(); |
| 102 | + extensionZipProcessBuilder.directory(gradleProjectDirectory); |
| 103 | + extensionZipProcessBuilder.command(getCommandForOs(gradleProjectDirectory), TASK); |
| 104 | + extensionZipProcessBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); |
| 105 | + |
| 106 | + if (!quiet) { |
| 107 | + extensionZipProcessBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); |
| 108 | + } |
| 109 | + final Process extensionZipProcess = extensionZipProcessBuilder.start(); |
| 110 | + final int extensionZipExitCode = extensionZipProcess.waitFor(); |
| 111 | + if (extensionZipExitCode != 0) { |
| 112 | + throw new IllegalStateException("Gradle build exited with code " + extensionZipExitCode); |
| 113 | + } |
| 114 | + |
| 115 | + final ProcessBuilder propertiesProcessBuilder = new ProcessBuilder(); |
| 116 | + propertiesProcessBuilder.directory(gradleProjectDirectory); |
| 117 | + propertiesProcessBuilder.command(getCommandForOs(gradleProjectDirectory), "properties", "-q"); |
| 118 | + propertiesProcessBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); |
| 119 | + |
| 120 | + final Process propertiesProcess = propertiesProcessBuilder.start(); |
| 121 | + final int propertiesExitCode = propertiesProcess.waitFor(); |
| 122 | + if (propertiesExitCode != 0) { |
| 123 | + throw new IllegalStateException("Gradle build exited with code " + propertiesExitCode); |
| 124 | + } |
| 125 | + |
| 126 | + final BufferedReader br = new BufferedReader(new InputStreamReader(propertiesProcess.getInputStream())); |
| 127 | + |
| 128 | + final Map<String, String> gradleProperties = br.lines() |
| 129 | + .filter(s -> s.matches(PROPERTY_REGEX)) |
| 130 | + .map(s -> { |
| 131 | + final String[] splits = s.split(": "); |
| 132 | + return new AbstractMap.SimpleEntry<>(splits[0], splits[1]); |
| 133 | + }) |
| 134 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 135 | + |
| 136 | + |
| 137 | + System.out.printf((BUILD_STOPPED) + "%n", this.gradleProjectDirectory); |
| 138 | + |
| 139 | + final String projectVersion = gradleProperties.get("version"); |
| 140 | + final String rootProject = gradleProperties.get("rootProject"); |
| 141 | + final Matcher matcher = ROOT_PROJECT_PATTERN.matcher(rootProject); |
| 142 | + final boolean b = matcher.find(); |
| 143 | + final String projectName = matcher.group(1); |
| 144 | + |
| 145 | + final ZipFile zipFile = new ZipFile( |
| 146 | + new File(gradleProjectDirectory, |
| 147 | + "build/hivemq-extension/" + projectName + "-" + projectVersion + ".zip")); |
| 148 | + final File tempDir = Files.createTempDirectory("").toFile(); |
| 149 | + |
| 150 | + zipFile.extractAll(tempDir.getAbsolutePath()); |
| 151 | + return new File(tempDir, projectName); |
| 152 | + |
| 153 | + } catch (final Exception e) { |
| 154 | + throw new RuntimeException("Exception while building the HiveMQ extension with gradle.", e); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private @NotNull String getCommandForOs(final @NotNull File gradleProjectFile) { |
| 159 | + if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX || SystemUtils.IS_OS_LINUX) { |
| 160 | + final String gradleWrapper = gradleProjectFile + "/gradlew"; |
| 161 | + final File gradleWrapperBashFile = new File(gradleWrapper); |
| 162 | + if (gradleWrapperBashFile.exists()) { |
| 163 | + if (!gradleWrapperBashFile.canExecute()) { |
| 164 | + throw new IllegalStateException("Gradle Wrapper " + gradleWrapperBashFile.getAbsolutePath() + " can not be executed."); |
| 165 | + } |
| 166 | + return gradleWrapperBashFile.getAbsolutePath(); |
| 167 | + } |
| 168 | + } else if (SystemUtils.IS_OS_WINDOWS) { |
| 169 | + final String gradleWrapperBat = gradleProjectFile + "/gradlew.bat"; |
| 170 | + final File gradleWrapperBatFile = new File(gradleWrapperBat); |
| 171 | + if (gradleWrapperBatFile.exists()) { |
| 172 | + if (!gradleWrapperBatFile.canExecute()) { |
| 173 | + throw new IllegalStateException("Gradle Wrapper " + gradleWrapperBatFile.getAbsolutePath() + " can not be executed."); |
| 174 | + } |
| 175 | + return gradleWrapperBatFile.getAbsolutePath(); |
| 176 | + } |
| 177 | + } |
| 178 | + throw new IllegalStateException("Unkown OS Version"); |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Suppress stdout of the gradle build. |
| 184 | + * |
| 185 | + * @return self |
| 186 | + * @since 1.3.0 |
| 187 | + */ |
| 188 | + @NotNull |
| 189 | + public GradleHiveMQExtensionSupplier quiet() { |
| 190 | + this.quiet = true; |
| 191 | + return this; |
| 192 | + } |
| 193 | +} |
0 commit comments