Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #79: Some internal refactorings #88

Merged
merged 3 commits into from May 7, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+505 −351
Diff settings

Always

Just for now

#79: JavaProjectHelper-related refactorings

  • Loading branch information...
tlinkowski committed Apr 17, 2019
commit 7793a22c7e16eee42f7833411cf8f3f7e3b56986
@@ -10,7 +10,7 @@
import java.util.Optional;

/**
* Generic helper for Gradle {@link Project} API that has {@link JavaPlugin} applied.
* Generic helper for Gradle {@link Project} API that is modular and has {@link JavaPlugin} applied.
*/
public final class JavaProjectHelper {

@@ -24,9 +24,21 @@ public Project project() {
return project;
}

public <T> T extension(Class<T> extensionType) {
return project.getExtensions().getByType(extensionType);
}

public <T> T extension(String name, Class<T> extensionType) {
return extensionType.cast(project.getExtensions().getByName(name));
}

public String moduleName() {
return extension("moduleName", String.class);
}

//region SOURCE SETS
public SourceSetContainer sourceSets() {
return project.getExtensions().getByType(SourceSetContainer.class);
return extension(SourceSetContainer.class);
}

public SourceSet sourceSet(String sourceSetName) {
@@ -37,7 +49,7 @@ public SourceSet mainSourceSet() {
return sourceSet(SourceSet.MAIN_SOURCE_SET_NAME);
}

public SourceSet testSourceSet(String sourceSetName) {
public SourceSet testSourceSet() {
return sourceSet(SourceSet.TEST_SOURCE_SET_NAME);
}
//endregion
@@ -47,16 +59,20 @@ public Task task(String taskName) {
return project.getTasks().getByName(taskName);
}

public JavaCompile compileJavaTask(String taskName) {
return (JavaCompile) task(taskName);
}

public Optional<Task> findTask(String taskName) {
return Optional.ofNullable(project.getTasks().findByName(taskName));
}

public Optional<JavaCompile> findCompileJavaTask(String taskName) {
return findTask(taskName).map(JavaCompile.class::cast);
public <T extends Task> T task(String taskName, Class<T> taskType) {
return taskType.cast(task(taskName));
}

public <T extends Task> Optional<T> findTask(String taskName, Class<T> taskType) {
return findTask(taskName).map(taskType::cast);
}

public JavaCompile compileJavaTask(String taskName) {
return task(taskName, JavaCompile.class);
}
//endregion

@@ -24,11 +24,11 @@ private void configureModularity(Project project, String moduleName) {

new CompileTask(project).configureCompileJava();
new CompileModuleInfoTask(project).configureCompileModuleInfoJava();
new CompileTestTask().configureCompileTestJava(project, moduleName);
new TestTask().configureTestJava(project, moduleName);
new RunTask().configureRun(project, moduleName);
new JavadocTask().configureJavaDoc(project);
ModularJavaExec.configure(project, moduleName);
ModularCreateStartScripts.configure(project, moduleName);
new CompileTestTask(project).configureCompileTestJava();
new TestTask(project).configureTestJava();
new RunTask(project).configureRun();
new JavadocTask(project).configureJavaDoc();
ModularJavaExec.configure(project);
ModularCreateStartScripts.configure(project);
}
}
@@ -0,0 +1,48 @@
package org.javamodularity.moduleplugin.internal;

import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.javamodularity.moduleplugin.tasks.PatchModuleExtension;

import java.util.Objects;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

public final class PatchModuleResolver {

private static final Logger LOGGER = Logging.getLogger(PatchModuleResolver.class);

private final PatchModuleExtension patchModuleExtension;
/**
* Takes a JAR name and resolves it to a full JAR path. If returned JAR patch is empty, the JAR is skipped.
*/
private final UnaryOperator<String> jarNameResolver;

public PatchModuleResolver(PatchModuleExtension patchModuleExtension, UnaryOperator<String> jarNameResolver) {
this.patchModuleExtension = patchModuleExtension;
this.jarNameResolver = jarNameResolver;
}

public Stream<String> toArgumentStream() {
return toValueStream().flatMap(value -> Stream.of("--patch-module", value));
}

public Stream<String> toValueStream() {
return patchModuleExtension.getConfig().stream()
.map(patch -> patch.split("="))
.map(this::resolvePatchModuleValue)
.filter(Objects::nonNull);
}

private String resolvePatchModuleValue(String[] parts) {
String moduleName = parts[0];
String jarName = parts[1];

String jarPath = jarNameResolver.apply(jarName);
if (jarPath.isEmpty()) {
LOGGER.warn("Skipped patching {} into {}", jarName, moduleName);
return null;
}
return moduleName + "=" + jarPath;
}
}
@@ -2,23 +2,16 @@

import org.gradle.api.Project;
import org.gradle.api.tasks.compile.JavaCompile;
import org.javamodularity.moduleplugin.JavaProjectHelper;
import org.javamodularity.moduleplugin.extensions.CompileModuleOptions;

abstract class AbstractCompileTask {

protected final Project project;
abstract class AbstractCompileTask extends AbstractModulePluginTask {

AbstractCompileTask(Project project) {
this.project = project;
super(project);
}

final CompileJavaTaskMutator createCompileJavaTaskMutator(
JavaCompile compileJava, CompileModuleOptions moduleOptions) {
return new CompileJavaTaskMutator(project, compileJava.getClasspath(), moduleOptions);
}

final JavaProjectHelper helper() {
return new JavaProjectHelper(project);
}
}
@@ -0,0 +1,17 @@
package org.javamodularity.moduleplugin.tasks;

import org.gradle.api.Project;
import org.javamodularity.moduleplugin.JavaProjectHelper;

abstract class AbstractModulePluginTask {

protected final Project project;

AbstractModulePluginTask(Project project) {
this.project = project;
}

protected final JavaProjectHelper helper() {
return new JavaProjectHelper(project);
}
}
@@ -4,10 +4,12 @@
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.compile.AbstractCompile;
import org.gradle.api.tasks.compile.JavaCompile;
import org.javamodularity.moduleplugin.JavaProjectHelper;
import org.javamodularity.moduleplugin.extensions.CompileModuleOptions;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

class CompileJavaTaskMutator {

@@ -36,26 +38,33 @@
* is {@code false}, {@code compileModuleInfoJava} if it is {@code true}
*/
void modularizeJavaCompileTask(JavaCompile javaCompile) {
PatchModuleExtension patchModuleExtension = project.getExtensions().getByType(PatchModuleExtension.class);
List<String> compilerArgs = buildCompilerArgs(javaCompile);
javaCompile.getOptions().setCompilerArgs(compilerArgs);
javaCompile.setClasspath(project.files());

// https://github.com/java9-modularity/gradle-modules-plugin/issues/45
helper().findTask(COMPILE_KOTLIN_TASK_NAME, AbstractCompile.class)
.ifPresent(compileKotlin -> javaCompile.setDestinationDir(compileKotlin.getDestinationDir()));
}

private List<String> buildCompilerArgs(JavaCompile javaCompile) {
var compilerArgs = new ArrayList<>(javaCompile.getOptions().getCompilerArgs());

compilerArgs.addAll(List.of("--module-path", compileJavaClasspath
.filter(patchModuleExtension::isUnpatched)
.getAsPath()));
String moduleName = helper().moduleName();
var patchModuleExtension = helper().extension(PatchModuleExtension.class);

Stream.of("--module-path", patchModuleExtension.getUnpatchedClasspathAsPath(compileJavaClasspath))
.forEach(compilerArgs::add);

String moduleName = (String) project.getExtensions().findByName("moduleName");
moduleOptions.mutateArgs(moduleName, compilerArgs);

compilerArgs.addAll(patchModuleExtension.configure(compileJavaClasspath));
javaCompile.getOptions().setCompilerArgs(compilerArgs);
javaCompile.setClasspath(project.files());
patchModuleExtension.resolve(compileJavaClasspath).toArgumentStream()
.forEach(compilerArgs::add);

// https://github.com/java9-modularity/gradle-modules-plugin/issues/45
AbstractCompile compileKotlin = (AbstractCompile) project.getTasks().findByName(COMPILE_KOTLIN_TASK_NAME);
if (compileKotlin != null) {
javaCompile.setDestinationDir(compileKotlin.getDestinationDir());
}
return compilerArgs;
}

private JavaProjectHelper helper() {
return new JavaProjectHelper(project);
}
}
@@ -20,7 +20,7 @@ public CompileModuleInfoTask(Project project) {
* @see CompileTask#configureCompileJava()
*/
public void configureCompileModuleInfoJava() {
helper().findCompileJavaTask(JavaPlugin.COMPILE_JAVA_TASK_NAME)
helper().findTask(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile.class)
.ifPresent(this::configureCompileModuleInfoJava);
}

@@ -17,7 +17,7 @@ public CompileTask(Project project) {
* @see CompileModuleInfoTask#configureCompileModuleInfoJava()
*/
public void configureCompileJava() {
helper().findCompileJavaTask(JavaPlugin.COMPILE_JAVA_TASK_NAME)
helper().findTask(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile.class)
.ifPresent(this::configureCompileJava);
}

@@ -4,53 +4,64 @@
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.compile.JavaCompile;
import org.javamodularity.moduleplugin.TestEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class CompileTestTask {
public class CompileTestTask extends AbstractModulePluginTask {

public void configureCompileTestJava(Project project, String moduleName) {
JavaCompile compileTestJava = (JavaCompile) project.getTasks().findByName(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME);
JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class);
PatchModuleExtension patchModuleExtension = project.getExtensions().getByType(PatchModuleExtension.class);
public CompileTestTask(Project project) {
super(project);
}

public void configureCompileTestJava() {
helper().findTask(JavaPlugin.COMPILE_TEST_JAVA_TASK_NAME, JavaCompile.class)
.ifPresent(this::configureCompileTestJava);
}

compileTestJava.getExtensions().create("moduleOptions", ModuleOptions.class, project);
SourceSet testSourceSet = javaConvention.getSourceSets().getByName(SourceSet.TEST_SOURCE_SET_NAME);
private void configureCompileTestJava(JavaCompile compileTestJava) {
var moduleOptions = compileTestJava.getExtensions().create("moduleOptions", ModuleOptions.class, project);

// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
compileTestJava.doFirst(new Action<Task>() {
@Override
public void execute(Task task) {
var args = new ArrayList<>(compileTestJava.getOptions().getCompilerArgs());
args.addAll(List.of(
"--module-path", compileTestJava.getClasspath()
.filter(patchModuleExtension::isUnpatched)
.getAsPath(),
"--patch-module", moduleName + "=" + testSourceSet.getJava().getSourceDirectories().getAsPath()
));
var compilerArgs = buildCompilerArgs(compileTestJava, moduleOptions);
compileTestJava.getOptions().setCompilerArgs(compilerArgs);
compileTestJava.setClasspath(project.files());
}
});
}

TestEngine.select(project).ifPresent(testEngine -> args.addAll(List.of(
"--add-modules", testEngine.moduleName,
"--add-reads", moduleName + "=" + testEngine.moduleName
)));
private List<String> buildCompilerArgs(JavaCompile compileTestJava, ModuleOptions moduleOptions) {
var compilerArgs = new ArrayList<>(compileTestJava.getOptions().getCompilerArgs());

ModuleOptions moduleOptions = compileTestJava.getExtensions().getByType(ModuleOptions.class);
moduleOptions.mutateArgs(moduleName, args);
String moduleName = helper().moduleName();
var patchModuleExtension = helper().extension(PatchModuleExtension.class);

args.addAll(patchModuleExtension.configure(compileTestJava.getClasspath()));
Stream.of(
"--module-path",
patchModuleExtension.getUnpatchedClasspathAsPath(compileTestJava.getClasspath()),
"--patch-module",
moduleName + "=" + helper().testSourceSet().getJava().getSourceDirectories().getAsPath()
).forEach(compilerArgs::add);

ModuleInfoTestHelper.mutateArgs(project, moduleName, args::add);
TestEngine.select(project).ifPresent(testEngine -> Stream.of(
"--add-modules", testEngine.moduleName,
"--add-reads", moduleName + "=" + testEngine.moduleName
).forEach(compilerArgs::add));

compileTestJava.getOptions().setCompilerArgs(args);
compileTestJava.setClasspath(project.files());
}
moduleOptions.mutateArgs(moduleName, compilerArgs);

});
patchModuleExtension.resolve(compileTestJava.getClasspath()).toArgumentStream()
.forEach(compilerArgs::add);

ModuleInfoTestHelper.mutateArgs(project, compilerArgs::add);

return compilerArgs;
}

}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.