From 47f99d65a09660863cb5d6b25b239ead3389f3ac Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 2 Feb 2022 18:50:13 +0100 Subject: feat: simplifies the whole gradle plugin - the `compileFregeTask` has a new optional input called `mainModuleName`. If it is set, it and only its dependencies will be compiled, otherwise all `.fr`files in the `mainSourceDir`will be compiled. - the internal `DependencyFregeTask` is deleted because it was only used by the vscode plugin and I decided to remove this gradle plugin as a dependency. - an example project folder was added to test the plugin manually. --- .../fhnw/thga/gradleplugins/CompileFregeTask.java | 50 ++++++++-------------- .../ch/fhnw/thga/gradleplugins/FregePlugin.java | 23 ++-------- .../ch/fhnw/thga/gradleplugins/ReplFregeTask.java | 9 ++++ .../internal/DependencyFregeTask.java | 41 ------------------ 4 files changed, 30 insertions(+), 93 deletions(-) delete mode 100644 src/main/java/ch/fhnw/thga/gradleplugins/internal/DependencyFregeTask.java (limited to 'src/main/java') diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java index bc3c8f7..c280607 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java @@ -3,16 +3,13 @@ package ch.fhnw.thga.gradleplugins; import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.function.BiFunction; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.inject.Inject; import org.gradle.api.DefaultTask; -import org.gradle.api.file.Directory; import org.gradle.api.file.DirectoryProperty; -import org.gradle.api.file.FileTree; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.model.ObjectFactory; import org.gradle.api.provider.ListProperty; @@ -31,25 +28,8 @@ import org.gradle.api.tasks.TaskAction; @CacheableTask public abstract class CompileFregeTask extends DefaultTask { + private static final String FREGE_FILES_GLOB_PATTERN = "**/*.fr"; private final JavaExec javaExec; - private static final String FREGE_FILES_GLOB_PATTERN = "**/*.fr"; - private static final BiFunction - excludeReplSourceFile = - (String replSource, - Directory srcDir) -> - { - if (replSource.isEmpty()) return srcDir.getAsFileTree(); - return srcDir.getAsFileTree().matching( - pattern -> - { - pattern.exclude( - String.format( - "**/%s", - replSource) - ); - } - ); - }; @InputFile @PathSensitive(PathSensitivity.RELATIVE) @@ -66,7 +46,7 @@ public abstract class CompileFregeTask extends DefaultTask { public abstract Property getFregeDependencies(); @Input - public abstract Property getReplSource(); + public abstract Property getFregeMainModuleName(); @OutputDirectory public abstract DirectoryProperty getFregeOutputDir(); @@ -82,15 +62,19 @@ public abstract class CompileFregeTask extends DefaultTask { } @Internal - public final Provider getSourceFileTree() { - return getReplSource().zip( - getFregeMainSourceDir(), - excludeReplSourceFile); + public final Provider> getCompileItems() { + return getFregeMainModuleName() + .map(name -> + { + return name.isEmpty() ? getFregeSourceFiles().get() + : List.of(name); + }); } @Internal - public final Provider> getSourceFiles() { - return getSourceFileTree() + public final Provider> getFregeSourceFiles() { + return getFregeMainSourceDir() + .map(srcDir -> srcDir.getAsFileTree()) .map(tree -> tree.matching(pattern -> pattern.include(FREGE_FILES_GLOB_PATTERN))) .map(tree -> tree.getFiles().stream() .map(file -> file.getAbsolutePath()) @@ -111,14 +95,14 @@ public abstract class CompileFregeTask extends DefaultTask { javaExec = objectFactory.newInstance(JavaExec.class); } - private List buildCompilerArgsFromProperties(List directoryArg) + private List buildCompilerArgsFromProperties(List targetDirectoryArg) { return Stream.of( getDependencyArg().get(), getFregeCompilerFlags().get(), - directoryArg, + targetDirectoryArg, getSourcePathArg().get(), - getSourceFiles().get()) + getCompileItems().get()) .filter(lists -> !lists.isEmpty()) .flatMap(Collection::stream) .collect(Collectors.toList()); @@ -126,11 +110,11 @@ public abstract class CompileFregeTask extends DefaultTask { @TaskAction public void compileFrege() { - List directoryArg = List.of( + List targetDirectoryArg = List.of( "-d", getFregeOutputDir().getAsFile().get().getAbsolutePath()); javaExec.setClasspath(getProject().files(getFregeCompilerJar())) - .setArgs(buildCompilerArgsFromProperties(directoryArg)).exec(); + .setArgs(buildCompilerArgsFromProperties(targetDirectoryArg)).exec(); } } \ No newline at end of file diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java index a318b81..ce2ce01 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java @@ -5,8 +5,6 @@ import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; import org.gradle.api.tasks.TaskProvider; -import ch.fhnw.thga.gradleplugins.internal.DependencyFregeTask; - public class FregePlugin implements Plugin { public static final String SETUP_FREGE_TASK_NAME = "setupFrege"; @@ -54,7 +52,7 @@ public class FregePlugin implements Plugin task.getFregeOutputDir().set(extension.getOutputDir()); task.getFregeCompilerFlags().set(extension.getCompilerFlags()); task.getFregeDependencies().set(implementation.getAsPath()); - task.getReplSource().set(""); + task.getFregeMainModuleName().set(""); } ); @@ -63,28 +61,15 @@ public class FregePlugin implements Plugin RunFregeTask.class, task -> { - task.dependsOn(compileFregeTask); - task.getFregeCompilerJar().set( - setupFregeCompilerTask.get().getFregeCompilerOutputPath()); - task.getFregeOutputDir().set(extension.getOutputDir()); + task.getMainModule().set(extension.getMainModule()); - task.getFregeDependencies().set(implementation.getAsPath()); - } - ); - - project.getTasks().register( - DEPS_FREGE_TASK_NAME, - DependencyFregeTask.class, - task -> - { task.dependsOn(compileFregeTask.map( compileTask -> { - compileTask.getReplSource().set(task.getReplSource()); + compileTask.getFregeMainModuleName().set(task.getMainModule()); return compileTask; }) .get()); - task.dependsOn(compileFregeTask); task.getFregeCompilerJar().set( setupFregeCompilerTask.get().getFregeCompilerOutputPath()); task.getFregeOutputDir().set(extension.getOutputDir()); @@ -101,7 +86,7 @@ public class FregePlugin implements Plugin task.dependsOn(compileFregeTask.map( compileTask -> { - compileTask.getReplSource().set(task.getReplSource()); + compileTask.getFregeMainModuleName().set(task.getReplSource()); return compileTask; }) .get()); diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java index dd18270..7903d7e 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java @@ -4,9 +4,11 @@ import org.gradle.api.DefaultTask; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.provider.Property; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.InputDirectory; import org.gradle.api.tasks.InputFile; +import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.options.Option; @@ -27,6 +29,13 @@ public abstract class ReplFregeTask extends DefaultTask { description = "The filename which you want to load into the repl, e.g. 'myFregeFile.fr'") public abstract Property getReplSource(); + @Internal + public final Provider getReplClassName() + { + return getReplSource() + .map(replSource -> replSource.substring(replSource.lastIndexOf("."))); + } + @TaskAction public void printStartFregeReplCommand() { System.out.println("Execute the following command to start the Frege Repl:"); diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/internal/DependencyFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/internal/DependencyFregeTask.java deleted file mode 100644 index 87f4d2b..0000000 --- a/src/main/java/ch/fhnw/thga/gradleplugins/internal/DependencyFregeTask.java +++ /dev/null @@ -1,41 +0,0 @@ -package ch.fhnw.thga.gradleplugins.internal; - -import org.gradle.api.DefaultTask; -import org.gradle.api.file.DirectoryProperty; -import org.gradle.api.file.RegularFileProperty; -import org.gradle.api.provider.Property; -import org.gradle.api.tasks.Input; -import org.gradle.api.tasks.InputDirectory; -import org.gradle.api.tasks.InputFile; -import org.gradle.api.tasks.TaskAction; -import org.gradle.api.tasks.options.Option; - -import ch.fhnw.thga.gradleplugins.SharedTaskLogic; - -public abstract class DependencyFregeTask extends DefaultTask { - @InputFile - public abstract RegularFileProperty getFregeCompilerJar(); - - @Input - public abstract Property getFregeDependencies(); - - @InputDirectory - public abstract DirectoryProperty getFregeOutputDir(); - - @Input - @Option(option = "replSource", - description = "The filename which you want to load into the repl, e.g. 'myFregeFile.fr'") - public abstract Property getReplSource(); - - - @TaskAction - public void fregeDependencies() { - System.out.println( - SharedTaskLogic.setupClasspath( - getProject(), - getFregeDependencies(), - getFregeCompilerJar(), - getFregeOutputDir()) - .get().getAsPath()); - } -} -- cgit From 43ee2210fa07f42a13ac6879ae167db6d2729742 Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 9 Feb 2022 10:43:29 +0100 Subject: refactor: introduces staged builder patterns for frege project setup (WIP) --- .../ch/fhnw/thga/gradleplugins/SetupFregeTask.java | 54 ++++++++++++++++------ 1 file changed, 41 insertions(+), 13 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/SetupFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/SetupFregeTask.java index 6bfe72d..c4e17d3 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/SetupFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/SetupFregeTask.java @@ -6,6 +6,7 @@ import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; +import java.nio.file.Paths; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; @@ -36,32 +37,59 @@ public abstract class SetupFregeTask extends DefaultTask { public abstract DirectoryProperty getDownloadDir(); @Internal - public Provider getFregeVersionJarName() { + public Provider getFregeVersionJarName() + { return getVersion().map(version -> "frege" + version + ".jar"); } @Internal - final public Provider getDownloadUrl() { + final public Provider getDownloadUrl() + { return getFregeVersionJarName() - .map(name -> String.join("/", FREGE_GITHUB_URL_PREFIX, getRelease().get(), name)); + .map(name -> String.join("/", FREGE_GITHUB_URL_PREFIX, getRelease().get(), name)); } @OutputFile - public Provider getFregeCompilerOutputPath() { + public Provider getFregeCompilerOutputPath() + { return getDownloadDir().file(getFregeVersionJarName()); } @TaskAction public void downloadFregeCompiler() { - String fregeCompilerOutputPath = getFregeCompilerOutputPath().get().getAsFile().getAbsolutePath(); - try (ReadableByteChannel readChannel = Channels.newChannel(new URL(getDownloadUrl().get()).openStream()); - FileOutputStream fregeCompilerOutputStream = new FileOutputStream(fregeCompilerOutputPath);) { - FileChannel writeChannel = fregeCompilerOutputStream.getChannel(); - writeChannel.transferFrom(readChannel, 0, Long.MAX_VALUE); - LOGGER.lifecycle(String.format("Successfully downloaded %s to: %s", getFregeVersionJarName().get(), - fregeCompilerOutputPath)); - } catch (IOException e) { + String fregeCompilerOutputPath = getFregeCompilerOutputPath() + .get() + .getAsFile() + .getAbsolutePath(); + if (Paths + .get(fregeCompilerOutputPath) + .toFile() + .exists() + ) + return; + + try (ReadableByteChannel readChannel = Channels + .newChannel(new URL(getDownloadUrl().get()) + .openStream() + ); + FileOutputStream fregeCompilerOutputStream = new FileOutputStream( + fregeCompilerOutputPath)) + { + FileChannel writeChannel = fregeCompilerOutputStream.getChannel(); + writeChannel.transferFrom( + readChannel, + 0, + Long.MAX_VALUE); + LOGGER.lifecycle( + String.format( + "Successfully downloaded %s to: %s", + getFregeVersionJarName().get(), + fregeCompilerOutputPath + ) + ); + } catch (IOException e) + { throw new GradleException(e.getMessage()); } } -} +} \ No newline at end of file -- cgit From 0aa4dd065ee78f9acc88b39625cc4a9b47f6c944 Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 9 Feb 2022 21:37:28 +0100 Subject: refactor: continues with staged builder refactoring --- src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java index 5bcbf3d..cfcd393 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java @@ -10,10 +10,10 @@ import org.gradle.api.provider.ListProperty; import org.gradle.api.provider.Property; public abstract class FregeExtension { - public static final String DEFAULT_DOWNLOAD_DIRECTORY = "lib"; - public static final String DEFAULT_RELATIVE_OUTPUT_DIR = "classes/main/frege"; - public static final String DEFAULT_RELATIVE_SOURCE_DIR = "src/main/frege"; - public static final List DEFAULT_COMPILER_FLAGS = List.of("-O", "-make"); + public static final String DEFAULT_RELATIVE_COMPILER_DOWNLOAD_DIR = "lib"; + public static final String DEFAULT_RELATIVE_OUTPUT_DIR = "classes/main/frege"; + public static final String DEFAULT_RELATIVE_SOURCE_DIR = "src/main/frege"; + public static final List DEFAULT_COMPILER_FLAGS = List.of("-O", "-make"); public abstract Property getVersion(); @@ -34,7 +34,7 @@ public abstract class FregeExtension { @Inject public FregeExtension(ProjectLayout projectLayout) { getCompilerDownloadDir() - .convention(projectLayout.getProjectDirectory().dir(DEFAULT_DOWNLOAD_DIRECTORY)); + .convention(projectLayout.getProjectDirectory().dir(DEFAULT_RELATIVE_COMPILER_DOWNLOAD_DIR)); getMainSourceDir() .convention(projectLayout.getProjectDirectory().dir(DEFAULT_RELATIVE_SOURCE_DIR)); -- cgit From 7ef464ab2c2a6a2e0b9ee857d6979ab98f1ec930 Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Fri, 11 Feb 2022 13:58:28 +0100 Subject: refactor: finishes the compileFregeTask refactoring --- src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java | 8 ++++++-- src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java index c280607..a3c42d2 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java @@ -25,6 +25,7 @@ import org.gradle.api.tasks.OutputDirectory; import org.gradle.api.tasks.PathSensitive; import org.gradle.api.tasks.PathSensitivity; import org.gradle.api.tasks.TaskAction; +import org.gradle.api.tasks.options.Option; @CacheableTask public abstract class CompileFregeTask extends DefaultTask { @@ -46,7 +47,10 @@ public abstract class CompileFregeTask extends DefaultTask { public abstract Property getFregeDependencies(); @Input - public abstract Property getFregeMainModuleName(); + @Option(option = "compileItem", + description = "The absolute path to the frege file or the module name" + ) + public abstract Property getFregeCompileItem(); @OutputDirectory public abstract DirectoryProperty getFregeOutputDir(); @@ -63,7 +67,7 @@ public abstract class CompileFregeTask extends DefaultTask { @Internal public final Provider> getCompileItems() { - return getFregeMainModuleName() + return getFregeCompileItem() .map(name -> { return name.isEmpty() ? getFregeSourceFiles().get() diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java index ce2ce01..957dc83 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java @@ -52,7 +52,7 @@ public class FregePlugin implements Plugin task.getFregeOutputDir().set(extension.getOutputDir()); task.getFregeCompilerFlags().set(extension.getCompilerFlags()); task.getFregeDependencies().set(implementation.getAsPath()); - task.getFregeMainModuleName().set(""); + task.getFregeCompileItem().set(""); } ); @@ -66,7 +66,7 @@ public class FregePlugin implements Plugin task.dependsOn(compileFregeTask.map( compileTask -> { - compileTask.getFregeMainModuleName().set(task.getMainModule()); + compileTask.getFregeCompileItem().set(task.getMainModule()); return compileTask; }) .get()); @@ -86,7 +86,7 @@ public class FregePlugin implements Plugin task.dependsOn(compileFregeTask.map( compileTask -> { - compileTask.getFregeMainModuleName().set(task.getReplSource()); + compileTask.getFregeCompileItem().set(task.getReplSource()); return compileTask; }) .get()); -- cgit From a4879784e7be87b5ee184b47eb8faba635019a5d Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 23 Feb 2022 11:04:50 +0100 Subject: refactor: finishes the replFregeTask refactoring --- .../ch/fhnw/thga/gradleplugins/FregeExtension.java | 8 ++++--- .../ch/fhnw/thga/gradleplugins/FregePlugin.java | 12 +++++----- .../ch/fhnw/thga/gradleplugins/ReplFregeTask.java | 28 +++++++++++++++++----- .../fhnw/thga/gradleplugins/SharedTaskLogic.java | 8 ++++++- 4 files changed, 40 insertions(+), 16 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java index cfcd393..18a9500 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java @@ -9,7 +9,8 @@ import org.gradle.api.file.ProjectLayout; import org.gradle.api.provider.ListProperty; import org.gradle.api.provider.Property; -public abstract class FregeExtension { +public abstract class FregeExtension +{ public static final String DEFAULT_RELATIVE_COMPILER_DOWNLOAD_DIR = "lib"; public static final String DEFAULT_RELATIVE_OUTPUT_DIR = "classes/main/frege"; public static final String DEFAULT_RELATIVE_SOURCE_DIR = "src/main/frege"; @@ -29,10 +30,11 @@ public abstract class FregeExtension { public abstract ListProperty getCompilerFlags(); - public abstract Property getReplSource(); + public abstract Property getReplModule(); @Inject - public FregeExtension(ProjectLayout projectLayout) { + public FregeExtension(ProjectLayout projectLayout) + { getCompilerDownloadDir() .convention(projectLayout.getProjectDirectory().dir(DEFAULT_RELATIVE_COMPILER_DOWNLOAD_DIR)); diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java index 957dc83..9b725ac 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java @@ -68,8 +68,8 @@ public class FregePlugin implements Plugin { compileTask.getFregeCompileItem().set(task.getMainModule()); return compileTask; - }) - .get()); + } + )); task.getFregeCompilerJar().set( setupFregeCompilerTask.get().getFregeCompilerOutputPath()); task.getFregeOutputDir().set(extension.getOutputDir()); @@ -82,14 +82,14 @@ public class FregePlugin implements Plugin ReplFregeTask.class, task -> { - task.getReplSource().set(extension.getReplSource()); + task.getReplModule().set(extension.getReplModule()); task.dependsOn(compileFregeTask.map( compileTask -> { - compileTask.getFregeCompileItem().set(task.getReplSource()); + compileTask.getFregeCompileItem().set(task.getReplModule()); return compileTask; - }) - .get()); + } + )); task.getFregeCompilerJar().set( setupFregeCompilerTask.get().getFregeCompilerOutputPath()); task.getFregeOutputDir().set(extension.getOutputDir()); diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java index 7903d7e..67af55e 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java @@ -1,7 +1,9 @@ package ch.fhnw.thga.gradleplugins; +import static ch.fhnw.thga.gradleplugins.SharedTaskLogic.extractClassNameFromFregeModuleName; import org.gradle.api.DefaultTask; import org.gradle.api.file.DirectoryProperty; +import org.gradle.api.file.FileTree; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.provider.Property; import org.gradle.api.provider.Provider; @@ -25,15 +27,29 @@ public abstract class ReplFregeTask extends DefaultTask { public abstract DirectoryProperty getFregeOutputDir(); @Input - @Option(option = "replSource", - description = "The filename which you want to load into the repl, e.g. 'myFregeFile.fr'") - public abstract Property getReplSource(); + @Option(option = "replModule", + description = "The full name of the module which you want to load into the repl, e.g. 'my.mod.Name'") + public abstract Property getReplModule(); @Internal public final Provider getReplClassName() { - return getReplSource() - .map(replSource -> replSource.substring(replSource.lastIndexOf("."))); + return getReplModule() + .map(replSource -> extractClassNameFromFregeModuleName(replSource)); + } + + @Internal + public final Provider getClasspathWithoutReplClassFile() + { + return getFregeOutputDir() + .map(outDir -> outDir.getAsFileTree()) + .map(tree -> tree.matching(pattern -> pattern.exclude("**/*.java"))) + .map(tree -> tree.matching(pattern -> pattern.exclude( + String.format( + "**/%s.class", + getReplClassName().get() + ) + ))); } @TaskAction @@ -45,7 +61,7 @@ public abstract class ReplFregeTask extends DefaultTask { getProject(), getFregeDependencies(), getFregeCompilerJar(), - getFregeOutputDir()) + getClasspathWithoutReplClassFile()) .get().getAsPath(), REPL_MAIN_CLASS)); } diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/SharedTaskLogic.java b/src/main/java/ch/fhnw/thga/gradleplugins/SharedTaskLogic.java index 57af617..e6191a3 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/SharedTaskLogic.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/SharedTaskLogic.java @@ -5,7 +5,8 @@ import org.gradle.api.file.FileCollection; import org.gradle.api.provider.Provider; import org.gradle.api.provider.Property; -public final class SharedTaskLogic { +public final class SharedTaskLogic +{ private SharedTaskLogic() {}; public static final Provider setupClasspath( @@ -19,4 +20,9 @@ public final class SharedTaskLogic { : project.files(depsClasspath, paths); }); } + + public static final String extractClassNameFromFregeModuleName(String moduleName) + { + return moduleName.substring(moduleName.lastIndexOf(".") + 1); + } } -- cgit From 049f8d3029b30dc58221e90aa8ddf69b3aa3b61e Mon Sep 17 00:00:00 2001 From: Thibault Gagnaux Date: Wed, 23 Feb 2022 19:29:54 +0100 Subject: feat: simplifies the replFrege task The replFregeTask has the following new logic: 1. Compiles the specified fregeRepl module (either in the `build.gradle` via command line option `--replModule=...`) and all its dependencies. 2. Sets up the correct classpath so that dependent modules don't have to be imported manually. In addition, it solves the shadowing problem by removing the replModule java and class file from the classpath. 3. It prints one single command to directly start the repl and load the specified module. Bonus: I designed the task so that you can even automate step 3 with the following bash command: `eval $(./gradlew -q replFrege)`. --- .../fhnw/thga/gradleplugins/CompileFregeTask.java | 19 ++++-- .../ch/fhnw/thga/gradleplugins/FregePlugin.java | 1 + .../ch/fhnw/thga/gradleplugins/ReplFregeTask.java | 69 ++++++++++++++++++---- 3 files changed, 70 insertions(+), 19 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java index a3c42d2..a39a07b 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java @@ -11,6 +11,7 @@ import javax.inject.Inject; import org.gradle.api.DefaultTask; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.logging.LogLevel; import org.gradle.api.model.ObjectFactory; import org.gradle.api.provider.ListProperty; import org.gradle.api.provider.Property; @@ -30,7 +31,7 @@ import org.gradle.api.tasks.options.Option; @CacheableTask public abstract class CompileFregeTask extends DefaultTask { private static final String FREGE_FILES_GLOB_PATTERN = "**/*.fr"; - private final JavaExec javaExec; + private JavaExec javaExec; @InputFile @PathSensitive(PathSensitivity.RELATIVE) @@ -113,12 +114,18 @@ public abstract class CompileFregeTask extends DefaultTask { } @TaskAction - public void compileFrege() { + public void compileFrege() + { + this.getLogging().captureStandardOutput(LogLevel.LIFECYCLE); List targetDirectoryArg = List.of( "-d", - getFregeOutputDir().getAsFile().get().getAbsolutePath()); - - javaExec.setClasspath(getProject().files(getFregeCompilerJar())) - .setArgs(buildCompilerArgsFromProperties(targetDirectoryArg)).exec(); + getFregeOutputDir().getAsFile().get().getAbsolutePath() + ); + javaExec.setClasspath( + getProject() + .files(getFregeCompilerJar())) + .setErrorOutput(System.out) + .setArgs(buildCompilerArgsFromProperties(targetDirectoryArg)) + .exec(); } } \ No newline at end of file diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java index 9b725ac..e1f410a 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java @@ -94,6 +94,7 @@ public class FregePlugin implements Plugin setupFregeCompilerTask.get().getFregeCompilerOutputPath()); task.getFregeOutputDir().set(extension.getOutputDir()); task.getFregeDependencies().set(implementation.getAsPath()); + task.getFregeMainSourceDir().set(extension.getMainSourceDir()); } ); } diff --git a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java index 67af55e..1a47f8f 100644 --- a/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java +++ b/src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java @@ -5,6 +5,8 @@ import org.gradle.api.DefaultTask; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.FileTree; import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; import org.gradle.api.provider.Property; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Input; @@ -15,6 +17,7 @@ import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.options.Option; public abstract class ReplFregeTask extends DefaultTask { + private static final Logger LOGGER = Logging.getLogger(ReplFregeTask.class); public static final String REPL_MAIN_CLASS = "frege.repl.FregeRepl"; @InputFile @@ -26,6 +29,9 @@ public abstract class ReplFregeTask extends DefaultTask { @InputDirectory public abstract DirectoryProperty getFregeOutputDir(); + @InputDirectory + public abstract DirectoryProperty getFregeMainSourceDir(); + @Input @Option(option = "replModule", description = "The full name of the module which you want to load into the repl, e.g. 'my.mod.Name'") @@ -39,30 +45,67 @@ public abstract class ReplFregeTask extends DefaultTask { } @Internal - public final Provider getClasspathWithoutReplClassFile() + public final Provider getReplClassFiles() { return getFregeOutputDir() .map(outDir -> outDir.getAsFileTree()) - .map(tree -> tree.matching(pattern -> pattern.exclude("**/*.java"))) - .map(tree -> tree.matching(pattern -> pattern.exclude( - String.format( - "**/%s.class", - getReplClassName().get() + .map(tree -> tree.matching( + pattern -> pattern.include( + String.format( + "**/%s.class", + getReplClassName().get() + ) ) - ))); + )); + } + + @Internal + public final Provider getReplJavaFiles() + { + return getFregeOutputDir() + .map(outDir -> outDir.getAsFileTree()) + .map(tree -> tree.matching( + pattern -> pattern.include( + String.format( + "**/%s.java", + getReplClassName().get() + ) + )) + ); + } + + @Internal + public final Provider getReplFregeFile() + { + return getFregeMainSourceDir() + .map(outDir -> outDir.getAsFileTree()) + .map(tree -> tree.matching( + pattern -> pattern.include( + String.format( + "**/%s.fr", + getReplClassName().get() + ) + )) + ); } @TaskAction - public void printStartFregeReplCommand() { - System.out.println("Execute the following command to start the Frege Repl:"); - System.out.println(String.format( - "java -cp %s %s", + public void printStartFregeReplCommand() + { + getProject().delete(getReplJavaFiles()); + getProject().delete(getReplClassFiles()); + LOGGER.lifecycle( + "Execute the following command to start the Frege Repl and load the Frege module:"); + LOGGER.quiet(String.format( + "(echo :l %s && cat) | java -cp %s %s", + getReplFregeFile().get().getAsPath(), SharedTaskLogic.setupClasspath( getProject(), getFregeDependencies(), getFregeCompilerJar(), - getClasspathWithoutReplClassFile()) + getFregeOutputDir()) .get().getAsPath(), - REPL_MAIN_CLASS)); + REPL_MAIN_CLASS) + ); } } -- cgit