diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 290de54..b8745e5 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -15,7 +15,7 @@ jobs: uses: actions/setup-java@v4 with: distribution: 'temurin' - java-version: '21' + java-version: '25' - name: 'Build' run: | ./gradlew \ diff --git a/build.gradle b/build.gradle index 8c45cc2..15e2465 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'org.moddingx' -java.toolchain.languageVersion = JavaLanguageVersion.of(21) +java.toolchain.languageVersion = JavaLanguageVersion.of(25) repositories { mavenCentral() @@ -15,14 +15,14 @@ repositories { dependencies { api('jakarta.annotation:jakarta.annotation-api') { version { strictly '[3.0.0,)'; prefer '3.0.0' } } - api('net.neoforged:srgutils') { version { strictly '[1.0.9,)'; prefer '1.0.9' } } - api('net.neoforged.gradle:userdev') { version { strictly "[7.0.180,8.0.0)"; prefer '7.0.180' } } - api('org.moddingx:LauncherLib') { version { strictly '[2.1.0,2.2.0)'; prefer '2.1.0' } } - api('com.google.code.gson:gson') { version { strictly '[2.12.1,)'; prefer '2.12.1' } } - api('commons-io:commons-io') { version { strictly '[2.18.0,)'; prefer '2.18.0' } } - api('org.apache.maven:maven-repository-metadata') { version { strictly '[3.9.9,)'; prefer '3.9.9' } } - implementation('net.darkhax.curseforgegradle:CurseForgeGradle') { version { strictly '[1.1.26,)'; prefer '1.1.26' } } - implementation('com.modrinth.minotaur:Minotaur') { version { strictly '[2.8.7,)'; prefer '2.8.7' } } + api('net.neoforged:srgutils') { version { strictly '[1.0.11,)'; prefer '1.0.11' } } + api('net.neoforged.gradle:userdev') { version { strictly "[7.1.20,8.0.0)"; prefer '7.1.20' } } + api('org.moddingx:LauncherLib') { version { strictly '[2.2.0,2.3.0)'; prefer '2.1.0' } } + api('com.google.code.gson:gson') { version { strictly '[2.13.2,)'; prefer '2.13.2' } } + api('commons-io:commons-io') { version { strictly '[2.21.0,)'; prefer '2.21.0' } } + api('org.apache.maven:maven-repository-metadata') { version { strictly '[3.9.12,)'; prefer '3.9.12' } } + implementation('net.darkhax.curseforgegradle:CurseForgeGradle') { version { strictly '[1.1.28,)'; prefer '1.1.28' } } + implementation('com.modrinth.minotaur:Minotaur') { version { strictly '[2.8.10,)'; prefer '2.8.10' } } } gradlePlugin { diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 41d9927..f8e1ee3 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 9a0a0e2..4eac4a8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-all.zip +networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 1b6c787..adff685 100755 --- a/gradlew +++ b/gradlew @@ -1,7 +1,7 @@ #!/bin/sh # -# Copyright © 2015-2021 the original authors. +# Copyright © 2015 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -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 @@ -114,7 +114,6 @@ case "$( uname )" in #( NONSTOP* ) nonstop=true ;; esac -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar # Determine the Java command to use to start the JVM. @@ -133,22 +132,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 @@ -165,7 +171,6 @@ fi # For Cygwin or MSYS, switch paths to Windows format before running java if "$cygwin" || "$msys" ; then APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) - CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) JAVACMD=$( cygpath --unix "$JAVACMD" ) @@ -193,18 +198,27 @@ 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, 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" \ - -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ "$@" +# 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..c4bdd3a 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,32 +59,33 @@ 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 :execute @rem Setup the command line -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%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/org/moddingx/modgradle/plugins/meta/MetaPlugin.java b/src/main/java/org/moddingx/modgradle/plugins/meta/MetaPlugin.java index 08566a8..43d9a14 100644 --- a/src/main/java/org/moddingx/modgradle/plugins/meta/MetaPlugin.java +++ b/src/main/java/org/moddingx/modgradle/plugins/meta/MetaPlugin.java @@ -3,15 +3,21 @@ import jakarta.annotation.Nonnull; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.process.ExecOperations; import org.moddingx.modgradle.ModGradle; -public class MetaPlugin implements Plugin { +import javax.inject.Inject; + +public abstract class MetaPlugin implements Plugin { + + @Inject + public abstract ExecOperations getExecOperations(); @Override public void apply(@Nonnull Project project) { ModGradle.init(project); this.setupRepositories(project); - project.getExtensions().create("mod", ModExtension.class, project); + project.getExtensions().create("mod", ModExtension.class, project, this.getExecOperations()); } private void setupRepositories(Project project) { diff --git a/src/main/java/org/moddingx/modgradle/plugins/meta/ModExtension.java b/src/main/java/org/moddingx/modgradle/plugins/meta/ModExtension.java index c57a455..8437c64 100644 --- a/src/main/java/org/moddingx/modgradle/plugins/meta/ModExtension.java +++ b/src/main/java/org/moddingx/modgradle/plugins/meta/ModExtension.java @@ -4,6 +4,7 @@ import jakarta.annotation.Nullable; import org.gradle.api.Project; import org.gradle.api.tasks.TaskProvider; +import org.gradle.process.ExecOperations; import org.moddingx.launcherlib.util.LazyValue; import org.moddingx.modgradle.plugins.meta.setup.ModBuildSetup; import org.moddingx.modgradle.plugins.meta.delegate.ModConfig; @@ -18,13 +19,15 @@ public class ModExtension extends GroovyObjectSupport { private final Project project; + private final ExecOperations execOps; private final ModPropertyAccess propertyAccess; @Nullable private Map properties; - public ModExtension(Project project) { + public ModExtension(Project project, ExecOperations execOps) { this.project = project; + this.execOps = execOps; this.properties = null; this.propertyAccess = new ModPropertyAccess(this); } @@ -34,7 +37,7 @@ public void configure(@DelegatesTo(value = ModConfig.Delegate.class, strategy = ModConfig config = ModConfig.configure(closure, new ModConfig(), ModConfig::delegate); this.properties = new HashMap<>(); try { - ModBuildSetup.configureBuild(new ProjectContext(this.project, this.propertyAccess, this.properties::put, this::dependsOnProperties), config); + ModBuildSetup.configureBuild(new ProjectContext(this.project, this.execOps, this.propertyAccess, this.properties::put, this::dependsOnProperties), config); } catch (RuntimeException e) { throw new RuntimeException("Mod configuration failed.", e); } @@ -63,7 +66,7 @@ public boolean isCase(String propertyName) { // propertyName in this } private void dependsOnProperties(TaskProvider taskProvider) { - this.project.afterEvaluate(p -> { + this.project.afterEvaluate(_ -> { if (this.properties != null) { taskProvider.configure(task -> { if (this.project.getGroup() instanceof String group) { diff --git a/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModContext.java b/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModContext.java index 00ed8c9..f892e11 100644 --- a/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModContext.java +++ b/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModContext.java @@ -4,6 +4,7 @@ import jakarta.annotation.Nullable; import org.gradle.api.Project; import org.gradle.api.tasks.SourceSet; +import org.gradle.process.ExecOperations; import org.moddingx.launcherlib.util.LazyValue; import org.moddingx.modgradle.ModGradle; import org.moddingx.modgradle.plugins.meta.delegate.ModConfig; @@ -38,15 +39,15 @@ public ModContext(ProjectContext context, ModConfig config) throws IOException { this.modid = Objects.requireNonNull(config.modid, "modid not set."); this.group = config.group != null ? config.group : this.project().getGroup().toString(); - this.version = resolveVersion(this.project(), this.group, config.version); + this.version = resolveVersion(this.project(), this.execOperations(), this.group, config.version); this.neoforge = Objects.requireNonNull(config.neoforge, "neoforge version not set."); this.license = Objects.requireNonNull(config.license, "license not set."); this.minecraft = config.minecraft != null ? config.minecraft : ModGradle.versions().neoforgeMinecraft(this.project(), this.neoforge); this.java = config.java != 0 ? config.java : ModGradle.versions().java(this.minecraft); this.resource = ModGradle.versions().resource(this.minecraft); this.data = ModGradle.versions().data(this.minecraft); - this.timestamp = GitTimestampUtils.tryGetCommitTimestamp(this.project()); - this.changelog = getChangelogProvider(this.project(), config.git.commitFormat, config.changelog); + this.timestamp = GitTimestampUtils.tryGetCommitTimestamp(this.project(), this.execOperations()); + this.changelog = getChangelogProvider(this.project(), this.execOperations(), config.git.commitFormat, config.changelog); this.additionalModSources = config.additionalModSources.stream().distinct().toList(); this.project().setGroup(this.group); @@ -62,17 +63,17 @@ public ModContext(ProjectContext context, ModConfig config) throws IOException { this.modProperty("changelog", this.changelog); } - private static String resolveVersion(Project project, String group, ModVersionConfig delegate) throws IOException { + private static String resolveVersion(Project project, ExecOperations execOps, String group, ModVersionConfig delegate) throws IOException { return switch (delegate.strategy) { case ModVersionConfig.Strategy.Constant.INSTANCE -> delegate.base == null ? project.getVersion().toString() : delegate.base; - case ModVersionConfig.Strategy.Maven maven when delegate.base == null -> throw new IllegalStateException("Needs a base version to get version from a maven repository."); + case ModVersionConfig.Strategy.Maven _ when delegate.base == null -> throw new IllegalStateException("Needs a base version to get version from a maven repository."); case ModVersionConfig.Strategy.Maven maven -> MavenVersionResolver.getVersion(project, maven.repository(), group, project.getName(), delegate.base); - case ModVersionConfig.Strategy.GitTag.INSTANCE -> GitTagVersionResolver.getVersion(project, delegate.base); + case ModVersionConfig.Strategy.GitTag.INSTANCE -> GitTagVersionResolver.getVersion(project, execOps, delegate.base); }; } - private static LazyValue getChangelogProvider(Project project, @Nullable String commitFormat, @Nullable Closure customChangelog) throws IOException { - LazyValue defaultChangelog = new LazyValue<>(() -> GitChangelogGenerator.generateChangelog(project, commitFormat)); + private static LazyValue getChangelogProvider(Project project, ExecOperations execOps, @Nullable String commitFormat, @Nullable Closure customChangelog) { + LazyValue defaultChangelog = new LazyValue<>(() -> GitChangelogGenerator.generateChangelog(project, execOps, commitFormat)); if (customChangelog == null) return defaultChangelog; return new LazyValue<>(() -> { ChangelogGenerationProperties properties = new ChangelogGenerationProperties(commitFormat, defaultChangelog); diff --git a/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModMappingsSetup.java b/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModMappingsSetup.java index 81c40b1..f00832d 100644 --- a/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModMappingsSetup.java +++ b/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModMappingsSetup.java @@ -26,7 +26,6 @@ public static void configureBuild(ModContext mod, ModMappingsConfig config) { Subsystems subsystems = mod.project().getExtensions().getByType(Subsystems.class); subsystems.parchment(parchment -> { if (parchmentConfig.artifact() != null) { - parchment.enabled(true); parchment.addRepository(true); parchment.parchmentArtifact(parchmentConfig.artifact().getDescriptor()); } else { @@ -34,7 +33,6 @@ public static void configureBuild(ModContext mod, ModMappingsConfig config) { String parchmentVersion = parchmentConfig.version(); if (parchmentVersion == null) parchmentVersion = latestParchmentVersion(mod.project(), minecraftVersion, parchmentConfig.minecraft() == null); if (parchmentVersion != null) { - parchment.enabled(true); parchment.addRepository(true); parchment.minecraftVersion(minecraftVersion); parchment.mappingsVersion(parchmentVersion); diff --git a/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModRunsSetup.java b/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModRunsSetup.java index 2a0a9af..4632938 100644 --- a/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModRunsSetup.java +++ b/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ModRunsSetup.java @@ -4,6 +4,7 @@ import net.neoforged.gradle.dsl.common.runs.run.Run; import net.neoforged.gradle.dsl.common.runs.run.RunManager; import org.gradle.api.tasks.SourceSet; +import org.gradle.util.internal.ConfigureUtil; import org.moddingx.modgradle.plugins.meta.delegate.ModRunsConfig; import org.moddingx.modgradle.util.JavaGradlePluginUtils; @@ -47,7 +48,7 @@ private static void configure(ModContext mod, Run run, boolean autoConfig, List< } } for (Closure action : actions) { - run.configure(action); + ConfigureUtil.configureSelf(action, run); } } } diff --git a/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ProjectContext.java b/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ProjectContext.java index 26141c8..bfbad88 100644 --- a/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ProjectContext.java +++ b/src/main/java/org/moddingx/modgradle/plugins/meta/setup/ProjectContext.java @@ -2,6 +2,7 @@ import org.gradle.api.Project; import org.gradle.api.tasks.TaskProvider; +import org.gradle.process.ExecOperations; import org.moddingx.modgradle.plugins.meta.ModPropertyAccess; import java.util.function.BiConsumer; @@ -10,16 +11,18 @@ public sealed class ProjectContext permits ModContext { private final Project project; + private final ExecOperations execOps; private final ModPropertyAccess properties; private final BiConsumer modPropertySetter; private final Consumer> dependsOnProperties; protected ProjectContext(ProjectContext context) { - this(context.project, context.properties, context.modPropertySetter, context.dependsOnProperties); + this(context.project, context.execOps, context.properties, context.modPropertySetter, context.dependsOnProperties); } - public ProjectContext(Project project, ModPropertyAccess properties, BiConsumer modPropertySetter, Consumer> dependsOnProperties) { + public ProjectContext(Project project, ExecOperations execOps, ModPropertyAccess properties, BiConsumer modPropertySetter, Consumer> dependsOnProperties) { this.project = project; + this.execOps = execOps; this.properties = properties; this.modPropertySetter = modPropertySetter; this.dependsOnProperties = dependsOnProperties; @@ -29,6 +32,10 @@ public Project project() { return this.project; } + public ExecOperations execOperations() { + return this.execOps; + } + public ModPropertyAccess properties() { return this.properties; } diff --git a/src/main/java/org/moddingx/modgradle/util/GitChangelogGenerator.java b/src/main/java/org/moddingx/modgradle/util/GitChangelogGenerator.java index aa483b2..8dc6e11 100644 --- a/src/main/java/org/moddingx/modgradle/util/GitChangelogGenerator.java +++ b/src/main/java/org/moddingx/modgradle/util/GitChangelogGenerator.java @@ -3,6 +3,7 @@ import jakarta.annotation.Nullable; import org.apache.commons.io.output.NullOutputStream; import org.gradle.api.Project; +import org.gradle.process.ExecOperations; import org.gradle.process.ExecResult; import java.io.ByteArrayOutputStream; @@ -11,18 +12,19 @@ public class GitChangelogGenerator { - public static String generateChangelog(Project project, @Nullable String commitFormat) { + public static String generateChangelog(Project project, ExecOperations execOps, @Nullable String commitFormat) { StringBuilder errorDiagnostic = new StringBuilder(); try { String logFormat = getLogFormat(commitFormat); - String commitRange = getCommitRange(project); + String commitRange = getCommitRange(project, execOps); errorDiagnostic.append(" commits=").append(commitRange); ByteArrayOutputStream output = new ByteArrayOutputStream(); - project.exec(spec -> { + execOps.exec(spec -> { spec.commandLine("git", "log", logFormat, commitRange); spec.setStandardOutput(output); spec.setErrorOutput(System.err); spec.setIgnoreExitValue(false); + spec.setWorkingDir(project.getProjectDir()); }); output.close(); return output.toString(StandardCharsets.UTF_8).strip(); @@ -39,7 +41,7 @@ private static String getLogFormat(@Nullable String commitFormat) { } } - private static String getCommitRange(Project project) throws IOException { + private static String getCommitRange(Project project, ExecOperations execOps) throws IOException { // Jenkins String jenkinsCommit = System.getenv("GIT_COMMIT"); String jenkinsPrevCommit = System.getenv("GIT_PREVIOUS_COMMIT"); @@ -58,21 +60,23 @@ private static String getCommitRange(Project project) throws IOException { // Git tag-based versioning: if a tag points to the HEAD commit, take all commits since the last tag. ByteArrayOutputStream currentTagOut = new ByteArrayOutputStream(); - ExecResult currentTagResult = project.exec(spec -> { + ExecResult currentTagResult = execOps.exec(spec -> { spec.commandLine("git", "describe", "--tags", "--exact-match", "HEAD"); spec.setStandardOutput(currentTagOut); spec.setErrorOutput(NullOutputStream.nullOutputStream()); spec.setIgnoreExitValue(true); + spec.setWorkingDir(project.getProjectDir()); }); currentTagOut.close(); if (currentTagResult.getExitValue() == 0) { String currentTag = currentTagOut.toString(StandardCharsets.UTF_8).strip(); ByteArrayOutputStream lastTagOut = new ByteArrayOutputStream(); - ExecResult lastTagResult = project.exec(spec -> { + ExecResult lastTagResult = execOps.exec(spec -> { spec.commandLine("git", "describe", "--tags", "--abbrev=0", "HEAD^"); spec.setStandardOutput(lastTagOut); spec.setErrorOutput(NullOutputStream.nullOutputStream()); spec.setIgnoreExitValue(true); + spec.setWorkingDir(project.getProjectDir()); }); lastTagOut.close(); if (lastTagResult.getExitValue() == 0) { diff --git a/src/main/java/org/moddingx/modgradle/util/GitTagVersionResolver.java b/src/main/java/org/moddingx/modgradle/util/GitTagVersionResolver.java index 000595a..3900b1d 100644 --- a/src/main/java/org/moddingx/modgradle/util/GitTagVersionResolver.java +++ b/src/main/java/org/moddingx/modgradle/util/GitTagVersionResolver.java @@ -2,6 +2,7 @@ import jakarta.annotation.Nullable; import org.gradle.api.Project; +import org.gradle.process.ExecOperations; import org.gradle.process.ExecResult; import java.io.*; @@ -16,10 +17,10 @@ public class GitTagVersionResolver { "GITHUB_REF", "GITHUB_REF_NAME", "GIT_COMMIT_TAG", "CI_COMMIT_REF_NAME" ); - public static String getVersion(Project project, @Nullable String defaultVersion) throws IOException { - Optional describeTag = getVersionFromDescribe(project); + public static String getVersion(Project project, ExecOperations execOps, @Nullable String defaultVersion) throws IOException { + Optional describeTag = getVersionFromDescribe(project, execOps); if (describeTag.isPresent()) return describeTag.get(); - Set tags = listAllTags(project); + Set tags = listAllTags(project, execOps); for (String env : GIT_REF_NAME_ENVIRONMENT) { String value = System.getenv(env); if (value == null) continue; @@ -34,13 +35,14 @@ public static String getVersion(Project project, @Nullable String defaultVersion } } - private static Optional getVersionFromDescribe(Project project) throws IOException { + private static Optional getVersionFromDescribe(Project project, ExecOperations execOps) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); - ExecResult result = project.exec(spec -> { + ExecResult result = execOps.exec(spec -> { spec.commandLine("git", "describe", "--tags", "--exact-match", "HEAD"); spec.setStandardOutput(output); spec.setErrorOutput(System.err); spec.setIgnoreExitValue(true); + spec.setWorkingDir(project.getProjectDir()); }); output.close(); if (result.getExitValue() != 0) return Optional.empty(); // No tag @@ -49,13 +51,14 @@ private static Optional getVersionFromDescribe(Project project) throws I } } - private static Set listAllTags(Project project) throws IOException { + private static Set listAllTags(Project project, ExecOperations execOps) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); - ExecResult result = project.exec(spec -> { + ExecResult result = execOps.exec(spec -> { spec.commandLine("git", "tag", "--list"); spec.setStandardOutput(output); spec.setErrorOutput(System.err); spec.setIgnoreExitValue(true); + spec.setWorkingDir(project.getProjectDir()); }); output.close(); if (result.getExitValue() != 0) { diff --git a/src/main/java/org/moddingx/modgradle/util/GitTimestampUtils.java b/src/main/java/org/moddingx/modgradle/util/GitTimestampUtils.java index 60f7e9d..a576047 100644 --- a/src/main/java/org/moddingx/modgradle/util/GitTimestampUtils.java +++ b/src/main/java/org/moddingx/modgradle/util/GitTimestampUtils.java @@ -2,6 +2,7 @@ import jakarta.annotation.Nullable; import org.gradle.api.Project; +import org.gradle.process.ExecOperations; import org.gradle.process.ExecResult; import java.io.ByteArrayOutputStream; @@ -11,16 +12,17 @@ public class GitTimestampUtils { @Nullable - public static String tryGetCommitTimestamp(Project project) { + public static String tryGetCommitTimestamp(Project project, ExecOperations execOps) { try { String commitEnv = System.getenv("GIT_COMMIT"); String ref = (commitEnv == null || commitEnv.isEmpty()) ? "HEAD" : commitEnv; ByteArrayOutputStream output = new ByteArrayOutputStream(); - ExecResult result = project.exec(spec -> { + ExecResult result = execOps.exec(spec -> { spec.commandLine("git", "show", "-s", "--format=%cd", "--date=iso-strict", ref); spec.setStandardOutput(output); spec.setErrorOutput(System.err); spec.setIgnoreExitValue(true); + spec.setWorkingDir(project.getProjectDir()); }); output.close(); if (result.getExitValue() == 0) {