Skip to content

Commit

Permalink
Merge pull request #3 from tricktron/f-incremental-compile
Browse files Browse the repository at this point in the history
Incremental Build and Cache Support
  • Loading branch information
tricktron authored Nov 17, 2021
2 parents c093758 + 8c987f0 commit a5fe1b4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ Optional configuration parameters inside `build.gradle`:
- outputDir: defaults to `<projectRoot>/build/classes/main/frege`
- compilerFlags: defaults to `['-O', '-make']`




### Added Tasks

- **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`.

### Build Cache

The `compileFrege` task supports incremental builds from build cache. Enable the build cache by setting `org.gradle.caching=true` in your `gradle.properites`.


## How to Contribute
Try to add another task, e.g. `fregeDoc` to the [FregePluginFunctionalTest.java](src/functionalTest/java/ch/fhnw/thga/gradleplugins/FregePluginFunctionalTest.java) file and try to make the test pass.
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.2.0-alpha
version = 1.3.0-alpha
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static ch.fhnw.thga.gradleplugins.FregePlugin.*;
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS;
import static org.gradle.testkit.runner.TaskOutcome.FAILED;
import static org.gradle.testkit.runner.TaskOutcome.UP_TO_DATE;
import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -214,6 +216,41 @@ void given_frege_code_in_custom_source_dir_and_custom_output_dir_and_minimal_bui
assertTrue(
new File(testProjectDir.getAbsolutePath() + "/build/frege/ch/fhnw/thga/Completion.class").exists());
}

@Test
void and_is_up_to_date_given_no_code_changes() throws Exception {
String completionFr = "Completion.fr";
String minimalBuildFileConfig = createFregeSection(fregeBuilder.version("'3.25.84'").release("'3.25alpha'").build());
setupDefaultFregeProjectStructure(SIMPLE_FREGE_CODE, completionFr, minimalBuildFileConfig);

BuildResult first = runGradleTask(COMPILE_FREGE_TASK_NAME);
assertEquals(SUCCESS, first.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome());

BuildResult second = runGradleTask(COMPILE_FREGE_TASK_NAME);
assertEquals(UP_TO_DATE, second.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome());
}

@Test
void and_is_cached_given_cache_hit() throws Exception {
String completionFr = "Completion.fr";
String minimalBuildFileConfig = createFregeSection(fregeBuilder.version("'3.25.84'").release("'3.25alpha'").build());
setupDefaultFregeProjectStructure(SIMPLE_FREGE_CODE, completionFr, minimalBuildFileConfig);

BuildResult first = runGradleTask(COMPILE_FREGE_TASK_NAME, "--build-cache");
assertEquals(SUCCESS, first.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome());

String codeChange = String.join(NEW_LINE, "module ch.fhnw.thga.Completion where",
NEW_LINE, NEW_LINE, " frob :: Int -> (Int, String)", NEW_LINE, " frob i = (i, \"Frege rocks\")",
NEW_LINE);
setupDefaultFregeProjectStructure(codeChange, completionFr, "");

BuildResult second = runGradleTask(COMPILE_FREGE_TASK_NAME, "--build-cache");
assertEquals(SUCCESS, second.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome());

setupDefaultFregeProjectStructure(SIMPLE_FREGE_CODE, completionFr, "");
BuildResult third = runGradleTask(COMPILE_FREGE_TASK_NAME, "--build-cache");
assertEquals(FROM_CACHE, third.task(":" + COMPILE_FREGE_TASK_NAME).getOutcome());
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.CacheableTask;
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.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;

@CacheableTask
public abstract class CompileFregeTask extends DefaultTask {
private final JavaExec javaExec;

@InputFile
@PathSensitive(PathSensitivity.RELATIVE)
public abstract RegularFileProperty getFregeCompilerJar();

@InputDirectory
@PathSensitive(PathSensitivity.RELATIVE)
public abstract DirectoryProperty getFregeMainSourceDir();

@Input
Expand Down Expand Up @@ -60,4 +66,4 @@ public void compileFrege() {
.flatMap(Collection::stream).collect(Collectors.toList());
javaExec.setClasspath(getProject().files(getFregeCompilerJar())).setArgs(compilerArgs).exec();
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/ch/fhnw/thga/gradleplugins/FregeExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
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<String> DEFAULT_COMPILER_FLAGS = List.of("-O", "-make");

public abstract Property<String> getVersion();

Expand All @@ -30,8 +32,8 @@ public abstract class FregeExtension {
@Inject
public FregeExtension(ProjectLayout projectLayout) {
getCompilerDownloadDir().convention(projectLayout.getProjectDirectory().dir(DEFAULT_DOWNLOAD_DIRECTORY));
getMainSourceDir().convention(projectLayout.getProjectDirectory());
getMainSourceDir().convention(projectLayout.getProjectDirectory().dir(DEFAULT_RELATIVE_SOURCE_DIR));
getOutputDir().convention(projectLayout.getBuildDirectory().dir(DEFAULT_RELATIVE_OUTPUT_DIR));
getCompilerFlags().convention(List.of("-O", "-make"));
getCompilerFlags().convention(DEFAULT_COMPILER_FLAGS);
}
}

0 comments on commit a5fe1b4

Please sign in to comment.