Skip to content

Commit

Permalink
Rewrite assets loading mechanism (#995)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrodbx authored Jul 17, 2023
1 parent 1907730 commit 09a33c0
Show file tree
Hide file tree
Showing 31 changed files with 211 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ class PaparazziPlugin : Plugin<Project> {
.artifactsFor(ArtifactType.ANDROID_RES.type) { it !is ProjectComponentIdentifier }
.artifactFiles

val localAssetDirs = project
.files(variant.sourceSets.flatMap { it.assetsDirectories })

// https://android.googlesource.com/platform/tools/base/+/96015063acd3455a76cdf1cc71b23b0828c0907f/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/MergeResources.kt#875

val moduleAssetDirs = variant.runtimeConfiguration
.artifactsFor(ArtifactType.ASSETS.type) { it is ProjectComponentIdentifier }
.artifactFiles

val aarAssetDirs = variant.runtimeConfiguration
.artifactsFor(ArtifactType.ASSETS.type) { it !is ProjectComponentIdentifier }
.artifactFiles

val packageAwareArtifactFiles = variant.runtimeConfiguration
.artifactsFor(ArtifactType.SYMBOL_LIST_WITH_PACKAGE_NAME.type)
.artifactFiles
Expand All @@ -161,6 +174,8 @@ class PaparazziPlugin : Plugin<Project> {
task.projectResourceDirs.from(localResourceDirs)
task.moduleResourceDirs.from(moduleResourceDirs)
task.aarExplodedDirs.from(aarExplodedDirs)
task.projectAssetDirs.from(localAssetDirs.plus(moduleAssetDirs))
task.aarAssetDirs.from(aarAssetDirs)
task.paparazziResources.set(buildDirectory.file("intermediates/paparazzi/${variant.name}/resources.txt"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ abstract class PrepareResourcesTask : DefaultTask() {
@get:PathSensitive(PathSensitivity.NONE)
abstract val aarExplodedDirs: ConfigurableFileCollection

@get:InputFiles
@get:PathSensitive(PathSensitivity.NONE)
abstract val projectAssetDirs: ConfigurableFileCollection

@get:InputFiles
@get:PathSensitive(PathSensitivity.NONE)
abstract val aarAssetDirs: ConfigurableFileCollection

@get:Input
abstract val mergeAssetsOutputDir: Property<String>

Expand Down Expand Up @@ -111,6 +119,10 @@ abstract class PrepareResourcesTask : DefaultTask() {
it.newLine()
it.write(aarExplodedDirs.joinFiles(gradleUserHomeDirectory))
it.newLine()
it.write(projectAssetDirs.joinFiles(projectDirectory))
it.newLine()
it.write(aarAssetDirs.joinFiles(gradleUserHomeDirectory))
it.newLine()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -874,17 +874,17 @@ class PaparazziPluginTest {
}

@Test
fun verifyOpenAssets() {
val fixtureRoot = File("src/test/projects/open-assets")
fun verifyOpenAssetsLegacyAssetLoadingIsOff() {
val fixtureRoot = File("src/test/projects/open-assets-legacy-asset-loading-off")

gradleRunner
.withArguments("testDebug", "--stacktrace")
.withArguments("consumer:testDebug", "--stacktrace")
.runFixture(fixtureRoot) { build() }
}

@Test
fun openTransitiveAssets() {
val fixtureRoot = File("src/test/projects/open-transitive-assets")
fun verifyOpenAssetsLegacyAssetLoadingIsOn() {
val fixtureRoot = File("src/test/projects/open-assets-legacy-asset-loading-on")

gradleRunner
.withArguments("consumer:testDebug", "--stacktrace")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

android {
namespace 'app.cash.paparazzi.plugin.test'
namespace 'app.cash.paparazzi.plugin.test.consumer'
compileSdk libs.versions.compileSdk.get() as int
defaultConfig {
minSdk libs.versions.minSdk.get() as int
Expand All @@ -20,6 +20,8 @@ android {
}

dependencies {
implementation project(':producer')
implementation files('libs/external.aar')
implementation project(':producer1')
implementation project(':producer2')
testImplementation libs.truth
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
consumer
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app.cash.paparazzi.plugin.test

import app.cash.paparazzi.Paparazzi
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test

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

@Test
fun testViews() {
val pairs = mapOf(
"consumer/secret.txt" to "consumer",
"producer1/secret.txt" to "producer1",
"producer2/secret.txt" to "producer2",
"external/secret.txt" to "external"
)

pairs.forEach { (key, value) ->
val contents =
paparazzi.context.assets.open(key).bufferedReader().use { it.readText() }
assertThat(contents).isEqualTo(value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

android {
namespace 'app.cash.paparazzi.plugin.test.module'
namespace 'app.cash.paparazzi.plugin.test.producer1'
compileSdk libs.versions.compileSdk.get() as int
defaultConfig {
minSdk libs.versions.minSdk.get() as int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
producer1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
plugins {
id 'com.android.library'
id 'app.cash.paparazzi'
id 'kotlin-android'
}

android {
namespace 'app.cash.paparazzi.plugin.test'
namespace 'app.cash.paparazzi.plugin.test.producer2'
compileSdk libs.versions.compileSdk.get() as int
defaultConfig {
minSdk libs.versions.minSdk.get() as int
Expand All @@ -13,4 +13,7 @@ android {
sourceCompatibility = libs.versions.javaTarget.get()
targetCompatibility = libs.versions.javaTarget.get()
}
kotlinOptions {
jvmTarget = libs.versions.javaTarget.get()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
producer2
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply from: '../test.settings.gradle'

include ':consumer'
include ':producer'
include ':producer1'
include ':producer2'
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'app.cash.paparazzi'
}

android {
namespace 'app.cash.paparazzi.plugin.test.consumer'
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()
}
}

dependencies {
implementation files('libs/external.aar')
implementation project(':producer1')
implementation project(':producer2')
testImplementation libs.truth
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
consumer
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app.cash.paparazzi.plugin.test

import app.cash.paparazzi.Paparazzi
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test

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

@Test
fun testViews() {
val pairs = mapOf(
"consumer/secret.txt" to "consumer",
"producer1/secret.txt" to "producer1",
"producer2/secret.txt" to "producer2",
"external/secret.txt" to "external"
)

pairs.forEach { (key, value) ->
val contents =
paparazzi.context.assets.open(key).bufferedReader().use { it.readText() }
assertThat(contents).isEqualTo(value)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
android.useAndroidX=true
app.cash.paparazzi.legacy.asset.loading=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
}

android {
namespace 'app.cash.paparazzi.plugin.test.producer1'
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()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
producer1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
}

android {
namespace 'app.cash.paparazzi.plugin.test.producer2'
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()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
producer2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apply from: '../test.settings.gradle'

include ':consumer'
include ':producer1'
include ':producer2'

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions paparazzi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ def generateTestConfig = tasks.register("generateTestConfig") {
writer.writeLine("")
writer.writeLine("")
writer.writeLine("")
writer.writeLine("")
writer.writeLine("")
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions paparazzi/src/main/java/app/cash/paparazzi/Environment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ data class Environment(
val resourcePackageNames: List<String>,
val localResourceDirs: List<String>,
val moduleResourceDirs: List<String>,
val libraryResourceDirs: List<String>
val libraryResourceDirs: List<String>,
val allModuleAssetDirs: List<String>,
val libraryAssetDirs: List<String>
) {
init {
val platformDirPath = Path.of(platformDir)
Expand Down Expand Up @@ -70,7 +72,9 @@ fun detectEnvironment(): Environment {
resourcePackageNames = configLines[5].split(),
localResourceDirs = configLines[6].split().map { projectDir.resolve(it).toString() },
moduleResourceDirs = configLines[7].split().map { projectDir.resolve(it).toString() },
libraryResourceDirs = configLines[8].split().map { artifactsCacheDir.resolve(it).toString() }
libraryResourceDirs = configLines[8].split().map { artifactsCacheDir.resolve(it).toString() },
allModuleAssetDirs = configLines[9].split().map { projectDir.resolve(it).toString() },
libraryAssetDirs = configLines[10].split().map { artifactsCacheDir.resolve(it).toString() }
)
}

Expand Down
1 change: 1 addition & 0 deletions paparazzi/src/main/java/app/cash/paparazzi/Flags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package app.cash.paparazzi
object Flags {
const val DEBUG_LINKED_OBJECTS = "app.cash.paparazzi.debug.linked.objects"
const val LEGACY_RESOURCE_LOADING = "app.cash.paparazzi.legacy.resource.loading"
const val LEGACY_ASSET_LOADING = "app.cash.paparazzi.legacy.asset.loading"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream

internal class PaparazziAssetRepository(private val assetPath: String) : AssetRepository() {
internal class PaparazziAssetRepository(
private val assetPath: String,
private val assetDirs: List<String> = emptyList()
) : AssetRepository() {
@Throws(FileNotFoundException::class)
private fun open(path: String): InputStream? {
val asset = File(path)
Expand All @@ -39,7 +42,19 @@ internal class PaparazziAssetRepository(private val assetPath: String) : AssetRe
override fun openAsset(
path: String,
mode: Int
): InputStream? = open("$assetPath/$path")
): InputStream? {
if (assetDirs.isEmpty()) {
return open("$assetPath/$path")
} else {
for (assetDir in assetDirs) {
val assetFile = open("$assetDir/$path")
if (assetFile != null) {
return assetFile
}
}
return null
}
}

@Throws(IOException::class)
override fun openNonAsset(
Expand Down
Loading

0 comments on commit 09a33c0

Please sign in to comment.