dweiss commented on code in PR #16483: URL: https://github.com/apache/lucene/pull/16483#discussion_r3738401924
########## gradle/wrapper/GradleWrapper.java: ########## @@ -0,0 +1,238 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URI; +import java.net.URL; +import java.net.URLClassLoader; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.time.Duration; +import java.util.Properties; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** + * A simpler version of <a + * href="https://github.com/gradle/gradle/blob/master/platforms/core-runtime/wrapper-shared/src/main/java/org/gradle/wrapper/">Gradle's + * real wrapper</a>. This one doesn't use {@code org.gradle.cli} or other Gradle-internal class, + * skipping property-merging/CLI-flag handling (no {@code -g}/{@code -q} support here). + */ +public class GradleWrapper { + + public static void main(String[] args) throws Exception { + Path propertiesFile = findWrapperProperties(Path.of("").toAbsolutePath()); + Properties props = new Properties(); + try (InputStream in = Files.newInputStream(propertiesFile)) { + props.load(in); + } + + Path gradleHome = ensureDistribution(props, resolveGradleUserHome()); + System.exit(launchGradle(gradleHome, args)); + } + + private static Path findWrapperProperties(Path start) { + for (Path dir = start; dir != null; dir = dir.getParent()) { + Path candidate = + dir.resolve("gradle").resolve("wrapper").resolve("gradle-wrapper.properties"); + if (Files.isRegularFile(candidate)) { + return candidate; + } + } + throw new IllegalStateException( + "Could not locate gradle/wrapper/gradle-wrapper.properties above " + start); + } + + private static Path resolveGradleUserHome() { + String sysProp = System.getProperty("gradle.user.home"); + if (sysProp != null && !sysProp.isEmpty()) { + return Path.of(sysProp); + } + String env = System.getenv("GRADLE_USER_HOME"); + if (env != null && !env.isEmpty()) { + return Path.of(env); + } + return Path.of(System.getProperty("user.home"), ".gradle"); + } + + private static Path ensureDistribution(Properties props, Path gradleUserHome) throws Exception { + String urlStr = require(props, "distributionUrl"); + URI uri = URI.create(urlStr); + String zipName = Path.of(uri.getPath()).getFileName().toString(); + String baseName = + zipName.endsWith(".zip") ? zipName.substring(0, zipName.length() - 4) : zipName; + + // Doesn't need to match Gradle's own MD5-based cache hash scheme (PathAssembler) -- this is + // an independent cache, keyed only well enough to dedupe by exact distributionUrl. + String hash = sha256Hex(urlStr).substring(0, 16); + Path installRoot = + gradleUserHome.resolve("wrapper").resolve("dists").resolve(baseName).resolve(hash); Review Comment: There is a global lock in the original wrapper here and it's for a reason - if somebody starts multiple builds in parallel it can potentially leave an inconsistent state of a global "cached" install. I'm also not sure we can reuse the same directory as the official distribution... not sure what they do to these files/ directories. Maybe it'd be good not to interfere? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
