This is an automated email from the ASF dual-hosted git repository.

jdaugherty pushed a commit to branch wrapper-rewrite
in repository https://gitbox.apache.org/repos/asf/grails-core.git

commit bbedae04dabb1a9be44e0085d37e36eeb16e34fa
Author: James Daugherty <[email protected]>
AuthorDate: Tue May 13 22:42:20 2025 -0400

    Rename GRAILS_HOME -> GRAILS_WRAPPER_HOME
---
 .../src/main/java/grails/init/GrailsUpdater.java   | 32 ++++++++++++--------
 .../{GrailsHome.java => GrailsWrapperHome.java}    | 35 ++++++++++++++--------
 .../main/java/grails/init/GrailsWrapperRepo.java   |  2 +-
 .../src/main/java/grails/init/Start.java           |  6 ++--
 4 files changed, 45 insertions(+), 30 deletions(-)

diff --git a/grails-wrapper/src/main/java/grails/init/GrailsUpdater.java 
b/grails-wrapper/src/main/java/grails/init/GrailsUpdater.java
index d8abe099d6..9ea1422e96 100644
--- a/grails-wrapper/src/main/java/grails/init/GrailsUpdater.java
+++ b/grails-wrapper/src/main/java/grails/init/GrailsUpdater.java
@@ -30,13 +30,14 @@ import java.net.URL;
 import java.nio.channels.Channels;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.List;
 
 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
 
 public class GrailsUpdater {
 
-    private final GrailsHome grailsHome;
+    private final GrailsWrapperHome grailsWrapperHome;
     private final GrailsVersion preferredVersion;
     private GrailsVersion updatedVersion;
 
@@ -45,7 +46,7 @@ public class GrailsUpdater {
     }
 
     public GrailsUpdater(List<GrailsReleaseType> allowedTypes, GrailsVersion 
preferredVersion, String possibleGrailsHome) throws IOException {
-        grailsHome = new GrailsHome(allowedTypes, possibleGrailsHome);
+        grailsWrapperHome = new GrailsWrapperHome(allowedTypes, 
possibleGrailsHome);
         this.preferredVersion = preferredVersion;
     }
 
@@ -58,11 +59,11 @@ public class GrailsUpdater {
             return updatedVersion;
         }
 
-        return grailsHome.latestVersion;
+        return grailsWrapperHome.latestVersion;
     }
 
     public File getExecutedJarFile() {
-        return 
grailsHome.getWrapperImplementation(grailsHome.getVersionDirectory(getSelectedVersion()));
+        return 
grailsWrapperHome.getWrapperImplementation(grailsWrapperHome.getVersionDirectory(getSelectedVersion()));
     }
 
     /**
@@ -71,13 +72,13 @@ public class GrailsUpdater {
      *
      */
     public boolean needsUpdating() {
-        File jarFile = grailsHome.getLatestWrapperImplementation();
+        File jarFile = grailsWrapperHome.getLatestWrapperImplementation();
         if(jarFile == null) {
             return true;
         }
 
         if(preferredVersion != null) {
-            if(!grailsHome.versions.contains(preferredVersion)) {
+            if (!grailsWrapperHome.versions.contains(preferredVersion)) {
                 return true;
             }
 
@@ -129,8 +130,8 @@ public class GrailsUpdater {
     private boolean updateJar(GrailsWrapperRepo repo, GrailsVersion version, 
String snapshotVersion) {
         boolean success = false;
 
-        final String localJarFilename = GrailsHome.CLI_COMBINED_PROJECT_NAME + 
"-" + version.version;
-        final String remoteJarFilename = snapshotVersion != null ? 
GrailsHome.CLI_COMBINED_PROJECT_NAME + "-" + snapshotVersion : 
GrailsHome.CLI_COMBINED_PROJECT_NAME + "-" + version.version;
+        final String localJarFilename = 
GrailsWrapperHome.CLI_COMBINED_PROJECT_NAME + "-" + version.version;
+        final String remoteJarFilename = snapshotVersion != null ? 
GrailsWrapperHome.CLI_COMBINED_PROJECT_NAME + "-" + snapshotVersion : 
GrailsWrapperHome.CLI_COMBINED_PROJECT_NAME + "-" + version.version;
         final String jarFileExtension = ".jar";
 
         try {
@@ -158,22 +159,27 @@ public class GrailsUpdater {
         return success;
     }
 
-    private boolean downloadWrapperJar(GrailsVersion toKeep, File 
downloadJarLocation, InputStream inputStream) throws IOException {
+    private boolean downloadWrapperJar(GrailsVersion version, File 
downloadJarLocation, InputStream inputStream) throws IOException {
         ReadableByteChannel rbc = Channels.newChannel(inputStream);
         try (FileOutputStream fos = new FileOutputStream(downloadJarLocation)) 
{
             fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
         }
 
         try {
-            grailsHome.cleanupOtherVersions(toKeep);
+            grailsWrapperHome.cleanupOtherVersions(version);
         }
         catch(Exception e) {
             System.err.println("Unable to cleanup old versions of the 
wrapper");
             e.printStackTrace();
         }
 
-        File directory = grailsHome.getVersionDirectory(toKeep);
-        Files.move(downloadJarLocation.getAbsoluteFile().toPath(), new 
File(directory, downloadJarLocation.getName()).getAbsoluteFile().toPath(), 
REPLACE_EXISTING);
+        File directory = grailsWrapperHome.getVersionDirectory(version);
+        if (!directory.exists()) {
+            directory.mkdirs();
+        }
+        Path jarFile = new File(directory, 
GrailsWrapperHome.CLI_COMBINED_PROJECT_NAME + "-" + version.version + 
".jar").toPath();
+        System.out.println("...Moving downloaded jar to: " + 
jarFile.toAbsolutePath());
+        Files.move(downloadJarLocation.getAbsoluteFile().toPath(), jarFile, 
REPLACE_EXISTING);
 
         return true;
     }
@@ -215,7 +221,7 @@ public class GrailsUpdater {
     }
 
     private String fetchSnapshotForVersion(GrailsWrapperRepo repo, 
GrailsVersion baseVersion) throws IOException, SAXException, 
ParserConfigurationException {
-        System.out.println("A Grails snapshot version has been detected.  
Downloading latest snapshot.");
+        System.out.println("...A Grails snapshot version has been detected.  
Downloading latest snapshot.");
 
         SAXParserFactory factory = SAXParserFactory.newInstance();
         SAXParser saxParser = factory.newSAXParser();
diff --git a/grails-wrapper/src/main/java/grails/init/GrailsHome.java 
b/grails-wrapper/src/main/java/grails/init/GrailsWrapperHome.java
similarity index 86%
rename from grails-wrapper/src/main/java/grails/init/GrailsHome.java
rename to grails-wrapper/src/main/java/grails/init/GrailsWrapperHome.java
index e2969ec3a5..232b70d56e 100644
--- a/grails-wrapper/src/main/java/grails/init/GrailsHome.java
+++ b/grails-wrapper/src/main/java/grails/init/GrailsWrapperHome.java
@@ -24,9 +24,9 @@ import java.util.List;
 import java.util.Objects;
 import java.util.stream.Stream;
 
-public class GrailsHome {
+public class GrailsWrapperHome {
     public static final String CLI_COMBINED_PROJECT_NAME = "grails-cli";
-    public static final List<String> GRAILS_HOME_MARKERS = 
List.of("settings.gradle", "gradlew", "gradlew.bat");
+    public static final List<String> GRAILS_MARKERS = 
List.of("settings.gradle", "gradlew", "gradlew.bat");
 
     public final File home;
     public final File wrapperDirectory;
@@ -34,7 +34,7 @@ public class GrailsHome {
     public final List<GrailsReleaseType> allowedReleaseTypes;
     public final GrailsVersion latestVersion;
 
-    public GrailsHome(List<GrailsReleaseType> allowedReleaseTypes, String 
forcedGrailsHome) throws IOException {
+    public GrailsWrapperHome(List<GrailsReleaseType> allowedReleaseTypes, 
String forcedGrailsHome) throws IOException {
         home = findGrailsHome(forcedGrailsHome).getCanonicalFile();
         this.allowedReleaseTypes = allowedReleaseTypes == null ? new 
ArrayList<>() : allowedReleaseTypes;
 
@@ -43,7 +43,7 @@ public class GrailsHome {
             wrapperDirectory.mkdirs();
         }
         else if(!wrapperDirectory.isDirectory()) {
-            throw new IllegalStateException("GRAILS_HOME must contain a 
wrapper directory. File exists instead at " + 
wrapperDirectory.getAbsolutePath());
+            throw new IllegalStateException("GRAILS_WRAPPER_HOME must contain 
a wrapper directory. File exists instead at " + 
wrapperDirectory.getAbsolutePath());
         }
 
         versions = determineVersions();
@@ -137,7 +137,7 @@ public class GrailsHome {
     }
 
     /**
-     * Finds the available versions in the GRAILS_HOME directory; sorted by 
smallest version to largest
+     * Finds the available versions in the GRAILS_WRAPPER_HOME directory; 
sorted by smallest version to largest
      */
     private List<GrailsVersion> determineVersions() {
         File[] children = wrapperDirectory.listFiles();
@@ -172,11 +172,11 @@ public class GrailsHome {
      * Locate the “Grails" home by:
      * 1. using the specified home
      * 2. using the environment variable
-     * 3. Looking in the current directory for a GRAILS_HOME_MARKERS or for a 
.grails directory
+     * 3. Looking in the current directory for a GRAILS_MARKERS or for a 
.grails directory
      * and all parent directories.  If none, is found, the current directory 
will be returned.
      * There is a special case for the current directory if inside of the 
grails core repository.
      *
-     * @return the GRAILS_HOME directory
+     * @return the GRAILS_WRAPPER_HOME directory
      * @throws IOException if canonicalization fails
      */
     public static File findGrailsHome(String grailsHomeOverride) throws 
IOException {
@@ -184,12 +184,21 @@ public class GrailsHome {
             return validateGrailsHome(grailsHomeOverride, "Specified Grails 
Home");
         }
 
-        String environmentOverride = System.getenv("GRAILS_HOME");
+        String environmentOverride = System.getenv("GRAILS_WRAPPER_HOME");
         if (environmentOverride != null && !environmentOverride.isEmpty()) {
-            return validateGrailsHome(environmentOverride, "GRAILS_HOME 
environment variable");
+            return validateGrailsHome(environmentOverride, 
"GRAILS_WRAPPER_HOME environment variable");
         }
 
-        return locateGrailsHome(new File("."));
+        // TODO: this previously allowed grails home to be in the grails 
project directory, but may no longer be needed
+        //return locateGrailsHome(new File("."));
+
+        File userHome = new 
File(System.getProperty("user.home")).getCanonicalFile();
+        File grailsHome = new File(userHome, ".grails");
+        if (grailsHome.exists() && !grailsHome.isDirectory()) {
+            throw new IllegalStateException("Grails Wrapper Home [" + 
grailsHome + "] is not a directory.");
+        }
+        grailsHome.mkdirs();
+        return grailsHome;
     }
 
     private static File validateGrailsHome(String possibleGrailsHome, String 
description) {
@@ -216,7 +225,7 @@ public class GrailsHome {
     }
 
     /**
-     * Locate the “Grails" home by first looking in `directory` for a 
GRAILS_HOME_MARKERS or for a .grails directory
+     * Locate the “Grails" home by first looking in `directory` for a 
GRAILS_MARKERS or for a .grails directory
      * and all parent directories.  If none, is found, the original directory 
will be returned.  Short circuit on the
      * home directory to avoid traversing into the root if possible.
      *
@@ -226,7 +235,7 @@ public class GrailsHome {
      */
     private static File locateGrailsHome(File directory) throws IOException {
         if (directory == null) {
-            throw new IllegalArgumentException("Cannot search for GRAILS_HOME 
from a null directory.");
+            throw new IllegalArgumentException("Cannot search for 
GRAILS_WRAPPER_HOME from a null directory.");
         }
 
         File userHome = new 
File(System.getProperty("user.home")).getCanonicalFile();
@@ -254,7 +263,7 @@ public class GrailsHome {
                 return originalDirectory;
             }
 
-            for (String name : GRAILS_HOME_MARKERS) {
+            for (String name : GRAILS_MARKERS) {
                 if (exists(searchDirectory, name)) {
                     return searchDirectory;
                 }
diff --git a/grails-wrapper/src/main/java/grails/init/GrailsWrapperRepo.java 
b/grails-wrapper/src/main/java/grails/init/GrailsWrapperRepo.java
index 4346879cf3..87a9ba102f 100644
--- a/grails-wrapper/src/main/java/grails/init/GrailsWrapperRepo.java
+++ b/grails-wrapper/src/main/java/grails/init/GrailsWrapperRepo.java
@@ -50,7 +50,7 @@ public class GrailsWrapperRepo {
 
     static GrailsWrapperRepo getSelectedRepo() {
         GrailsWrapperRepo repo = new GrailsWrapperRepo();
-        repo.repoPath = "org/apache/grails/" + 
GrailsHome.CLI_COMBINED_PROJECT_NAME;
+        repo.repoPath = "org/apache/grails/" + 
GrailsWrapperHome.CLI_COMBINED_PROJECT_NAME;
 
         String configured = getConfiguredMavenUrl();
         if (configured != null) {
diff --git a/grails-wrapper/src/main/java/grails/init/Start.java 
b/grails-wrapper/src/main/java/grails/init/Start.java
index a9d9efe69a..5531be7454 100644
--- a/grails-wrapper/src/main/java/grails/init/Start.java
+++ b/grails-wrapper/src/main/java/grails/init/Start.java
@@ -31,12 +31,12 @@ import java.util.Properties;
 import java.util.stream.Collectors;
 
 /**
- * The purpose of this class is to download the expanded Grails wrapper jars 
into GRAILS_HOME (`.grails` in the project root)
+ * The purpose of this class is to download the expanded Grails wrapper jars 
into GRAILS_WRAPPER_HOME (`.grails` in the project root)
  * This class is not meant to be distributed as part of SDKMAN since we'll 
distribute the expanded jars with it.
  * After downloading the jars, it will delegate to the downloaded grails-cli 
project.
  *
  * There are 3 ways this class can be used:
- * 1. in testing a grails release (run from a non-project directory) // will 
require GRAILS_HOME to be manually set
+ * 1. in testing a grails release (run from a non-project directory) // 
requires GRAILS_REPO_URL set to ~/.m2/repository
  * 2. running from a non-project directory (end user usage)
  * 3. running from inside a grails project
  */
@@ -68,7 +68,7 @@ public class Start {
             }
 
             URLClassLoader child = new URLClassLoader(new 
URL[]{updater.getExecutedJarFile().toURI().toURL()});
-            Class<?> classToLoad = Class.forName("grails.init.RunCommand", 
true, child);
+            Class<?> classToLoad = 
Class.forName("org.apache.grails.cli.DelegatingShellApplication", true, child);
             Method main = classToLoad.getMethod("main", String[].class);
             main.invoke(null, (Object[]) adjustedArgs);
         } catch (Exception e) {

Reply via email to