From 8ffb7f86375208f231930a73be8186a87612cea6 Mon Sep 17 00:00:00 2001 From: garan Date: Fri, 6 Dec 2024 17:51:27 +0000 Subject: [PATCH] Adds output from validation tasks to enable use of Gradle caching --- .../src/main/kotlin/MemoryFootprintTask.kt | 16 +++++++++------- .../src/main/kotlin/ValidateWffFilesTask.kt | 13 ++++++------- .../src/main/kotlin/WffValidatorPlugin.kt | 12 +++++++++++- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/MemoryFootprintTask.kt b/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/MemoryFootprintTask.kt index 46dfc8bbc..cf76d2b8b 100644 --- a/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/MemoryFootprintTask.kt +++ b/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/MemoryFootprintTask.kt @@ -20,10 +20,12 @@ import org.gradle.api.GradleException import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.RegularFileProperty import org.gradle.api.provider.Property +import org.gradle.api.tasks.CacheableTask import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputDirectory import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction @@ -34,17 +36,13 @@ import javax.inject.Inject /** * Runs the Memory Footprint checker tool. */ +@CacheableTask abstract class MemoryFootprintTask() : DefaultTask() { - init { - // Setting the outputs to always be up to date, meaning that this task will only run if the - // input changes (or if the last run was not successful). - this.outputs.upToDateWhen { true } - } - @get:Inject abstract val execOperations: ExecOperations @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) abstract val apkLocation: DirectoryProperty @get:Internal @@ -54,6 +52,9 @@ abstract class MemoryFootprintTask() : DefaultTask() { @get:PathSensitive(PathSensitivity.RELATIVE) abstract val memoryFootprintJarPath: RegularFileProperty + @get:OutputFile + abstract val memoryFootprintOutputFile: RegularFileProperty + @get:Input abstract val wffVersion: Property @@ -66,7 +67,7 @@ abstract class MemoryFootprintTask() : DefaultTask() { throw GradleException("Expected only one APK!") val apkPath = File(artifacts.elements.single().outputFile).toPath() - execOperations.javaexec { + val result = execOperations.javaexec { it.classpath = project.files(memoryFootprintJarPath) it.args( "--schema-version", @@ -76,5 +77,6 @@ abstract class MemoryFootprintTask() : DefaultTask() { "--verbose" ) } + memoryFootprintOutputFile.get().asFile.writeText(result.toString()) } } diff --git a/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/ValidateWffFilesTask.kt b/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/ValidateWffFilesTask.kt index 58cc89770..97153793e 100644 --- a/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/ValidateWffFilesTask.kt +++ b/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/ValidateWffFilesTask.kt @@ -22,6 +22,7 @@ import org.gradle.api.tasks.CacheableTask import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction @@ -36,12 +37,6 @@ import javax.inject.Inject */ @CacheableTask abstract class ValidateWffFilesTask : DefaultTask() { - init { - // Setting the outputs to always be up to date, meaning that this task will only run if the - // input changes (or if the last run was not successful). - this.outputs.upToDateWhen { true } - } - @get:Inject abstract val execOperations: ExecOperations @@ -54,6 +49,9 @@ abstract class ValidateWffFilesTask : DefaultTask() { @get:PathSensitive(PathSensitivity.RELATIVE) abstract val validatorJarPath: RegularFileProperty + @get:OutputFile + abstract val validatorOutputFile: RegularFileProperty + @get:Input abstract val wffVersion: Property @@ -62,12 +60,13 @@ abstract class ValidateWffFilesTask : DefaultTask() { val changedFiles = inputs.getFileChanges(wffFiles) changedFiles.forEach { change -> if (change.changeType != ChangeType.REMOVED) { - execOperations.javaexec { + val result = execOperations.javaexec { it.classpath = project.files(validatorJarPath) // Stop-on-fail ensures that the Gradle Task throws an exception when a WFF file fails // to validate. it.args(wffVersion.get().toString(), "--stop-on-fail", change.file.absolutePath) } + validatorOutputFile.get().asFile.writeText(result.toString()) } } } diff --git a/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/WffValidatorPlugin.kt b/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/WffValidatorPlugin.kt index d59f8995d..26564b73a 100644 --- a/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/WffValidatorPlugin.kt +++ b/WatchFaceFormat/validator-plugin/plugins/src/main/kotlin/WffValidatorPlugin.kt @@ -38,9 +38,12 @@ private const val DEFAULT_RELEASE_TAG = "release" private const val VALIDATOR_URL = "https://github.com/google/watchface/releases/download/%s/dwf-format-2-validator-1.0.jar" private const val VALIDATOR_PATH = "validator/validator-%s.jar" +private const val VALIDATOR_OUTPUT_PATH = "validator/validator-%s.txt" + private const val MEMORY_FOOTPRINT_URL = "https://github.com/google/watchface/releases/download/%s/memory-footprint.jar" private const val MEMORY_FOOTPRINT_PATH = "memory-footprint/memory-footprint-%s.jar" +private const val MEMORY_FOOTPRINT_OUTPUT_PATH = "memory-footprint/memory-footprint-%s.txt" class WffValidatorPlugin : Plugin { private lateinit var manifestPath: String @@ -52,9 +55,12 @@ class WffValidatorPlugin : Plugin { val toolReleaseTag = project.properties["release"] ?: DEFAULT_RELEASE_TAG val validatorUrl = VALIDATOR_URL.format(toolReleaseTag) - val memoryFootprintUrl = MEMORY_FOOTPRINT_URL.format(toolReleaseTag) val validatorJar = VALIDATOR_PATH.format(toolReleaseTag) + val validatorOutput = VALIDATOR_OUTPUT_PATH.format(toolReleaseTag) + + val memoryFootprintUrl = MEMORY_FOOTPRINT_URL.format(toolReleaseTag) val memoryFootprintJar = MEMORY_FOOTPRINT_PATH.format(toolReleaseTag) + val memoryFootprintOutput = MEMORY_FOOTPRINT_OUTPUT_PATH.format(toolReleaseTag) val validatorDownloadTask = project.tasks.register(DOWNLOAD_VALIDATOR_TASK) { @@ -75,7 +81,9 @@ class WffValidatorPlugin : Plugin { if (wffFileCollection.isEmpty) { throw GradleException("No WFF XML files found in project!") } + val validatorOutputPath = project.layout.buildDirectory.file(validatorOutput) validatorJarPath.set(validatorDownloadTask.get().toolJarPath) + validatorOutputFile.set(validatorOutputPath) wffFiles.setFrom(wffFileCollection) wffVersion.set(getWffVersion(manifestPath)) } @@ -108,10 +116,12 @@ class WffValidatorPlugin : Plugin { } proj.tasks.register(MEMORY_FOOTPRINT_TASK) { + val memoryFootprintOutputPath = project.layout.buildDirectory.file(memoryFootprintOutput) memoryFootprintJarPath.set(memoryFootprintDownloadTask.get().toolJarPath) apkLocation.set(apkDirectoryProvider) artifactsLoader.set(loader) wffVersion.set(getWffVersion(manifestPath)) + memoryFootprintOutputFile.set(memoryFootprintOutputPath) dependsOn(ASSEMBLE_DEBUG_TASK) } }