From e51c93078833212aa209e1e99e3addb796022705 Mon Sep 17 00:00:00 2001 From: shinvi Date: Wed, 19 May 2021 19:46:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A2=9E=E9=87=8F=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E6=94=AF=E6=8C=81,=20=E4=BC=98=E5=8C=96gradle=20plugi?= =?UTF-8?q?n=E7=9A=84=E7=BC=96=E8=AF=91=E9=80=9F=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- component-gradle-plugin/build.gradle | 21 ++++++- .../plugin/transform/ComponentTransform.kt | 58 ++++++++++++------- ...mponentInitializatorManagerClassVisitor.kt | 41 ++++++++----- .../ComponentServiceManagerClassVisitor.kt | 23 +++----- 4 files changed, 92 insertions(+), 51 deletions(-) diff --git a/component-gradle-plugin/build.gradle b/component-gradle-plugin/build.gradle index f1d309b..26f8f9b 100644 --- a/component-gradle-plugin/build.gradle +++ b/component-gradle-plugin/build.gradle @@ -2,6 +2,7 @@ plugins { id 'java-library' id 'kotlin' id 'com.github.dcendents.android-maven' +// id 'maven' } group = 'com.github.qstumn' @@ -15,6 +16,7 @@ dependencies { implementation "com.android.tools.build:gradle:4.1.2" implementation gradleApi() implementation project(":component-annotation-compiler") +// implementation 'com.github.qstumn.ComponentHepler:component-annotation-compiler:1.1.0' } task sourceJar(type: Jar) { classifier = 'sources' @@ -25,4 +27,21 @@ artifacts { tasks.withType(JavaCompile) { options.encoding = "UTF-8" -} \ No newline at end of file +} + +//uploadArchives { +// repositories { +// mavenDeployer { +// repository(url: 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath) +// +// pom.project { +// version '1.1.0-dev5' +// artifactId 'component-gradle-plugin' +// groupId 'com.github.qstumn.dev.ComponentHepler' +// packaging = 'jar' +// description = 'component-gradle-plugin' +// } +// } +// +// } +//} \ No newline at end of file diff --git a/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/transform/ComponentTransform.kt b/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/transform/ComponentTransform.kt index 92ec6d3..86fbcac 100644 --- a/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/transform/ComponentTransform.kt +++ b/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/transform/ComponentTransform.kt @@ -1,9 +1,6 @@ package q.rorbin.component.gradle.plugin.transform -import com.android.build.api.transform.Format -import com.android.build.api.transform.QualifiedContent -import com.android.build.api.transform.Transform -import com.android.build.api.transform.TransformInvocation +import com.android.build.api.transform.* import com.android.utils.FileUtils import org.apache.commons.codec.digest.DigestUtils import q.rorbin.component.ComponentInitializatorManager @@ -28,11 +25,12 @@ class ComponentTransform : Transform() { QualifiedContent.Scope.EXTERNAL_LIBRARIES ) - override fun isIncremental(): Boolean = false + override fun isIncremental(): Boolean = true override fun transform(transformInvocation: TransformInvocation?) { super.transform(transformInvocation) transformInvocation ?: return + val serviceHelpers = mutableListOf() val initializatorHelpers = mutableListOf() var serviceHelperManager: File? = null @@ -40,12 +38,25 @@ class ComponentTransform : Transform() { val inputs = transformInvocation.inputs val outputProvider = transformInvocation.outputProvider + + if (!transformInvocation.isIncremental) { + outputProvider.deleteAll() + } + val servicePackage = "${ComponentService::class.java.`package`.name}." val initializatorPackage = "${ComponentInitializator::class.java.`package`.name}." + inputs.forEach { input -> //遍历所有的文件夹收集serviceHelpers和initializatorHelpers input.directoryInputs.forEach { dirInput -> - dirInput.file.traverse( + val inputDir = dirInput.file + val destDir = outputProvider.getContentLocation( + dirInput.name, + dirInput.contentTypes, + dirInput.scopes, + Format.DIRECTORY + ) + inputDir.traverse( { file -> file.extension == "class" }, FileType.FILE ) { file -> @@ -57,23 +68,23 @@ class ComponentTransform : Transform() { initializatorHelpers.add("$initializatorPackage$name") } } - val dest = outputProvider.getContentLocation( - dirInput.name, - dirInput.contentTypes, - dirInput.scopes, - Format.DIRECTORY - ) - FileUtils.copyDirectory(dirInput.file, dest) + if (transformInvocation.isIncremental) { + for ((file, status) in dirInput.changedFiles) { + val destFile = + File(destDir, FileUtils.relativePossiblyNonExistingPath(file, inputDir)) + when (status) { + Status.ADDED, Status.CHANGED -> FileUtils.copyFile(file, destFile) + Status.REMOVED -> FileUtils.delete(destFile) + } + } + } else { + FileUtils.copyDirectory(inputDir, destDir) + } } //遍历所有的jar文件收集serviceHelpers和initializatorHelpers, 并记录当前serviceHelperManager或initializatorHelperManager是否在这个jar中 input.jarInputs.forEach { jarInput -> - var jarName = jarInput.name - val md5 = DigestUtils.md5Hex(jarInput.file.absolutePath) - if (jarName.endsWith(".jar")) { - jarName = jarName.substring(0, jarName.length - 4) - } val dest = outputProvider.getContentLocation( - jarName + md5, + jarInput.name, jarInput.contentTypes, jarInput.scopes, Format.JAR @@ -103,7 +114,14 @@ class ComponentTransform : Transform() { } } } - FileUtils.copyFile(jarInput.file, dest) + if (transformInvocation.isIncremental) { + when (jarInput.status) { + Status.ADDED, Status.CHANGED -> FileUtils.copyFile(jarInput.file, dest) + Status.REMOVED -> FileUtils.delete(dest) + } + } else { + FileUtils.copyFile(jarInput.file, dest) + } } } println("serviceHelpers gather result : $serviceHelpers, serviceHelperManager : $serviceHelperManager") diff --git a/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/visitor/ComponentInitializatorManagerClassVisitor.kt b/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/visitor/ComponentInitializatorManagerClassVisitor.kt index 7cf692d..5df6052 100644 --- a/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/visitor/ComponentInitializatorManagerClassVisitor.kt +++ b/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/visitor/ComponentInitializatorManagerClassVisitor.kt @@ -7,33 +7,42 @@ import q.rorbin.component.ComponentInitializatorManager /** * @author changhai.qiu */ -class ComponentInitializatorManagerClassVisitor(private val classVisitor: ClassVisitor, private val helpers: MutableList) : ClassVisitor(Opcodes.ASM5, classVisitor) { +class ComponentInitializatorManagerClassVisitor( + private val classVisitor: ClassVisitor, + private val helpers: MutableList +) : ClassVisitor(Opcodes.ASM5, classVisitor) { override fun visitMethod( - access: Int, - name: String, - descriptor: String, - signature: String?, - exceptions: Array? + access: Int, + name: String, + descriptor: String, + signature: String?, + exceptions: Array? ): MethodVisitor { - var methodVisitor = classVisitor.visitMethod(access, name, descriptor, signature, exceptions) + var methodVisitor = + classVisitor.visitMethod(access, name, descriptor, signature, exceptions) if (name == "addAllInitializator") { - methodVisitor = AddAllInitializatorMethodAdapter(methodVisitor, access, name, descriptor) + methodVisitor = AddAllInitializatorMethodAdapter(methodVisitor) } return methodVisitor } - private inner class AddAllInitializatorMethodAdapter(mv: MethodVisitor, access: Int, private val name: String?, desc: String?) - : AdviceAdapter(Opcodes.ASM5, mv, access, name, desc) { + private inner class AddAllInitializatorMethodAdapter(val visitor: MethodVisitor) : + MethodVisitor(Opcodes.ASM5) { - override fun onMethodEnter() { - super.onMethodEnter() + override fun visitCode() { for (helper in helpers) { - mv.visitVarInsn(Opcodes.ALOAD, 0) - mv.visitLdcInsn(helper) - mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(ComponentInitializatorManager::class.java), - "addInitializator", "(Ljava/lang/String;)V", false) + visitor.visitVarInsn(Opcodes.ALOAD, 0) + visitor.visitLdcInsn(helper) + visitor.visitMethodInsn( + Opcodes.INVOKESPECIAL, + Type.getInternalName(ComponentInitializatorManager::class.java), + "addInitializator", + "(Ljava/lang/String;)V", + false + ) } + visitor.visitInsn(Opcodes.RETURN) } } } diff --git a/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/visitor/ComponentServiceManagerClassVisitor.kt b/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/visitor/ComponentServiceManagerClassVisitor.kt index eabd90a..d92a394 100644 --- a/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/visitor/ComponentServiceManagerClassVisitor.kt +++ b/component-gradle-plugin/src/main/java/q/rorbin/component/gradle/plugin/visitor/ComponentServiceManagerClassVisitor.kt @@ -18,34 +18,29 @@ class ComponentServiceManagerClassVisitor( descriptor: String, signature: String?, exceptions: Array? - ): MethodVisitor { + ): MethodVisitor? { var methodVisitor = classVisitor.visitMethod(access, name, descriptor, signature, exceptions) if (name == "initServices") { - methodVisitor = InitServicesMethodAdapter(methodVisitor, access, name, descriptor) + methodVisitor = InitServicesMethodAdapter(methodVisitor) } return methodVisitor } - private inner class InitServicesMethodAdapter( - mv: MethodVisitor, - access: Int, - name: String?, - desc: String? - ) : AdviceAdapter(Opcodes.ASM5, mv, access, name, desc) { - - override fun onMethodEnter() { - super.onMethodEnter() + private inner class InitServicesMethodAdapter(val visitor: MethodVisitor) : + MethodVisitor(Opcodes.ASM5) { + override fun visitCode() { for (helper in helpers) { - mv.visitVarInsn(Opcodes.ALOAD, 0) - mv.visitLdcInsn(helper) - mv.visitMethodInsn( + visitor.visitVarInsn(Opcodes.ALOAD, 0) + visitor.visitLdcInsn(helper) + visitor.visitMethodInsn( Opcodes.INVOKESPECIAL, Type.getInternalName( ComponentServiceManager::class.java ), "initServiceByHelper", "(Ljava/lang/String;)V", false ) } + visitor.visitInsn(Opcodes.RETURN) } } }