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.
+181 −145
Diff settings

Always

Just for now

Prev

#79: extracted StartScriptsMutator from RunTaskMutator

  • Loading branch information...
tlinkowski committed Apr 17, 2019
commit e708e3a09734315e05cfde06e3f2c205968d77e3
@@ -0,0 +1,35 @@
package org.javamodularity.moduleplugin.tasks;

import org.gradle.api.Project;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.JavaExec;
import org.javamodularity.moduleplugin.JavaProjectHelper;

import java.util.Objects;

abstract class AbstractExecutionMutator {

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

protected final JavaExec execTask;
protected final Project project;

AbstractExecutionMutator(JavaExec execTask, Project project) {
this.execTask = execTask;
this.project = project;
}

protected final String getMainClassName() {
String mainClassName = Objects.requireNonNull(execTask.getMain());
if (!mainClassName.contains("/")) {
LOGGER.warn("No module was provided for main class, assuming the current module. Prefer providing 'mainClassName' in the following format: '$moduleName/a.b.Main'");
return helper().moduleName() + "/" + mainClassName;
}
return mainClassName;
}

protected final JavaProjectHelper helper() {
return new JavaProjectHelper(project);
}
}
@@ -55,7 +55,7 @@ private static void configure(ModularCreateStartScripts startScriptsTask, Projec
startScriptsTask.setDefaultJvmOpts(defaultJvmOpts);
}

var mutator = new RunTaskMutator(runTask, project);
var mutator = new StartScriptsMutator(runTask, project);
mutator.updateStartScriptsTask(startScriptsTask);
mutator.movePatchedLibs();
}
@@ -15,13 +15,18 @@ public void configureRun() {
}

private void doConfigureRun() {
JavaExec runTask = helper().task(ApplicationPlugin.TASK_RUN_NAME, JavaExec.class);
var mutator = new RunTaskMutator(runTask, project);
var mutator = new RunTaskMutator(getRunTask(), project);
mutator.configureRun();
project.afterEvaluate(p -> configureStartScripts());
}

private void configureStartScripts() {
var mutator = new StartScriptsMutator(getRunTask(), project);
mutator.updateStartScriptsTask(ApplicationPlugin.TASK_START_SCRIPTS_NAME);
mutator.movePatchedLibs();
}

project.afterEvaluate(p -> {
mutator.updateStartScriptsTask(ApplicationPlugin.TASK_START_SCRIPTS_NAME);
mutator.movePatchedLibs();
});
private JavaExec getRunTask() {
return helper().task(ApplicationPlugin.TASK_RUN_NAME, JavaExec.class);
}
}
@@ -1,135 +1,25 @@
package org.javamodularity.moduleplugin.tasks;

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.distribution.Distribution;
import org.gradle.api.distribution.DistributionContainer;
import org.gradle.api.file.FileCopyDetails;
import org.gradle.api.file.RelativePath;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.application.CreateStartScripts;
import org.javamodularity.moduleplugin.JavaProjectHelper;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

