Reland "Update gradle custom tasks to use worker api"

This reverts commit 4fa23f0a7a389c73ae8cf49f957ec09996216fb5.

Bug: b/270105162
Change-Id: I4941884c3279fb417a587ec2ae7e21113845141c
diff --git a/buildSrc/src/main/java/desugaredlibrary/CustomConversionAsmRewriterTask.java b/buildSrc/src/main/java/desugaredlibrary/CustomConversionAsmRewriterTask.java
index bae3c1c..1a09676 100644
--- a/buildSrc/src/main/java/desugaredlibrary/CustomConversionAsmRewriterTask.java
+++ b/buildSrc/src/main/java/desugaredlibrary/CustomConversionAsmRewriterTask.java
@@ -9,10 +9,12 @@
 import java.io.UncheckedIOException;
 import javax.inject.Inject;
 import org.gradle.api.DefaultTask;
+import org.gradle.api.file.RegularFileProperty;
 import org.gradle.api.tasks.InputFile;
 import org.gradle.api.tasks.OutputDirectory;
 import org.gradle.api.tasks.TaskAction;
-import org.gradle.workers.IsolationMode;
+import org.gradle.workers.WorkAction;
+import org.gradle.workers.WorkParameters;
 import org.gradle.workers.WorkerExecutor;
 
 public class CustomConversionAsmRewriterTask extends DefaultTask {
@@ -47,29 +49,31 @@
 
   @TaskAction
   void exec() {
-    workerExecutor.submit(
-        Run.class,
-        config -> {
-          config.setIsolationMode(IsolationMode.NONE);
-          config.params(rawJar, outputDirectory);
-        });
+    workerExecutor
+        .noIsolation()
+        .submit(
+            Run.class,
+            parameters -> {
+              parameters.getRawJar().set(rawJar);
+              parameters.getOutputDirectory().set(outputDirectory);
+            });
   }
 
-  public static class Run implements Runnable {
+  public interface RunParameters extends WorkParameters {
+    RegularFileProperty getRawJar();
 
-    private final File rawJar;
-    private final File outputDirectory;
+    RegularFileProperty getOutputDirectory();
+  }
 
-    @Inject
-    public Run(File rawJar, File outputDirectory) {
-      this.rawJar = rawJar;
-      this.outputDirectory = outputDirectory;
-    }
+  public abstract static class Run implements WorkAction<RunParameters> {
 
     @Override
-    public void run() {
+    public void execute() {
       try {
-        CustomConversionAsmRewriter.generateJars(rawJar.toPath(), outputDirectory.toPath());
+        RunParameters parameters = getParameters();
+        CustomConversionAsmRewriter.generateJars(
+            parameters.getRawJar().getAsFile().get().toPath(),
+            parameters.getOutputDirectory().getAsFile().get().toPath());
       } catch (IOException e) {
         throw new UncheckedIOException(e);
       }
diff --git a/buildSrc/src/main/java/dx/DexMergerTask.java b/buildSrc/src/main/java/dx/DexMergerTask.java
index 5dea52b..409337c 100644
--- a/buildSrc/src/main/java/dx/DexMergerTask.java
+++ b/buildSrc/src/main/java/dx/DexMergerTask.java
@@ -3,20 +3,20 @@
 // BSD-style license that can be found in the LICENSE file.
 package dx;
 
-import com.google.common.base.Optional;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Set;
 import javax.inject.Inject;
 import org.gradle.api.DefaultTask;
 import org.gradle.api.file.FileCollection;
-import org.gradle.api.tasks.InputFile;
+import org.gradle.api.file.RegularFileProperty;
+import org.gradle.api.provider.SetProperty;
 import org.gradle.api.tasks.InputFiles;
 import org.gradle.api.tasks.OutputFile;
 import org.gradle.api.tasks.TaskAction;
-import org.gradle.workers.IsolationMode;
+import org.gradle.workers.WorkAction;
+import org.gradle.workers.WorkParameters;
 import org.gradle.workers.WorkerExecutor;
 import utils.Utils;
 
@@ -26,7 +26,6 @@
 
   private FileCollection source;
   private File destination;
-  private Optional<File> dexMergerExecutable = Optional.absent(); // Worker API cannot handle null.
 
   @Inject
   public DexMergerTask(WorkerExecutor workerExecutor) {
@@ -51,57 +50,39 @@
     this.destination = destination;
   }
 
-  @InputFile
-  @org.gradle.api.tasks.Optional
-  public File getDexMergerExecutable() {
-    return dexMergerExecutable.orNull();
-  }
-
-  public void setDexMergerExecutable(File dexMergerExecutable) {
-    this.dexMergerExecutable = Optional.fromNullable(dexMergerExecutable);
-  }
-
   @TaskAction
   void exec() {
-    workerExecutor.submit(
-        RunDexMerger.class,
-        config -> {
-          File executable =
-              dexMergerExecutable.isPresent()
-                  ? dexMergerExecutable.get()
-                  : config
-                      .getForkOptions()
-                      .getWorkingDir()
-                      .toPath()
-                      .resolve(Utils.dexMergerExecutable())
-                      .toFile();
-          config.setIsolationMode(IsolationMode.NONE);
-          config.params(source.getFiles(), destination, executable);
-        });
+    workerExecutor
+        .noIsolation()
+        .submit(
+            RunDexMerger.class,
+            parameters -> {
+              parameters.getSources().set(source.getFiles());
+              parameters.getDestination().set(destination);
+            });
   }
 
-  public static class RunDexMerger implements Runnable {
-    private final Set<File> sources;
-    private final File destination;
-    private final File dexMergerExecutable;
+  public interface RunDexMergerParameters extends WorkParameters {
 
-    @Inject
-    public RunDexMerger(Set<File> sources, File destination, File dexMergerExecutable) {
-      this.sources = sources;
-      this.destination = destination;
-      this.dexMergerExecutable = dexMergerExecutable;
-    }
+    SetProperty<File> getSources();
+
+    RegularFileProperty getDestination();
+
+    RegularFileProperty getDexMergerExecutable();
+  }
+
+  public abstract static class RunDexMerger implements WorkAction<RunDexMergerParameters> {
 
     @Override
-    public void run() {
+    public void execute() {
       try {
+        RunDexMergerParameters parameters = getParameters();
         List<String> command = new ArrayList<>();
-        command.add(dexMergerExecutable.getCanonicalPath());
-        command.add(destination.getCanonicalPath());
-        for (File source : sources) {
+        command.add(Utils.dexMergerExecutable().toString());
+        command.add(parameters.getDestination().getAsFile().get().getCanonicalPath());
+        for (File source : parameters.getSources().get()) {
           command.add(source.getCanonicalPath());
         }
-
         Process dexMerger = new ProcessBuilder(command).inheritIO().start();
         int exitCode = dexMerger.waitFor();
         if (exitCode != 0) {
diff --git a/buildSrc/src/main/java/dx/DxTask.java b/buildSrc/src/main/java/dx/DxTask.java
index e05e8fb..a66733a 100644
--- a/buildSrc/src/main/java/dx/DxTask.java
+++ b/buildSrc/src/main/java/dx/DxTask.java
@@ -3,22 +3,23 @@
 // BSD-style license that can be found in the LICENSE file.
 package dx;
 
-import com.google.common.base.Optional;
 import java.io.File;
 import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Set;
 import javax.inject.Inject;
 import org.gradle.api.DefaultTask;
 import org.gradle.api.file.FileCollection;
+import org.gradle.api.file.RegularFileProperty;
+import org.gradle.api.provider.Property;
+import org.gradle.api.provider.SetProperty;
 import org.gradle.api.tasks.Input;
-import org.gradle.api.tasks.InputFile;
 import org.gradle.api.tasks.InputFiles;
 import org.gradle.api.tasks.OutputDirectory;
 import org.gradle.api.tasks.TaskAction;
-import org.gradle.workers.IsolationMode;
+import org.gradle.workers.WorkAction;
+import org.gradle.workers.WorkParameters;
 import org.gradle.workers.WorkerExecutor;
 import utils.Utils;
 
@@ -28,7 +29,6 @@
 
   private FileCollection source;
   private File destination;
-  private Optional<File> dxExecutable = Optional.absent(); // Worker API cannot handle null.
   private boolean debug;
 
   @Inject
@@ -54,16 +54,6 @@
     this.destination = destination;
   }
 
-  @InputFile
-  @org.gradle.api.tasks.Optional
-  public File getDxExecutable() {
-    return dxExecutable.orNull();
-  }
-
-  public void setDxExecutable(File dxExecutable) {
-    this.dxExecutable = Optional.fromNullable(dxExecutable);
-  }
-
   @Input
   public boolean isDebug() {
     return debug;
@@ -75,49 +65,41 @@
 
   @TaskAction
   void exec() {
-    workerExecutor.submit(
-        RunDx.class,
-        config -> {
-          File executable =
-              dxExecutable.isPresent()
-                  ? dxExecutable.get()
-                  : config
-                      .getForkOptions()
-                      .getWorkingDir()
-                      .toPath()
-                      .resolve(Utils.dxExecutable())
-                      .toFile();
-          config.setIsolationMode(IsolationMode.NONE);
-          config.params(source.getFiles(), destination, executable, debug);
-        });
+    workerExecutor
+        .noIsolation()
+        .submit(
+            RunDx.class,
+            parameters -> {
+              parameters.getSources().set(source.getFiles());
+              parameters.getDestination().set(destination);
+              parameters.getDebug().set(debug);
+            });
   }
 
-  public static class RunDx implements Runnable {
-    private final Set<File> sources;
-    private final File destination;
-    private final File dxExecutable;
-    private final boolean debug;
+  public interface RunDxParameters extends WorkParameters {
 
-    @Inject
-    public RunDx(Set<File> sources, File destination, File dxExecutable, boolean debug) {
-      this.sources = sources;
-      this.destination = destination;
-      this.dxExecutable = dxExecutable;
-      this.debug = debug;
-    }
+    SetProperty<File> getSources();
+
+    RegularFileProperty getDestination();
+
+    Property<Boolean> getDebug();
+  }
+
+  public abstract static class RunDx implements WorkAction<RunDxParameters> {
 
     @Override
-    public void run() {
+    public void execute() {
+      RunDxParameters parameters = getParameters();
       try {
         List<String> command = new ArrayList<>();
-        command.add(dxExecutable.getCanonicalPath());
+        command.add(Utils.dxExecutable().toString());
         command.add("--dex");
         command.add("--output");
-        command.add(destination.getCanonicalPath());
-        if (debug) {
+        command.add(parameters.getDestination().getAsFile().get().getCanonicalPath());
+        if (parameters.getDebug().get()) {
           command.add("--debug");
         }
-        for (File source : sources) {
+        for (File source : parameters.getSources().get()) {
           command.add(source.getCanonicalPath());
         }
 
diff --git a/buildSrc/src/main/java/smali/SmaliTask.java b/buildSrc/src/main/java/smali/SmaliTask.java
index 845592e..dd4e10c 100644
--- a/buildSrc/src/main/java/smali/SmaliTask.java
+++ b/buildSrc/src/main/java/smali/SmaliTask.java
@@ -9,14 +9,16 @@
 import java.io.IOException;
 import java.io.UncheckedIOException;
 import java.util.List;
-import java.util.Set;
 import javax.inject.Inject;
 import org.gradle.api.DefaultTask;
 import org.gradle.api.file.FileTree;
+import org.gradle.api.file.RegularFileProperty;
+import org.gradle.api.provider.SetProperty;
 import org.gradle.api.tasks.InputFiles;
 import org.gradle.api.tasks.OutputFile;
 import org.gradle.api.tasks.TaskAction;
-import org.gradle.workers.IsolationMode;
+import org.gradle.workers.WorkAction;
+import org.gradle.workers.WorkParameters;
 import org.gradle.workers.WorkerExecutor;
 import org.jf.smali.Smali;
 import org.jf.smali.SmaliOptions;
@@ -53,28 +55,33 @@
 
   @TaskAction
   void exec() {
-    workerExecutor.submit(RunSmali.class, config -> {
-      config.setIsolationMode(IsolationMode.NONE);
-      config.params(source.getFiles(), destination);
-    });
+    workerExecutor
+        .noIsolation()
+        .submit(
+            RunSmali.class,
+            parameters -> {
+              parameters.getSources().set(source.getFiles());
+              parameters.getDestination().set(destination);
+            });
   }
 
-  public static class RunSmali implements Runnable {
-    private final Set<File> sources;
-    private final File destination;
+  public interface RunSmaliParameters extends WorkParameters {
 
-    @Inject
-    public RunSmali(Set<File> sources, File destination) {
-      this.sources = sources;
-      this.destination = destination;
-    }
+    SetProperty<File> getSources();
+
+    RegularFileProperty getDestination();
+  }
+
+  public abstract static class RunSmali implements WorkAction<RunSmaliParameters> {
 
     @Override
-    public void run() {
+    public void execute() {
       try {
-        List<String> fileNames = sources.stream().map(File::toString).collect(toList());
+        RunSmaliParameters parameters = getParameters();
+        List<String> fileNames =
+            parameters.getSources().get().stream().map(File::toString).collect(toList());
         SmaliOptions options = new SmaliOptions();
-        options.outputDexFile = destination.getCanonicalPath();
+        options.outputDexFile = parameters.getDestination().getAsFile().get().getCanonicalPath();
         Smali.assemble(options, fileNames);
       } catch (IOException e) {
         throw new UncheckedIOException(e);
diff --git a/buildSrc/src/main/java/tasks/DownloadDependency.java b/buildSrc/src/main/java/tasks/DownloadDependency.java
index 59596a8..6d7a75c 100644
--- a/buildSrc/src/main/java/tasks/DownloadDependency.java
+++ b/buildSrc/src/main/java/tasks/DownloadDependency.java
@@ -15,11 +15,14 @@
 import java.util.stream.Collectors;
 import javax.inject.Inject;
 import org.gradle.api.DefaultTask;
+import org.gradle.api.file.RegularFileProperty;
+import org.gradle.api.provider.Property;
 import org.gradle.api.tasks.InputFiles;
 import org.gradle.api.tasks.OutputDirectory;
 import org.gradle.api.tasks.TaskAction;
 import org.gradle.internal.os.OperatingSystem;
-import org.gradle.workers.IsolationMode;
+import org.gradle.workers.WorkAction;
+import org.gradle.workers.WorkParameters;
 import org.gradle.workers.WorkerExecutor;
 
 public class DownloadDependency extends DefaultTask {
@@ -88,29 +91,34 @@
     if (outputDir.exists() && outputDir.isDirectory()) {
       outputDir.delete();
     }
-    workerExecutor.submit(RunDownload.class, config -> {
-      config.setIsolationMode(IsolationMode.NONE);
-      config.params(type, sha1File);
-    });
+    workerExecutor
+        .noIsolation()
+        .submit(
+            RunDownload.class,
+            parameters -> {
+              parameters.getType().set(type);
+              parameters.getSha1File().set(sha1File);
+            });
   }
 
-  public static class RunDownload implements Runnable {
-    private final Type type;
-    private final File sha1File;
+  public interface RunDownloadParameters extends WorkParameters {
+    Property<Type> getType();
 
-    @Inject
-    public RunDownload(Type type, File sha1File) {
-      this.type = type;
-      this.sha1File = sha1File;
-    }
+    RegularFileProperty getSha1File();
+  }
+
+  public abstract static class RunDownload implements WorkAction<RunDownloadParameters> {
 
     @Override
-    public void run() {
+    public void execute() {
       try {
+        RunDownloadParameters parameters = getParameters();
+        Type type = parameters.getType().get();
+        File sha1File = parameters.getSha1File().getAsFile().get();
         if (type == Type.GOOGLE_STORAGE) {
-          downloadFromGoogleStorage();
+          downloadFromGoogleStorage(sha1File);
         } else if (type == Type.X20) {
-          downloadFromX20();
+          downloadFromX20(sha1File);
         } else {
           throw new RuntimeException("Unexpected or missing dependency type: " + type);
         }
@@ -119,7 +127,7 @@
       }
     }
 
-    private void downloadFromGoogleStorage() throws IOException, InterruptedException {
+    private void downloadFromGoogleStorage(File sha1File) throws IOException, InterruptedException {
       List<String> args = Arrays.asList("-n", "-b", "r8-deps", "-s", "-u", sha1File.toString());
       if (OperatingSystem.current().isWindows()) {
         List<String> command = new ArrayList<>();
@@ -133,7 +141,7 @@
       }
     }
 
-    private void downloadFromX20() throws IOException, InterruptedException {
+    private void downloadFromX20(File sha1File) throws IOException, InterruptedException {
       if (OperatingSystem.current().isWindows()) {
         throw new RuntimeException("Downloading from x20 unsupported on windows");
       }