Skip to content

Commit

Permalink
Remove variant name input and add swap DirectoryProperty inputs with …
Browse files Browse the repository at this point in the history
…String properties
  • Loading branch information
geoff-powell committed Feb 23, 2023
1 parent 5d4aefa commit 5215c6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Project> {
Expand Down Expand Up @@ -95,18 +99,24 @@ class PaparazziPlugin : Plugin<Project> {
"preparePaparazzi${variantSlug}Resources",
PrepareResourcesTask::class.java
) { task ->
fun DirectoryProperty.asRelativePathString(childDirectory: Provider<Directory>): Provider<String> =
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"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,25 +29,21 @@ import org.gradle.api.tasks.TaskAction

@CacheableTask
abstract class PrepareResourcesTask : DefaultTask() {
@get:Input
abstract val variantName: Property<String>

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

@get:InputDirectory
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val mergeResourcesOutput: DirectoryProperty
@get:Input
abstract val mergeResourcesOutput: Property<String>

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

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

@get:InputDirectory
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val mergeAssetsOutput: DirectoryProperty
@get:Input
abstract val mergeAssetsOutput: Property<String>

@get:Input
abstract val nonTransitiveRClassEnabled: Property<Boolean>
Expand All @@ -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)
Expand All @@ -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
}
}

0 comments on commit 5215c6b

Please sign in to comment.