dsmiley commented on code in PR #16543: URL: https://github.com/apache/lucene/pull/16543#discussion_r3837079324
########## build-tools/build-infra/src/main/java/org/apache/lucene/gradle/IntranetGradleSetup.java: ########## @@ -0,0 +1,494 @@ +/* + * 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. + */ +package org.apache.lucene.gradle; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.math.BigInteger; +import java.net.HttpURLConnection; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLConnection; +import java.nio.channels.FileChannel; +import java.nio.channels.FileLock; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.PosixFilePermissions; +import java.security.MessageDigest; +import java.util.Comparator; +import java.util.List; +import java.util.Locale; +import java.util.Properties; +import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Standalone class run by {@code gradlew}/{@code gradlew.bat} (before the gradle wrapper) when the + * {@value #WRAPPER_URL_ENV} and/or {@value #DISTRIBUTION_URL_ENV} environment variables are set. It + * supports environments that cannot reach GitHub and services.gradle.org by downloading: + * + * <ul> + * <li>{@code gradle-wrapper.jar} from {@value #WRAPPER_URL_ENV} into {@code gradle/wrapper/} + * (verified against the checksum in {@code gradle-wrapper.jar.sha256}, so that {@code + * WrapperDownloader} doesn't need to fetch it), + * <li>the gradle distribution from {@value #DISTRIBUTION_URL_ENV}, installing it in the gradle + * user home exactly where the gradle wrapper would install the "official" distribution (under + * the hash of the {@code distributionUrl} from {@code gradle-wrapper.properties}), so that + * the wrapper considers it already installed. + * </ul> + * + * <p>Both URLs may contain a {@code ${gradleVersion}} placeholder, replaced with the version parsed + * from {@code distributionUrl} in {@code gradle-wrapper.properties}. + * + * <p>Ensure this class has no dependencies outside of standard java libraries as it's run directly + * from source. It only uses JDK 11 language features and APIs so that it runs on any reasonably + * recent JDK (17+ is required by {@link #checkVersion()}; the actual build requires a newer one). + */ +public class IntranetGradleSetup { + /** + * Copied to keep the class isolated from any other classes. + * + * @see "https://github.com/apache/lucene/issues/15399" + */ + @Retention(RetentionPolicy.CLASS) + @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) + private @interface SuppressForbidden { + /** A reason for suppressing should always be given. */ + String reason(); + } + + public static final String DISTRIBUTION_URL_ENV = "LUCENE_GRADLE_DISTRIBUTION_URL"; + public static final String WRAPPER_URL_ENV = "LUCENE_GRADLE_WRAPPER_URL"; + + private static final Pattern DISTRIBUTION_NAME = + Pattern.compile("gradle-(?<version>.+?)-(bin|all)\\.zip"); + private static final Pattern PLACEHOLDER = Pattern.compile("\\$\\{([^}]*)\\}"); + private static final boolean IS_WINDOWS = + System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("windows"); + + private final Path projectDir; + private final Path gradleUserHome; + + public static void main(String[] args) { + if (args.length < 1) { + System.err.println("Usage: java IntranetGradleSetup.java <project dir> [gradle arguments]"); + System.exit(2); + } + + try { + checkVersion(); + new IntranetGradleSetup(Paths.get(args[0]), args).run(); + } catch (Exception e) { + System.err.println("ERROR: " + e.getMessage()); + System.exit(3); + } + } + + public static void checkVersion() { + int major = Runtime.version().feature(); + if (major < 17) { + throw new IllegalStateException("java version must be 17 or later, your version: " + major); + } + } + + IntranetGradleSetup(Path projectDir, String[] gradleArgs) { + this.projectDir = projectDir.toAbsolutePath().normalize(); + + // Gradle user home: -g/--gradle-user-home, GRADLE_USER_HOME or ~/.gradle. + String userHome = null; + for (int i = 1; i < gradleArgs.length; i++) { + String arg = gradleArgs[i]; + if ((arg.equals("-g") || arg.equals("--gradle-user-home")) && i + 1 < gradleArgs.length) { + userHome = gradleArgs[++i]; + } else if (arg.startsWith("--gradle-user-home=")) { + userHome = arg.substring("--gradle-user-home=".length()); + } + } + if (userHome == null) { + userHome = System.getenv("GRADLE_USER_HOME"); + } + this.gradleUserHome = + userHome != null + ? Paths.get(userHome) + : Paths.get(System.getProperty("user.home"), ".gradle"); + } + + void run() throws Exception { + Path wrapperDir = projectDir.resolve("gradle").resolve("wrapper"); + Path propertiesFile = wrapperDir.resolve("gradle-wrapper.properties"); + if (!Files.exists(propertiesFile)) { + throw new IOException("Wrapper property file not found: " + propertiesFile); + } + Properties props = new Properties(); + try (Reader reader = Files.newBufferedReader(propertiesFile, StandardCharsets.UTF_8)) { + props.load(reader); + } + String distributionUrl = props.getProperty("distributionUrl"); + if (distributionUrl == null) { + throw new IOException("No 'distributionUrl' in " + propertiesFile); + } + Matcher m = + DISTRIBUTION_NAME.matcher(distributionUrl.replaceAll("\\?.*", "").replaceAll(".*/", "")); Review Comment: should have a comment to clarify what this is doing, maybe with an example -- 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]
