From 245c92ae988e24f3c863261b134af79e3ab57731 Mon Sep 17 00:00:00 2001
From: huangx <549631030@qq.com>
Date: Wed, 2 Aug 2023 19:10:33 +0800
Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E6=94=BE=E6=89=80=E6=9C=89=E5=8A=9F?=
=?UTF-8?q?=E8=83=BD=E8=87=AA=E5=AE=9A=E4=B9=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
app/build.gradle | 148 +++++++++++++++++-
.../plugin/junkcode/ext/JunkCodeConfig.groovy | 22 ++-
.../junkcode/task/AndroidJunkCodeTask.groovy | 12 +-
.../junkcode/task/GenerateJunkCodeTask.groovy | 12 +-
.../hx/plugin/junkcode/utils/JunkUtil.groovy | 135 +++++++++++-----
5 files changed, 283 insertions(+), 46 deletions(-)
diff --git a/app/build.gradle b/app/build.gradle
index 2b019e4f..0e3b9785 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -38,6 +38,9 @@ android {
if (PLUGIN_ENABLE.toBoolean()) {
androidJunkCode {
debug true
+ /**
+ * 生成逻辑由插件完成
+ */
def config = {
packageBase = "cn.hx.plugin.ui"
packageCount = 30
@@ -49,9 +52,152 @@ if (PLUGIN_ENABLE.toBoolean()) {
drawableCount = 300
stringCount = 300
}
+
+ /**
+ * 生成逻辑自定义
+ */
+ def partCustomConfig = {
+ packageBase = "cn.hx.plugin.ui"
+ packageCount = 3
+ activityCountPerPackage = 30
+ excludeActivityJavaFile = false
+ otherCountPerPackage = 50
+ methodCountPerClass = 20
+ resPrefix = "junk_"
+ drawableCount = 300
+ stringCount = 300
+
+ //自定义生成包名(设置此项后packageBase将无效)
+ packageCreator = { tuple2 ->
+ //int:下标 [0,packageCount)
+ def index = tuple2.first
+ //StringBuilder: 生成包名格式xx.xx.xx
+ def packageNameBuilder = tuple2.second
+ packageNameBuilder.append("cn.hx.package" + index)
+ }
+
+ /**
+ * 自定义生成Activity
+ */
+ activityCreator = { tuple4 ->
+ //int:下标 [0,activityCountPerPackage)
+ def index = tuple4.first
+ //StringBuilder: 生成Activity文件名
+ def activityNameBuilder = tuple4.second
+ //StringBuilder: 生成layout文件名
+ def layoutNameBuilder = tuple4.third
+ //StringBuilder: 生成layout内容
+ def layoutContentBuilder = tuple4.fourth
+
+ //例
+ activityNameBuilder.append("Activity${index}")
+ layoutNameBuilder.append("activity_${index}")
+ layoutContentBuilder.append('''
+
+
+
+''')
+ }
+
+ //自定义生成类名(Activity除外)
+ classNameCreator = { tuple2 ->
+ //int:下标 [0,otherCountPerPackage)
+ def index = tuple2.first
+ //StringBuilder: 生成Java文件名
+ def classNameBuilder = tuple2.second
+
+ //例
+ classNameBuilder.append("Class${index}")
+ }
+
+ //自定义生成方法名
+ methodNameCreator = { tuple2 ->
+ //int:下标 [0,methodCountPerClass)
+ def index = tuple2.first
+ //StringBuilder: 生成的方法名
+ def classNameBuilder = tuple2.second
+
+ //例
+ classNameBuilder.append("method${index}")
+ }
+
+ //自定义生成drawable(只支持xml)
+ drawableCreator = { tuple3 ->
+ //int:下标 [0,drawableCount)
+ def index = tuple3.first
+ //StringBuilder: 生成drawable文件名
+ def fileNameBuilder = tuple3.second
+ //StringBuilder: 生成drawable文件内容
+ def contentBuilder = tuple3.third
+
+ //例
+ fileNameBuilder.append("drawable${index}")
+ contentBuilder.append('''
+
+
+
+
+''')
+ }
+
+ //自定义生成 string
+ stringCreator = { tuple3 ->
+ //int:下标 [0,drawableCount)
+ def index = tuple3.first
+ //StringBuilder: 生成string名
+ def keyBuilder = tuple3.second
+ //StringBuilder: 生成string值
+ def valueBuilder = tuple3.third
+
+ //例
+ keyBuilder.append("string${index}")
+ valueBuilder.append("value${index}")
+ }
+
+ //自定义生成keep.xm
+ keepCreator = { tuple2 ->
+ //StringBuilder:生成文件名
+ def fileNameBuilder = tuple2.first
+ //StringBuilder: 生成的文件内容
+ def contentBuilder = tuple2.second
+
+ //例
+ fileNameBuilder.append("android_junk_code_keep")
+ contentBuilder.append( "\n")
+ }
+
+ //自定义类实现(类名已经实现随机,Activity类已经实现了onCreate,其它自己实现随机)
+ //注意设置了此实现将忽略 methodGenerator,methodCountPerClass
+ //TypeSpec.Builder用法请参考(https://github.com/square/javapoet)
+// typeGenerator = { typeBuilder ->
+// //例
+// for (i in 0..<10) {
+// typeBuilder.addMethod(MethodSpec.methodBuilder("method" + i)
+// .addCode("" + "int total = 0;\n" + "for (int i = 0; i < 10; i++) {\n" + " total += i;\n" + "}\n")
+// .build())
+// }
+// }
+
+ //自定义方法实现(方法名已经实现随机,其它自己实现随机)
+ //MethodSpec.Builder用法请参考(https://github.com/square/javapoet)
+// methodGenerator = { methodBuilder ->
+// //例
+// methodBuilder.addCode("" + "int total = 0;\n" + "for (int i = 0; i < 10; i++) {\n" + " total += i;\n" + "}\n")
+// }
+ }
+
variantConfig {
debug config
- release config
+ release partCustomConfig
}
}
}
diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/ext/JunkCodeConfig.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/ext/JunkCodeConfig.groovy
index 8bbb24bd..871de13b 100644
--- a/library/src/main/groovy/cn/hx/plugin/junkcode/ext/JunkCodeConfig.groovy
+++ b/library/src/main/groovy/cn/hx/plugin/junkcode/ext/JunkCodeConfig.groovy
@@ -32,14 +32,34 @@ class JunkCodeConfig {
@Internal
Action methodGenerator = null
+ @Deprecated
@Internal
Action layoutGenerator = null
+ @Deprecated
@Internal
Action drawableGenerator = null
@Internal
- Action stringGenerator = null
+ Action> packageCreator = null
+
+ @Internal
+ Action> activityCreator = null
+
+ @Internal
+ Action> classNameCreator = null
+
+ @Internal
+ Action> methodNameCreator = null
+
+ @Internal
+ Action> drawableCreator = null
+
+ @Internal
+ Action> stringCreator = null
+
+ @Internal
+ Action> keepCreator = null
@Internal
String name = ""
diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/task/AndroidJunkCodeTask.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/task/AndroidJunkCodeTask.groovy
index 49a0c574..dcbb93ef 100644
--- a/library/src/main/groovy/cn/hx/plugin/junkcode/task/AndroidJunkCodeTask.groovy
+++ b/library/src/main/groovy/cn/hx/plugin/junkcode/task/AndroidJunkCodeTask.groovy
@@ -33,10 +33,16 @@ abstract class AndroidJunkCodeTask extends DefaultTask {
resOutDir.deleteDir()
for (int i = 0; i < config.packageCount; i++) {
String packageName
- if (config.packageBase.isEmpty()) {
- packageName = JunkUtil.generateName(i)
+ if (config.packageCreator) {
+ def packageNameBuilder = new StringBuffer()
+ config.packageCreator.execute(new Tuple2(i, packageNameBuilder))
+ packageName = packageNameBuilder.toString()
} else {
- packageName = config.packageBase + "." + JunkUtil.generateName(i)
+ if (config.packageBase.isEmpty()) {
+ packageName = JunkUtil.generateName(i)
+ } else {
+ packageName = config.packageBase + "." + JunkUtil.generateName(i)
+ }
}
def list = JunkUtil.generateActivity(javaOutDir, resOutDir, namespace, packageName, config)
activityList.addAll(list)
diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/task/GenerateJunkCodeTask.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/task/GenerateJunkCodeTask.groovy
index 11225f3b..81fe04ed 100644
--- a/library/src/main/groovy/cn/hx/plugin/junkcode/task/GenerateJunkCodeTask.groovy
+++ b/library/src/main/groovy/cn/hx/plugin/junkcode/task/GenerateJunkCodeTask.groovy
@@ -35,10 +35,16 @@ abstract class GenerateJunkCodeTask extends DefaultTask {
resDir.deleteDir()
for (int i = 0; i < config.packageCount; i++) {
String packageName
- if (config.packageBase.isEmpty()) {
- packageName = JunkUtil.generateName(i)
+ if (config.packageCreator) {
+ def packageNameBuilder = new StringBuffer()
+ config.packageCreator.execute(new Tuple2(i, packageNameBuilder))
+ packageName = packageNameBuilder.toString()
} else {
- packageName = config.packageBase + "." + JunkUtil.generateName(i)
+ if (config.packageBase.isEmpty()) {
+ packageName = JunkUtil.generateName(i)
+ } else {
+ packageName = config.packageBase + "." + JunkUtil.generateName(i)
+ }
}
def list = JunkUtil.generateActivity(javaDir, resDir, namespace, packageName, config)
activityList.addAll(list)
diff --git a/library/src/main/groovy/cn/hx/plugin/junkcode/utils/JunkUtil.groovy b/library/src/main/groovy/cn/hx/plugin/junkcode/utils/JunkUtil.groovy
index 0112276c..845f491c 100644
--- a/library/src/main/groovy/cn/hx/plugin/junkcode/utils/JunkUtil.groovy
+++ b/library/src/main/groovy/cn/hx/plugin/junkcode/utils/JunkUtil.groovy
@@ -118,10 +118,22 @@ class JunkUtil {
static List generateActivity(File javaDir, File resDir, String namespace, String packageName, JunkCodeConfig config) {
def activityList = new ArrayList()
for (int i = 0; i < config.activityCountPerPackage; i++) {
- def activityPreName = generateName(i)
- def className = activityPreName.capitalize() + "Activity"
- def layoutName = "${config.resPrefix.toLowerCase()}${packageName.replace(".", "_")}_activity_${activityPreName}"
- generateLayout(resDir, layoutName, config)
+ def className
+ def layoutName
+ if (config.activityCreator) {
+ def activityNameBuilder = new StringBuilder()
+ def layoutNameBuilder = new StringBuilder()
+ def layoutContentBuilder = new StringBuilder()
+ config.activityCreator.execute(new Tuple4(i, activityNameBuilder, layoutNameBuilder, layoutContentBuilder))
+ className = activityNameBuilder.toString()
+ layoutName = layoutNameBuilder.toString()
+ writeStringToFile(new File(resDir, "layout/${layoutName}.xml"), layoutContentBuilder.toString())
+ } else {
+ def activityPreName = generateName(i)
+ className = activityPreName.capitalize() + "Activity"
+ layoutName = "${config.resPrefix.toLowerCase()}${packageName.replace(".", "_")}_activity_${activityPreName}"
+ generateLayout(resDir, layoutName, config)
+ }
if (!config.excludeActivityJavaFile) {
def typeBuilder = TypeSpec.classBuilder(className)
typeBuilder.superclass(ClassName.get("android.app", "Activity"))
@@ -140,7 +152,14 @@ class JunkUtil {
} else {
//其它方法
for (int j = 0; j < config.methodCountPerClass; j++) {
- def methodName = generateName(j)
+ def methodName
+ if (config.methodNameCreator) {
+ def methodNameBuilder = new StringBuilder()
+ config.methodNameCreator.execute(new Tuple2(j, methodNameBuilder))
+ methodName = methodNameBuilder.toString()
+ } else {
+ methodName = generateName(j)
+ }
def methodBuilder = MethodSpec.methodBuilder(methodName)
if (config.methodGenerator) {
config.methodGenerator.execute(methodBuilder)
@@ -165,15 +184,29 @@ class JunkUtil {
* @param config
*/
static void generateJava(File javaDir, String packageName, JunkCodeConfig config) {
- for (int j = 0; j < config.otherCountPerPackage; j++) {
- def className = generateName(j).capitalize()
+ for (int i = 0; i < config.otherCountPerPackage; i++) {
+ def className
+ if (config.classNameCreator) {
+ def classNameBuilder = new StringBuilder()
+ config.classNameCreator.execute(new Tuple2(i, classNameBuilder))
+ className = classNameBuilder.toString()
+ } else {
+ className = generateName(i).capitalize()
+ }
def typeBuilder = TypeSpec.classBuilder(className)
if (config.typeGenerator) {
config.typeGenerator.execute(typeBuilder)
} else {
typeBuilder.addModifiers(Modifier.PUBLIC)
- for (int k = 0; k < config.methodCountPerClass; k++) {
- def methodName = generateName(k)
+ for (int j = 0; j < config.methodCountPerClass; j++) {
+ def methodName
+ if (config.methodNameCreator) {
+ def methodNameBuilder = new StringBuilder()
+ config.methodNameCreator.execute(new Tuple2(j, methodNameBuilder))
+ methodName = methodNameBuilder.toString()
+ } else {
+ methodName = generateName(j)
+ }
def methodBuilder = MethodSpec.methodBuilder(methodName)
if (config.methodGenerator) {
config.methodGenerator.execute(methodBuilder)
@@ -197,9 +230,9 @@ class JunkUtil {
static void generateLayout(File resDir, String layoutName, JunkCodeConfig config) {
def layoutFile = new File(resDir, "layout/${layoutName}.xml")
if (config.layoutGenerator) {
- def builder = new StringBuilder()
- config.layoutGenerator.execute(builder)
- writeStringToFile(layoutFile, builder.toString())
+ def contentBuilder = new StringBuilder()
+ config.layoutGenerator.execute(contentBuilder)
+ writeStringToFile(layoutFile, contentBuilder.toString())
} else {
def layoutStr = String.format(ResTemplate.LAYOUT_TEMPLATE, generateId())
writeStringToFile(layoutFile, layoutStr)
@@ -212,16 +245,28 @@ class JunkUtil {
* @param config
*/
static void generateDrawableFiles(File resDir, JunkCodeConfig config) {
- for (int i = 0; i < config.drawableCount; i++) {
- def drawableName = "${config.resPrefix.toLowerCase()}${generateName(i)}"
- def drawableFile = new File(resDir, "drawable/${drawableName}.xml")
- if (config.drawableGenerator) {
- def builder = new StringBuilder()
- config.drawableGenerator.execute(builder)
- writeStringToFile(drawableFile, builder.toString())
- } else {
- def drawableStr = String.format(ResTemplate.DRAWABLE, generateColor())
- writeStringToFile(drawableFile, drawableStr)
+ if (config.drawableGenerator) {
+ def contentBuilder = new StringBuilder()
+ for (int i = 0; i < config.drawableCount; i++) {
+ def drawableName = "${config.resPrefix.toLowerCase()}${generateName(i)}"
+ contentBuilder.setLength(0)
+ config.drawableGenerator.execute(contentBuilder)
+ writeStringToFile(new File(resDir, "drawable/${drawableName}.xml"), contentBuilder.toString())
+ }
+ } else if (config.drawableCreator) {
+ def fileNameBuilder = new StringBuilder()
+ def contentBuilder = new StringBuilder()
+ for (int i = 0; i < config.drawableCount; i++) {
+ fileNameBuilder.setLength(0)
+ contentBuilder.setLength(0)
+ config.drawableCreator.execute(new Tuple3(i, fileNameBuilder, contentBuilder))
+ def drawableName = fileNameBuilder.toString()
+ writeStringToFile(new File(resDir, "drawable/${drawableName}.xml"), contentBuilder.toString())
+ }
+ } else {
+ for (int i = 0; i < config.drawableCount; i++) {
+ def drawableName = "${config.resPrefix.toLowerCase()}${generateName(i)}"
+ writeStringToFile(new File(resDir, "drawable/${drawableName}.xml"), String.format(ResTemplate.DRAWABLE, generateColor()))
}
}
}
@@ -233,23 +278,27 @@ class JunkUtil {
*/
static void generateStringsFile(File resDir, JunkCodeConfig config) {
def stringFile = new File(resDir, "values/strings.xml")
- StringBuilder sb = new StringBuilder()
- StringBuilder valueSb = new StringBuilder()
- sb.append("\n")
+ StringBuilder contentBuilder = new StringBuilder()
+ StringBuilder keyBuilder = new StringBuilder()
+ StringBuilder valueBuilder = new StringBuilder()
+ contentBuilder.append("\n")
for (int i = 0; i < config.stringCount; i++) {
- def key = "${config.resPrefix.toLowerCase()}${generateName(i)}"
+ def key
def value
- if (config.stringGenerator) {
- valueSb.setLength(0)
- config.stringGenerator.execute(valueSb)
- value = valueSb.toString()
+ if (config.stringCreator) {
+ keyBuilder.setLength(0)
+ valueBuilder.setLength(0)
+ config.stringCreator.execute(new Tuple3(i, keyBuilder, valueBuilder))
+ key = keyBuilder.toString()
+ value = valueBuilder.toString()
} else {
+ key = "${config.resPrefix.toLowerCase()}${generateName(i)}"
value = generateName(i)
}
- sb.append("${value}\n")
+ contentBuilder.append("${value}\n")
}
- sb.append("\n")
- writeStringToFile(stringFile, sb.toString())
+ contentBuilder.append("\n")
+ writeStringToFile(stringFile, contentBuilder.toString())
}
/**
@@ -258,11 +307,21 @@ class JunkUtil {
* @param config
*/
static void generateKeep(File resDir, JunkCodeConfig config) {
- def keepFile = new File(resDir, "raw/android_junk_code_keep.xml")
- StringBuilder sb = new StringBuilder()
- sb.append("\n")
- writeStringToFile(keepFile, sb.toString())
+ def keepName
+ def keepContent
+ if (config.keepCreator) {
+ def fileNameBuilder = new StringBuilder()
+ def contentBuilder = new StringBuilder()
+ config.keepCreator.execute(new Tuple2(fileNameBuilder, contentBuilder))
+ keepName = fileNameBuilder.toString()
+ keepContent = contentBuilder.toString()
+ } else {
+ keepName = "android_junk_code_keep"
+ keepContent = "\n"
+ }
+ def keepFile = new File(resDir, "raw/${keepName}.xml")
+ writeStringToFile(keepFile, keepContent)
}
/**