diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 02130286..be86def3 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -11,7 +11,6 @@ import com.beust.kobalt.kotlin.BuildFile import com.beust.kobalt.kotlin.BuildFileCompiler import com.beust.kobalt.maven.DepFactory import com.beust.kobalt.maven.Http -import com.beust.kobalt.maven.KobaltException import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.misc.* import com.beust.kobalt.wrapper.Wrapper @@ -145,25 +144,8 @@ private class Main @Inject constructor( if (! buildFile.exists()) { error(buildFile.path.toFile().path + " does not exist") } else { - var allProjects = arrayListOf() - try { - allProjects.addAll(buildFileCompilerFactory.create(listOf(buildFile), pluginInfo) - .compileBuildFiles(args)) - } catch(ex: KobaltException) { - throw ex - } catch(ex: Throwable) { - ex.printStackTrace() - // This can happen if the ABI for the build script file changed. Try to wipe .kobalt. - log(2, "Couldn't parse preBuildScript.jar: ${ex.message}") - if (! File(".kobalt").deleteRecursively()) { - warn("Couldn't delete the .kobalt directory, please delete it manually and try again") - return 1 - } else { - log(1, "Deleted .kobalt") - allProjects.addAll(buildFileCompilerFactory.create(listOf(buildFile), pluginInfo) - .compileBuildFiles(args)) - } - } + val allProjects = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo) + .compileBuildFiles(args) // // Now that we have projects, add all the repos from repo contributors that need a Project @@ -171,6 +153,7 @@ private class Main @Inject constructor( allProjects.forEach { addReposFromContributors(it) } log(2, "Final list of repos:\n " + Kobalt.repos.joinToString("\n ")) + if (args.tasks) { // // List of tasks diff --git a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt index 975952a7..2faed971 100644 --- a/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/kotlin/BuildFileCompiler.kt @@ -26,6 +26,11 @@ import java.util.* import java.util.jar.JarInputStream import javax.inject.Inject +/** + * Manage the compilation of Build.kt. There are two passes for this processing: + * 1) Extract the repos() and plugins() statements in a separate .kt and compile it into preBuildScript.jar. + * 2) Actually build the whole Build.kt file after adding to the classpath whatever phase 1 found (plugins, repos) + */ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val buildFiles: List, @Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins, val jvmCompiler: JvmCompiler, val pluginProperties: PluginProperties) { @@ -39,14 +44,20 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b private val SCRIPT_JAR = "buildScript.jar" fun compileBuildFiles(args: Args): List { + // + // Create the KobaltContext + // val context = KobaltContext(args) context.pluginInfo = pluginInfo context.pluginProperties = pluginProperties Kobalt.context = context + // + // Find all the projects in the build file, possibly compiling them + // val allProjects = findProjects(context) - plugins.applyPlugins(context, allProjects) + return allProjects } @@ -56,6 +67,15 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b val pluginUrls = findPlugInUrls(context, buildFile) val buildScriptJarFile = File(KFiles.findBuildScriptLocation(buildFile, SCRIPT_JAR)) + // If the script jar files were generated by a different version, wipe them in case the API + // changed in-between + buildScriptJarFile.parentFile.let { dir -> + if (! isSameVersionFile(dir)) { + log(1, "Detected new installation, wiping $dir") + dir.listFiles().map { it.delete() } + } + } + maybeCompileBuildFile(context, buildFile, buildScriptJarFile, pluginUrls) val buildScriptInfo = parseBuildScriptJarFile(buildScriptJarFile, pluginUrls) result.addAll(buildScriptInfo.projects) @@ -127,6 +147,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b if (! isUpToDate(buildFile, File(buildScriptJar))) { buildScriptJarFile.parentFile.mkdirs() generateJarFile(context, BuildFile(Paths.get(pluginSourceFile.path), "Plugins"), buildScriptJarFile) + generateVersionFile(buildScriptJarFile.parentFile) } // @@ -144,6 +165,15 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b return result } + private val VERSION_FILE = "version.txt" + + private fun generateVersionFile(directory: File) { + KFiles.saveFile(File(directory, VERSION_FILE), Kobalt.version) + } + + private fun isSameVersionFile(directory: File) = + File(directory, VERSION_FILE).readText() == Kobalt.version + private fun generateJarFile(context: KobaltContext, buildFile: BuildFile, buildScriptJarFile: File) { val kotlintDeps = jvmCompiler.calculateDependencies(null, context, listOf()) val deps: List = kotlintDeps.map { it.jarFile.get().absolutePath }