Skip to content

Commit

Permalink
Set up run system
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebemish committed Dec 3, 2023
1 parent a4bdf63 commit d964f4e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
12 changes: 12 additions & 0 deletions src/main/java/dev/lukebemish/flix/gradle/FlixGradleExtension.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.lukebemish.flix.gradle;

import dev.lukebemish.flix.gradle.task.FlixCompile;
import dev.lukebemish.flix.gradle.task.FlixRun;
import dev.lukebemish.flix.gradle.task.LibLevel;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
Expand Down Expand Up @@ -139,6 +140,17 @@ public void application() {
manifest.getAttributes().put("Main-Class", "Main");
});
});

var main = sourceSets.getByName("main");
var flixSource = (SourceDirectorySet) main.getExtensions().getByName("flix");
var flixClasspath = project.getConfigurations().maybeCreate(FlixGradlePlugin.sourcedNameOf(main, FlixGradlePlugin.FLIX_CLASSPATH_CONFIGURATION_NAME));
project.getTasks().register("runFlix", FlixRun.class, task -> {
task.getFlixInput().from(flixClasspath);
task.getSource().set(flixSource.getSourceDirectories());
task.setDescription("Runs this project with flix");
task.setGroup("application");
task.getOutputs().upToDateWhen(t -> false);
});
}

// Enabling this will likely require changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public AbstractFlixCompile(Project project, ExecActionFactory execActionFactory)
this.getJavaLauncher().convention(getJavaToolchainService().launcherFor(javaPluginExtension.getToolchain()));
}

protected Path runExec(Properties properties) {
protected Path runExec(Properties properties, String... args) {
Path tempDir = getTemporaryDir().toPath();

Path tempClasses = tempDir.resolve("classes");
Expand Down Expand Up @@ -120,6 +120,7 @@ protected Path runExec(Properties properties) {
exec.classpath(classpath.toArray());
exec.getMainClass().set("dev.lukebemish.flix.gradle.wrapper.Bootstrap");
exec.args(propertiesFile.toAbsolutePath().toString());
exec.args((Object[]) args);

exec.setWorkingDir(runDir.toFile());

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/dev/lukebemish/flix/gradle/task/FlixRun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.lukebemish.flix.gradle.task;

import org.gradle.api.Project;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import org.gradle.process.internal.ExecActionFactory;

import javax.inject.Inject;
import java.util.Properties;

public abstract class FlixRun extends AbstractFlixCompile {
@Input
public abstract ListProperty<String> getArgs();

@Inject
public FlixRun(Project project, ExecActionFactory execActionFactory) {
super(project, execActionFactory);
}

@TaskAction
public void exec() {
Properties properties = getOptions().create();
addFlixInput(properties);
properties.put("command", "run");

String[] args = getArgs().get().toArray(String[]::new);

runExec(properties, args);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dev.lukebemish.flix.gradle.wrapper;

import ca.uwaterloo.flix.api.Bootstrap;
import ca.uwaterloo.flix.api.Flix;
import ca.uwaterloo.flix.util.Formatter;
import ca.uwaterloo.flix.language.CompilationMessage;
import ca.uwaterloo.flix.runtime.CompilationResult;
import ca.uwaterloo.flix.util.Validation;
import scala.Option;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -33,24 +32,13 @@ public static void main(String[] args) {

FlixCommand command = FlixCommand.valueOf(properties.getProperty("command").toUpperCase(Locale.ROOT));

Path currentDir = Paths.get("").toAbsolutePath();

Bootstrap bootstrap = new Bootstrap(currentDir, Option.empty());
Flix flix = new Flix();
flix.setOptions(options.create());
options.configure(flix);

switch (command) {
case COMPILE -> {
Flix flix = new Flix();
flix.setOptions(options.create());
options.configure(flix);
var validation = flix.compile();
if (!(validation instanceof Validation.Success)) {
scala.collection.Iterable<String> list = flix.mkMessages(validation.toHardFailure().errors());
while (!list.isEmpty()) {
System.err.println(list.head());
list = list.tail();
}
throw new RuntimeException("Compilation failed");
}
compile(flix);
}
case DOC -> {
// TODO: Implement
Expand All @@ -65,20 +53,31 @@ public static void main(String[] args) {
System.arraycopy(args, 1, newArgs, 0, args.length);
}

var validation = bootstrap.run(options.create(), newArgs);
var main = compile(flix).getMain();

if (!(validation instanceof Validation.Success)) {
var list = validation.toHardFailure().errors();
while (!list.isEmpty()) {
System.err.println(list.head().message(Formatter.getDefault()));
list = list.tail();
}
throw new RuntimeException("Run failed");
if (main.isEmpty()) {
throw new RuntimeException("No main function found");
} else {
main.get().apply(newArgs);
}
}
}
}

private static CompilationResult compile(Flix flix) {
var validation = flix.compile();
if (!(validation instanceof Validation.Success<CompilationResult, CompilationMessage> success)) {
scala.collection.Iterable<String> list = flix.mkMessages(validation.toHardFailure().errors());
while (!list.isEmpty()) {
System.err.println(list.head());
list = list.tail();
}
throw new RuntimeException("Compilation failed");
} else {
return success.get();
}
}

public enum FlixCommand {
COMPILE,
DOC,
Expand Down

0 comments on commit d964f4e

Please sign in to comment.