J2K KotlinModuleXmlBuilder: convert

Original commit: a36e6a2e07
This commit is contained in:
Ilya Chernikov
2016-01-18 23:06:22 +03:00
committed by Zalim Bashorov
parent 2537e44fc2
commit 2878d8d56d
@@ -14,133 +14,123 @@
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.modules; package org.jetbrains.kotlin.modules
import org.jetbrains.annotations.NotNull; import com.intellij.openapi.util.io.FileUtil.toSystemIndependentName
import org.jetbrains.jps.builders.java.JavaModuleBuildTargetType; import com.intellij.openapi.util.text.StringUtil.escapeXml
import org.jetbrains.kotlin.config.IncrementalCompilation; import org.jetbrains.jps.builders.java.JavaModuleBuildTargetType
import org.jetbrains.kotlin.jps.build.JvmSourceRoot; import org.jetbrains.kotlin.cli.common.modules.ModuleXmlParser.*
import org.jetbrains.kotlin.utils.Printer; import org.jetbrains.kotlin.config.IncrementalCompilation
import org.jetbrains.kotlin.jps.build.JvmSourceRoot
import org.jetbrains.kotlin.utils.Printer
import java.io.File
import java.io.File; class KotlinModuleXmlBuilder {
import java.util.Collection; private val xml = StringBuilder()
import java.util.Collections; private val p = Printer(xml)
import java.util.List; private var done = false
import java.util.Set;
import static com.intellij.openapi.util.io.FileUtil.toSystemIndependentName; init {
import static com.intellij.openapi.util.text.StringUtil.escapeXml; openTag(p, MODULES)
import static org.jetbrains.kotlin.cli.common.modules.ModuleXmlParser.*;
public class KotlinModuleXmlBuilder {
private final StringBuilder xml = new StringBuilder();
private final Printer p = new Printer(xml);
private boolean done = false;
public KotlinModuleXmlBuilder() {
openTag(p, MODULES);
} }
public KotlinModuleXmlBuilder addModule( fun addModule(
String moduleName, moduleName: String,
String outputDir, outputDir: String,
List<File> sourceFiles, sourceFiles: List<File>,
List<JvmSourceRoot> javaSourceRoots, javaSourceRoots: List<JvmSourceRoot>,
Collection<File> classpathRoots, classpathRoots: Collection<File>,
JavaModuleBuildTargetType targetType, targetType: JavaModuleBuildTargetType,
Set<File> directoriesToFilterOut, directoriesToFilterOut: Set<File>,
@NotNull List<File> friendDirs friendDirs: List<File>): KotlinModuleXmlBuilder {
) { assert(!done) { "Already done" }
assert !done : "Already done";
if (targetType.isTests()) { if (targetType.isTests) {
p.println("<!-- Module script for tests -->"); p.println("<!-- Module script for tests -->")
} }
else { else {
p.println("<!-- Module script for production -->"); p.println("<!-- Module script for production -->")
} }
p.println("<", MODULE, " ", p.println("<", MODULE, " ",
NAME, "=\"", escapeXml(moduleName), "\" ", NAME, "=\"", escapeXml(moduleName), "\" ",
TYPE, "=\"", escapeXml(targetType.getTypeId()), "\" ", TYPE, "=\"", escapeXml(targetType.typeId), "\" ",
OUTPUT_DIR, "=\"", getEscapedPath(new File(outputDir)), "\">" OUTPUT_DIR, "=\"", getEscapedPath(File(outputDir)), "\">")
); p.pushIndent()
p.pushIndent();
for (File friendDir : friendDirs) { for (friendDir in friendDirs) {
p.println("<", FRIEND_DIR, " ", PATH, "=\"", getEscapedPath(friendDir), "\"/>"); p.println("<", FRIEND_DIR, " ", PATH, "=\"", getEscapedPath(friendDir), "\"/>")
} }
for (File sourceFile : sourceFiles) { for (sourceFile in sourceFiles) {
p.println("<", SOURCES, " ", PATH, "=\"", getEscapedPath(sourceFile), "\"/>"); p.println("<", SOURCES, " ", PATH, "=\"", getEscapedPath(sourceFile), "\"/>")
} }
processJavaSourceRoots(javaSourceRoots); processJavaSourceRoots(javaSourceRoots)
processClasspath(classpathRoots, directoriesToFilterOut); processClasspath(classpathRoots, directoriesToFilterOut)
closeTag(p, MODULE); closeTag(p, MODULE)
return this; return this
} }
private void processClasspath( private fun processClasspath(
@NotNull Collection<File> files, files: Collection<File>,
@NotNull Set<File> directoriesToFilterOut directoriesToFilterOut: Set<File>) {
) { p.println("<!-- Classpath -->")
p.println("<!-- Classpath -->"); for (file in files) {
for (File file : files) { val isOutput = directoriesToFilterOut.contains(file) && !IncrementalCompilation.isEnabled()
boolean isOutput = directoriesToFilterOut.contains(file) && !IncrementalCompilation.isEnabled();
if (isOutput) { if (isOutput) {
// For IDEA's make (incremental compilation) purposes, output directories of the current module and its dependencies // For IDEA's make (incremental compilation) purposes, output directories of the current module and its dependencies
// appear on the class path, so we are at risk of seeing the results of the previous build, i.e. if some class was // appear on the class path, so we are at risk of seeing the results of the previous build, i.e. if some class was
// removed in the sources, it may still be there in binaries. Thus, we delete these entries from the classpath. // removed in the sources, it may still be there in binaries. Thus, we delete these entries from the classpath.
p.println("<!-- Output directory, commented out -->"); p.println("<!-- Output directory, commented out -->")
p.println("<!-- "); p.println("<!-- ")
p.pushIndent(); p.pushIndent()
} }
p.println("<", CLASSPATH, " ", PATH, "=\"", getEscapedPath(file), "\"/>"); p.println("<", CLASSPATH, " ", PATH, "=\"", getEscapedPath(file), "\"/>")
if (isOutput) { if (isOutput) {
p.popIndent(); p.popIndent()
p.println("-->"); p.println("-->")
} }
} }
} }
private void processJavaSourceRoots(@NotNull List<JvmSourceRoot> roots) { private fun processJavaSourceRoots(roots: List<JvmSourceRoot>) {
p.println("<!-- Java source roots -->"); p.println("<!-- Java source roots -->")
for (JvmSourceRoot root : roots) { for (root in roots) {
p.print("<"); p.print("<")
p.printWithNoIndent(JAVA_SOURCE_ROOTS, " ", PATH, "=\"", getEscapedPath(root.getFile()), "\""); p.printWithNoIndent(JAVA_SOURCE_ROOTS, " ", PATH, "=\"", getEscapedPath(root.file), "\"")
if (root.getPackagePrefix() != null) { if (root.packagePrefix != null) {
p.printWithNoIndent(" ", JAVA_SOURCE_PACKAGE_PREFIX, "=\"", root.getPackagePrefix(), "\""); p.printWithNoIndent(" ", JAVA_SOURCE_PACKAGE_PREFIX, "=\"", root.packagePrefix, "\"")
} }
p.printWithNoIndent("/>"); p.printWithNoIndent("/>")
p.println(); p.println()
} }
} }
public CharSequence asText() { fun asText(): CharSequence {
if (!done) { if (!done) {
closeTag(p, MODULES); closeTag(p, MODULES)
done = true; done = true
} }
return xml; return xml
} }
private static void openTag(Printer p, String tag) { private fun openTag(p: Printer, tag: String) {
p.println("<" + tag + ">"); p.println("<$tag>")
p.pushIndent(); p.pushIndent()
} }
private static void closeTag(Printer p, String tag) { private fun closeTag(p: Printer, tag: String) {
p.popIndent(); p.popIndent()
p.println("</" + tag + ">"); p.println("</$tag>")
} }
private static String getEscapedPath(File sourceFile) { private fun getEscapedPath(sourceFile: File): String {
return escapeXml(toSystemIndependentName(sourceFile.getPath())); return escapeXml(toSystemIndependentName(sourceFile.path))
} }
} }