diff --git a/paparazzi/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt b/paparazzi/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt index d4c0118e74..e98bf1d935 100644 --- a/paparazzi/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt +++ b/paparazzi/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PaparazziPlugin.kt @@ -28,10 +28,13 @@ import org.gradle.api.Project import org.gradle.api.Task import org.gradle.api.artifacts.type.ArtifactTypeDefinition import org.gradle.api.attributes.Attribute +import org.gradle.api.file.Directory +import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.FileCollection import org.gradle.api.internal.artifacts.transform.UnzipTransform import org.gradle.api.logging.LogLevel.LIFECYCLE import org.gradle.api.plugins.JavaBasePlugin +import org.gradle.api.provider.Provider import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskProvider import org.gradle.api.tasks.options.Option @@ -43,6 +46,7 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper import org.jetbrains.kotlin.gradle.plugin.KotlinMultiplatformPluginWrapper import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget import java.util.Locale +import kotlin.io.path.invariantSeparatorsPathString @Suppress("unused") class PaparazziPlugin : Plugin { @@ -95,18 +99,24 @@ class PaparazziPlugin : Plugin { "preparePaparazzi${variantSlug}Resources", PrepareResourcesTask::class.java ) { task -> + fun DirectoryProperty.asRelativePathString(childDirectory: Provider): Provider = + flatMap { buildDir -> + childDirectory.map { + childDir -> buildDir.asFile.toPath().relativize(childDir.asFile.toPath()).invariantSeparatorsPathString + } + } + val android = project.extensions.getByType(BaseExtension::class.java) val nonTransitiveRClassEnabled = (project.findProperty("android.nonTransitiveRClass") as? String).toBoolean() - task.variantName.set(variant.name) task.packageName.set(android.packageName()) task.artifactFiles.from(packageAwareArtifacts.artifactFiles) task.nonTransitiveRClassEnabled.set(nonTransitiveRClassEnabled) - task.mergeResourcesOutput.set(mergeResourcesOutputDir) + task.mergeResourcesOutput.set(project.layout.buildDirectory.asRelativePathString(mergeResourcesOutputDir)) task.targetSdkVersion.set(android.targetSdkVersion()) task.compileSdkVersion.set(android.compileSdkVersion()) - task.mergeAssetsOutput.set(mergeAssetsOutputDir) + task.mergeAssetsOutput.set(project.layout.buildDirectory.asRelativePathString(mergeAssetsOutputDir)) task.paparazziResources.set(project.layout.buildDirectory.file("intermediates/paparazzi/${variant.name}/resources.txt")) } diff --git a/paparazzi/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PrepareResourcesTask.kt b/paparazzi/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PrepareResourcesTask.kt index 5f23f597d6..88e6bc0f32 100644 --- a/paparazzi/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PrepareResourcesTask.kt +++ b/paparazzi/paparazzi-gradle-plugin/src/main/java/app/cash/paparazzi/gradle/PrepareResourcesTask.kt @@ -17,13 +17,10 @@ package app.cash.paparazzi.gradle import org.gradle.api.DefaultTask import org.gradle.api.file.ConfigurableFileCollection -import org.gradle.api.file.Directory -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.InputFiles import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.PathSensitive @@ -32,15 +29,12 @@ import org.gradle.api.tasks.TaskAction @CacheableTask abstract class PrepareResourcesTask : DefaultTask() { - @get:Input - abstract val variantName: Property @get:Input abstract val packageName: Property - @get:InputDirectory - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val mergeResourcesOutput: DirectoryProperty + @get:Input + abstract val mergeResourcesOutput: Property @get:Input abstract val targetSdkVersion: Property @@ -48,9 +42,8 @@ abstract class PrepareResourcesTask : DefaultTask() { @get:Input abstract val compileSdkVersion: Property - @get:InputDirectory - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val mergeAssetsOutput: DirectoryProperty + @get:Input + abstract val mergeAssetsOutput: Property @get:Input abstract val nonTransitiveRClassEnabled: Property @@ -62,8 +55,6 @@ abstract class PrepareResourcesTask : DefaultTask() { @get:OutputFile abstract val paparazziResources: RegularFileProperty - private val buildDirectory = project.layout.buildDirectory - @TaskAction // TODO: figure out why this can't be removed as of Kotlin 1.6+ @OptIn(ExperimentalStdlibApi::class) @@ -82,27 +73,22 @@ abstract class PrepareResourcesTask : DefaultTask() { } else { mainPackage } - val projectDirectory = buildDirectory.get() out.bufferedWriter() .use { it.write(mainPackage) it.newLine() - it.write(projectDirectory.relativize(mergeResourcesOutput.get())) + it.write(mergeResourcesOutput.get()) it.newLine() it.write(targetSdkVersion.get()) it.newLine() // Use compileSdkVersion for system framework resources. it.write("platforms/android-${compileSdkVersion.get()}/") it.newLine() - it.write(projectDirectory.relativize(mergeAssetsOutput.get())) + it.write(mergeAssetsOutput.get()) it.newLine() it.write(resourcePackageNames) it.newLine() } } - - private fun Directory.relativize(child: Directory): String { - return asFile.toPath().relativize(child.asFile.toPath()).toFile().invariantSeparatorsPath - } }