diff --git a/CHANGELOG.md b/CHANGELOG.md index 68471ae..5ced5ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,12 +5,23 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning]. ## Unreleased -- / + +- added check for minimal Gradle version +- added check for minimal ModDevGradle version +- updated internal ModDevGradle version to 2.0.80 +- updated minimum ModDevGradle version to 2.0.64-beta +- updated Gradle wrapper to 8.12.1 +- updated minimum Gradle version to 8.12.1 +- changed datagen run directory to temporary directory to avoid crashes with file-based runtime mods +- changed game test namespace to the test mod instead of the main mod +- changed game test run directory to temporary directory to improve load time ## [1.1.1] - 2024-09-16 + - fixed compile error with newest ModDevGradle ([MDG#158](https://github.com/neoforged/ModDevGradle/pull/158)) ## [1.1.0] - 2024-09-05 + - added `localImplementation` and `testLocalImplementation` to load dependencies into compile & runtime classpath without being transitive for consumers ## [1.0.1] - 2024-09-04 diff --git a/README.md b/README.md index b897cb8..75fc609 100644 --- a/README.md +++ b/README.md @@ -263,7 +263,9 @@ almostgradle.launchArgs.mixinDebugOutput = true ## Data Generation -This feature generates a run configuration for data generation. +This feature generates a run configuration for data generation. The game directory is set to a temporary folder inside +the build directory to avoid crashes with file-based runtime mods. If you rely on runtime mods in the data generation, +they have to be loaded via Gradle. ### Defaults: diff --git a/build.gradle.kts b/build.gradle.kts index 0f75220..1255c60 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ group = "com.almostreliable" version = "1.1.1" plugins { - id("com.gradle.plugin-publish") version "1.2.2" + id("com.gradle.plugin-publish") version "1.3.1" } gradlePlugin { @@ -38,12 +38,12 @@ repositories { buildscript { dependencies { - classpath("net.neoforged:moddev-gradle:2.0.+") + classpath("net.neoforged:moddev-gradle:2.0.80") classpath("com.github.gmazzo.buildconfig:plugin:5.4.0") } } dependencies { - compileOnly("net.neoforged:moddev-gradle:2.0.+") + compileOnly("net.neoforged:moddev-gradle:2.0.80") implementation("com.github.gmazzo.buildconfig:plugin:5.4.0") } diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 249e583..a4b76b9 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 559f465..accaf1a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ -#Sat Aug 10 11:34:15 CEST 2024 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists +distributionBase = GRADLE_USER_HOME +distributionPath = wrapper/dists +distributionUrl = https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip +networkTimeout = 10000 +validateDistributionUrl = true +zipStoreBase = GRADLE_USER_HOME +zipStorePath = wrapper/dists diff --git a/gradlew b/gradlew index 1b6c787..f3b75f3 100644 --- a/gradlew +++ b/gradlew @@ -15,6 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# SPDX-License-Identifier: Apache-2.0 +# ############################################################################## # @@ -55,7 +57,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -80,13 +82,11 @@ do esac done -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -APP_NAME="Gradle" +# This is normally unused +# shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -133,22 +133,29 @@ location of your Java installation." fi else JAVACMD=java - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac case $MAX_FD in #( '' | soft) :;; #( *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -193,11 +200,15 @@ if "$cygwin" || "$msys" ; then done fi -# Collect all arguments for the java command; -# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of -# shell script including quotes and variable substitutions, so put them in -# double quotes to make sure that they get re-expanded; and -# * put everything else in single quotes, so that it's not re-expanded. + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ @@ -205,6 +216,12 @@ set -- \ org.gradle.wrapper.GradleWrapperMain \ "$@" +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + # Use "xargs" to parse quoted args. # # With -n1 it outputs one arg per line, with the quotes and backslashes removed. diff --git a/gradlew.bat b/gradlew.bat index 107acd3..9d21a21 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -13,8 +13,10 @@ @rem See the License for the specific language governing permissions and @rem limitations under the License. @rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem -@if "%DEBUG%" == "" @echo off +@if "%DEBUG%"=="" @echo off @rem ########################################################################## @rem @rem Gradle startup script for Windows @@ -25,7 +27,8 @@ if "%OS%"=="Windows_NT" setlocal set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @@ -40,13 +43,13 @@ if defined JAVA_HOME goto findJavaFromJavaHome set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute +if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -56,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -75,13 +78,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar :end @rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd +if %ERRORLEVEL% equ 0 goto mainEnd :fail rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% :mainEnd if "%OS%"=="Windows_NT" endlocal diff --git a/src/main/java/com/almostreliable/almostgradle/AlmostGradleExtension.java b/src/main/java/com/almostreliable/almostgradle/AlmostGradleExtension.java index 6bf1c9a..fb6f102 100644 --- a/src/main/java/com/almostreliable/almostgradle/AlmostGradleExtension.java +++ b/src/main/java/com/almostreliable/almostgradle/AlmostGradleExtension.java @@ -23,6 +23,7 @@ public abstract class AlmostGradleExtension { public static final String NAME = "almostgradle"; public static final String MAVEN = "mavenJava"; + public static final String TESTMOD_ID = "testmod"; private final Project project; private final RecipeViewers recipeViewers; @@ -100,6 +101,9 @@ public String getMinecraftVersion() { } public void setup(Action onSetup) { + Utils.ensureMinimalGradleVersion(project, "8.12.1"); + Utils.ensureMinimalPluginVersion(project, "net.neoforged.moddev", "2.0.64-beta"); + onSetup.execute(this); log("đź“•Setting up project through AlmostGradle plugin..."); @@ -186,7 +190,7 @@ private void applyApiSourceSet() { private void applyBasicMod() { var neoForge = project.getExtensions().getByType(NeoForgeExtension.class); var javaPlugin = project.getExtensions().getByType(JavaPluginExtension.class); - neoForge.getVersion().set(getNeoforgeVersion()); + neoForge.setVersion(getNeoforgeVersion()); var mainMod = neoForge.getMods().maybeCreate(getModId()); var mainSourceSet = javaPlugin.getSourceSets().getByName("main"); @@ -226,6 +230,9 @@ private void applyDataGen(NeoForgeExtension neoForge, ModModel mainMod, SourceSe }); neoForge.getRuns().create("datagen", (run) -> { run.data(); + run + .getGameDirectory() + .set(project.getLayout().getProjectDirectory().dir("build").dir("tmp").dir("datagenRuns")); run.getLoadedMods().set(Set.of(mainMod)); run .getProgramArguments() @@ -315,7 +322,7 @@ private void applyTestMod() { var testSourceSet = javaPlugin.getSourceSets().getByName("test"); var modId = this.getModId(); neoForge.mods((mods) -> { - mods.create("testmod", (mod) -> { + mods.create(TESTMOD_ID, (mod) -> { mod.sourceSet(testSourceSet); }); }); @@ -325,16 +332,19 @@ private void applyTestMod() { var exampleScripts = this.project.getRootDir().toPath().resolve("example_scripts").toString(); runs.create("gametest", (run) -> { run.server(); + run + .getGameDirectory() + .set(project.getLayout().getProjectDirectory().dir("build").dir("tmp").dir("gametestRuns")); run.getSourceSet().set(testSourceSet); run.systemProperty("neoforge.gameTestServer", "true"); - run.systemProperty("neoforge.enabledGameTestNamespaces", modId); + run.systemProperty("neoforge.enabledGameTestNamespaces", TESTMOD_ID); run.systemProperty(modId + ".example_scripts", exampleScripts); }); - runs.create("testmod", (run) -> { + runs.create(TESTMOD_ID, (run) -> { run.client(); run.getSourceSet().set(testSourceSet); run.systemProperty("neoforge.gameTestServer", "true"); - run.systemProperty("neoforge.enabledGameTestNamespaces", modId); + run.systemProperty("neoforge.enabledGameTestNamespaces", TESTMOD_ID); run.systemProperty(modId + ".example_scripts", exampleScripts); }); }); diff --git a/src/main/java/com/almostreliable/almostgradle/Utils.java b/src/main/java/com/almostreliable/almostgradle/Utils.java index 010b24b..a5187fc 100644 --- a/src/main/java/com/almostreliable/almostgradle/Utils.java +++ b/src/main/java/com/almostreliable/almostgradle/Utils.java @@ -1,16 +1,22 @@ package com.almostreliable.almostgradle; +import org.gradle.api.GradleException; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.Dependency; +import org.gradle.api.artifacts.DependencySet; import org.gradle.api.artifacts.repositories.ArtifactRepository; import org.gradle.api.artifacts.repositories.MavenArtifactRepository; import javax.annotation.Nullable; import java.net.URI; import java.net.URISyntaxException; +import java.util.regex.Pattern; public class Utils { + private static final Pattern VERSION_SUFFIX_PATTERN = Pattern.compile("-.*$"); + public static boolean isMissingMavenRepository(Project project, String repository) { try { URI uri = new URI(repository); @@ -47,6 +53,59 @@ public static void createLocalImplementation(Project project, String compileClas }); } + public static void ensureMinimalGradleVersion(Project project, String minimumVersion) { + String gradleVersion = project.getGradle().getGradleVersion(); + if (!isVersionAtLeast(gradleVersion, minimumVersion)) { + throw new GradleException("Gradle version " + gradleVersion + + " is less than the minimum required version " + minimumVersion + "!"); + } + } + + public static void ensureMinimalPluginVersion(Project project, String pluginId, String minimumVersion) { + DependencySet dependencies = project + .getBuildscript() + .getConfigurations() + .getByName("classpath") + .getDependencies(); + + for (Dependency dependency : dependencies) { + if (!pluginId.equals(dependency.getGroup())) { + continue; + } + + String dependencyVersion = dependency.getVersion(); + if (dependencyVersion != null && !isVersionAtLeast(dependencyVersion, minimumVersion)) { + throw new GradleException("Plugin '" + pluginId + "' version " + dependencyVersion + + " is less than the minimum required version " + minimumVersion + "!"); + } + + return; + } + + throw new GradleException("Required plugin '" + pluginId + "' with minimum version " + minimumVersion + + " not found!"); + } + + private static boolean isVersionAtLeast(String version, String minimumVersion) { + String[] versionParts = VERSION_SUFFIX_PATTERN.matcher(version).replaceAll("").split("\\."); + String[] minimumParts = VERSION_SUFFIX_PATTERN.matcher(minimumVersion).replaceAll("").split("\\."); + + int length = Math.max(versionParts.length, minimumParts.length); + for (int i = 0; i < length; i++) { + int vPart = i < versionParts.length ? Integer.parseInt(versionParts[i]) : 0; + int mPart = i < minimumParts.length ? Integer.parseInt(minimumParts[i]) : 0; + + if (vPart < mPart) { + return false; + } + if (vPart > mPart) { + return true; + } + } + + return true; + } + public static void log(Project project, String key, Object value) { project.getLogger().lifecycle(String.format("%-25s -> %s", key, value)); }