cn.hx.plugin.junkcode.utils.JunkUtil.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of android-junk-code Show documentation
Show all versions of android-junk-code Show documentation
generate junk code for android
package cn.hx.plugin.junkcode.utils
import cn.hx.plugin.junkcode.ext.JunkCodeConfig
import cn.hx.plugin.junkcode.template.ResTemplate
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.TypeSpec
import org.gradle.api.Project
import javax.lang.model.element.Modifier
import java.nio.file.Files
import java.nio.file.Path
class JunkUtil {
static random = new Random()
static abc = "abcdefghijklmnopqrstuvwxyz".toCharArray()
static color = "0123456789abcdef".toCharArray()
/**
* 生成名称
* @param index
* @return
*/
static String generateName(int index) {
def sb = new StringBuilder()
for (i in 0..4) {
sb.append(abc[random.nextInt(abc.size())])
}
int temp = index
while (temp >= 0) {
sb.append(abc[temp % abc.size()])
temp = temp / abc.size()
if (temp == 0) {
temp = -1
}
}
sb.append(index.toString())
return sb.toString()
}
/**
* 生成随机方法
* @param methodBuilder
*/
static void generateMethods(MethodSpec.Builder methodBuilder) {
switch (random.nextInt(5)) {
case 0:
methodBuilder.addStatement("long now = \$T.currentTimeMillis()", System.class)
.beginControlFlow("if (\$T.currentTimeMillis() < now)", System.class)
.addStatement("\$T.out.println(\$S)", System.class, "Time travelling, woo hoo!")
.nextControlFlow("else if (\$T.currentTimeMillis() == now)", System.class)
.addStatement("\$T.out.println(\$S)", System.class, "Time stood still!")
.nextControlFlow("else")
.addStatement("\$T.out.println(\$S)", System.class, "Ok, time still moving forward")
.endControlFlow()
break
case 1:
methodBuilder.addCode("" + "int total = 0;\n" + "for (int i = 0; i < 10; i++) {\n" + " total += i;\n" + "}\n")
break
case 2:
methodBuilder.beginControlFlow("try")
.addStatement("throw new Exception(\$S)", "Failed")
.nextControlFlow("catch (\$T e)", Exception.class)
.addStatement("throw new \$T(e)", RuntimeException.class)
.endControlFlow()
break
case 3:
methodBuilder.returns(Date.class)
.addStatement("return new \$T()", Date.class)
break
case 4:
methodBuilder.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addParameter(String[].class, "args")
.addStatement("\$T.out.println(\$S)", System.class, "Hello")
break
default:
methodBuilder.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(void.class)
.addParameter(String[].class, "args")
.addStatement("\$T.out.println(\$S)", System.class, "Hello")
}
}
/**
* 生成颜色代码
* @return
*/
static String generateColor() {
def sb = new StringBuilder()
sb.append("#")
for (i in 0..5) {
sb.append(color[random.nextInt(color.size())])
}
return sb.toString()
}
/**
* 生成id代码
* @return
*/
static String generateId() {
def sb = new StringBuilder()
for (i in 0..5) {
sb.append(abc[random.nextInt(abc.size())])
}
return sb.toString()
}
/**
* 生成Activity
* @param javaDir
* @param packageName
* @param config
*/
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 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"))
typeBuilder.addModifiers(Modifier.PUBLIC)
//onCreate方法
def bundleClassName = ClassName.get("android.os", "Bundle")
typeBuilder.addMethod(MethodSpec.methodBuilder("onCreate")
.addAnnotation(Override.class)
.addModifiers(Modifier.PROTECTED)
.addParameter(bundleClassName, "savedInstanceState")
.addStatement("super.onCreate(savedInstanceState)")
.addStatement("setContentView(\$T.layout.${layoutName})", ClassName.get(namespace, "R"))
.build())
if (config.typeGenerator) {
config.typeGenerator.execute(typeBuilder)
} else {
//其它方法
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)
} else {
generateMethods(methodBuilder)
}
typeBuilder.addMethod(methodBuilder.build())
}
}
def javaFile = JavaFile.builder(packageName, typeBuilder.build()).build()
writeJavaToFile(javaDir, javaFile)
activityList.add(packageName + "." + className)
}
}
return activityList
}
/**
* 生成java文件
* @param javaDir
* @param packageName
* @param config
*/
static void generateJava(File javaDir, String packageName, JunkCodeConfig config) {
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 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)
} else {
generateMethods(methodBuilder)
}
typeBuilder.addMethod(methodBuilder.build())
}
}
def javaFile = JavaFile.builder(packageName, typeBuilder.build()).build()
writeJavaToFile(javaDir, javaFile)
}
}
/**
* 生成layout文件
* @param resDir
* @param layoutName
* @param config
*/
static void generateLayout(File resDir, String layoutName, JunkCodeConfig config) {
def layoutFile = new File(resDir, "layout/${layoutName}.xml")
if (config.layoutGenerator) {
def contentBuilder = new StringBuilder()
config.layoutGenerator.execute(contentBuilder)
writeStringToFile(layoutFile, contentBuilder.toString())
} else {
def layoutStr = String.format(ResTemplate.LAYOUT_TEMPLATE, generateId())
writeStringToFile(layoutFile, layoutStr)
}
}
/**
* 生成drawable
* @param resDir
* @param config
*/
static void generateDrawableFiles(File resDir, JunkCodeConfig config) {
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()))
}
}
}
/**
* 生成strings.xml
* @param resDir
* @param config
*/
static void generateStringsFile(File resDir, JunkCodeConfig config) {
def stringFile = new File(resDir, "values/strings.xml")
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
def value
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)
}
contentBuilder.append("${value} \n")
}
contentBuilder.append(" \n")
writeStringToFile(stringFile, contentBuilder.toString())
}
/**
* 生成keep.xml
* @param resDir
* @param config
*/
static void generateKeep(File resDir, JunkCodeConfig config) {
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 {
if (config.resPrefix.isEmpty()) {
return
}
keepName = "android_junk_code_keep"
keepContent = " \n"
}
def keepFile = new File(resDir, "raw/${keepName}.xml")
writeStringToFile(keepFile, keepContent)
}
/**
* 生成AndroidManifest.xml
* @param manifestFile
* @param activityList
*/
static void generateManifest(File manifestFile, List activityList) {
StringBuilder sb = new StringBuilder()
sb.append("\n")
sb.append(" \n")
for (i in 0..\n")
}
sb.append(" \n")
sb.append(" ")
writeStringToFile(manifestFile, sb.toString())
}
/**
* 生成proguard-rules.pro
*
* @param manifestFile
* @param activityList
*/
static void generateProguard(File proguardFile, List packageList) {
StringBuilder sb = new StringBuilder()
for (i in 0.. 7 || androidComponents.pluginVersion.minor >= 4)) {
return true
}
return false
}
}