Skip to content

Commit

Permalink
#10: changing the phase of reconfiguring the compilation tasks and co…
Browse files Browse the repository at this point in the history
…nfirming that incremental compilation works as expected
  • Loading branch information
zyxist committed Mar 13, 2018
1 parent 0e3b7de commit 83d589b
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 87 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'com.zyxist'
version = '0.3.0'
version = '0.3.1'

targetCompatibility = '1.8'
sourceCompatibility = '1.8'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,6 @@
*/
package com.zyxist.chainsaw;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;

Expand Down Expand Up @@ -47,12 +46,14 @@ public void configureTasks(final Project project, String category) {
});
}

private void configureTask(Project project, ConfigurableTask taskType, TaskConfigurator configurator) {
private void configureTask(Project project, ConfigurableTask taskType, TaskConfigurator<Task> configurator) {
final Task task = project.getTasks().findByName(taskType.taskName());
configurator.updateConfiguration(project, task);
task.doFirst(configurator.doFirst(project, task));
configurator
.doFirst(project, task)
.ifPresent(action -> task.doFirst(action));
configurator
.doLast(project, task)
.ifPresent(action -> task.doLast((Action<Task>)action));
.ifPresent(action -> task.doLast(action));
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/zyxist/chainsaw/TaskConfigurator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,7 +30,9 @@ public interface TaskConfigurator<T> {
default void updateConfiguration(Project project, T task) {
}

Action<Task> doFirst(final Project project, final T task);
default Optional<Action<Task>> doFirst(final Project project, final T task) {
return Optional.empty();
}

default Optional<Action<Task>> doLast(final Project project, final T task) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
import com.zyxist.chainsaw.TaskConfigurator;
import com.zyxist.chainsaw.algorithms.ModulePatcher;
import com.zyxist.chainsaw.jigsaw.JigsawCLI;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.compile.JavaCompile;

import static com.zyxist.chainsaw.ChainsawPlugin.PATCH_CONFIGURATION_NAME;
Expand All @@ -34,16 +32,14 @@ public CompileJavaConfigurator(JavaModule moduleConfig) {
}

@Override
public Action<Task> doFirst(Project project, JavaCompile compileJava) {
return task -> {
JigsawCLI cli = new JigsawCLI(compileJava.getClasspath().getAsPath());
ModulePatcher patcher = new ModulePatcher(moduleConfig.getHacks().getPatchedDependencies());
patcher
.patchFrom(project, PATCH_CONFIGURATION_NAME)
.forEach((k, patchedModule) -> cli.patchList().patch(patchedModule));
public void updateConfiguration(Project project, JavaCompile compileJava) {
JigsawCLI cli = new JigsawCLI(compileJava.getClasspath().getAsPath());
ModulePatcher patcher = new ModulePatcher(moduleConfig.getHacks().getPatchedDependencies());
patcher
.patchFrom(project, PATCH_CONFIGURATION_NAME)
.forEach((k, patchedModule) -> cli.patchList().patch(patchedModule));

compileJava.getOptions().getCompilerArgs().addAll(cli.generateArgs());
compileJava.setClasspath(project.files());
};
compileJava.getOptions().getCompilerArgs().addAll(cli.generateArgs());
compileJava.setClasspath(project.files());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.gradle.api.tasks.javadoc.Javadoc;
import org.gradle.external.javadoc.StandardJavadocDocletOptions;

import java.util.Optional;

public class GenerateJavadocConfigurator implements TaskConfigurator<Javadoc> {
private final JavaModule moduleConfig;

Expand All @@ -33,14 +35,11 @@ public GenerateJavadocConfigurator(JavaModule moduleConfig) {
}

@Override
public Action<Task> doFirst(Project project, Javadoc javadoc) {
return new Action<Task>() {
@Override
public void execute(Task task) {
JigsawCLI cli = new JigsawCLI(javadoc.getClasspath().getAsPath());
StandardJavadocDocletOptions options = (StandardJavadocDocletOptions) javadoc.getOptions();
options.addStringOption(JigsawFlags.JAVADOC_MODULE_PATH, cli.getModulePath());
}
};
public Optional<Action<Task>> doFirst(Project project, Javadoc javadoc) {
return Optional.of(task -> {
JigsawCLI cli = new JigsawCLI(javadoc.getClasspath().getAsPath());
StandardJavadocDocletOptions options = (StandardJavadocDocletOptions) javadoc.getOptions();
options.addStringOption(JigsawFlags.JAVADOC_MODULE_PATH, cli.getModulePath());
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,29 +47,23 @@ public void updateConfiguration(Project project, CreateStartScripts task) {
}

@Override
public Action<Task> doFirst(Project project, CreateStartScripts startScripts) {
return new Action<Task>() {
@Override
public void execute(Task task) {
JigsawCLI cli = new JigsawCLI(LIBS_PLACEHOLDER);
cli.module(moduleConfig.getName(), startScripts.getMainClassName());
public Optional<Action<Task>> doFirst(Project project, CreateStartScripts startScripts) {
return Optional.of(task -> {
JigsawCLI cli = new JigsawCLI(LIBS_PLACEHOLDER);
cli.module(moduleConfig.getName(), startScripts.getMainClassName());

startScripts.setClasspath(project.files());
startScripts.setDefaultJvmOpts(cli.generateArgs());
}
};
startScripts.setClasspath(project.files());
startScripts.setDefaultJvmOpts(cli.generateArgs());
});
}

@Override
public Optional<Action<Task>> doLast(Project project, CreateStartScripts startScripts) {
return Optional.of(new Action<Task>() {
@Override
public void execute(Task task) {
File bashScript = new File(startScripts.getOutputDir(), startScripts.getApplicationName());
replaceLibsPlaceHolder(bashScript.toPath(), "\\$APP_HOME/lib");
File batFile = new File(startScripts.getOutputDir(), startScripts.getApplicationName() + ".bat");
replaceLibsPlaceHolder(batFile.toPath(), "%APP_HOME%\\lib");
}
return Optional.of(task -> {
File bashScript = new File(startScripts.getOutputDir(), startScripts.getApplicationName());
replaceLibsPlaceHolder(bashScript.toPath(), "\\$APP_HOME/lib");
File batFile = new File(startScripts.getOutputDir(), startScripts.getApplicationName() + ".bat");
replaceLibsPlaceHolder(batFile.toPath(), "%APP_HOME%\\lib");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.gradle.api.tasks.SourceSetContainer;

import java.io.File;
import java.util.Optional;

import static com.zyxist.chainsaw.ChainsawPlugin.PATCH_CONFIGURATION_NAME;

Expand All @@ -44,8 +45,8 @@ public void updateConfiguration(Project project, JavaExec task) {
}

@Override
public Action<Task> doFirst(Project project, final JavaExec run) {
return task -> {
public Optional<Action<Task>> doFirst(Project project, final JavaExec run) {
return Optional.of(task -> {
final SourceSet mainSourceSet = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("main");

File resourceOutDir = mainSourceSet.getOutput().getResourcesDir();
Expand All @@ -61,7 +62,7 @@ public Action<Task> doFirst(Project project, final JavaExec run) {

run.setJvmArgs(cli.generateArgs());
run.setClasspath(project.files());
};
});
}

private String stripResources(String classpath, File resourceOutputDir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import com.zyxist.chainsaw.jigsaw.JigsawCLI;
import com.zyxist.chainsaw.jigsaw.cli.PatchItem;
import com.zyxist.chainsaw.jigsaw.cli.ReadItem;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.compile.JavaCompile;
Expand All @@ -42,34 +40,28 @@ public CompileTestJavaConfigurator(JavaModule moduleConfig, TestEngine testEngin
}

@Override
public void updateConfiguration(Project project, JavaCompile task) {
task.getInputs().property("moduleName", moduleConfig.getName());
}

@Override
public Action<Task> doFirst(Project project, JavaCompile compileJava) {
return task -> {
JigsawCLI cli = new JigsawCLI(compileJava.getClasspath().getAsPath());
ModulePatcher patcher = new ModulePatcher(moduleConfig.getHacks().getPatchedDependencies());
final SourceSet test = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("test");
public void updateConfiguration(Project project, JavaCompile compileJava) {
compileJava.getInputs().property("moduleName", moduleConfig.getName());
JigsawCLI cli = new JigsawCLI(compileJava.getClasspath().getAsPath());
ModulePatcher patcher = new ModulePatcher(moduleConfig.getHacks().getPatchedDependencies());
final SourceSet test = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("test");

cli.addModules()
.addAll(testEngine.getTestEngineModules())
.addAll(moduleConfig.getExtraTestModules());
cli.readList()
.read(new ReadItem(moduleConfig.getName())
.toAll(testEngine.getTestEngineModules())
.toAll(moduleConfig.getExtraTestModules()));
patcher
.patchFrom(project, PATCH_CONFIGURATION_NAME)
.forEach((k, patchedModule) -> cli.patchList().patch(patchedModule));
cli.patchList().patch(
new PatchItem(moduleConfig.getName())
.with(test.getJava().getSourceDirectories().getAsPath())
);
cli.addModules()
.addAll(testEngine.getTestEngineModules())
.addAll(moduleConfig.getExtraTestModules());
cli.readList()
.read(new ReadItem(moduleConfig.getName())
.toAll(testEngine.getTestEngineModules())
.toAll(moduleConfig.getExtraTestModules()));
patcher
.patchFrom(project, PATCH_CONFIGURATION_NAME)
.forEach((k, patchedModule) -> cli.patchList().patch(patchedModule));
cli.patchList().patch(
new PatchItem(moduleConfig.getName())
.with(test.getJava().getSourceDirectories().getAsPath())
);

compileJava.getOptions().getCompilerArgs().addAll(cli.generateArgs());
compileJava.setClasspath(project.files());
};
compileJava.getOptions().getCompilerArgs().addAll(cli.generateArgs());
compileJava.setClasspath(project.files());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.gradle.api.tasks.testing.Test;

import java.util.Objects;
import java.util.Optional;

import static com.zyxist.chainsaw.ChainsawPlugin.PATCH_CONFIGURATION_NAME;

Expand All @@ -47,8 +48,8 @@ public void updateConfiguration(Project project, Test task) {
}

@Override
public Action<Task> doFirst(Project project, final Test testTask) {
return task -> {
public Optional<Action<Task>> doFirst(Project project, final Test testTask) {
return Optional.of(task -> {
JigsawCLI cli = new JigsawCLI(testTask.getClasspath().getAsPath());
ModulePatcher patcher = new ModulePatcher(moduleConfig.getHacks().getPatchedDependencies());
final SourceSet test = ((SourceSetContainer) project.getProperties().get("sourceSets")).getByName("test");
Expand All @@ -70,6 +71,6 @@ public Action<Task> doFirst(Project project, final Test testTask) {

testTask.setJvmArgs(cli.generateArgs());
testTask.setClasspath(project.files());
};
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class JigsawProjectBuilder {
private boolean useApt = false;
private boolean useJavadoc = false;
private boolean allowNameViolations = false;
private boolean incrementalCompilation = false;

private final List<String> applyPluginClasspath = new ArrayList<>();
private final List<String> appliedPlugins = new ArrayList<>();
Expand Down Expand Up @@ -117,6 +118,11 @@ public JigsawProjectBuilder dontUseExplicitModuleName() {
return this;
}

public JigsawProjectBuilder enableIncrementalCompilation() {
this.incrementalCompilation = true;
return this;
}

public JigsawProjectBuilder requiredModule(String module) {
this.requiredModules.add(module);
return this;
Expand Down Expand Up @@ -199,6 +205,11 @@ public JigsawProjectBuilder createJavaFile(JavaCodeFactory factory) throws IOExc
return createFile(path, factory);
}

public JigsawProjectBuilder replaceJavaFile(JavaCodeFactory factory) throws IOException {
String path = JAVA_SRC + "/" + factory.getFilename(this);
return replaceFile(path, factory);
}

public JigsawProjectBuilder createJavaResourceFile(JavaCodeFactory factory) throws IOException {
String path = JAVA_RESOURCES + "/" + factory.getFilename(this);
return createFile(path, factory);
Expand All @@ -221,6 +232,17 @@ private JigsawProjectBuilder createFile(String path, JavaCodeFactory factory) th
return this;
}

private JigsawProjectBuilder replaceFile(String path, JavaCodeFactory factory) throws IOException {
createDirectories(path);
File file = new File(tmpDir.getRoot(), path);
if (file.exists()) {
file.delete();
}
file = tmpDir.newFile(path);
ResourceGroovyMethods.leftShift(file, factory.generateCode(this));
return this;
}

public JigsawProjectBuilder createGradleBuild() throws IOException {
StringBuilder build = new StringBuilder();

Expand Down Expand Up @@ -253,11 +275,14 @@ public JigsawProjectBuilder createGradleBuild() throws IOException {
}

private void generateCompilerArgs(StringBuilder build) {
if (!compilerArgs.isEmpty()) {
if (!compilerArgs.isEmpty() || incrementalCompilation) {
build.append("tasks.withType(JavaCompile) {\n");
for (String arg: compilerArgs) {
build.append(" options.compilerArgs << '" +arg + "'\n");
}
if (incrementalCompilation) {
build.append(" options.incremental = true\n");
}
build.append("}\n");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,8 +19,18 @@ import com.zyxist.chainsaw.builder.JavaCodeFactory
import com.zyxist.chainsaw.builder.JigsawProjectBuilder

class RunnableJavaClassFactory implements JavaCodeFactory {
private final String textToDisplay;

RunnableJavaClassFactory(String textToDisplay) {
this.textToDisplay = textToDisplay
}

static RunnableJavaClassFactory runnableJavaClass() {
return new RunnableJavaClassFactory();
return new RunnableJavaClassFactory("Hello World!")
}

static RunnableJavaClassFactory runnableJavaClass(String textToDisplay) {
return new RunnableJavaClassFactory(textToDisplay)
}

@Override
Expand All @@ -38,7 +48,7 @@ public class AClass {
}
public static void main(String... args) {
new AClass().aMethod("Hello World!");
new AClass().aMethod("${this.textToDisplay}");
}
}
"""
Expand Down
Loading

0 comments on commit 83d589b

Please sign in to comment.