Skip to content

Commit

Permalink
开放所有功能自定义
Browse files Browse the repository at this point in the history
  • Loading branch information
qq549631030 committed Aug 2, 2023
1 parent 76c6d40 commit 245c92a
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 46 deletions.
148 changes: 147 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ android {
if (PLUGIN_ENABLE.toBoolean()) {
androidJunkCode {
debug true
/**
* 生成逻辑由插件完成
*/
def config = {
packageBase = "cn.hx.plugin.ui"
packageCount = 30
Expand All @@ -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('''<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>''')
}

//自定义生成类名(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('''<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="4dp" />
<stroke
android:width="1dp"
android:color="#333333" />
</shape>
''')
}

//自定义生成 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( "<resources xmlns:tools=\"http://schemas.android.com/tools\"\n" +
" tools:keep=\"@layout/junk_*, @drawable/junk_*\" />\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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,34 @@ class JunkCodeConfig {
@Internal
Action<MethodSpec.Builder> methodGenerator = null

@Deprecated
@Internal
Action<StringBuilder> layoutGenerator = null

@Deprecated
@Internal
Action<StringBuilder> drawableGenerator = null

@Internal
Action<StringBuilder> stringGenerator = null
Action<Tuple2<Integer, StringBuilder>> packageCreator = null

@Internal
Action<Tuple4<Integer, StringBuilder, StringBuilder, StringBuilder>> activityCreator = null

@Internal
Action<Tuple2<Integer, StringBuilder>> classNameCreator = null

@Internal
Action<Tuple2<Integer, StringBuilder>> methodNameCreator = null

@Internal
Action<Tuple3<Integer, StringBuilder, StringBuilder>> drawableCreator = null

@Internal
Action<Tuple3<Integer, StringBuilder, StringBuilder>> stringCreator = null

@Internal
Action<Tuple2<StringBuilder, StringBuilder>> keepCreator = null

@Internal
String name = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 245c92a

Please sign in to comment.