From 219106f3920abeea5213097d5be6cbc6d68e1fee Mon Sep 17 00:00:00 2001 From: Stan Hebben Date: Mon, 9 Jun 2014 15:29:17 +0200 Subject: [PATCH] Start! --- .gitignore | 3 + build.gradle | 24 +++ buildSrc/build.gradle | 10 ++ .../tasks/RegisterZenClassesTask.java | 145 ++++++++++++++++++ common.gradle | 56 +++++++ settings.gradle | 20 +++ 6 files changed, 258 insertions(+) create mode 100644 .gitignore create mode 100644 build.gradle create mode 100644 buildSrc/build.gradle create mode 100644 buildSrc/src/main/java/minetweaker/tasks/RegisterZenClassesTask.java create mode 100644 common.gradle create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..33355e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.nb-gradle +build +.gradle diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..423e70c --- /dev/null +++ b/build.gradle @@ -0,0 +1,24 @@ +import org.gradle.api.artifacts.* + +apply plugin: 'base' // To add "clean" task to the root project. + +subprojects { + apply from: rootProject.file('common.gradle') +} + +task mergedJavadoc(type: Javadoc, description: 'Creates Javadoc from all the projects.') { + title = 'All modules' + destinationDir = new File(project.buildDir, 'merged-javadoc') + + // Note: The closures below are executed lazily. + source { + subprojects*.sourceSets*.main*.allSource + } + classpath.from { + subprojects*.configurations*.compile*.copyRecursive({ !(it instanceof ProjectDependency); })*.resolve() + } +} + +task wrapper(type: Wrapper, description: 'Creates and deploys the Gradle wrapper to the current directory.') { + gradleVersion = '1.11' +} diff --git a/buildSrc/build.gradle b/buildSrc/build.gradle new file mode 100644 index 0000000..3978a6e --- /dev/null +++ b/buildSrc/build.gradle @@ -0,0 +1,10 @@ +apply plugin: 'java' + +repositories { + mavenCentral(); +} + +dependencies { + compile gradleApi() + compile 'org.ow2.asm:asm-debug-all:4.1' +} diff --git a/buildSrc/src/main/java/minetweaker/tasks/RegisterZenClassesTask.java b/buildSrc/src/main/java/minetweaker/tasks/RegisterZenClassesTask.java new file mode 100644 index 0000000..7197d75 --- /dev/null +++ b/buildSrc/src/main/java/minetweaker/tasks/RegisterZenClassesTask.java @@ -0,0 +1,145 @@ +package minetweaker.tasks; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.gradle.api.DefaultTask; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.InputDirectory; +import org.gradle.api.tasks.OutputDirectory; +import org.gradle.api.tasks.TaskAction; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; +import org.objectweb.asm.Type; + +/** + * Generates a native registrar class. Finds all classes with a @ZenClass or + * @ZenExpansion annotation and generates a class with a single static + * getClasses(List) method. Overrides existing files if they exist. + * (handy for having a stub in the original source) + * + * @author Stan Hebben + */ +public class RegisterZenClassesTask extends DefaultTask { + @InputDirectory + public File inputDir; + + @OutputDirectory + public File outputDir; + + @Input + public String className; + + @Input + public String classPackage; + + @TaskAction + public void doTask() { + List classNames = new ArrayList(); + iterate(inputDir, null, classNames); + + for (String className : classNames) { + System.out.println("Class: " + className); + } + + String fullClassName = classPackage.replace('.', '/') + '/' + className; + System.out.println("Full classname: " + fullClassName); + + // generate class + ClassWriter output = new ClassWriter(ClassWriter.COMPUTE_MAXS); + output.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC, fullClassName, null, "java/lang/Object", null); + + MethodVisitor method = output.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "getClasses", "(Ljava/util/List;)V", null, null); + method.visitCode(); + + for (String className : classNames) { + method.visitLdcInsn(Type.getType(className)); + method.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z"); + } + method.visitInsn(Opcodes.RETURN); + + method.visitMaxs(0, 0); + method.visitEnd(); + + output.visitEnd(); + + // write output file + File outputFile = new File(outputDir, fullClassName + ".class"); + File outputFileDir = outputFile.getParentFile(); + if (!outputFileDir.exists()) { + outputFileDir.mkdirs(); + } + + try { + FileOutputStream outputStream = new FileOutputStream(outputFile); + outputStream.write(output.toByteArray()); + outputStream.close(); + } catch (IOException ex) { + Logger.getLogger(RegisterZenClassesTask.class.getName()).log(Level.SEVERE, null, ex); + } + } + + private void iterate(File dir, String pkg, List classNames) { + for (File f : dir.listFiles()) { + if (f.isDirectory()) { + if (pkg == null) { + iterate(f, f.getName(), classNames); + } else { + iterate(f, pkg + "/" + f.getName(), classNames); + } + } else if (f.isFile()) { + if (f.getName().endsWith(".class")) { + processJavaClass(f, pkg, classNames); + } + } + } + } + + private void processJavaClass(File cls, String pkg, List classNames) { + try { + InputStream input = new BufferedInputStream(new FileInputStream(cls)); + ClassReader reader = new ClassReader(input); + + AnnotationDetector detector = new AnnotationDetector(); + reader.accept(detector, ClassReader.SKIP_CODE); + input.close(); + + if (detector.isAnnotated) { + classNames.add(pkg + "/" + cls.getName().substring(0, cls.getName().length() - 6)); + } + } catch (IOException ex) { + + } + } + + private static class AnnotationDetector extends ClassVisitor { + private boolean isAnnotated = false; + + public AnnotationDetector() { + super(Opcodes.ASM4); + } + + @Override + public AnnotationVisitor visitAnnotation(String desc, boolean visible) { + if (desc.equals("Lstanhebben/zenscript/annotations/ZenExpansion;")) { + isAnnotated = true; + } else if (desc.equals("Lstanhebben/zenscript/annotations/ZenClass;")) { + isAnnotated = true; + } + + return super.visitAnnotation(desc, visible); + } + } +} diff --git a/common.gradle b/common.gradle new file mode 100644 index 0000000..34d24fe --- /dev/null +++ b/common.gradle @@ -0,0 +1,56 @@ +// +// This file is to be applied to every subproject. +// + +apply plugin: 'java' +apply plugin: 'maven' + +String mavenGroupId = 'stanhebben.minetweaker' +String mavenVersion = '3.0' + +sourceCompatibility = '1.8' +[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' + +repositories { + mavenCentral(); + // You may define additional repositories, or even remove "mavenCentral()". + // Read more about repositories here: + // http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories +} + +dependencies { + // Adding dependencies here will add the dependencies to each subproject. + testCompile group: 'junit', name: 'junit', version: '4.10' +} + +String mavenArtifactId = name + +group = mavenGroupId +version = mavenVersion + +task sourcesJar(type: Jar, dependsOn: classes, description: 'Creates a jar from the source files.') { + classifier = 'sources' + from sourceSets.main.allSource +} + +artifacts { + archives jar + archives sourcesJar +} + +configure(install.repositories.mavenInstaller) { + pom.project { + groupId = mavenGroupId + artifactId = mavenArtifactId + version = mavenVersion + } +} + +task createFolders(description: 'Creates the source folders if they do not exist.') doLast { + sourceSets*.allSource*.srcDirs*.each { File srcDir -> + if (!srcDir.isDirectory()) { + println "Creating source folder: ${srcDir}" + srcDir.mkdirs() + } + } +} diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..c6261bf --- /dev/null +++ b/settings.gradle @@ -0,0 +1,20 @@ +rootProject.name = 'MineTweaker3' + +// Find the directories containing a "build.gradle" file in the root directory +// of the project. That is, every directory containing a "build.gradle" will +// be automatically the subproject of this project. +def subDirs = rootDir.listFiles(new FileFilter() { + public boolean accept(File file) { + if (!file.isDirectory()) { + return false + } + if (file.name == 'buildSrc') { + return false + } + return new File(file, 'build.gradle').isFile() + } +}); + +subDirs.each { File dir -> + include dir.name +}