Skip to content

Commit

Permalink
Fix configuration cache bug when generating sources (cashapp#1424)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrodbx authored May 14, 2024
1 parent 5f04fae commit 3d94ca0
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,24 @@ public class PaparazziPlugin : Plugin<Project> {
task.targetSdkVersion.set(android.targetSdkVersion())
task.compileSdkVersion.set(android.compileSdkVersion())
task.projectResourceDirs.set(
project.provider {
val resourcesComputer = variant.mergeResourcesProvider?.get()?.resourcesComputer
val generateResValuesDirs = resourcesComputer?.generatedResOutputDir ?: project.files()
val extraGeneratedResDirs = resourcesComputer?.extraGeneratedResFolders ?: project.files()

extraGeneratedResDirs.map(projectDirectory::relativize) + localResourceDirs.relativize(projectDirectory) + generateResValuesDirs.map(projectDirectory::relativize)
run {
val resourcesComputer = variant.mergeResourcesProvider
val extraGeneratedResDirs =
resourcesComputer?.map { it.resourcesComputer.extraGeneratedResFolders }
?: project.provider { project.files() }
val generatedResOutputDirs =
resourcesComputer?.map { it.resourcesComputer.generatedResOutputDir }
?: project.provider { project.files() }
extraGeneratedResDirs
.zip(project.provider { localResourceDirs }, FileCollection::plus)
.zip(generatedResOutputDirs, FileCollection::plus)
.flatMap { it.relativize(projectDirectory) }
}
)
task.moduleResourceDirs.set(project.provider { moduleResourceDirs.relativize(projectDirectory) })
task.aarExplodedDirs.set(project.provider { aarExplodedDirs.relativize(gradleHomeDir) })
task.projectAssetDirs.set(project.provider { localAssetDirs.plus(moduleAssetDirs).relativize(projectDirectory) })
task.aarAssetDirs.set(project.provider { aarAssetDirs.relativize(gradleHomeDir) })
task.moduleResourceDirs.set(moduleResourceDirs.relativize(projectDirectory))
task.aarExplodedDirs.set(aarExplodedDirs.relativize(gradleHomeDir))
task.projectAssetDirs.set(localAssetDirs.plus(moduleAssetDirs).relativize(projectDirectory))
task.aarAssetDirs.set(aarAssetDirs.relativize(gradleHomeDir))
task.paparazziResources.set(buildDirectory.file("intermediates/paparazzi/${variant.name}/resources.json"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ package app.cash.paparazzi.gradle.utils

import org.gradle.api.file.Directory
import org.gradle.api.file.FileCollection
import org.gradle.api.provider.Provider
import java.io.File

internal fun FileCollection.relativize(directory: Directory) = files.map(directory::relativize)
internal fun FileCollection.relativize(directory: Directory): Provider<List<String>> =
elements.map { files -> files.map { file -> directory.relativize(file.asFile) } }

internal fun Directory.relativize(child: File): String {
return asFile.toPath().relativize(child.toPath()).toFile().invariantSeparatorsPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,17 @@ class PaparazziPluginTest {
.runFixture(fixtureRoot) { build() }
}

@Test
fun configurationCacheWorksWithGeneratedSources() {
val fixtureRoot = File("src/test/projects/configuration-cache-generated-sources")

// check to avoid plugin regressions that might affect Gradle's configuration caching
// https://docs.gradle.org/current/userguide/configuration_cache.html
gradleRunner
.withArguments("testDebug", "--configuration-cache", "--stacktrace")
.runFixture(fixtureRoot) { build() }
}

@Test
fun interceptViewEditMode() {
val fixtureRoot = File("src/test/projects/edit-mode-intercept")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'app.cash.paparazzi'
}

android {
namespace 'app.cash.paparazzi.plugin.test'
compileSdk libs.versions.compileSdk.get() as int
defaultConfig {
minSdk libs.versions.minSdk.get() as int
}
compileOptions {
sourceCompatibility = libs.versions.javaTarget.get()
targetCompatibility = libs.versions.javaTarget.get()
}
kotlinOptions {
jvmTarget = libs.versions.javaTarget.get()
}
}

androidComponents {
onVariants(selector().all()) { variant ->
def sourceGen = project.tasks.register("do${variant.name.capitalize()}SourceGen", SourceGenTask)
variant.sources.res.addGeneratedSourceDirectory(
sourceGen,
{ task -> task.outputDirectory }
)
}
}

abstract class SourceGenTask extends DefaultTask {
@OutputDirectory
abstract DirectoryProperty getOutputDirectory()
}

apply from: '../guava-fix.gradle'
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (C) 2020 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.paparazzi.plugin.test

import app.cash.paparazzi.Paparazzi
import org.junit.Rule
import org.junit.Test

class VerifyTest {
@get:Rule
val paparazzi = Paparazzi()

@Test
fun verify() {}
}

0 comments on commit 3d94ca0

Please sign in to comment.