Skip to content

Commit

Permalink
Merge pull request #13 from tricktron/f-repl-frege-v2
Browse files Browse the repository at this point in the history
Frege Repl Refactoring
  • Loading branch information
tricktron authored Dec 11, 2021
2 parents 8fed781 + e7b73f3 commit 3c4bfae
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 359 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ Optional configuration parameters inside `build.gradle`:
- **setupFrege**: Downloads the specified version of the Frege compiler.
- **compileFrege**: All your `*.fr` files in `mainSourceDir` get compiled to `outputDir`.
- **runFrege**: Runs the Frege module specified by `mainModule`. Alternatively you can also pass the main module by command line, e.g: `gradle runFrege --mainModule=my.mod.Name`.
- **replFrege**: Starts the Frege REPL with the Frege compiler, `outputDir` and specified `dependencies` on the classpath.
- **replFrege**: Takes care of all project dependencies and prints the command to start the Frege REPL, e.g: `java -cp <your-correct-classpath-with-all-dependencies> frege.repl.FregeRepl`.

### Compile Dependencies
### Dependencies

Compile dependencies can be configured as expected in your `build.gradle` file, using the `implementation` scope, e.g.:
Dependencies can be configured as expected in your `build.gradle` file, using the `implementation` scope, e.g.:

```groovy
repositories {
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ check.dependsOn functionalTest

tasks.withType(Test).configureEach {
useJUnitPlatform()
maxParallelForks 6
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group = ch.fhnw.thga
version = 1.4.1-alpha
version = 1.5.0-alpha

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFile;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.TaskProvider;

import ch.fhnw.thga.gradleplugins.internal.DependencyFregeTask;

public class FregePlugin implements Plugin<Project> {
public static final String SETUP_FREGE_TASK_NAME = "setupFrege";
public static final String COMPILE_FREGE_TASK_NAME = "compileFrege";
public static final String RUN_FREGE_TASK_NAME = "runFrege";
public static final String REPL_FREGE_TASK_NAME = "replFrege";
public static final String DEPS_FREGE_TASK_NAME = "depsFrege";
public static final String FREGE_PLUGIN_ID = "ch.fhnw.thga.frege";
public static final String FREGE_EXTENSION_NAME = "frege";
public static final String FREGE_IMPLEMENTATION_SCOPE = "implementation";

private FileCollection setupClasspath(Project project, Configuration dependencies,
Provider<RegularFile> fregeCompilerJar) {
if (dependencies.isEmpty()) {
return project.files(fregeCompilerJar);
} else {
return project.files(fregeCompilerJar, dependencies.getAsPath());
}
}

@Override
public void apply(Project project) {
Configuration implementation = project.getConfigurations().create(FREGE_IMPLEMENTATION_SCOPE);
Expand Down Expand Up @@ -41,11 +56,16 @@ public void apply(Project project) {
task.getMainModule().set(extension.getMainModule());
task.getFregeDependencies().set(implementation.getAsPath());
});
project.getTasks().register(DEPS_FREGE_TASK_NAME,
DependencyFregeTask.class, task -> {
task.dependsOn(setupFregeCompilerTask);
task.getClasspath().setFrom(setupClasspath(project, implementation,
setupFregeCompilerTask.get().getFregeCompilerOutputPath()));
});
project.getTasks().register(REPL_FREGE_TASK_NAME, ReplFregeTask.class, task -> {
task.dependsOn(compileFregeTask);
task.getFregeCompilerJar().set(setupFregeCompilerTask.get().getFregeCompilerOutputPath());
task.getFregeOutputDir().set(extension.getOutputDir());
task.getFregeDependencies().set(implementation.getAsPath());
task.dependsOn(setupFregeCompilerTask);
task.getFregeClasspath().setFrom(setupClasspath(project, implementation,
setupFregeCompilerTask.get().getFregeCompilerOutputPath()));
});
}
}
50 changes: 7 additions & 43 deletions src/main/java/ch/fhnw/thga/gradleplugins/ReplFregeTask.java
Original file line number Diff line number Diff line change
@@ -1,55 +1,19 @@
package ch.fhnw.thga.gradleplugins;

import javax.inject.Inject;

import org.gradle.api.DefaultTask;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.model.ObjectFactory;
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.JavaExec;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.TaskAction;

public abstract class ReplFregeTask extends DefaultTask {
public static final Logger LOGGER = Logging.getLogger(SetupFregeTask.class);
public static final String REPL_MAIN_CLASS = "frege.repl.FregeRepl";

private final JavaExec javaExec;

@InputFile
public abstract RegularFileProperty getFregeCompilerJar();

@InputDirectory
public abstract DirectoryProperty getFregeOutputDir();

@Input
public abstract Property<String> getFregeDependencies();

@Internal
public final Provider<FileCollection> getClasspath() {
return getFregeDependencies().map(depsClasspath -> {
return depsClasspath.isEmpty() ? getProject().files(getFregeCompilerJar(), getFregeOutputDir())
: getProject().files(getFregeCompilerJar(), getFregeOutputDir(), depsClasspath);
});
}

@Inject
public ReplFregeTask(ObjectFactory objectFactory) {
javaExec = objectFactory.newInstance(JavaExec.class);
}
@InputFiles
public abstract ConfigurableFileCollection getFregeClasspath();

@TaskAction
public void startFregeRepl() {
javaExec.setStandardInput(System.in);
javaExec.getMainClass().set(REPL_MAIN_CLASS);
javaExec.setClasspath(getClasspath().get()).exec();
public void printStartFregeReplCommand() {
System.out.println("Execute the following command to start the Frege Repl:");
System.out.println(String.format("java -cp %s %s", getFregeClasspath().getAsPath(), REPL_MAIN_CLASS));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ch.fhnw.thga.gradleplugins.internal;

import org.gradle.api.DefaultTask;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.TaskAction;

public abstract class DependencyFregeTask extends DefaultTask {
@InputFiles
public abstract ConfigurableFileCollection getClasspath();

@TaskAction
public void fregeDependencies() {
System.out.println(getClasspath().getAsPath());
}
}

0 comments on commit 3c4bfae

Please sign in to comment.