Skip to content

Commit

Permalink
Add deploy shell and rename plugin name.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhi1ong committed Dec 18, 2017
1 parent e9c414c commit 10bed9d
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 99 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'com.alibaba.arouter'

android {
compileSdkVersion Integer.parseInt(COMPILE_SDK_VERSION)
Expand Down
15 changes: 0 additions & 15 deletions arouter-auto-register/build.gradle

This file was deleted.

This file was deleted.

This file was deleted.

21 changes: 21 additions & 0 deletions arouter-register/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apply plugin: 'groovy'

ext {
bintrayName = 'arouter-register'
artifact = bintrayName
libraryName = 'ARouter register gradle plugin'
libraryDescription = 'A gradle plugin used for arouter route map register'
libraryVersion = arouter_register_version
}

dependencies {
compile gradleApi()
compile localGroovy()
}

dependencies {
compile 'com.android.tools.build:gradle:2.1.3'
}

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/bintrayv1.gradle'
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.billy.android.register
package com.alibaba.android.arouter.register.core

import com.alibaba.android.arouter.register.utils.Logger
import com.alibaba.android.arouter.register.utils.ScanSetting
import org.apache.commons.io.IOUtils
import org.objectweb.asm.*

Expand All @@ -11,17 +13,17 @@ import java.util.zip.ZipEntry
* generate register code into LogisticsCenter.class
* @author billy.qi email: [email protected]
*/
class RouterRegisterCodeGenerator {
RouterRegisterSetting extension
class RegisterCodeGenerator {
ScanSetting extension

private RouterRegisterCodeGenerator(RouterRegisterSetting extension) {
private RegisterCodeGenerator(ScanSetting extension) {
this.extension = extension
}

static void insertInitCodeTo(RouterRegisterSetting registerSetting) {
static void insertInitCodeTo(ScanSetting registerSetting) {
if (registerSetting != null && !registerSetting.classList.isEmpty()) {
RouterRegisterCodeGenerator processor = new RouterRegisterCodeGenerator(registerSetting)
File file = RouterRegisterTransform.fileContainsInitClass
RegisterCodeGenerator processor = new RegisterCodeGenerator(registerSetting)
File file = RegisterTransform.fileContainsInitClass
if (file.getName().endsWith('.jar'))
processor.insertInitCodeIntoJarFile(file)
}
Expand All @@ -47,8 +49,10 @@ class RouterRegisterCodeGenerator {
ZipEntry zipEntry = new ZipEntry(entryName)
InputStream inputStream = file.getInputStream(jarEntry)
jarOutputStream.putNextEntry(zipEntry)
if (RouterRegisterSetting.GENERATE_TO_CLASS_FILE_NAME == entryName) {
println('codeInsertToClassName:' + entryName)
if (ScanSetting.GENERATE_TO_CLASS_FILE_NAME == entryName) {

Logger.i('Insert init code to class >> ' + entryName)

def bytes = referHackWhenInit(inputStream)
jarOutputStream.write(bytes)
} else {
Expand Down Expand Up @@ -92,16 +96,16 @@ class RouterRegisterCodeGenerator {
String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions)
//generate code into this method
if (name == RouterRegisterSetting.GENERATE_TO_METHOD_NAME) {
mv = new MyMethodVisitor(Opcodes.ASM5, mv)
if (name == ScanSetting.GENERATE_TO_METHOD_NAME) {
mv = new RouteMethodVisitor(Opcodes.ASM5, mv)
}
return mv
}
}

class MyMethodVisitor extends MethodVisitor {
class RouteMethodVisitor extends MethodVisitor {

MyMethodVisitor(int api, MethodVisitor mv) {
RouteMethodVisitor(int api, MethodVisitor mv) {
super(api, mv)
}

Expand All @@ -116,7 +120,7 @@ class RouterRegisterCodeGenerator {
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, name, "<init>", "()V", false)
// generate invoke register method into LogisticsCenter.loadRouterMap()
mv.visitMethodInsn(Opcodes.INVOKESTATIC
, RouterRegisterSetting.GENERATE_TO_CLASS_NAME
, ScanSetting.GENERATE_TO_CLASS_NAME
, extension.registerMethodName
, "(L${extension.interfaceName};)V"
, false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.billy.android.register
package com.alibaba.android.arouter.register.core

import com.alibaba.android.arouter.register.utils.Logger
import com.alibaba.android.arouter.register.utils.ScanSetting
import com.alibaba.android.arouter.register.utils.ScanUtil
import com.android.build.api.transform.*
import com.android.build.gradle.internal.pipeline.TransformManager
import org.apache.commons.codec.digest.DigestUtils
Expand All @@ -10,17 +13,17 @@ import org.gradle.api.Project
* transform api
* <p>
* 1. Scan all classes to find which classes implement the specified interface
* 2. Generate register code into class file: {@link RouterRegisterSetting#GENERATE_TO_CLASS_FILE_NAME}
* 2. Generate register code into class file: {@link ScanSetting#GENERATE_TO_CLASS_FILE_NAME}
* @author billy.qi email: [email protected]
* @since 17/3/21 11:48
*/
class RouterRegisterTransform extends Transform {
class RegisterTransform extends Transform {

Project project
static ArrayList<RouterRegisterSetting> registerList
static ArrayList<ScanSetting> registerList
static File fileContainsInitClass;

RouterRegisterTransform(Project project) {
RegisterTransform(Project project) {
this.project = project
}

Expand All @@ -30,7 +33,7 @@ class RouterRegisterTransform extends Transform {
*/
@Override
String getName() {
return "arouter-auto-register"
return ScanSetting.PLUGIN_NAME
}

@Override
Expand Down Expand Up @@ -58,8 +61,11 @@ class RouterRegisterTransform extends Transform {
, Collection<TransformInput> referencedInputs
, TransformOutputProvider outputProvider
, boolean isIncremental) throws IOException, TransformException, InterruptedException {
println("\n\n============================ start arouter-auto-register ============================\n")
long time = System.currentTimeMillis()

Logger.i('Start scan register info in jar file.')

long startTime = System.currentTimeMillis()

inputs.each { TransformInput input ->

// scan all jars
Expand All @@ -76,8 +82,8 @@ class RouterRegisterTransform extends Transform {
File dest = outputProvider.getContentLocation(destName + "_" + hexName, jarInput.contentTypes, jarInput.scopes, Format.JAR)

//scan jar file to find classes
if (RouterScanUtil.shouldProcessPreDexJar(src.absolutePath)) {
RouterScanUtil.scanJar(src, dest)
if (ScanUtil.shouldProcessPreDexJar(src.absolutePath)) {
ScanUtil.scanJar(src, dest)
}
FileUtils.copyFile(src, dest)

Expand All @@ -90,35 +96,33 @@ class RouterRegisterTransform extends Transform {
root += File.separator
directoryInput.file.eachFileRecurse { File file ->
def path = file.absolutePath.replace(root, '')
if(file.isFile() && RouterScanUtil.shouldProcessClass(path)){
RouterScanUtil.scanClass(file)
if(file.isFile() && ScanUtil.shouldProcessClass(path)){
ScanUtil.scanClass(file)
}
}

// copy to dest
FileUtils.copyDirectory(directoryInput.file, dest)
}
}
def scanFinishTime = System.currentTimeMillis()
println(">>>>>> register scan all class cost time: " + (scanFinishTime - time) + " ms")

Logger.i('Scan finish, current cost time ' + (System.currentTimeMillis() - startTime) + "ms")

if (fileContainsInitClass) {
registerList.each { ext ->
println("\ninsert register code to file:" + fileContainsInitClass.absolutePath)
Logger.i('Insert register code to file ' + fileContainsInitClass.absolutePath)

if (ext.classList.isEmpty()) {
project.logger.error("No class implements found for interface:" + ext.interfaceName)
Logger.e("No class implements found for interface:" + ext.interfaceName)
} else {
ext.classList.each {
println(it)
Logger.i(it)
}
RouterRegisterCodeGenerator.insertInitCodeTo(ext)
RegisterCodeGenerator.insertInitCodeTo(ext)
}
}
}
def finishTime = System.currentTimeMillis()
println("\n>>>>>> register insert code cost time: " + (finishTime - scanFinishTime) + " ms")
println(">>>>>> register cost time: " + (finishTime - time) + " ms")
println("\n============================ finish arouter-auto-register ============================\n\n")
}

Logger.i("Generate code finish, current cost time: " + (System.currentTimeMillis() - startTime) + "ms")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.alibaba.android.arouter.register.launch

import com.alibaba.android.arouter.register.utils.Logger
import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.alibaba.android.arouter.register.utils.ScanSetting
import com.alibaba.android.arouter.register.core.RegisterTransform
import org.gradle.api.Plugin
import org.gradle.api.Project
/**
* Simple version of AutoRegister plugin for ARouter
* @author billy.qi email: [email protected]
* @since 17/12/06 15:35
*/
public class PluginLaunch implements Plugin<Project> {

@Override
public void apply(Project project) {
def isApp = project.plugins.hasPlugin(AppPlugin)
//only application module needs this plugin to generate register code
if (isApp) {
Logger.make(project)

Logger.i('project(' + project.name + ') apply arouter-register plugin')

def android = project.extensions.getByType(AppExtension)
def transformImpl = new RegisterTransform(project)

//init arouter-auto-register settings
ArrayList<ScanSetting> list = new ArrayList<>(3)
list.add(new ScanSetting('IRouteRoot', 'registerRouteRoot'))
list.add(new ScanSetting('IInterceptorGroup', 'registerInterceptor'))
list.add(new ScanSetting('IProviderGroup', 'registerProvider'))
RegisterTransform.registerList = list
//register this plugin
android.registerTransform(transformImpl)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.alibaba.android.arouter.register.utils

import org.gradle.api.Project

/**
* Format log
*
* @author zhilong <a href="mailto:[email protected]">Contact me.</a>
* @version 1.0
* @since 2017/12/18 下午2:43
*/
class Logger {
static org.gradle.api.logging.Logger logger

static void make(Project project) {
logger = project.getLogger()
}

static void i(String info) {
if (null != info && null != logger) {
logger.info("ARouter::Register >>> " + info)
}
}

static void e(String error) {
if (null != error && null != logger) {
logger.error("ARouter::Register >>> " + error)
}
}

static void w(String warning) {
if (null != warning && null != logger) {
logger.warn("ARouter::Register >>> " + warning)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.billy.android.register
package com.alibaba.android.arouter.register.utils
/**
* register setting
* @author billy.qi email: [email protected]
* @since 17/3/28 11:48
*/
class RouterRegisterSetting {
class ScanSetting {
static final String PLUGIN_NAME = "com.alibaba.arouter"
/**
* The register code is generated into this class
*/
Expand Down Expand Up @@ -50,7 +51,7 @@ class RouterRegisterSetting {
* @param interfaceName interface to scan
* @param registerMethod method to generate code into
*/
RouterRegisterSetting(String interfaceName, String registerMethod){
ScanSetting(String interfaceName, String registerMethod){
this.interfaceName = INTERFACE_PACKAGE_NAME + interfaceName
this.registerMethodName = registerMethod
}
Expand Down
Loading

0 comments on commit 10bed9d

Please sign in to comment.