Skip to content

Commit

Permalink
use TaskProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
dengwei03 committed Jun 18, 2020
1 parent fdbfc8e commit 7fb2d10
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.smartdengg.plugin

import com.android.build.gradle.*
import com.android.build.gradle.api.BaseVariant
import com.android.build.gradle.internal.pipeline.TransformTask
import com.android.builder.model.AndroidProject
import com.android.utils.FileUtils
import com.smartdengg.compile.WovenClass
Expand All @@ -12,6 +13,7 @@ import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.tasks.TaskProvider
import org.gradle.internal.reflect.Instantiator

import javax.inject.Inject
Expand Down Expand Up @@ -63,28 +65,28 @@ class DebounceGradlePlugin implements Plugin<Project> {
}

static void createWriteMappingTask(Project project, BaseVariant variant,
Map<String, List<WovenClass>> variantWeavedClassesMap) {
Map<String, List<WovenClass>> variantWovenClassesMap) {

def mappingTaskName = "outputMappingFor${variant.name.capitalize()}"
Task debounceTask = project.tasks[
"transformClassesWith${DebounceIncrementalTransform.TASK_NAME.capitalize()}For${variant.name.capitalize()}"]

OutputMappingTask outputMappingTask = project.tasks.create(//
name: "${mappingTaskName}",
type: OutputMappingTask) {
classes = variantWeavedClassesMap
variantName = variant.name
outputMappingFile =
FileUtils.join(project.buildDir, AndroidProject.FD_OUTPUTS, 'debounce', 'logs',
variant.name, 'classes.txt')
}

def transformTaskName = "transformClassesWith${DebounceIncrementalTransform.TASK_NAME.capitalize()}For${variant.name.capitalize()}"

TaskProvider<TransformTask> debounceTask = project.tasks.named(transformTaskName)
TaskProvider<OutputMappingTask> mappingTask = project.tasks.
register("${mappingTaskName}", OutputMappingTask) { OutputMappingTask task ->
task.classes = variantWovenClassesMap
task.variantName = variant.name
task.mappingFile =
FileUtils.join(project.buildDir, AndroidProject.FD_OUTPUTS, 'debounce', 'logs',
variant.name, 'classes.txt')
task.onlyIf { debounceTask.get().didWork }
task.inputFiles = debounceTask.get().outputs.files
}

mappingTask.configure(Utils.taskTimedConfigure)
debounceTask.configure(Utils.taskTimedConfigure)
outputMappingTask.configure(Utils.taskTimedConfigure)

debounceTask.finalizedBy(outputMappingTask)
outputMappingTask.onlyIf { debounceTask.didWork }
outputMappingTask.inputFiles = debounceTask.outputs.files
debounceTask.configure { Task task ->
task.finalizedBy(mappingTask)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class OutputMappingTask extends DefaultTask {
Property<String> variantName = project.objects.property(String.class)

@OutputFile
RegularFileProperty outputMappingFile = project.objects.fileProperty()
RegularFileProperty mappingFile = project.objects.fileProperty()

@Internal
MapProperty<String, List<WovenClass>> classes = project.
Expand All @@ -45,7 +45,7 @@ class OutputMappingTask extends DefaultTask {
void writeMapping(InputChanges inputChanges) {

boolean loggable = (project.extensions["$DebounceExtension.NAME"] as DebounceExtension).loggable
File mappingFile = outputMappingFile.get().asFile
File file = mappingFile.get().asFile
List<WovenClass> wovenClasses = (List<WovenClass>) classes.get()[variantName.get()]

if (loggable && inputChanges.isIncremental()) {
Expand All @@ -56,9 +56,9 @@ class OutputMappingTask extends DefaultTask {
}
}

FileUtils.touch(mappingFile)
Files.asCharSink(mappingFile, Charsets.UTF_8).write("")
PrintWriter writer = PrintWriterUtil.createPrintWriterOut(mappingFile)
FileUtils.touch(file)
Files.asCharSink(file, Charsets.UTF_8).write("")
PrintWriter writer = PrintWriterUtil.createPrintWriterOut(file)

try {
wovenClasses.findAll {
Expand All @@ -71,9 +71,9 @@ class OutputMappingTask extends DefaultTask {
}
}
} finally {
PrintWriterUtil.closePrintWriter(mappingFile, writer)
PrintWriterUtil.closePrintWriter(file, writer)
ColoredLogger.log(
"Wrote TXT report to file://${PrintWriterUtil.fileName(mappingFile)}")
"Wrote TXT report to file://${PrintWriterUtil.fileName(file)}")
}
}
}

0 comments on commit 7fb2d10

Please sign in to comment.