public class RunTaskMutator {
private static final String LIBS_PLACEHOLDER = "APP_HOME_LIBS_PLACEHOLDER";
private static final String PATCH_LIBS_PLACEHOLDER = "APP_HOME_PATCH_LIBS_PLACEHOLDER";
private static final Logger LOGGER = Logging.getLogger(RunTaskMutator.class);

private final JavaExec execTask;
private final Project project;
public class RunTaskMutator extends AbstractExecutionMutator {

public RunTaskMutator(JavaExec execTask, Project project) {
this.execTask = execTask;
this.project = project;
super(execTask, project);
}

public void configureRun() {
execTask.getExtensions().create("moduleOptions", ModuleOptions.class, project);
updateJavaExecTask();
}

public void updateStartScriptsTask(String taskStartScriptsName) {
CreateStartScripts startScriptsTask = helper().task(taskStartScriptsName, CreateStartScripts.class);
updateStartScriptsTask(startScriptsTask);
}

public void updateStartScriptsTask(CreateStartScripts startScriptsTask) {

// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
startScriptsTask.doFirst(new Action<Task>() {
@Override
public void execute(Task task) {
configureStartScriptsDoFirst(startScriptsTask);
}
});

// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
startScriptsTask.doLast(new Action<Task>() {
@Override
public void execute(Task task) {
configureStartScriptsDoLast(startScriptsTask);
}
});
}

private void configureStartScriptsDoFirst(CreateStartScripts startScriptsTask) {
List<String> jvmArgs = buildStartScriptsJvmArgs(startScriptsTask);
startScriptsTask.setDefaultJvmOpts(jvmArgs);
startScriptsTask.setClasspath(project.files());

if (ModularCreateStartScripts.UNDEFINED_MAIN_CLASS_NAME.equals(startScriptsTask.getMainClassName())) {
startScriptsTask.setMainClassName(/* helper().moduleName() + "/" + */ execTask.getMain());
}
}

private List<String> buildStartScriptsJvmArgs(CreateStartScripts startScriptsTask) {
var jvmArgs = new ArrayList<String>();

String moduleName = helper().moduleName();
var patchModuleExtension = helper().extension(PatchModuleExtension.class);

var moduleJvmArgs = List.of(
"--module-path", LIBS_PLACEHOLDER,
"--module", getMainClassName()
);

ModuleOptions moduleOptions = execTask.getExtensions().getByType(ModuleOptions.class);
moduleOptions.mutateArgs(moduleName, jvmArgs);

buildPatchModuleArgStream(patchModuleExtension).forEach(jvmArgs::add);

startScriptsTask.getDefaultJvmOpts().forEach(jvmArgs::add);

jvmArgs.addAll(moduleJvmArgs);

return jvmArgs;
}

private Stream<String> buildPatchModuleArgStream(PatchModuleExtension patchModuleExtension) {
return patchModuleExtension.resolve(jarName -> PATCH_LIBS_PLACEHOLDER + "/" + jarName).toArgumentStream();
}

private void configureStartScriptsDoLast(CreateStartScripts startScriptsTask) {
Path outputDir = startScriptsTask.getOutputDir().toPath();

Path bashScript = outputDir.resolve(startScriptsTask.getApplicationName());
replaceLibsPlaceHolder(bashScript, "\\$APP_HOME/lib", "\\$APP_HOME/patchlibs");

Path batFile = outputDir.resolve(startScriptsTask.getApplicationName() + ".bat");
replaceLibsPlaceHolder(batFile, "%APP_HOME%\\\\lib", "%APP_HOME%\\\\patchlibs");
}

public void movePatchedLibs() {
var patchModuleExtension = helper().extension(PatchModuleExtension.class);
if (patchModuleExtension.getConfig().isEmpty()) {
return;
}

Distribution mainDistribution = helper().extension("distributions", DistributionContainer.class)
.getByName("main");
mainDistribution.contents(
copySpec -> copySpec.filesMatching(patchModuleExtension.getJars(), this::updateRelativePath)
);
}

private void updateRelativePath(FileCopyDetails fileCopyDetails) {
RelativePath updatedRelativePath = fileCopyDetails.getRelativePath().getParent().getParent()
.append(true, "patchlibs", fileCopyDetails.getName());
fileCopyDetails.setRelativePath(updatedRelativePath);
}

private void updateJavaExecTask() {
// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
execTask.doFirst(new Action<Task>() {
@@ -169,30 +59,4 @@ public void execute(Task task) {
jvmArgs.addAll(moduleJvmArgs);
return jvmArgs;
}

private String getMainClassName() {
String mainClassName = Objects.requireNonNull(execTask.getMain());
if (!mainClassName.contains("/")) {
LOGGER.warn("No module was provided for main class, assuming the current module. Prefer providing 'mainClassName' in the following format: '$moduleName/a.b.Main'");
return helper().moduleName() + "/" + mainClassName;
}
return mainClassName;
}

private static void replaceLibsPlaceHolder(Path path, String libText, String patchLibText) {
try {
String bashScript = Files.readString(path);
String updatedBashScript = bashScript
.replaceAll(LIBS_PLACEHOLDER, libText)
.replaceAll(PATCH_LIBS_PLACEHOLDER, patchLibText);

Files.writeString(path, updatedBashScript);
} catch (IOException e) {
throw new GradleException("Couldn't replace placeholder in " + path);
}
}

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

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.distribution.Distribution;
import org.gradle.api.distribution.DistributionContainer;
import org.gradle.api.file.FileCopyDetails;
import org.gradle.api.file.RelativePath;
import org.gradle.api.tasks.JavaExec;
import org.gradle.api.tasks.application.CreateStartScripts;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class StartScriptsMutator extends AbstractExecutionMutator {

private static final String LIBS_PLACEHOLDER = "APP_HOME_LIBS_PLACEHOLDER";
private static final String PATCH_LIBS_PLACEHOLDER = "APP_HOME_PATCH_LIBS_PLACEHOLDER";

public StartScriptsMutator(JavaExec execTask, Project project) {
super(execTask, project);
}

public void updateStartScriptsTask(String taskStartScriptsName) {
CreateStartScripts startScriptsTask = helper().task(taskStartScriptsName, CreateStartScripts.class);
updateStartScriptsTask(startScriptsTask);
}

public void updateStartScriptsTask(CreateStartScripts startScriptsTask) {

// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
startScriptsTask.doFirst(new Action<Task>() {
@Override
public void execute(Task task) {
configureStartScriptsDoFirst(startScriptsTask);
}
});

// don't convert to lambda: https://github.com/java9-modularity/gradle-modules-plugin/issues/54
startScriptsTask.doLast(new Action<Task>() {
@Override
public void execute(Task task) {
configureStartScriptsDoLast(startScriptsTask);
}
});
}

private void configureStartScriptsDoFirst(CreateStartScripts startScriptsTask) {
List<String> jvmArgs = buildStartScriptsJvmArgs(startScriptsTask);
startScriptsTask.setDefaultJvmOpts(jvmArgs);
startScriptsTask.setClasspath(project.files());

if (ModularCreateStartScripts.UNDEFINED_MAIN_CLASS_NAME.equals(startScriptsTask.getMainClassName())) {
startScriptsTask.setMainClassName(/* helper().moduleName() + "/" + */ execTask.getMain());
}
}

private List<String> buildStartScriptsJvmArgs(CreateStartScripts startScriptsTask) {
var jvmArgs = new ArrayList<String>();

String moduleName = helper().moduleName();
var patchModuleExtension = helper().extension(PatchModuleExtension.class);

var moduleJvmArgs = List.of(
"--module-path", LIBS_PLACEHOLDER,
"--module", getMainClassName()
);

ModuleOptions moduleOptions = execTask.getExtensions().getByType(ModuleOptions.class);
moduleOptions.mutateArgs(moduleName, jvmArgs);

buildPatchModuleArgStream(patchModuleExtension).forEach(jvmArgs::add);

startScriptsTask.getDefaultJvmOpts().forEach(jvmArgs::add);

jvmArgs.addAll(moduleJvmArgs);

return jvmArgs;
}

private Stream<String> buildPatchModuleArgStream(PatchModuleExtension patchModuleExtension) {
return patchModuleExtension.resolve(jarName -> PATCH_LIBS_PLACEHOLDER + "/" + jarName).toArgumentStream();
}

private void configureStartScriptsDoLast(CreateStartScripts startScriptsTask) {
Path outputDir = startScriptsTask.getOutputDir().toPath();

Path bashScript = outputDir.resolve(startScriptsTask.getApplicationName());
replaceLibsPlaceHolder(bashScript, "\\$APP_HOME/lib", "\\$APP_HOME/patchlibs");

Path batFile = outputDir.resolve(startScriptsTask.getApplicationName() + ".bat");
replaceLibsPlaceHolder(batFile, "%APP_HOME%\\\\lib", "%APP_HOME%\\\\patchlibs");
}

public void movePatchedLibs() {
var patchModuleExtension = helper().extension(PatchModuleExtension.class);
if (patchModuleExtension.getConfig().isEmpty()) {
return;
}

Distribution mainDistribution = helper().extension("distributions", DistributionContainer.class)
.getByName("main");
mainDistribution.contents(
copySpec -> copySpec.filesMatching(patchModuleExtension.getJars(), this::updateRelativePath)
);
}

private void updateRelativePath(FileCopyDetails fileCopyDetails) {
RelativePath updatedRelativePath = fileCopyDetails.getRelativePath().getParent().getParent()
.append(true, "patchlibs", fileCopyDetails.getName());
fileCopyDetails.setRelativePath(updatedRelativePath);
}

private static void replaceLibsPlaceHolder(Path path, String libText, String patchLibText) {
try {
String bashScript = Files.readString(path);
String updatedBashScript = bashScript
.replaceAll(LIBS_PLACEHOLDER, libText)
.replaceAll(PATCH_LIBS_PLACEHOLDER, patchLibText);

Files.writeString(path, updatedBashScript);
} catch (IOException e) {
throw new GradleException("Couldn't replace placeholder in " + path);
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.