Skip to content

Commit

Permalink
Merge pull request #2 from tricktron/f-compiler-flags
Browse files Browse the repository at this point in the history
Frege Compiler Flags
  • Loading branch information
tricktron authored Nov 16, 2021
2 parents 69a88ef + a8696e0 commit c093758
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Optional configuration parameters inside `build.gradle`:
- compilerDownloadDir: defaults to `<projectRoot>/lib`
- mainSourceDir: defaults to `<projectRoot>/src/main/frege`
- outputDir: defaults to `<projectRoot>/build/classes/main/frege`
- compilerFlags: defaults to `['-O', '-make']`



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.1.0-alpha
version = 1.2.0-alpha
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@
@TestInstance(Lifecycle.PER_CLASS)
public class FregePluginFunctionalTest {
private static final String NEW_LINE = System.lineSeparator();
private static final String SIMPLE_FREGE_CODE = String.join(NEW_LINE,
"module ch.fhnw.thga.Completion where", NEW_LINE,
NEW_LINE,
" complete :: Int -> (Int, String)", NEW_LINE,
" complete i = (i, \"Frege rocks\")", NEW_LINE);
private static final String SIMPLE_FREGE_CODE = String.join(NEW_LINE, "module ch.fhnw.thga.Completion where",
NEW_LINE, NEW_LINE, " complete :: Int -> (Int, String)", NEW_LINE, " complete i = (i, \"Frege rocks\")",
NEW_LINE);

private static FregeDTOBuilder fregeBuilder;

Expand Down Expand Up @@ -77,10 +75,10 @@ private BuildResult runAndFailGradleTask(String taskName, String... args) {
.buildAndFail();
}

private void setupDefaultFregeProjectStructure(String fregeCode, String fregeFileName, String buildFileConfig) throws Exception {
private void setupDefaultFregeProjectStructure(String fregeCode, String fregeFileName, String buildFileConfig)
throws Exception {
Files.createDirectories(testProjectDir.toPath().resolve(Paths.get("src", "main", "frege")));
File fregeFile = testProjectDir.toPath().resolve(Paths.get("src", "main", "frege", fregeFileName))
.toFile();
File fregeFile = testProjectDir.toPath().resolve(Paths.get("src", "main", "frege", fregeFileName)).toFile();
writeToFile(fregeFile, fregeCode);
appendToFile(buildFile, buildFileConfig);
}
Expand Down Expand Up @@ -163,6 +161,38 @@ void given_frege_code_in_default_source_dir_and_minimal_build_file_config() thro
.exists());
}

@Test
void given_frege_code_and_many_compiler_flags() throws Exception {
String completionFr = "Completion.fr";
String buildConfigWithCompilerFlags = createFregeSection(fregeBuilder.version("'3.25.84'")
.release("'3.25alpha'").compilerFlags("['-v', '-make', '-O', '-hints']").build());
setupDefaultFregeProjectStructure(SIMPLE_FREGE_CODE, completionFr, buildConfigWithCompilerFlags);

BuildResult result = runGradleTask(COMPILE_FREGE_TASK_NAME);

assertTrue(project.getTasks().getByName(COMPILE_FREGE_TASK_NAME) instanceof CompileFregeTask);
assertEquals(SUCCESS, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome());
assertTrue(new File(
testProjectDir.getAbsolutePath() + "/build/classes/main/frege/ch/fhnw/thga/Completion.java")
.exists());
assertTrue(new File(
testProjectDir.getAbsolutePath() + "/build/classes/main/frege/ch/fhnw/thga/Completion.class")
.exists());
}

@Test
void given_frege_code_and_illegal_compiler_flags() throws Exception {
String completionFr = "Completion.fr";
String buildConfigWithIllegalCompilerFlags = createFregeSection(fregeBuilder.version("'3.25.84'")
.release("'3.25alpha'").compilerFlags("['-make', '-bla']").build());
setupDefaultFregeProjectStructure(SIMPLE_FREGE_CODE, completionFr, buildConfigWithIllegalCompilerFlags);

BuildResult result = runAndFailGradleTask(COMPILE_FREGE_TASK_NAME);

assertTrue(project.getTasks().getByName(COMPILE_FREGE_TASK_NAME) instanceof CompileFregeTask);
assertEquals(FAILED, result.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome());
}

@Test
void given_frege_code_in_custom_source_dir_and_custom_output_dir_and_minimal_build_file_config()
throws Exception {
Expand Down Expand Up @@ -196,8 +226,7 @@ void given_frege_file_with_main_function_and_main_module_config() throws Excepti
" main = do", NEW_LINE, " println \"Frege rocks\"", NEW_LINE);
String mainFr = "Main.fr";
String buildFileConfig = createFregeSection(
fregeBuilder.version("'3.25.84'").release("'3.25alpha'")
.mainModule("'ch.fhnw.thga.Main'").build());
fregeBuilder.version("'3.25.84'").release("'3.25alpha'").mainModule("'ch.fhnw.thga.Main'").build());
setupDefaultFregeProjectStructure(fregeCode, mainFr, buildFileConfig);

