-
-
Save ohmerhe/bf31d22270bac958ea40e43d24a244f0 to your computer and use it in GitHub Desktop.
Stupid and simple convert a Scala file (already renamed to .kt extension) to Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env kscript | |
import java.io.File | |
// usage - one argument a .kt file (Scala file that was only renamed) | |
// or a directory | |
try { | |
main(args) | |
} catch (e: Exception) { | |
e.printStackTrace() | |
} | |
fun convert(lines: List<String>): List<String> { | |
val methodNoBracsRegex = ".*fun\\s+\\w+\\s+[:=].*".toRegex() | |
val linesWithoutLicense = lines | |
// The below lines just removed license comment | |
// if (lines[0].startsWith("package ")) | |
// lines | |
// else | |
// lines.drop(15) | |
val result = mutableListOf<String>() | |
linesWithoutLicense.forEach { lineBeforeConv -> | |
val convertedLine = lineBeforeConv | |
.replace("extends", ":") | |
.replace(" def ", " fun ") | |
.replace("BigInt(", "BigInteger(") | |
.replace("trait", "interface") | |
.replace("[", "<") | |
.replace("]", ">") | |
.replace(" = {", " {") | |
.replace(" new ", " ") | |
.replace(" Future<", " CompletableFuture<") | |
.replace(" Promise<", " CompletableFuture<") | |
.replace(" Array<Byte>(", " ByteArray(") | |
.replace(" Array<Char>(", " CharArray(") | |
.replace("with", ",") | |
.replace("match", "when") | |
.replace("case class", "data class") | |
.replace("case _", "else") | |
.replace("case ", "") | |
.replace("=>", "->") | |
.replace(".asInstanceOf<", " as ") //manually fix > | |
.replace("final ", "") | |
.replace("fun this(", "constructor(") | |
.replace(" Seq<", " List<") | |
.replace(" IndexedSeq<", " List<") | |
.replace("<:", ":") | |
when { | |
convertedLine.startsWith("import ") -> { | |
val importsLines = if (convertedLine.contains("{")) { | |
val before = convertedLine.substringBefore("{") | |
convertedLine.substringAfter("{").substringBefore("}").split(",") | |
.map { "$before${it.trim()}" } | |
} else listOf(convertedLine) | |
importsLines.map { it.replace("_", "*") }.forEach { | |
result.add(it) | |
} | |
} | |
convertedLine.matches(methodNoBracsRegex) -> { | |
if (convertedLine.contains(":")) | |
result.add(convertedLine.replace(":", "():")) | |
else | |
result.add(convertedLine.replace("=", "()=")) | |
} | |
else -> result.add(convertedLine) | |
} | |
} | |
return result | |
} | |
fun main(args: Array<String>) { | |
val fileName = args[0] | |
if (fileName.endsWith(".kt")) { | |
workOnFile(fileName) | |
} else { | |
File(fileName).walk().forEach { | |
if (it.name.endsWith(".kt")) { | |
workOnFile(it.path) | |
} | |
} | |
} | |
} | |
fun readFileAsLinesUsingReadLines(fileName: String): List<String> = File(fileName).readLines() | |
fun workOnFile(fileName: String) { | |
if (!File(fileName).isFile) { | |
println("WARN: file not exists $fileName") | |
return | |
} | |
println("working on $fileName") | |
val lines = readFileAsLinesUsingReadLines(fileName) | |
val fileContent = convert(lines).joinToString("\n") | |
File(fileName).writeText(fileContent) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment