From 4c7736b62761d293432742abb48a6e8d464fd141 Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Fri, 10 Jan 2025 12:01:24 +0100 Subject: [PATCH] Replace all uses of Project with appropriate injected services --- README.md | 4 ++ .../hhu/stups/sablecc/gradle/SableCCTask.java | 47 ++++++++++++++----- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index abc1f2e..0b8e60a 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,10 @@ tasks.named("generateSableCCSource").configure { ## Changelog +## Version 1.2.0 (not released yet) + +* Fix deprecation warnings on Gradle 8.12 about accessing `Project` at execution time. + ## Version 1.1.0 * Fix `sourcesJar` task not always depending on `generateSableCCSource` as intended. This caused deprecation warnings from Gradle about undeclared dependencies. diff --git a/src/main/java/de/hhu/stups/sablecc/gradle/SableCCTask.java b/src/main/java/de/hhu/stups/sablecc/gradle/SableCCTask.java index 8dfdef2..8979639 100644 --- a/src/main/java/de/hhu/stups/sablecc/gradle/SableCCTask.java +++ b/src/main/java/de/hhu/stups/sablecc/gradle/SableCCTask.java @@ -1,14 +1,21 @@ package de.hhu.stups.sablecc.gradle; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; +import javax.inject.Inject; + import org.gradle.api.NonNullApi; +import org.gradle.api.file.ConfigurableFileTree; import org.gradle.api.file.Directory; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.FileCollection; +import org.gradle.api.file.FileSystemOperations; import org.gradle.api.file.FileTree; +import org.gradle.api.model.ObjectFactory; import org.gradle.api.tasks.Classpath; import org.gradle.api.tasks.IgnoreEmptyDirectories; import org.gradle.api.tasks.Input; @@ -18,24 +25,37 @@ import org.gradle.api.tasks.PathSensitive; import org.gradle.api.tasks.PathSensitivity; import org.gradle.api.tasks.SourceTask; import org.gradle.api.tasks.TaskAction; +import org.gradle.process.ExecOperations; import org.gradle.work.NormalizeLineEndings; @NonNullApi -public class SableCCTask extends SourceTask { +public abstract class SableCCTask extends SourceTask { + private final ObjectFactory objectFactory; + private FileCollection sableCCClasspath; private String maxHeapSize; private final DirectoryProperty destinationJavaDir; private final DirectoryProperty destinationResourcesDir; - public SableCCTask() { + @Inject + @SuppressWarnings("ConstructorNotProtectedInAbstractClass") // Must be public so that Gradle can find the constructor for dependency injection + public SableCCTask(ObjectFactory objectFactory) { super(); + this.objectFactory = objectFactory; + this.sableCCClasspath = null; this.maxHeapSize = null; - this.destinationJavaDir = this.getProject().getObjects().directoryProperty(); - this.destinationResourcesDir = this.getProject().getObjects().directoryProperty(); + this.destinationJavaDir = objectFactory.directoryProperty(); + this.destinationResourcesDir = objectFactory.directoryProperty(); } + @Inject + protected abstract ExecOperations getExecOperations(); + + @Inject + protected abstract FileSystemOperations getFs(); + @IgnoreEmptyDirectories @NormalizeLineEndings @PathSensitive(PathSensitivity.NONE) @@ -82,14 +102,14 @@ public class SableCCTask extends SourceTask { } @TaskAction - void execute() { + void execute() throws IOException { // Delete any previously generated source files, so that no longer existing token and node classes aren't kept around. - this.getProject().delete(this.getDestinationJavaDir(), this.getDestinationResourcesDir()); - this.getProject().mkdir(this.getDestinationJavaDir()); - this.getProject().mkdir(this.getDestinationResourcesDir()); + this.getFs().delete(spec -> spec.delete(this.getDestinationJavaDir(), this.getDestinationResourcesDir())); + Files.createDirectories(this.getDestinationJavaDir().getAsFile().toPath()); + Files.createDirectories(this.getDestinationResourcesDir().getAsFile().toPath()); // Call SableCC to generate the source files. - this.getProject().javaexec(spec -> { + this.getExecOperations().javaexec(spec -> { spec.setClasspath(this.getSableCCClasspath()); spec.setMaxHeapSize(this.getMaxHeapSize()); spec.getMainClass().set("org.sablecc.sablecc.SableCC"); @@ -103,11 +123,16 @@ public class SableCCTask extends SourceTask { }); // Move generated dat files from Java source directory to resources directory. - this.getProject().copy(spec -> { + this.getFs().copy(spec -> { spec.from(this.getDestinationJavaDir()); spec.into(this.getDestinationResourcesDir()); spec.include("**/*.dat"); }); - this.getProject().delete(this.getProject().fileTree(this.getDestinationJavaDir(), files -> files.include("**/*.dat"))); + this.getFs().delete(spec -> { + ConfigurableFileTree fileTree = this.objectFactory.fileTree(); + fileTree.from(this.getDestinationJavaDir()); + fileTree.include("**/*.dat"); + spec.delete(fileTree); + }); } } -- GitLab