generated from cortinico/kotlin-gradle-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from izhangzhihao/izhangzhihao-patch-1
- Loading branch information
Showing
10 changed files
with
133 additions
and
6 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
9 changes: 9 additions & 0 deletions
9
example/bin/main/com/github/izhangzhihao/unmeta/example/SampleCode.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,9 @@ | ||
package com.github.izhangzhihao.unmeta.example | ||
|
||
/** | ||
* a tail-recursive function with keyword tailrec | ||
*/ | ||
tailrec fun factorial(n: Long, a: Long = 1): Long { | ||
return if (n == 1L) a | ||
else factorial(n - 1, n * a) | ||
} |
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
35 changes: 35 additions & 0 deletions
35
plugin-build/plugin/bin/main/io/github/izhangzhihao/unmeta/UnmetaClassVisitor.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,35 @@ | ||
package io.github.izhangzhihao.unmeta | ||
|
||
import org.gradle.api.logging.Logger | ||
import org.objectweb.asm.AnnotationVisitor | ||
import org.objectweb.asm.ClassVisitor | ||
import org.objectweb.asm.Opcodes | ||
|
||
class UnmetaClassVisitor(private val path: String, cv: ClassVisitor, private val logger: Logger) : | ||
ClassVisitor(Opcodes.ASM9, cv), Opcodes { | ||
|
||
var modified = false | ||
|
||
override fun visitAnnotation(desc: String?, visible: Boolean): AnnotationVisitor? { | ||
return when (desc) { | ||
"Lkotlin/Metadata;" -> { | ||
logger.debug("Removed @Metadata annotation from $path") | ||
modified = true | ||
null | ||
} | ||
"Lkotlin/coroutines/jvm/internal/DebugMetadata;" -> { | ||
logger.debug("Removed @DebugMetadata annotation from $path") | ||
modified = true | ||
null | ||
} | ||
"Lkotlin/jvm/internal/SourceDebugExtension;" -> { | ||
logger.debug("Removed @SourceDebugExtension annotation from $path") | ||
modified = true | ||
null | ||
} | ||
else -> { | ||
super.visitAnnotation(desc, visible) | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
plugin-build/plugin/bin/main/io/github/izhangzhihao/unmeta/UnmetaExtension.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,11 @@ | ||
package io.github.izhangzhihao.unmeta | ||
|
||
import org.gradle.api.Project | ||
import org.gradle.api.provider.Property | ||
import javax.inject.Inject | ||
|
||
open class UnmetaExtension @Inject constructor(project: Project) { | ||
private val objects = project.objects | ||
|
||
val enable: Property<Boolean> = objects.property(Boolean::class.java) | ||
} |
16 changes: 16 additions & 0 deletions
16
plugin-build/plugin/bin/main/io/github/izhangzhihao/unmeta/UnmetaPlugin.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,16 @@ | ||
package io.github.izhangzhihao.unmeta | ||
|
||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
|
||
class UnmetaPlugin : Plugin<Project> { | ||
|
||
private lateinit var extension: UnmetaExtension | ||
|
||
override fun apply(project: Project) { | ||
extension = project.extensions.create("unmeta", UnmetaExtension::class.java, project) | ||
val unmetaTaskTask = project.tasks.create("unmeta", UnmetaTask::class.java) | ||
unmetaTaskTask.enable.set(extension.enable) | ||
project.tasks.getByName("compileKotlin").finalizedBy(unmetaTaskTask) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
plugin-build/plugin/bin/main/io/github/izhangzhihao/unmeta/UnmetaTask.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,56 @@ | ||
package io.github.izhangzhihao.unmeta | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.Optional | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.tasks.options.Option | ||
import org.objectweb.asm.ClassReader | ||
import org.objectweb.asm.ClassWriter | ||
import java.io.File | ||
|
||
abstract class UnmetaTask : DefaultTask() { | ||
|
||
init { | ||
description = "Drop @Metadata & @DebugMetadata from kotlin classes" | ||
group = "build" | ||
} | ||
|
||
@get:Input | ||
@get:Option(option = "enable", description = "is unmeta enabled") | ||
@get:Optional | ||
abstract val enable: Property<Boolean> | ||
|
||
@TaskAction | ||
fun unmetaAction() { | ||
if (!enable.get()) { | ||
logger.warn("unmeta is disabled") | ||
} else { | ||
logger.info("Start drop @Metadata & @DebugMetadata from kotlin classes") | ||
project.buildDir.listFiles() | ||
?.forEach { file -> | ||
if (file.isDirectory) { | ||
dropMetadata(file) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun dropMetadata(file: File) { | ||
file.walk() | ||
.filter { it.path.contains("classes") && it.path.endsWith(".class") } | ||
.forEach { | ||
if (it.isFile) { | ||
val sourceClassBytes = it.readBytes() | ||
val classReader = ClassReader(sourceClassBytes) | ||
val classWriter = ClassWriter(classReader, ClassWriter.COMPUTE_MAXS) | ||
val unmetaClassVisitor = UnmetaClassVisitor(it.absolutePath, classWriter, logger) | ||
classReader.accept(unmetaClassVisitor, ClassReader.SKIP_DEBUG) | ||
if (unmetaClassVisitor.modified) { | ||
it.writeBytes(classWriter.toByteArray()) | ||
} | ||
} | ||
} | ||
} | ||
} |
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