diff options
author | Thibault Gagnaux <tgagnaux@gmail.com> | 2022-02-23 19:29:54 +0100 |
---|---|---|
committer | Thibault Gagnaux <tgagnaux@gmail.com> | 2022-02-23 19:29:54 +0100 |
commit | 049f8d3029b30dc58221e90aa8ddf69b3aa3b61e (patch) | |
tree | e6c5e82733f910ebb0db7dba4fc74eb8dc339e77 /src/main/java | |
parent | a4879784e7be87b5ee184b47eb8faba635019a5d (diff) | |
download | frege-gradle-plugin-049f8d3029b30dc58221e90aa8ddf69b3aa3b61e.tar.gz frege-gradle-plugin-049f8d3029b30dc58221e90aa8ddf69b3aa3b61e.tar.bz2 frege-gradle-plugin-049f8d3029b30dc58221e90aa8ddf69b3aa3b61e.zip |
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)`.
Diffstat (limited to 'src/main/java')
3 files changed, 70 insertions, 19 deletions
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<String> 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<Project> 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<FileTree> getClasspathWithoutReplClassFile() + public final Provider<FileTree> 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<FileTree> getReplJavaFiles() + { + return getFregeOutputDir() + .map(outDir -> outDir.getAsFileTree()) + .map(tree -> tree.matching( + pattern -> pattern.include( + String.format( + "**/%s.java", + getReplClassName().get() + ) + )) + ); + } + + @Internal + public final Provider<FileTree> 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) + ); } } |