mirror of
https://github.com/fankes/unmeta-gradle-plugin.git
synced 2025-09-05 18:45:17 +08:00
Introduce stats and use relative path in report
This commit is contained in:
14
README.md
14
README.md
@@ -69,6 +69,20 @@ unmeta {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Report
|
||||||
|
|
||||||
|
Generated task report is located at `$BUILD_DIR/outputs/logs/unmeta-report.txt`
|
||||||
|
|
||||||
|
```
|
||||||
|
Start dropping @DebugMetadata from kotlin classes
|
||||||
|
Removed @DebugMetadata annotation from com\example\main\MainActivity$greetings$2.class
|
||||||
|
Removed @DebugMetadata annotation from com\example\main\MainActivity$onCreate$2.class
|
||||||
|
Task finished.
|
||||||
|
Class files scanned: 7
|
||||||
|
Class files modified: 7
|
||||||
|
Execution time: 12ms.
|
||||||
|
```
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
Feel free to open a issue or submit a pull request for any bugs/improvements.
|
Feel free to open a issue or submit a pull request for any bugs/improvements.
|
||||||
|
@@ -5,7 +5,6 @@ import org.objectweb.asm.ClassVisitor
|
|||||||
import org.objectweb.asm.Opcodes
|
import org.objectweb.asm.Opcodes
|
||||||
|
|
||||||
class UnmetaClassVisitor(
|
class UnmetaClassVisitor(
|
||||||
val path: String,
|
|
||||||
classVisitor: ClassVisitor,
|
classVisitor: ClassVisitor,
|
||||||
) : ClassVisitor(Opcodes.ASM7, classVisitor), Opcodes {
|
) : ClassVisitor(Opcodes.ASM7, classVisitor), Opcodes {
|
||||||
|
|
||||||
|
@@ -7,7 +7,7 @@ import javax.inject.Inject
|
|||||||
|
|
||||||
const val DEFAULT_IS_ENABLED = true
|
const val DEFAULT_IS_ENABLED = true
|
||||||
const val DEFAULT_VERBOSE = false
|
const val DEFAULT_VERBOSE = false
|
||||||
const val DEFAULT_OUTPUT_FILE = "outputs/logs/unmeta.txt"
|
const val DEFAULT_OUTPUT_FILE = "outputs/logs/unmeta-report.txt"
|
||||||
|
|
||||||
@Suppress("UnnecessaryAbstractClass")
|
@Suppress("UnnecessaryAbstractClass")
|
||||||
abstract class UnmetaExtension @Inject constructor(project: Project) {
|
abstract class UnmetaExtension @Inject constructor(project: Project) {
|
||||||
|
@@ -31,7 +31,14 @@ abstract class UnmetaTask : DefaultTask() {
|
|||||||
@get:OutputFile
|
@get:OutputFile
|
||||||
abstract val outputFile: RegularFileProperty
|
abstract val outputFile: RegularFileProperty
|
||||||
|
|
||||||
private val fileLogger by lazy { outputFile.get().asFile }
|
private val fileLogger by lazy {
|
||||||
|
outputFile.get().asFile.apply {
|
||||||
|
writeText("", Charsets.UTF_8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var scannedFiles = 0
|
||||||
|
private var modifiedFiles = 0
|
||||||
|
|
||||||
@TaskAction
|
@TaskAction
|
||||||
fun unmetaAction() {
|
fun unmetaAction() {
|
||||||
@@ -41,12 +48,20 @@ abstract class UnmetaTask : DefaultTask() {
|
|||||||
}
|
}
|
||||||
log("Start dropping @DebugMetadata from kotlin classes")
|
log("Start dropping @DebugMetadata from kotlin classes")
|
||||||
val executionMs = measureTimeMillis {
|
val executionMs = measureTimeMillis {
|
||||||
val kotlinClassesPath = project.buildDir.absolutePath + "/tmp/kotlin-classes/${variantName.get()}"
|
val kotlinClassesBasePathName = project.buildDir.absolutePath + "/tmp/kotlin-classes/${variantName.get()}"
|
||||||
File(kotlinClassesPath).listFiles()?.forEach { file ->
|
val kotlinClassesBasePath = File(kotlinClassesBasePathName)
|
||||||
if (file.isDirectory) removeAnnotation(file)
|
kotlinClassesBasePath.listFiles()
|
||||||
}
|
?.filter { it.isDirectory }
|
||||||
|
?.forEach { directory -> removeAnnotation(directory, kotlinClassesBasePath) }
|
||||||
}
|
}
|
||||||
log("Unmeta Total Time: ${executionMs}ms")
|
log(
|
||||||
|
listOf(
|
||||||
|
"Task finished.",
|
||||||
|
"Class files scanned: $scannedFiles",
|
||||||
|
"Class files modified: $modifiedFiles",
|
||||||
|
"Execution time: ${executionMs}ms.",
|
||||||
|
).joinToString("\n"),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun log(message: String) {
|
private fun log(message: String) {
|
||||||
@@ -57,17 +72,19 @@ abstract class UnmetaTask : DefaultTask() {
|
|||||||
fileLogger.appendText(message + System.lineSeparator(), Charsets.UTF_8)
|
fileLogger.appendText(message + System.lineSeparator(), Charsets.UTF_8)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun removeAnnotation(directory: File) {
|
private fun removeAnnotation(directory: File, basePath: File) {
|
||||||
directory.walk()
|
directory.walk()
|
||||||
.filter { it.path.contains("classes") && it.path.endsWith(".class") && it.isFile }
|
.filter { it.path.contains("classes") && it.path.endsWith(".class") && it.isFile }
|
||||||
.forEach {
|
.forEach {
|
||||||
|
++scannedFiles
|
||||||
val sourceClassBytes = it.readBytes()
|
val sourceClassBytes = it.readBytes()
|
||||||
val classReader = ClassReader(sourceClassBytes)
|
val classReader = ClassReader(sourceClassBytes)
|
||||||
val classWriter = ClassWriter(classReader, ClassWriter.COMPUTE_MAXS)
|
val classWriter = ClassWriter(classReader, ClassWriter.COMPUTE_MAXS)
|
||||||
val unmetaClassVisitor = UnmetaClassVisitor(it.absolutePath, classWriter)
|
val unmetaClassVisitor = UnmetaClassVisitor(classWriter)
|
||||||
classReader.accept(unmetaClassVisitor, ClassReader.SKIP_DEBUG)
|
classReader.accept(unmetaClassVisitor, ClassReader.SKIP_DEBUG)
|
||||||
if (unmetaClassVisitor.isModified) {
|
if (unmetaClassVisitor.isModified) {
|
||||||
log("Removed @DebugMetadata annotation from ${unmetaClassVisitor.path}")
|
++modifiedFiles
|
||||||
|
log("- Removed @DebugMetadata annotation from ${it.toRelativeString(basePath)}")
|
||||||
it.writeBytes(classWriter.toByteArray())
|
it.writeBytes(classWriter.toByteArray())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user