Each platform is processed separately but in parallel.
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.preprocessor
|
package org.jetbrains.kotlin.preprocessor
|
||||||
|
|
||||||
interface Evaluator : Function1<List<Conditional>, Boolean>
|
interface Evaluator : (List<Conditional>) -> Boolean
|
||||||
|
|
||||||
abstract class PlatformEvaluator : Evaluator {
|
abstract class PlatformEvaluator : Evaluator {
|
||||||
final override fun invoke(conditions: List<Conditional>): Boolean = evaluate(conditions.filterIsInstance())
|
final override fun invoke(conditions: List<Conditional>): Boolean = evaluate(conditions.filterIsInstance())
|
||||||
|
|||||||
+141
-98
@@ -26,6 +26,8 @@ import org.jetbrains.kotlin.idea.JetFileType
|
|||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
@@ -33,62 +35,21 @@ fun main(args: Array<String>) {
|
|||||||
|
|
||||||
val sourcePath = File(args.first())
|
val sourcePath = File(args.first())
|
||||||
|
|
||||||
processRecursive(sourcePath, File("libraries/stdlib/target/jvm6"))
|
// val evaluators = listOf(JvmPlatformEvaluator(version = 7), JsPlatformEvaluator())
|
||||||
|
|
||||||
val configuration = CompilerConfiguration()
|
//println("Using condition evaluator: $evaluators")
|
||||||
val environment = KotlinCoreEnvironment.createForProduction(Disposable { }, configuration, emptyList())
|
|
||||||
|
|
||||||
val project = environment.project
|
val targetPath = File("libraries/stdlib/target")
|
||||||
val jetPsiFactory = JetPsiFactory(project)
|
|
||||||
val fileType = JetFileType.INSTANCE
|
|
||||||
|
|
||||||
val evaluators = listOf(JvmPlatformEvaluator(version = 7), JsPlatformEvaluator())
|
|
||||||
|
|
||||||
|
|
||||||
|
val profiles = listOf(6, 7, 8).map { Preprocessor(createJvmProfile(targetPath, version = it)) }
|
||||||
|
|
||||||
println("Using condition evaluator: $evaluators")
|
val pool = Executors.newFixedThreadPool(4)
|
||||||
|
|
||||||
(sourcePath.walk() as Sequence<File>)
|
profiles.forEach { pool.submit { it.processSources(sourcePath) } }
|
||||||
.filter { it.isFile && it.extension == fileType.defaultExtension }
|
|
||||||
.forEach { sourceFile ->
|
|
||||||
val sourceText = sourceFile.readText().convertLineSeparators()
|
|
||||||
val psiFile = jetPsiFactory.createFile(sourceFile.name, sourceText)
|
|
||||||
println("$psiFile")
|
|
||||||
|
|
||||||
val visitor = CollectModificationsVisitor(evaluators)
|
pool.shutdown()
|
||||||
psiFile.accept(visitor)
|
pool.awaitTermination(1, TimeUnit.MINUTES)
|
||||||
val modifications = visitor.elementModifications
|
|
||||||
|
|
||||||
for ((evaluator, list) in modifications) {
|
|
||||||
if (list.isNotEmpty()) {
|
|
||||||
val resultText = applyModifications(list, sourceText, evaluator)
|
|
||||||
println("Version of $sourceFile for $evaluator")
|
|
||||||
println(resultText)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun applyModifications(modifications: List<Modification>, sourceText: String, evaluator: Evaluator): String {
|
|
||||||
var prevIndex = 0
|
|
||||||
val result = StringBuilder()
|
|
||||||
for ((range, selector) in modifications) {
|
|
||||||
result.append(sourceText, prevIndex, range.startOffset)
|
|
||||||
val rangeText = range.substring(sourceText)
|
|
||||||
val newValue = selector(rangeText)
|
|
||||||
if (newValue.isEmpty()) {
|
|
||||||
result.append("/* Not available on $evaluator */")
|
|
||||||
repeat(StringUtil.getLineBreakCount(rangeText)) {
|
|
||||||
result.append("\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
result.append(newValue)
|
|
||||||
}
|
|
||||||
prevIndex = range.endOffset
|
|
||||||
}
|
|
||||||
result.append(sourceText, prevIndex, sourceText.length())
|
|
||||||
return result.toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -122,95 +83,177 @@ class CollectModificationsVisitor(evaluators: List<Evaluator>) : JetTreeVisitorV
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
println("declaration: ${declaration.javaClass.simpleName} $name${if (annotations.isNotEmpty()) ", annotations: ${annotations.joinToString { it.toString() }}, evaluation result: $declResults" else ""}")
|
//println("declaration: ${declaration.javaClass.simpleName} $name${if (annotations.isNotEmpty()) ", annotations: ${annotations.joinToString { it.toString() }}, evaluation result: $declResults" else ""}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
data class Profile(val name: String, val evaluator: Evaluator, val targetRoot: File)
|
data class Profile(val name: String, val evaluator: Evaluator, val targetRoot: File)
|
||||||
|
|
||||||
|
fun createJvmProfile(targetRoot: File, version: Int) = Profile("JVM$version", JvmPlatformEvaluator(version), File(targetRoot, "jvm$version"))
|
||||||
|
fun createJsProfile(targetRoot: File) = Profile("JS", JsPlatformEvaluator(), File(targetRoot, "js"))
|
||||||
|
|
||||||
public class Preprocessor {
|
|
||||||
|
public class Preprocessor(val profile: Profile) {
|
||||||
|
|
||||||
val fileType = JetFileType.INSTANCE
|
val fileType = JetFileType.INSTANCE
|
||||||
|
val jetPsiFactory: JetPsiFactory
|
||||||
|
|
||||||
|
init {
|
||||||
|
val configuration = CompilerConfiguration()
|
||||||
|
val environment = KotlinCoreEnvironment.createForProduction(Disposable { }, configuration, emptyList())
|
||||||
|
|
||||||
|
val project = environment.project
|
||||||
|
jetPsiFactory = JetPsiFactory(project)
|
||||||
|
}
|
||||||
|
|
||||||
sealed class FileProcessingResult {
|
sealed class FileProcessingResult {
|
||||||
object Skip : FileProcessingResult()
|
object Skip : FileProcessingResult()
|
||||||
object Copy : FileProcessingResult()
|
object Copy : FileProcessingResult()
|
||||||
|
|
||||||
class Modify(val resultText: String) : FileProcessingResult()
|
class Modify(val sourceText: String, val modifications: List<Modification>) : FileProcessingResult()
|
||||||
}
|
}
|
||||||
|
|
||||||
// private fun processFile(sourceFile: File, evaluator: Evaluator): FileProcessingResult {
|
public fun processSources(sourceRoot: File) {
|
||||||
// if (sourceFile.extension != fileType.defaultExtension)
|
processDirectorySingleEvaluator(sourceRoot, profile.targetRoot, profile.evaluator)
|
||||||
// return FileProcessingResult.Copy
|
}
|
||||||
//
|
|
||||||
// val sourceText = sourceFile.readText().convertLineSeparators()
|
|
||||||
// val psiFile = jetPsiFactory.createFile(sourceFile.name, sourceText)
|
|
||||||
// println("$psiFile")
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun processDirectory(sourceRoot: File, targetRelativeRoot: File, profiles: List<Profile>) {
|
private fun processFileSingleEvaluator(sourceFile: File, evaluator: Evaluator): FileProcessingResult {
|
||||||
|
if (sourceFile.extension != fileType.defaultExtension)
|
||||||
|
return FileProcessingResult.Copy
|
||||||
|
|
||||||
val (sourceFiles, sourceDirectories) = sourceRoot.listFiles().partition { !it.isDirectory }
|
val sourceText = sourceFile.readText().convertLineSeparators()
|
||||||
|
val psiFile = jetPsiFactory.createFile(sourceFile.name, sourceText)
|
||||||
|
println("$psiFile")
|
||||||
|
|
||||||
// TODO: keep processed file list for each profile
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun processRecursive(sourceRoot: File, targetRoot: File) {
|
val fileAnnotations = psiFile.parseConditionalAnnotations()
|
||||||
val (sourceFiles, sourceDirectories) = sourceRoot.listFiles().partition { !it.isDirectory }
|
if (!evaluator(fileAnnotations))
|
||||||
|
return FileProcessingResult.Skip
|
||||||
|
|
||||||
val processedFiles = hashSetOf<File>()
|
|
||||||
for (sourceFile in sourceFiles)
|
|
||||||
{
|
|
||||||
// TODO: only if .kt
|
|
||||||
val resultText = processFileText(sourceFile)
|
|
||||||
|
|
||||||
// if (keepFile)
|
val visitor = CollectModificationsVisitor(listOf(evaluator))
|
||||||
|
psiFile.accept(visitor)
|
||||||
|
|
||||||
val destFile = sourceFile.makeRelativeTo(sourceRoot, targetRoot)
|
val list = visitor.elementModifications.values().single()
|
||||||
// if no modifications — copy
|
return if (list.isNotEmpty())
|
||||||
|
FileProcessingResult.Modify(sourceText, list)
|
||||||
|
else
|
||||||
|
FileProcessingResult.Copy
|
||||||
|
}
|
||||||
|
|
||||||
processedFiles += destFile
|
private fun processDirectorySingleEvaluator(sourceRoot: File, targetRoot: File, evaluator: Evaluator) {
|
||||||
|
val (sourceFiles, sourceDirectories) = sourceRoot.listFiles().partition { !it.isDirectory }
|
||||||
|
|
||||||
if (destFile.exists()) {
|
val processedFiles = hashSetOf<File>()
|
||||||
if (destFile.isDirectory)
|
for (sourceFile in sourceFiles)
|
||||||
|
{
|
||||||
|
val result = processFileSingleEvaluator(sourceFile, evaluator)
|
||||||
|
if (result is FileProcessingResult.Skip)
|
||||||
|
continue
|
||||||
|
|
||||||
|
val destFile = sourceFile.makeRelativeTo(sourceRoot, targetRoot)
|
||||||
|
processedFiles += destFile
|
||||||
|
|
||||||
|
if (destFile.exists() && destFile.isDirectory)
|
||||||
destFile.deleteRecursively()
|
destFile.deleteRecursively()
|
||||||
else
|
|
||||||
if (destFile.isTextEqualTo(resultText))
|
// if no modifications — copy
|
||||||
|
if (result is FileProcessingResult.Copy) {
|
||||||
|
FileUtil.copy(sourceFile, destFile)
|
||||||
|
} else if (result is FileProcessingResult.Modify) {
|
||||||
|
val resultText = applyModifications(result.modifications, result.sourceText, evaluator)
|
||||||
|
if (destFile.exists() && destFile.isTextEqualTo(resultText))
|
||||||
continue
|
continue
|
||||||
|
destFile.writeText(resultText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (sourceDir in sourceDirectories) {
|
||||||
|
val destDir = sourceDir.makeRelativeTo(sourceRoot, targetRoot)
|
||||||
|
if (!destDir.exists()) {
|
||||||
|
destDir.mkdirsOrFail()
|
||||||
|
}
|
||||||
|
else if (!destDir.isDirectory) {
|
||||||
|
destDir.delete()
|
||||||
|
}
|
||||||
|
processDirectorySingleEvaluator(sourceDir, destDir, evaluator)
|
||||||
|
processedFiles += destDir
|
||||||
|
}
|
||||||
|
|
||||||
|
targetRoot.listFiles().forEach { targetFile ->
|
||||||
|
if (!processedFiles.remove(processedFiles.find { FileUtil.filesEqual(it, targetFile) })) {
|
||||||
|
targetFile.deleteRecursively()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
destFile.writeText(resultText)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (sourceDir in sourceDirectories) {
|
|
||||||
val destDir = sourceDir.makeRelativeTo(sourceRoot, targetRoot)
|
private fun processFileMultiEvaluators(sourceFile: File, evaluators: List<Evaluator>): List<FileProcessingResult> {
|
||||||
if (!destDir.exists()) {
|
if (sourceFile.extension != fileType.defaultExtension)
|
||||||
destDir.mkdirsOrFail()
|
return evaluators map { FileProcessingResult.Copy }
|
||||||
|
|
||||||
|
val sourceText = sourceFile.readText().convertLineSeparators()
|
||||||
|
val psiFile = jetPsiFactory.createFile(sourceFile.name, sourceText)
|
||||||
|
println("$psiFile")
|
||||||
|
|
||||||
|
val results = hashMapOf<Evaluator, FileProcessingResult>()
|
||||||
|
|
||||||
|
val fileAnnotations = psiFile.parseConditionalAnnotations()
|
||||||
|
evaluators.forEach { evaluator ->
|
||||||
|
if (!evaluator(fileAnnotations))
|
||||||
|
results += evaluator to FileProcessingResult.Skip
|
||||||
}
|
}
|
||||||
else if (!destDir.isDirectory) {
|
|
||||||
destDir.delete()
|
val visitor = CollectModificationsVisitor(evaluators - results.keySet())
|
||||||
|
psiFile.accept(visitor)
|
||||||
|
|
||||||
|
for ((evaluator, list) in visitor.elementModifications) {
|
||||||
|
val result =
|
||||||
|
if (list.isNotEmpty())
|
||||||
|
FileProcessingResult.Modify(sourceText, list)
|
||||||
|
else
|
||||||
|
FileProcessingResult.Copy
|
||||||
|
results += evaluator to result
|
||||||
}
|
}
|
||||||
processRecursive(sourceDir, destDir)
|
return evaluators.map { results[it]!! }
|
||||||
processedFiles += destDir
|
|
||||||
}
|
}
|
||||||
|
|
||||||
targetRoot.listFiles().forEach { targetFile ->
|
private fun processDirectoryMultiEvaluators(sourceRoot: File, targetRelativeRoot: File, profiles: List<Profile>) {
|
||||||
if (!processedFiles.remove(processedFiles.find { FileUtil.filesEqual(it, targetFile) })) {
|
|
||||||
targetFile.deleteRecursively()
|
val (sourceFiles, sourceDirectories) = sourceRoot.listFiles().partition { !it.isDirectory }
|
||||||
}
|
|
||||||
|
// TODO: keep processed file list for each profile
|
||||||
|
val processedFiles = profiles.toMap({ it }, { hashSetOf<File>()})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun applyModifications(modifications: List<Modification>, sourceText: String, evaluator: Evaluator): String {
|
||||||
|
var prevIndex = 0
|
||||||
|
val result = StringBuilder()
|
||||||
|
for ((range, selector) in modifications) {
|
||||||
|
result.append(sourceText, prevIndex, range.startOffset)
|
||||||
|
val rangeText = range.substring(sourceText)
|
||||||
|
val newValue = selector(rangeText)
|
||||||
|
if (newValue.isEmpty()) {
|
||||||
|
result.append("/* Not available on $evaluator */")
|
||||||
|
repeat(StringUtil.getLineBreakCount(rangeText)) {
|
||||||
|
result.append("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.append(newValue)
|
||||||
|
}
|
||||||
|
prevIndex = range.endOffset
|
||||||
|
}
|
||||||
|
result.append(sourceText, prevIndex, sourceText.length())
|
||||||
|
return result.toString()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun processFileText(sourceFile: File): String = sourceFile.readText()
|
|
||||||
|
|
||||||
fun String.convertLineSeparators(): String = StringUtil.convertLineSeparators(this)
|
fun String.convertLineSeparators(): String = StringUtil.convertLineSeparators(this)
|
||||||
|
|
||||||
|
|
||||||
fun File.isTextEqualTo(content: String): Boolean = readText().lines() == content.lines()
|
fun File.isTextEqualTo(content: String): Boolean = readText().lines() == content.lines()
|
||||||
|
|
||||||
fun File.makeRelativeTo(from: File, to: File) = File(to, relativeTo(from))
|
fun File.makeRelativeTo(from: File, to: File) = File(to, relativeTo(from))
|
||||||
|
|||||||
Reference in New Issue
Block a user