From 6b034710f841057cedd30624433aec98cdb43fb5 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sun, 11 Oct 2015 09:22:18 -0700 Subject: [PATCH] Delete ScriptCompiler. --- src/main/kotlin/com/beust/kobalt/Main.kt | 6 - .../com/beust/kobalt/kotlin/ScriptCompiler.kt | 134 ------------------ .../com/beust/kobalt/misc/MainModule.kt | 26 ++-- 3 files changed, 12 insertions(+), 154 deletions(-) delete mode 100644 src/main/kotlin/com/beust/kobalt/kotlin/ScriptCompiler.kt diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index c2b0adcc..3ae59202 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -5,16 +5,11 @@ import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.internal.* import com.beust.kobalt.kotlin.BuildFile -import com.beust.kobalt.kotlin.ScriptCompiler import com.beust.kobalt.maven.* import com.beust.kobalt.misc.* -import com.beust.kobalt.SystemProperties import com.beust.kobalt.kotlin.ScriptCompiler2 -import com.beust.kobalt.plugin.publish.JCenterApi -import com.beust.kobalt.plugin.publish.UnauthenticatedJCenterApi import com.beust.kobalt.wrapper.Wrapper import com.google.inject.Guice -import com.google.inject.Injector import java.io.File import java.nio.file.Paths import java.util.* @@ -35,7 +30,6 @@ public fun main(argv: Array) { } private class Main @Inject constructor( - val scriptCompilerFactory: ScriptCompiler.IFactory, val script2: ScriptCompiler2.IFactory, val plugins: Plugins, val taskManager: TaskManager, diff --git a/src/main/kotlin/com/beust/kobalt/kotlin/ScriptCompiler.kt b/src/main/kotlin/com/beust/kobalt/kotlin/ScriptCompiler.kt deleted file mode 100644 index cd9af012..00000000 --- a/src/main/kotlin/com/beust/kobalt/kotlin/ScriptCompiler.kt +++ /dev/null @@ -1,134 +0,0 @@ -package com.beust.kobalt.kotlin - -import com.beust.kobalt.Plugins -import com.beust.kobalt.api.Kobalt -import com.beust.kobalt.api.Plugin -import com.beust.kobalt.api.Project -import com.beust.kobalt.api.annotation.Task -import com.beust.kobalt.maven.KobaltException -import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.KobaltLogger -import com.beust.kobalt.plugin.kotlin.kotlinCompilePrivate -import com.google.inject.assistedinject.Assisted -import java.io.File -import java.io.FileInputStream -import java.io.InputStream -import java.lang.reflect.Modifier -import java.net.URLClassLoader -import java.util.jar.JarInputStream -import javax.inject.Inject -import kotlin.properties.Delegates - -/** - * Compile Build.kt into a jar file - */ -public class ScriptCompiler @Inject constructor( - @Assisted("jarFiles") val jarFiles: List, - @Assisted("instantiate") val instantiate: (ClassLoader, String) -> Class<*>, - val files: KFiles) : KobaltLogger { - - interface IFactory { - fun create(@Assisted("jarFiles") jarFiles: List, - @Assisted("instantiate") instantiate: (ClassLoader, String) -> Class<*>) : ScriptCompiler - } - - private var buildScriptJarFile by Delegates.notNull() - - public class CompileOutput(val projects: List, val plugins: List, val classLoader: ClassLoader) - - public fun compile(buildFile: BuildFile, lastModified: Long, jarFileName: String) : CompileOutput { - - if (! buildFile.exists()) { - throw KobaltException("Couldn't find ${buildFile.name}") - } - - buildScriptJarFile = File(jarFileName) - buildScriptJarFile.parentFile.mkdirs() - - log(2, "Running build file ${buildFile.name} jar: ${buildScriptJarFile}") - - if (buildFile.exists() && buildScriptJarFile.exists() - && lastModified < buildScriptJarFile.lastModified()) { - log(2, "Build file is up to date") - } else { - log(2, "Need to recompile ${buildFile.name}") - generateJarFile(buildFile) - } - val pi = instantiateBuildFile() - return CompileOutput(pi.projects, arrayListOf(), pi.classLoader) - } - - private fun generateJarFile(buildFile: BuildFile) { - kotlinCompilePrivate { - classpath(files.kobaltJar) - classpath(jarFiles) - sourceFiles(buildFile.path.toFile().absolutePath) - output = buildScriptJarFile.absolutePath - }.compile() - } - - class ProjectInfo(val projects: List, val classLoader: ClassLoader) - - private fun instantiateBuildFile() : ProjectInfo { - val result = arrayListOf() - var stream : InputStream? = null - val classLoader = URLClassLoader(arrayOf(buildScriptJarFile.toURI().toURL())) - try { - log(1, "!!!!!!!!! CREATED CLASSLOADER FOR buildScriptJarFile: $classLoader") - stream = JarInputStream(FileInputStream(buildScriptJarFile)) - var entry = stream.nextJarEntry - - val classes = hashSetOf>() - while (entry != null) { - val name = entry.name; - if (name.endsWith(".class")) { - val className = name.substring(0, name.length() - 6).replace("/", ".") - var cl : Class<*>? = instantiate(classLoader, className) - if (cl != null) { - classes.add(cl) - } else { - throw KobaltException("Couldn't instantiate ${className}") - } - } - entry = stream.nextJarEntry; - } - - // Invoke all the "val" found on the _DefaultPackage class (the Build.kt file) - classes.filter { cls -> - cls.name != "_DefaultPackage" - }.forEach { cls -> - cls.methods.forEach { method -> - // Invoke vals and see if they return a Project - if (method.name.startsWith("get") && Modifier.isStatic(method.modifiers)) { - val r = method.invoke(null) - if (r is Project) { - log(2, "Found project ${r} in class ${cls}") - result.add(r) - } - } else { - val taskAnnotation = method.getAnnotation(Task::class.java) - if (taskAnnotation != null) { -// Plugins.defaultPlugin.addTask(taskAnnotation, ) - Plugins.defaultPlugin.methodTasks.add(Plugin.MethodTask(method, taskAnnotation)) - } - - }} -// cls.methods.filter { method -> -// method.getName().startsWith("get") && Modifier.isStatic(method.getModifiers()) -// }.forEach { -// val r = it.invoke(null) -// if (r is Project) { -// log(2, "Found project ${r} in class ${cls}") -// result.add(r) -// } -// } - } - } finally { - stream?.close() - } - - // Now that we all the projects, sort them topologically - return ProjectInfo(Kobalt.sortProjects(result), classLoader) - } -} - diff --git a/src/main/kotlin/com/beust/kobalt/misc/MainModule.kt b/src/main/kotlin/com/beust/kobalt/misc/MainModule.kt index b90a68be..93735433 100644 --- a/src/main/kotlin/com/beust/kobalt/misc/MainModule.kt +++ b/src/main/kotlin/com/beust/kobalt/misc/MainModule.kt @@ -1,7 +1,6 @@ package com.beust.kobalt.misc import com.beust.kobalt.Args -import com.beust.kobalt.kotlin.ScriptCompiler import com.beust.kobalt.kotlin.ScriptCompiler2 import com.beust.kobalt.maven.ArtifactFetcher import com.beust.kobalt.maven.LocalRepo @@ -41,7 +40,6 @@ public open class MainModule(val args: Args) : AbstractModule() { PomGenerator.IFactory::class.java, JCenterApi.IFactory::class.java, Pom.IFactory::class.java, - ScriptCompiler.IFactory::class.java, ScriptCompiler2.IFactory::class.java, ArtifactFetcher.IFactory::class.java) .forEach { @@ -58,17 +56,17 @@ public open class MainModule(val args: Args) : AbstractModule() { }) - // bindListener(Matchers.any(), object: TypeListener { - // override fun hear(typeLiteral: TypeLiteral?, typeEncounter: TypeEncounter?) { - // val bean = object: InjectionListener { - // override public fun afterInjection(injectee: I) { - // if (Scopes.isCircularProxy(injectee)) { - // println("CYCLE: " + typeLiteral?.getRawType()?.getName()); - // } - // } - // } - // typeEncounter?.register(bean) - // } - // }) +// bindListener(Matchers.any(), object: TypeListener { +// override fun hear(typeLiteral: TypeLiteral?, typeEncounter: TypeEncounter?) { +// val bean = object: InjectionListener { +// override public fun afterInjection(injectee: I) { +// if (Scopes.isCircularProxy(injectee)) { +// println("CYCLE: " + typeLiteral?.getRawType()?.getName()); +// } +// } +// } +// typeEncounter?.register(bean) +// } +// }) } }