Skip to content

Commit

Permalink
Update Task implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgrefer committed Sep 17, 2022
1 parent eae7bad commit 0f841fd
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 166 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.freefair.gradle.plugins.lombok.tasks;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.gradle.api.DefaultTask;
Expand All @@ -11,6 +10,7 @@
import org.gradle.api.internal.file.FileTreeInternal;
import org.gradle.api.internal.file.UnionFileTree;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.*;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.process.ExecOperations;
Expand All @@ -19,10 +19,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand All @@ -34,22 +31,22 @@
@Getter
@Setter
@CacheableTask
public class Delombok extends DefaultTask implements LombokTask {
public abstract class Delombok extends DefaultTask implements LombokTask {

@Getter(AccessLevel.NONE)
private final FileSystemOperations fileSystemOperations;
@Getter(AccessLevel.NONE)
private final ExecOperations execOperations;
@Inject
protected abstract FileSystemOperations getFileSystemOperations();
@Inject
protected abstract ExecOperations getExecOperations();

@Nested
@Optional
private final Property<JavaLauncher> launcher = getProject().getObjects().property(JavaLauncher.class);
public abstract Property<JavaLauncher> getLauncher();

/**
* Print the name of each file as it is being delombok-ed.
*/
@Console
private final Property<Boolean> verbose = getProject().getObjects().property(Boolean.class);
public abstract Property<Boolean> getVerbose();

/**
* Sets formatting rules.
Expand All @@ -63,7 +60,7 @@ public class Delombok extends DefaultTask implements LombokTask {
* No warnings or errors will be emitted to standard error.
*/
@Console
private final Property<Boolean> quiet = getProject().getObjects().property(Boolean.class);
public abstract Property<Boolean> getQuiet();

/**
* Sets the encoding of your source files.
Expand All @@ -72,76 +69,70 @@ public class Delombok extends DefaultTask implements LombokTask {
*/
@Input
@Optional
private final Property<String> encoding = getProject().getObjects().property(String.class);
public abstract Property<String> getEncoding();

/**
* Print delombok-ed code to standard output instead of saving it in target directory.
*/
@Input
@Optional
private final Property<Boolean> print = getProject().getObjects().property(Boolean.class);
public abstract Property<Boolean> getPrint();

/**
* Directory to save delomboked files to.
*/
@OutputDirectory
private final DirectoryProperty target = getProject().getObjects().directoryProperty();
public abstract DirectoryProperty getTarget();

/**
* Classpath (analogous to javac -cp option).
*/
@Classpath
@Optional
private final ConfigurableFileCollection classpath = getProject().files();
public abstract ConfigurableFileCollection getClasspath();

/**
* Sourcepath (analogous to javac -sourcepath option).
*/
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
@Optional
private final ConfigurableFileCollection sourcepath = getProject().files();
public abstract ConfigurableFileCollection getSourcepath();

/**
* override Bootclasspath (analogous to javac -bootclasspath option)
*/
@Classpath
@Optional
private final ConfigurableFileCollection bootclasspath = getProject().files();
public abstract ConfigurableFileCollection getBootclasspath();

/**
* Module path (analogous to javac --module-path option)
*/
@Classpath
@Optional
private final ConfigurableFileCollection modulePath = getProject().files();
public abstract ConfigurableFileCollection getModulePath();

/**
* Lombok will only delombok source files.
* Without this option, non-java, non-class files are copied to the target directory.
*/
@Input
@Optional
private final Property<Boolean> nocopy = getProject().getObjects().property(Boolean.class);
public abstract Property<Boolean> getNocopy();

@Classpath
private final ConfigurableFileCollection lombokClasspath = getProject().files();
public abstract ConfigurableFileCollection getLombokClasspath();

@Internal
private final ConfigurableFileCollection input = getProject().files();

@Inject
public Delombok(FileSystemOperations fileSystemOperations, ExecOperations execOperations) {
this.fileSystemOperations = fileSystemOperations;
this.execOperations = execOperations;
}
public abstract ConfigurableFileCollection getInput();

@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
@SkipWhenEmpty
@IgnoreEmptyDirectories
protected FileTree getFilteredInput() {
List<FileTreeInternal> collect = input.getFiles().stream()
List<FileTreeInternal> collect = getInput().getFiles().stream()
.filter(File::isDirectory)
.map(dir -> getProject().fileTree(dir))
.map(FileTreeInternal.class::cast)
Expand All @@ -152,65 +143,65 @@ protected FileTree getFilteredInput() {

@TaskAction
public void delombok() throws IOException {
fileSystemOperations.delete(spec -> spec.delete(getTarget()).setFollowSymlinks(false));
getFileSystemOperations().delete(spec -> spec.delete(getTarget()).setFollowSymlinks(false));

List<String> args = new LinkedList<>();

if (verbose.getOrElse(false)) {
if (getVerbose().getOrElse(false)) {
args.add("--verbose");
}
getFormat().forEach((key, value) -> {
String formatValue = key + (value != null && !value.isEmpty() ? ":" + value : "");
args.add("--format=" + formatValue);
});
if (quiet.getOrElse(false)) {
if (getQuiet().getOrElse(false)) {
args.add("--quiet");
}
if (getEncoding().isPresent()) {
args.add("--encoding=" + getEncoding().get());
}

if (print.getOrElse(false)) {
if (getPrint().getOrElse(false)) {
args.add("--print");
}

if (target.isPresent()) {
if (getTarget().isPresent()) {
args.add("--target=" + escape(getTarget().getAsFile().get().getAbsolutePath()));
}

if (!classpath.isEmpty()) {
if (!getClasspath().isEmpty()) {
args.add("--classpath=" + escape(getClasspath().getAsPath()));
}
if (!sourcepath.isEmpty()) {
if (!getSourcepath().isEmpty()) {
args.add("--sourcepath=" + escape(getSourcepath().getAsPath()));
}
if (!bootclasspath.isEmpty()) {
if (!getBootclasspath().isEmpty()) {
args.add("--bootclasspath=" + escape(getBootclasspath().getAsPath()));
}

if (!modulePath.isEmpty()) {
if (!getModulePath().isEmpty()) {
args.add("--module-path=" + escape(getModulePath().getAsPath()));
}

if (nocopy.getOrElse(false)) {
if (getNocopy().getOrElse(false)) {
args.add("--nocopy");
}

File optionsFile = new File(getTemporaryDir(), "delombok.options");

Files.write(optionsFile.toPath(), args);

execOperations.javaexec(delombok -> {
if (launcher.isPresent()) {
delombok.setExecutable(launcher.get().getExecutablePath().getAsFile().getAbsolutePath());
getExecOperations().javaexec(delombok -> {
if (getLauncher().isPresent()) {
delombok.setExecutable(getLauncher().get().getExecutablePath().getAsFile().getAbsolutePath());
}
delombok.setClasspath(getLombokClasspath());
delombok.getMainClass().set("lombok.launch.Main");
delombok.args("delombok");

delombok.args("@" + optionsFile);

delombok.args(input.getFiles().stream()
delombok.args(getInput().getFiles().stream()
.filter(File::isDirectory)
.collect(Collectors.toList())
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.freefair.gradle.plugins.lombok.tasks;

import lombok.AccessLevel;
import lombok.Getter;
import org.gradle.api.NonNullApi;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.internal.ProcessOperations;
Expand All @@ -18,29 +16,25 @@
*
* @author Lars Grefer
*/
@Getter
@NonNullApi
@CacheableTask
public class LombokApiJar extends LombokJarTask {

@Getter(AccessLevel.NONE)
private final FileSystemOperations fileSystemOperations;
@Getter(AccessLevel.NONE)
private final ProcessOperations processOperations;
public abstract class LombokApiJar extends LombokJarTask {

@Inject
public LombokApiJar(FileSystemOperations fileSystemOperations, ProcessOperations processOperations) {
this.fileSystemOperations = fileSystemOperations;
this.processOperations = processOperations;
protected abstract FileSystemOperations getFileSystemOperations();
@Inject
protected abstract ProcessOperations getProcessOperations();

public LombokApiJar() {
getArchiveAppendix().convention("api");
}

@TaskAction
public void copy() {
fileSystemOperations.delete(spec -> spec.delete(getArchiveFile()).setFollowSymlinks(false));
getFileSystemOperations().delete(spec -> spec.delete(getArchiveFile()).setFollowSymlinks(false));

File destinationDir = getDestinationDirectory().getAsFile().get();
processOperations.javaexec(apiJar -> {
getProcessOperations().javaexec(apiJar -> {
if (getLauncher().isPresent()) {
apiJar.setExecutable(getLauncher().get().getExecutablePath().getAsFile().getAbsolutePath());
}
Expand Down

0 comments on commit 0f841fd

Please sign in to comment.