From 397b60b60ab20e4ba282eefd7e06678fac9aa8a0 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Wed, 29 Mar 2017 15:52:28 -0700 Subject: [PATCH] Filter non existent source directory. --- .../com/beust/kobalt/app/BuildFileCompiler.kt | 131 ++++------- .../kotlin/com/beust/kobalt/app/BuildFiles.kt | 7 +- .../com/beust/kobalt/app/ParsedBuildFile.kt | 214 ------------------ 3 files changed, 48 insertions(+), 304 deletions(-) delete mode 100644 src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt b/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt index 29d3fcbd..a7f62ca2 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildFileCompiler.kt @@ -11,7 +11,6 @@ import com.beust.kobalt.internal.IncrementalManager import com.beust.kobalt.internal.KobaltSettings import com.beust.kobalt.internal.ParallelLogger import com.beust.kobalt.internal.PluginInfo -import com.beust.kobalt.internal.build.BuildSources import com.beust.kobalt.internal.build.IBuildSources import com.beust.kobalt.internal.build.VersionFile import com.beust.kobalt.maven.DependencyManager @@ -76,97 +75,63 @@ class BuildFileCompiler @Inject constructor(@Assisted("buildSources") val buildS return projectResult } - val parsedBuildFiles = arrayListOf<_ParsedBuildFile>() - class FindProjectResult(val context: KobaltContext, val projects: List, val pluginUrls: List, val taskResult: TaskResult) -// private fun findProjects(context: KobaltContext): FindProjectResult { -// buildFiles.run(File(".").absolutePath, context) -// val pluginUrls = Plugins.dynamicPlugins.map { it.jarFile.get().toURI().toURL()} -// val projects = listOf() -// val result = FindProjectResult(context, projects, pluginUrls, TaskResult()) -// return result -// } - private fun findProjects(context: KobaltContext): FindProjectResult { val root = buildSources.root var errorTaskResult: TaskResult? = null val projects = arrayListOf() - run { -// buildFiles.forEach { buildFile -> - // Parse kobalt/src/Build.kt -// val parsedBuildFile = parseBuildFile(context, buildSources) -// parsedBuildFiles.add(parsedBuildFile) -// val pluginUrls = parsedBuildFile.pluginUrls -// val buildScriptJarFile = File(KFiles.findBuildScriptLocation(buildSources, SCRIPT_JAR)) + // Parse the build files in kobalt/src/*.kt, which will analyze all the buildScriptInfo{} sections + // and possibly add new source build directories. The output of this process is a new Build.kt + // file that contains the aggregation of all the build files with the profiles applied and with + // the included build files inserted at the correct line. + val newBuildKt = buildFiles.parseBuildFiles(root.absolutePath, context) - val newBuildKt = buildFiles.run(root.absolutePath, context) + // + // Save the current build script absolute directory + // + context.internalContext.absoluteDir = buildSources.root - // - // Save the current build script absolute directory - // - context.internalContext.absoluteDir = buildSources.root - - // If the script jar files were generated by a different version, wipe them in case the API - // changed in-between - val buildScriptJarDir = KFiles.findBuildScriptDir(root.absolutePath) - buildScriptJarDir.let { dir -> - if (! VersionFile.isSameVersionFile(dir)) { - kobaltLog(1, "Detected new installation, wiping $dir") - dir.listFiles().map(File::delete) - } + // If buildScript.jar was generated by a different version, wipe them it case the API + // changed in-between + val buildScriptJarDir = KFiles.findBuildScriptDir(root.absolutePath) + buildScriptJarDir.let { dir -> + if (! VersionFile.isSameVersionFile(dir)) { + kobaltLog(1, "Detected new installation, wiping $dir") + dir.listFiles().map(File::delete) } - - val buildScriptJarFile = File(KFiles.findBuildScriptDir(root.absolutePath), SCRIPT_JAR) - - // - // Compile the newly generated Build.kt file - // - val pluginUrls = Plugins.dynamicPlugins.map { it.jarFile.get().toURI().toURL() } - val containsProfiles = false - val taskResult = maybeCompileBuildFile(context, listOf(newBuildKt.absolutePath), - buildScriptJarFile, pluginUrls, context.internalContext.forceRecompile, - containsProfiles) - - // - // Now that Build.kt has been parsed, we might have additional build files (buildSources will - // return additional build files) so we parse again. - // -// Kobalt.buildSourceDirs.forEach { dir -> -// val additionalSourceFiles = BuildSources(File(dir)) -// val newParsedBuildFile = parseBuildFile(context, additionalSourceFiles) -// -// // Write the modified Build.kt (e.g. maybe profiles were applied) to a temporary file, -// // compile it, jar it in buildScript.jar and run it -// val modifiedBuildFile = KFiles.createTempBuildFileInTempDirectory(deleteOnExit = true) -// KFiles.saveFile(modifiedBuildFile, newParsedBuildFile.nonBuildScriptCode) -// val taskResult = maybeCompileBuildFile(context, listOf(modifiedBuildFile.path), -// buildScriptJarFile, pluginUrls, context.internalContext.forceRecompile, -// newParsedBuildFile.containsProfiles) - if (taskResult.success) { - projects.addAll(buildScriptUtil.runBuildScriptJarFile(buildScriptJarFile, pluginUrls, context)) - } else { - if (errorTaskResult == null) { - errorTaskResult = taskResult - } -// } - } - - // Clear the absolute dir - context.internalContext.absoluteDir = null - } - val pluginUrls = parsedBuildFiles.flatMap { it.pluginUrls } - return FindProjectResult(context, projects, pluginUrls, - if (errorTaskResult != null) errorTaskResult!! else TaskResult()) - } - private fun maybeCompileBuildFile(context: KobaltContext, buildSources: BuildSources, buildScriptJarFile: File, - pluginUrls: List, forceRecompile: Boolean, containsProfiles: Boolean) : TaskResult - = maybeCompileBuildFile(context, buildSources.findSourceFiles().map { it.path }, buildScriptJarFile, - pluginUrls, forceRecompile, containsProfiles) + val buildScriptJarFile = File(KFiles.findBuildScriptDir(root.absolutePath), SCRIPT_JAR) + + // + // Compile the newly generated Build.kt file + // + val pluginUrls = Plugins.dynamicPlugins.map { it.jarFile.get().toURI().toURL() } + val containsProfiles = false + val taskResult = maybeCompileBuildFile(context, listOf(newBuildKt.absolutePath), + buildScriptJarFile, pluginUrls, context.internalContext.forceRecompile, + containsProfiles) + + // + // Run the new Build.kt + // + if (taskResult.success) { + projects.addAll(buildScriptUtil.runBuildScriptJarFile(buildScriptJarFile, pluginUrls, context)) + } else { + if (errorTaskResult == null) { + errorTaskResult = taskResult + } + } + + // Clear the absolute dir + context.internalContext.absoluteDir = null + + return FindProjectResult(context, projects, pluginUrls, + if (errorTaskResult != null) errorTaskResult else TaskResult()) + } fun maybeCompileBuildFile(context: KobaltContext, sourceFiles: List, buildScriptJarFile: File, pluginUrls: List, forceRecompile: Boolean, containsProfiles: Boolean) : TaskResult { @@ -208,12 +173,4 @@ class BuildFileCompiler @Inject constructor(@Assisted("buildSources") val buildS return result } } - - /** - * Generate the script file with only the plugins()/repos() directives and run it. Then return - * - the source code for the modified Build.kt (after profiles are applied) - * - the URL's of all the plug-ins that were found. - */ -// private fun _parseBuildFile(context: KobaltContext, buildSources: IBuildSources) = -// _ParsedBuildFile(buildSources, context, buildScriptUtil, dependencyManager, files) } diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt b/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt index dcc3cf13..61e6c6c7 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildFiles.kt @@ -38,7 +38,7 @@ fun main(argv: Array) { context.pluginInfo = PluginInfo(KobaltPluginXml(), null, null) Kobalt.init(MainModule(args, KobaltSettings.readSettingsXml())) val bf = Kobalt.INJECTOR.getInstance(BuildFiles::class.java) - bf.run(homeDir("kotlin/klaxon/"), context) + bf.parseBuildFiles(homeDir("kotlin/klaxon/"), context) } class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, @@ -67,7 +67,8 @@ class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, val result = arrayListOf() val sourceDirs = arrayListOf().apply { add(root + File.separator + KOBALT_SRC) }.map(::File) - sourceDirs.forEach { dir -> + // It's possible for no build file to be present (e.g. testing) + sourceDirs.filter { it.exists() }.forEach { dir -> result.addAll(findFiles(dir, { it.name.endsWith(".kt") })) } return result @@ -76,7 +77,7 @@ class BuildFiles @Inject constructor(val factory: BuildFileCompiler.IFactory, /** * @return the new Build.kt */ - fun run(projectDir: String, context: KobaltContext) : File { + fun parseBuildFiles(projectDir: String, context: KobaltContext) : File { val sourceDirs = arrayListOf().apply { add(projectDir + File.separator + KOBALT_SRC) } val map = hashMapOf() val newSourceDirs = arrayListOf() diff --git a/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt b/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt deleted file mode 100644 index 19ae4012..00000000 --- a/src/main/kotlin/com/beust/kobalt/app/ParsedBuildFile.kt +++ /dev/null @@ -1,214 +0,0 @@ -package com.beust.kobalt.app - -import com.beust.kobalt.api.KobaltContext -import com.beust.kobalt.api.Project -import com.beust.kobalt.internal.build.IBuildSources -import com.beust.kobalt.maven.DependencyManager -import com.beust.kobalt.misc.BuildScriptInfo -import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.kobaltLog -import com.beust.kobalt.misc.warn -import com.google.inject.Singleton -import java.net.URL - -@Singleton -class _ParsedBuildFile(val buildSources: IBuildSources, val context: KobaltContext, val buildScriptUtil: -BuildScriptUtil, - val dependencyManager: DependencyManager, val files: KFiles) { - private val profileLines = arrayListOf() - private val projects = arrayListOf() - private val activeProfiles = arrayListOf() - private val nonBuildScript = arrayListOf() - - var containsProfiles = false - val pluginUrls = arrayListOf() - - /** - * Contains the addition of all the build files corrected with the active profiles and with - * the buildScript{} sections removed. - */ - val nonBuildScriptCode : String get() = nonBuildScript.joinToString("\n") - - init { - // Because profiles may have changed between two builds, we have to delete preBuildScript.jar file - // every time and then generate a new one (or skip that phase if no buildScript{} was found in the - // buid files) -// preBuildScriptJarFile.delete() - -// val buildScriptInfos = parseBuildFile() - - // Only generate preBuildScript.jar if we found at least one buildScript{} -// if (buildScriptInfos.isNotEmpty()) { -// parseBuildScriptInfo(buildScriptInfos) -// } - -// generateFinalBuildFile(buildScriptInfos) - } - -// private fun generateFinalBuildFile(buildScriptInfos: List) { -// // -// // Write the build file to `nonBuildScript` excluding the buildScript{} directives since we already ran them -// // -// var lineNumber = 1 -// buildSources.findSourceFiles().forEach { buildFile -> -// val buildScriptInfo = buildScriptInfos.find { it.file == buildFile } -// if (buildFile == buildScriptInfo?.file) { -// println("Found file with buildScript in it: " + buildFile) -// } -// buildFile.forEachLine() { line -> -// if (buildScriptInfo == null || ! buildScriptInfo.isInSection(lineNumber)) { -// val cpl = correctProfileLine(line) -// if (cpl.startsWith("import")) nonBuildScript.add(0, cpl) -// else nonBuildScript.add(cpl) -// } -// lineNumber++ -// } -// } -// } - - /** - * If the current line matches one of the profiles, turn the declaration into - * val profile = true, otherwise return the same line - */ - fun correctProfileLine(line: String): String { - (context.profiles as List).forEach { profile -> - val re = Regex(".*va[rl][ \\t]+([a-zA-Z0-9_]+)[ \\t]*.*profile\\(\\).*") - val oldRe = Regex(".*va[rl][ \\t]+([a-zA-Z0-9_]+)[ \\t]*=[ \\t]*[tf][ra][ul][es].*") - val matcher = re.matchEntire(line) - val oldMatcher = oldRe.matchEntire(line) - - fun profileMatch(matcher: MatchResult?) : Pair { - val variable = if (matcher != null) matcher.groups[1]?.value else null - return Pair(profile == variable, variable) - } - - if ((matcher != null && matcher.groups.isNotEmpty()) - || (oldMatcher != null && oldMatcher.groups.isNotEmpty())) { - containsProfiles = true - val match = profileMatch(matcher) - val oldMatch = profileMatch(oldMatcher) - if (match.first || oldMatch.first) { - val variable = if (match.first) match.second else oldMatch.second - - if (oldMatch.first) { - warn("Old profile syntax detected for \"$line\"," + - " please update to \"val $variable by profile()\"") - } - - with("val $variable = true") { - kobaltLog(2, "Activating profile $profile in build file") - activeProfiles.add(profile) - profileLines.add(this) - return this - } - } - } - } - return line - } - -// private fun parseBuildFile() : List { -// fun applyProfiles(lines: List): List { -// val result = arrayListOf() -// lines.forEach { line -> -// result.add(correctProfileLine(line)) -// -// } -// return result -// } -// -// // -// // Take all the build files and adjust them with the active profiles -// // -// val buildScriptInfos = arrayListOf() -// val buildWithCorrectProfiles = arrayListOf() -// val buildFiles = buildSources.findSourceFiles() -// buildFiles.forEach { -// buildWithCorrectProfiles.addAll(applyProfiles(it.readLines())) -// -// // -// // Now extract all the `buildScript{}` blocks from all these build files -// // -// val lsi = BlockExtractor(Pattern.compile("^val.*buildScript.*\\{"), '{', '}') -// .extractBlock(it, buildWithCorrectProfiles) -// if (lsi != null) buildScriptInfos.add(lsi) -// } -// -// return buildScriptInfos -// } - - /** - * Generate preBuildScript.jar based on the buildScript{} found in the build files. - */ -// private fun parseBuildScriptInfo(buildScriptInfos: List) { -// buildScriptInfos.forEach { buildScriptInfo -> -// buildScriptInfo.sections.forEach { section -> -// val buildScriptSection = (buildScriptInfo.imports + -// buildScriptInfo.fullBuildFile.subList(section.start - 1, section.end)) -// .joinToString("\n") -// println("=== Compiling\n" + buildScriptSection + " for line " + (section.start - 1)) -// -// // -// // Compile and run preBuildScriptCode, which contains all the plugins() calls extracted. This -// // will add all the dynamic plugins found in this code to Plugins.dynamicPlugins -// // -// val buildScriptSourceFile = KFiles.createTempBuildFileInTempDirectory(deleteOnExit = true) -// buildScriptSourceFile.writeText(buildScriptSection, Charset.defaultCharset()) -// kobaltLog(2, "Saved " + KFiles.fixSlashes(buildScriptSourceFile.absolutePath)) -// -// // -// // Compile to preBuildScript.jar -// // -// -// val dir = preBuildScriptJarFile.parentFile -// dir.mkdirs() -// val bsJar = java.io.File(dir, "buildScript-" + section.start + ".jar") -// generateJarFile(context, listOf(buildScriptSourceFile.path), bsJar) -// VersionFile.generateVersionFile(preBuildScriptJarFile.parentFile) -// Kobalt.context!!.internalContext.buildFileOutOfDate = true -// -// // -// // Run preBuildScript.jar to initialize plugins and repos -// // -// val currentDirs = arrayListOf().apply { addAll(Kobalt.buildSourceDirs) } -// projects.addAll(buildScriptUtil.runBuildScriptJarFile(bsJar, arrayListOf(), context)) -// val newDirs = arrayListOf().apply { addAll(Kobalt.buildSourceDirs) } -// newDirs.removeAll(currentDirs) -// buildScriptInfo.includedBuildSourceDirs.add(IncludedBuildSourceDir(section.start - 1, newDirs)) -// println("*** ADDED DIRECTORIES " + newDirs) -// } -// } -// -// // -// // All the plug-ins are now in Plugins.dynamicPlugins, download them if they're not already -// // -// Plugins.dynamicPlugins.forEach { -// pluginUrls.add(it.jarFile.get().toURI().toURL()) -// } -// } - -// private fun generateJarFile(context: KobaltContext, sourceFiles: List, -// buildScriptJarFile: File, originalFile: BuildFile? = null) { -// // -// // Compile the jar file -// // -// val kotlinDeps = dependencyManager.calculateDependencies(null, context) -// val deps: List = kotlinDeps.map { it.jarFile.get().absolutePath } -// val outputJar = File(buildScriptJarFile.absolutePath) -// val saved = context.internalContext.noIncrementalKotlin -// val result = kotlinCompilePrivate { -// classpath(files.kobaltJar) -// classpath(deps) -// sourceFiles(sourceFiles) -// output = outputJar -// noIncrementalKotlin = true -// }.compile(context = context) -// if (! result.success) { -// val org = originalFile?.realPath ?: sourceFiles.joinToString(",") -// throw KobaltException("Couldn't compile $org:\n" + result.errorMessage) -// } -// -// context.internalContext.noIncrementalKotlin = saved -// } -} -