BuildResult result = runGradleTask(RUN_FREGE_TASK_NAME);
Expand All @@ -209,9 +238,8 @@ void given_frege_file_with_main_function_and_main_module_config() throws Excepti
@Test
void given_frege_file_without_main_function() throws Exception {
String completionFr = "Completion.fr";
String buildFileConfig = createFregeSection(
fregeBuilder.version("'3.25.84'").release("'3.25alpha'")
.mainModule("'ch.fhnw.thga.Completion'").build());
String buildFileConfig = createFregeSection(fregeBuilder.version("'3.25.84'").release("'3.25alpha'")
.mainModule("'ch.fhnw.thga.Completion'").build());
setupDefaultFregeProjectStructure(SIMPLE_FREGE_CODE, completionFr, buildFileConfig);

BuildResult result = runAndFailGradleTask(RUN_FREGE_TASK_NAME);
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/ch/fhnw/thga/gradleplugins/CompileFregeTask.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package ch.fhnw.thga.gradleplugins;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
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.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;
Expand All @@ -23,19 +30,34 @@ public abstract class CompileFregeTask extends DefaultTask {
@InputDirectory
public abstract DirectoryProperty getFregeMainSourceDir();

@Input
public abstract ListProperty<String> getFregeCompilerFlags();

@OutputDirectory
public abstract DirectoryProperty getFregeOutputDir();

@Internal
public final Provider<String> getFregeMainSourcePath() {
return getFregeMainSourceDir().map(srcDir -> srcDir.getAsFile().getAbsolutePath());
}

@Internal
public final Provider<List<String>> getSourcePathArg() {
return getFregeMainSourcePath().map(srcPath -> List.of("-sp", srcPath));
}

@Inject
public CompileFregeTask(ObjectFactory objectFactory) {
javaExec = objectFactory.newInstance(JavaExec.class);
}

@TaskAction
public void compileFrege() {
String fregeMainSourceDir = getFregeMainSourceDir().getAsFile().get().getAbsolutePath();
List<String> args = List.of("-v", "-d", getFregeOutputDir().get().getAsFile().getAbsolutePath(), "-sp",
fregeMainSourceDir, fregeMainSourceDir);
javaExec.setClasspath(getProject().files(getFregeCompilerJar())).setArgs(args).exec();
List<String> directoryArg = List.of("-d", getFregeOutputDir().getAsFile().get().getAbsolutePath());
List<String> compilerArgs = Stream
.of(getFregeCompilerFlags().get(), directoryArg, getSourcePathArg().get(),
List.of(getFregeMainSourcePath().get()))
.flatMap(Collection::stream).collect(Collectors.toList());
javaExec.setClasspath(getProject().files(getFregeCompilerJar())).setArgs(compilerArgs).exec();
}
}
7 changes: 6 additions & 1 deletion src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package ch.fhnw.thga.gradleplugins;

import java.util.List;

import javax.inject.Inject;

import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;

public abstract class FregeExtension {
Expand All @@ -22,11 +25,13 @@ public abstract class FregeExtension {

public abstract DirectoryProperty getOutputDir();

public abstract ListProperty<String> getCompilerFlags();

@Inject
public FregeExtension(ProjectLayout projectLayout) {
getCompilerDownloadDir().convention(projectLayout.getProjectDirectory().dir(DEFAULT_DOWNLOAD_DIRECTORY));
getMainSourceDir().convention(projectLayout.getProjectDirectory());
getOutputDir().convention(projectLayout.getBuildDirectory().dir(DEFAULT_RELATIVE_OUTPUT_DIR));
getCompilerFlags().convention(List.of("-O", "-make"));
}

}
1 change: 1 addition & 0 deletions src/main/java/ch/fhnw/thga/gradleplugins/FregePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void apply(Project project) {
task.getFregeCompilerJar().set(setupFregeCompilerTask.get().getFregeCompilerOutputPath());
task.getFregeMainSourceDir().set(extension.getMainSourceDir());
task.getFregeOutputDir().set(extension.getOutputDir());
task.getFregeCompilerFlags().set(extension.getCompilerFlags());
});
project.getTasks().register(RUN_FREGE_TASK_NAME, RunFregeTask.class, task -> {
task.dependsOn(compileFregeTask);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Provider<String> getFregeVersionJarName() {
}

@Internal
public Provider<String> getDownloadUrl() {
final public Provider<String> getDownloadUrl() {
return getFregeVersionJarName()
.map(name -> String.join("/", FREGE_GITHUB_URL_PREFIX, getRelease().get(), name));
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/ch/fhnw/thga/gradleplugins/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface Builder {

Builder mainModule(String mainModule);

Builder compilerFlags(String compilerFlags);

FregeDTO build();
}
8 changes: 7 additions & 1 deletion src/test/java/ch/fhnw/thga/gradleplugins/FregeDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ public class FregeDTO {
public final String mainSourceDir;
public final String outputDir;
public final String mainModule;
public final String compilerFlags;

public FregeDTO(String version, String release, String compilerDownloadDir, String mainSourceDir,
String outputDir, String mainModule) {
String outputDir, String mainModule, String compilerFlags) {
this.version = version;
this.release = release;
this.compilerDownloadDir = compilerDownloadDir;
this.mainSourceDir = mainSourceDir;
this.outputDir = outputDir;
this.mainModule = mainModule;
this.compilerFlags = compilerFlags;
}

public String getVersion() {
Expand All @@ -48,6 +50,10 @@ public String getMainModule() {
return mainModule;
}

public String getCompilerFlags() {
return compilerFlags;
}

private String getFieldValue(Field field) {
try {
return field.get(this).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public final class FregeDTOBuilder implements Builder {
private String mainSourceDir = "";
private String outputDir = "";
private String mainModule = "";
private String compilerFlags = "";

private static volatile FregeDTOBuilder instance;

Expand Down Expand Up @@ -65,7 +66,13 @@ public Builder mainModule(String mainModule) {
return this;
}

@Override
public Builder compilerFlags(String compilerFlags) {
this.compilerFlags = compilerFlags;
return this;
}

public FregeDTO build() {
return new FregeDTO(version, release, compilerDownloadDir, mainSourceDir, outputDir, mainModule);
return new FregeDTO(version, release, compilerDownloadDir, mainSourceDir, outputDir, mainModule, compilerFlags);
}
}

0 comments on commit c093758

Please sign in to comment.