-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PreviewTransform to aggregate methods
- Loading branch information
1 parent
f71e77c
commit b6629e4
Showing
7 changed files
with
526 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...n/com/emergetools/android/gradle/tasks/snapshots/transform/AggregatePreviewMethodsTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.emergetools.android.gradle.tasks.snapshots.transform | ||
|
||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.decodeFromStream | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.ConfigurableFileCollection | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.tasks.InputFiles | ||
import org.gradle.api.tasks.OutputFile | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
abstract class AggregatePreviewMethodsTask : DefaultTask() { | ||
@get:InputFiles | ||
abstract val inputFiles: ConfigurableFileCollection | ||
|
||
@get:OutputFile | ||
abstract val outputFile: RegularFileProperty | ||
|
||
@TaskAction | ||
fun aggregate() { | ||
val output = outputFile.get().asFile | ||
output.parentFile.mkdirs() | ||
|
||
output.writeText("") | ||
|
||
// Aggregate all preview method files | ||
val list = inputFiles.flatMap { Json.decodeFromStream<List<ComposePreviewSnapshotConfig>>(it.inputStream()) } | ||
output.writeText(Json.encodeToString(list)) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../com/emergetools/android/gradle/tasks/snapshots/transform/ComposePreviewSnapshotConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.emergetools.android.gradle.tasks.snapshots.transform | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class ComposePreviewSnapshotConfig( | ||
val fullyQualifiedClassName: String? = null, | ||
val originalFqn: String, | ||
val sourceFileName: String? = null, | ||
var isAppStoreSnapshot: Boolean? = null, | ||
val previewParameter: PreviewParameter? = null, | ||
var name: String? = null, | ||
var group: String? = null, | ||
var uiMode: Int? = null, | ||
var locale: String? = null, | ||
var fontScale: Float? = null, | ||
var heightDp: Int? = null, | ||
var widthDp: Int? = null, | ||
var showBackground: Boolean? = null, | ||
var backgroundColor: Int? = null, | ||
var showSystemUi: Boolean? = null, | ||
var device: String? = null, | ||
var apiLevel: Int? = null, | ||
var wallpaper: Int? = null, | ||
) | ||
|
||
@Serializable | ||
data class PreviewParameter( | ||
val parameterName: String, | ||
val providerClassFqn: String, | ||
val limit: Int? = null, | ||
val index: Int? = null | ||
) | ||
|
||
fun String.cleanName(): String { | ||
var newName = this.replace("/", ".") | ||
// Strip .class suffix | ||
if (newName.endsWith(".class")) { | ||
newName = newName.substring(0, newName.length - 6) | ||
} | ||
return newName | ||
} | ||
|
||
fun String.removeClassName(): String { | ||
return this.substringBeforeLast(".") | ||
} |
89 changes: 89 additions & 0 deletions
89
...tlin/com/emergetools/android/gradle/tasks/snapshots/transform/PreviewAnalyzerTransform.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.emergetools.android.gradle.tasks.snapshots.transform | ||
|
||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
import org.gradle.api.artifacts.transform.InputArtifact | ||
import org.gradle.api.artifacts.transform.TransformAction | ||
import org.gradle.api.artifacts.transform.TransformOutputs | ||
import org.gradle.api.artifacts.transform.TransformParameters | ||
import org.gradle.api.file.FileSystemLocation | ||
import org.gradle.api.provider.Provider | ||
import org.objectweb.asm.ClassReader | ||
import org.objectweb.asm.Opcodes | ||
import org.objectweb.asm.tree.AnnotationNode | ||
import org.objectweb.asm.tree.ClassNode | ||
import java.io.File | ||
import java.util.jar.JarFile | ||
|
||
abstract class PreviewAnalyzerTransform : TransformAction<TransformParameters.None> { | ||
@get:InputArtifact | ||
abstract val inputArtifact: Provider<FileSystemLocation> | ||
|
||
override fun transform(outputs: TransformOutputs) { | ||
val input = inputArtifact.get().asFile | ||
if (!input.name.endsWith(".jar") && !input.name.endsWith(".aar") && !input.isDirectory) { | ||
return | ||
} | ||
|
||
val outputFile = outputs.file("output.txt") | ||
|
||
val previewMethods = when { | ||
input.isDirectory -> findMethodsInDirectory(input) | ||
input.name.endsWith(".jar") -> analyzeJarFile(input) | ||
input.name.endsWith(".aar") -> analyzeAarFile(input) | ||
else -> throw IllegalArgumentException("Unsupported input type: $input") | ||
} | ||
|
||
val json = Json.encodeToString(previewMethods.toList()) | ||
|
||
outputFile.writeText(json) | ||
} | ||
|
||
private fun analyzeJarFile(inputJar: File): Sequence<ComposePreviewSnapshotConfig> { | ||
val methods = mutableListOf<ComposePreviewSnapshotConfig>() | ||
JarFile(inputJar).use { jarFile -> | ||
jarFile.entries().asSequence() | ||
.filter { it.name.endsWith(".class") } | ||
.forEach { classEntry -> | ||
jarFile.getInputStream(classEntry).use { inputStream -> | ||
methods.addAll(extractPreviewMethodsFromBytes(classEntry.realName , inputStream.readBytes())) | ||
} | ||
} | ||
} | ||
return methods.asSequence() | ||
} | ||
|
||
private fun findMethodsInDirectory(directory: File) : Sequence<ComposePreviewSnapshotConfig> { | ||
return directory.findPreviewMethodsInDirectory() | ||
} | ||
|
||
private fun analyzeAarFile(aarFile: File) : Sequence<ComposePreviewSnapshotConfig> { | ||
TODO("analzying aar file not yet implemented") | ||
// Implementation of AAR analysis using ZipFile to avoid copying to temp dir | ||
// This is more configuration cache friendly than using Project.zipTree | ||
} | ||
|
||
|
||
|
||
companion object { | ||
fun File.findPreviewMethodsInDirectory(): Sequence<ComposePreviewSnapshotConfig> { | ||
return walk() | ||
.filter { it.name.endsWith(".class") } | ||
.flatMap { classFile -> | ||
extractPreviewMethodsFromBytes(classFile.name, classFile.readBytes()) | ||
} | ||
} | ||
|
||
fun extractPreviewMethodsFromBytes(fileName: String, byteStream: ByteArray): List<ComposePreviewSnapshotConfig> { | ||
val classReader = ClassReader(byteStream) | ||
val methodNames = mutableListOf<ComposePreviewSnapshotConfig>() | ||
|
||
val visitor = SnapshotAggregatorClassVisitor(Opcodes.ASM9, fileName, classReader.className, methodNames) | ||
|
||
classReader.accept(visitor, ClassReader.EXPAND_FRAMES) | ||
|
||
return methodNames | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.