Skip to content

Commit

Permalink
Adds output from validation tasks to enable use of Gradle caching
Browse files Browse the repository at this point in the history
  • Loading branch information
garanj committed Dec 6, 2024
1 parent 79b9e73 commit 8ffb7f8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<Int>

Expand All @@ -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",
Expand All @@ -76,5 +77,6 @@ abstract class MemoryFootprintTask() : DefaultTask() {
"--verbose"
)
}
memoryFootprintOutputFile.get().asFile.writeText(result.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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<Int>

Expand All @@ -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())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {
private lateinit var manifestPath: String
Expand All @@ -52,9 +55,12 @@ class WffValidatorPlugin : Plugin<Project> {
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<WffToolDownloadTask>(DOWNLOAD_VALIDATOR_TASK) {
Expand All @@ -75,7 +81,9 @@ class WffValidatorPlugin : Plugin<Project> {
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))
}
Expand Down Expand Up @@ -108,10 +116,12 @@ class WffValidatorPlugin : Plugin<Project> {
}

proj.tasks.register<MemoryFootprintTask>(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)
}
}
Expand Down

0 comments on commit 8ffb7f8

Please sign in to comment.