Skip to content

Instantly share code, notes, and snippets.

@oshai
Last active February 9, 2023 10:45
Show Gist options
  • Save oshai/bd1a632d6d7f06f4ab3d1f2800c7b2d4 to your computer and use it in GitHub Desktop.
Save oshai/bd1a632d6d7f06f4ab3d1f2800c7b2d4 to your computer and use it in GitHub Desktop.
Stupid and simple convert a Scala file (already renamed to .kt extension) to Kotlin
#!/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 (!fileName.fileExists) {
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)
}
@JohnSColeman
Copy link

fileName.fileExists ? how about !File(fileName).isFile

what is the difference?

fun workOnFile(fileName: String) {
if (!fileName.fileExists) {

String.fileExists is not implemented

@oshai
Copy link
Author

oshai commented Jan 28, 2021

fileName.fileExists ? how about !File(fileName).isFile

what is the difference?

fun workOnFile(fileName: String) {
if (!fileName.fileExists) {

String.fileExists is not implemented

😀 probably I had this extension somewhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment