Skip to content

Commit ba153fc

Browse files
committed
see 11/14 log
1 parent 924278c commit ba153fc

22 files changed

+1049
-21
lines changed

build.gradle

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
buildscript {
33
apply from: 'config.gradle'
44
repositories {
5+
if (localDebugPlugin) {
6+
maven() {
7+
url uri(new File(project.rootDir, "maven"))
8+
}
9+
}
510
google()
611
jcenter()
712
}
813

914
dependencies {
1015
classpath dep.gradle
11-
// classpath 'com.blankj:bus-gradle-plugin:1.3'
1216
classpath dep.kotlin_gradle_plugin
17+
18+
classpath dep.android_maven_gradle_plugin
19+
classpath dep.gradle_bintray_plugin
20+
21+
// classpath dep.bus_gradle_plugin
1322
}
1423
}
1524

bus-gradle-plugin/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/build
2+
local.properties

bus-gradle-plugin/build.gradle

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
plugins {
2+
id 'groovy'
3+
id 'java-gradle-plugin' // for gradlePlugin DSL
4+
id 'com.gradle.plugin-publish' version "0.10.0" //for pluginPublish.gradle
5+
}
6+
7+
apply {
8+
from "../gradle/pluginPublish.gradle"
9+
if (rootProject.localDebugPlugin) {
10+
plugin 'maven'
11+
from "../gradle/localMavenUpload.gradle"
12+
} else {
13+
plugin 'com.github.dcendents.android-maven'
14+
plugin 'com.jfrog.bintray'
15+
from "../gradle/bintrayUploadJava.gradle"
16+
}
17+
}
18+
19+
gradlePlugin {
20+
plugins {
21+
busPlugin {
22+
id = 'com.blankj.bus'
23+
implementationClass = 'com.blankj.bus.BusPlugin'
24+
}
25+
}
26+
}
27+
28+
dependencies {
29+
implementation dep.gradle
30+
implementation dep.javassist
31+
implementation dep.commons_io
32+
implementation dep.junit
33+
}
34+
35+
group = rootProject.group
36+
version = rootProject.versionName
37+
38+
//./gradlew bus-gradle-plugin:bintrayUpload
39+
//./gradlew publishPlugins

bus-gradle-plugin/project.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
project.name=StaticBusPlugin
2+
project.siteUrl=https://github.com/Blankj/StaticBus
3+
project.gitUrl=https://github.com/Blankj/StaticBus.git
4+
5+
#javadoc
6+
javadoc.name=StaticBus
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.blankj.bus
2+
3+
class BusExtension {
4+
5+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.blankj.bus
2+
3+
import com.blankj.util.ZipUtils
4+
import javassist.CtClass
5+
import javassist.CtMethod
6+
import org.apache.commons.io.FileUtils
7+
8+
class BusInject {
9+
10+
static void start(HashMap<String, String> bus, File busJar) {
11+
String jarPath = busJar.getAbsolutePath()
12+
String decompressedJarPath = jarPath.substring(0, jarPath.length() - 4);
13+
File decompressedJar = new File(decompressedJarPath)
14+
ZipUtils.unzipFile(busJar, decompressedJar)
15+
16+
CtClass busUtils = Config.mPool.get(Config.CLASS_BUS_UTILS)
17+
CtMethod callMethod = busUtils.getDeclaredMethod("post");
18+
callMethod.insertAfter(getInsertContent(bus));
19+
busUtils.writeFile(decompressedJarPath)
20+
busUtils.defrost();
21+
FileUtils.forceDelete(busJar)
22+
ZipUtils.zipFile(decompressedJar, busJar)
23+
FileUtils.forceDelete(decompressedJar)
24+
}
25+
26+
private static String getInsertContent(HashMap<String, String> bus) {
27+
StringBuilder sb = new StringBuilder();
28+
bus.each { String key, String val ->
29+
String name = key
30+
String[] method = val.split(' ')
31+
String returnType = method[0]
32+
String methodName = method[1]
33+
34+
sb.append('if ("').append(name).append('".equals($1)) {\n')
35+
36+
int st = methodName.indexOf('(')
37+
int end = methodName.length()
38+
String params = methodName.substring(st + 1, end - 1);
39+
if (params != '') {
40+
String[] paramArr = params.split(",")
41+
42+
StringBuilder args = new StringBuilder()
43+
for (int i = 0; i < paramArr.length; i++) {
44+
if (paramArr[i] == 'char') {
45+
args.append(',$2[').append(i).append('].toString().charAt(0)')
46+
} else if (paramArr[i] == 'boolean') {
47+
args.append(',Boolean.parseBoolean($2[').append(i).append('].toString())')
48+
} else if (paramArr[i] == 'byte') {
49+
args.append(',Byte.parseByte($2[').append(i).append('].toString())')
50+
} else if (paramArr[i] == 'short') {
51+
args.append(',Short.parseShort($2[').append(i).append('].toString())')
52+
} else if (paramArr[i] == 'int') {
53+
args.append(',Integer.parseInt($2[').append(i).append('].toString())')
54+
} else if (paramArr[i] == 'long') {
55+
args.append(',Long.parseLong($2[').append(i).append('].toString())')
56+
} else if (paramArr[i] == 'float') {
57+
args.append(',Float.parseFloat($2[').append(i).append('].toString())')
58+
} else if (paramArr[i] == 'double') {
59+
args.append(',Double.parseDouble($2[').append(i).append('].toString())')
60+
} else {
61+
args.append(',(').append(paramArr[i]).append(')$2[').append(i).append(']')
62+
}
63+
}
64+
methodName = methodName.substring(0, st + 1) + args.substring(1) + ")"
65+
}
66+
67+
if (returnType.equals('void')) {
68+
sb.append(methodName).append(';\n').append('return null;\n')
69+
} else {
70+
sb.append('return ($w)').append(methodName).append(';\n')
71+
}
72+
sb.append("}")
73+
}
74+
sb.append('android.util.Log.e("BusUtils", "bus of <" + $1 + "> didn\'t exist.");')
75+
return sb.toString()
76+
}
77+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.blankj.bus
2+
3+
import com.android.build.gradle.AppExtension
4+
import com.android.build.gradle.AppPlugin
5+
import com.blankj.util.LogUtils
6+
import com.blankj.util.Utils
7+
import org.gradle.api.Plugin
8+
import org.gradle.api.Project
9+
10+
class BusPlugin implements Plugin<Project> {
11+
12+
@Override
13+
void apply(Project project) {
14+
if (project.plugins.hasPlugin(AppPlugin)) {
15+
Utils.init(project)
16+
17+
LogUtils.l('project(' + project.name + ') apply bus gradle plugin!')
18+
19+
project.extensions.create(Config.EXT_NAME, BusExtension)
20+
21+
def android = project.extensions.getByType(AppExtension)
22+
23+
android.registerTransform(new BusTransform())
24+
project.afterEvaluate {
25+
def ext = project[Config.EXT_NAME] as BusExtension
26+
}
27+
}
28+
}
29+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.blankj.bus
2+
3+
import com.blankj.util.ZipUtils
4+
import com.blankj.utilcode.util.BusUtils
5+
import groovy.io.FileType
6+
import javassist.CtClass
7+
import javassist.CtMethod
8+
import org.apache.commons.io.FileUtils
9+
10+
class BusScan {
11+
12+
HashMap<String, String> busMap
13+
List<File> scans
14+
File busJar
15+
16+
BusScan() {
17+
busMap = [:]
18+
scans = []
19+
}
20+
21+
void scanJar(File jar) {
22+
File tmp = new File(jar.getParent(), "temp_" + jar.getName())
23+
ZipUtils.unzipFile(jar, tmp)
24+
scanDir(tmp)
25+
FileUtils.forceDelete(tmp)
26+
}
27+
28+
void scanDir(File root) {
29+
String rootPath = root.getAbsolutePath()
30+
if (!rootPath.endsWith(Config.FILE_SEP)) {
31+
rootPath += Config.FILE_SEP
32+
}
33+
34+
if (root.isDirectory()) {
35+
root.eachFileRecurse(FileType.FILES) { File file ->
36+
def fileName = file.name
37+
if (!fileName.endsWith('.class')
38+
|| fileName.startsWith('R$')
39+
|| fileName == 'R.class'
40+
|| fileName == 'BuildConfig.class') {
41+
return
42+
}
43+
44+
def filePath = file.absolutePath
45+
def packagePath = filePath.replace(rootPath, '')
46+
def className = packagePath.replace(Config.FILE_SEP, ".")
47+
// delete .class
48+
className = className.substring(0, className.length() - 6)
49+
50+
CtClass ctClass = Config.mPool.get(className)
51+
52+
CtMethod[] methods = ctClass.getDeclaredMethods();
53+
for (CtMethod method : methods) {
54+
if (method.hasAnnotation(BusUtils.Subscribe)) {
55+
String name = method.getAnnotation(BusUtils.Subscribe).name()
56+
String sign = method.getReturnType().getName() + ' ' + method.getLongName()
57+
busMap.put(name, sign)
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.blankj.bus
2+
3+
import com.android.build.api.transform.*
4+
import com.android.build.gradle.internal.pipeline.TransformManager
5+
import com.blankj.util.JsonUtils
6+
import com.blankj.util.LogUtils
7+
import com.blankj.util.Utils
8+
import org.apache.commons.io.FileUtils
9+
10+
class BusTransform extends Transform {
11+
12+
@Override
13+
String getName() {
14+
return "busTransform"
15+
}
16+
17+
@Override
18+
Set<QualifiedContent.ContentType> getInputTypes() {
19+
return TransformManager.CONTENT_CLASS
20+
}
21+
22+
@Override
23+
Set<? super QualifiedContent.Scope> getScopes() {
24+
return TransformManager.SCOPE_FULL_PROJECT
25+
}
26+
27+
@Override
28+
boolean isIncremental() {
29+
return false
30+
}
31+
32+
@Override
33+
void transform(TransformInvocation transformInvocation)
34+
throws TransformException, InterruptedException, IOException {
35+
super.transform(transformInvocation)
36+
LogUtils.l(getName() + " started")
37+
38+
long stTime = System.currentTimeMillis();
39+
40+
def inputs = transformInvocation.getInputs()
41+
def referencedInputs = transformInvocation.getReferencedInputs()
42+
def outputProvider = transformInvocation.getOutputProvider()
43+
def isIncremental = transformInvocation.isIncremental()
44+
45+
outputProvider.deleteAll()
46+
47+
Config.initClassPool()
48+
BusScan busScan = new BusScan()
49+
50+
inputs.each { TransformInput input ->
51+
input.directoryInputs.each { DirectoryInput dirInput ->// 遍历文件夹
52+
File dir = dirInput.file
53+
Config.mPool.appendClassPath(dir.absolutePath)
54+
55+
def dest = outputProvider.getContentLocation(
56+
dirInput.name,
57+
dirInput.contentTypes,
58+
dirInput.scopes,
59+
Format.DIRECTORY
60+
)
61+
FileUtils.copyDirectory(dir, dest)
62+
63+
LogUtils.l("scan " + dirInput.name)
64+
busScan.scanDir(dir)
65+
}
66+
67+
input.jarInputs.each { JarInput jarInput ->// 遍历 jar 文件
68+
File jar = jarInput.file
69+
Config.mPool.appendClassPath(jarInput.file.absolutePath)
70+
71+
def jarName = jarInput.name
72+
def dest = outputProvider.getContentLocation(
73+
jarName,
74+
jarInput.contentTypes,
75+
jarInput.scopes,
76+
Format.JAR
77+
)
78+
FileUtils.copyFile(jar, dest)
79+
80+
if (jumpScan(jarName)) {
81+
LogUtils.l("jump " + jarName)
82+
return
83+
}
84+
85+
if (jarName.startsWith("com.blankj:bus:")) {
86+
busScan.busJar = dest
87+
return
88+
}
89+
90+
LogUtils.l("scan " + jarName)
91+
busScan.scanJar(jar)
92+
}
93+
}
94+
95+
if (busScan.busJar != null) {
96+
File jsonFile = new File(Utils.project.projectDir.getAbsolutePath(), "__bus__.json")
97+
String busJson = JsonUtils.getFormatJson(busScan.busMap)
98+
FileUtils.write(jsonFile, busJson)
99+
LogUtils.l(busJson)
100+
BusInject.start(busScan.busMap, busScan.busJar)
101+
} else {
102+
LogUtils.l('u should <implementation "com.blankj:utilcode:1.30.+"> ' +
103+
'or <implementation "com.blankj:bus:1.0+">')
104+
}
105+
106+
LogUtils.l(getName() + " finished: " + (System.currentTimeMillis() - stTime) + "ms")
107+
}
108+
109+
private static boolean jumpScan(String jarName) {
110+
boolean isExcept = false
111+
for (String except : Config.EXCEPTS) {
112+
if (jarName.startsWith(except)) {
113+
isExcept = true
114+
break
115+
}
116+
}
117+
isExcept
118+
}
119+
}

0 commit comments

Comments
 (0)