Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlinとずっと一緒にいたい話 with Kotlin Scripting

umakoz
September 14, 2019

Kotlinとずっと一緒にいたい話 with Kotlin Scripting

umakoz

September 14, 2019
Tweet

Transcript

  1. Kotlin ͱ ͣͬͱҰॹʹ͍͍ͨ࿩ with Kotlin Scripting LINE Fukuoka അݟ ੣

    @umakoz Coroutine ϋϯζΦϯ by DroidKaigi @ LINE Fukuoka
  2. generate.main.kts overview Command-line͔Β࣮ߦ͢Δscript @file:Repository("https://jcenter.bintray.com") @file:DependsOn("org.apache.poi:poi-ooxml:4.1.0") @file:DependsOn("org.freemarker:freemarker:2.3.29") @file:Import("EventXlsxParser.kt") import freemarker.template.Configuration import

    ... val filePath = args[0] val events = EventXlsxParser().parse("${filePath}${File.separator}Events.xlsx") val configuration = Configuration(Configuration.getVersion()).apply { setDirectoryForTemplateLoading(File("./templates")) } val template = configuration.getTemplate("ActionEvent.ftl") val writer = FileWriter("${filePath}${File.separator}ActionEvent.kt") val data = hashMapOf<String, Any>("events" to events) template.process(data, writer)
  3. generate.main.kts note 2 • file-level annotaionʹΑΓscriptػೳ֦ு͢Δ @file:ImportͰඞཁͳґଘ΋@file:DependsOnͰࢦఆ͢Δ // maven repository

    @file:Repository("https://jcenter.bintray.com") // 3rd-party library @file:DependsOn("org.apache.poi:poi-ooxml:4.1.0") @file:DependsOn("org.freemarker:freemarker:2.3.29") // import another Kotlin code @file:Import("EventXlsxParser.kt")
  4. EventXlsxParser.kt import java.nio.file.Paths import org.apache.poi.ss.usermodel.WorkbookFactory class EventXlsxParser { fun parseXlsx(filePath:

    String): List<Map<String, String>> { val file = Paths.get(filePath).toFile() val workbook = WorkbookFactory.create(file) val sheet = workbook.getSheet("Action") val keys = sheet.first().map { cell -> cell.stringCellValue } return sheet.rowIterator().asSequence().drop(1) .map { row -> keys.mapIndexed { index, key -> key to row.getCell(index).stringCellValue }.toMap() } .toList() } }
  5. generate.main.kts note 5 • ͋ͱ͸ී௨ͷcodeΛॻ͘ val events = EventXlsxParser().parse("${filePath}${File.separator}Events.xlsx") val

    configuration = Configuration(Configuration.getVersion()).apply { setDirectoryForTemplateLoading(File("./")) } val template = configuration.getTemplate("ActionEvent.ftl") val writer = FileWriter("${filePath}${File.separator}ActionEvent.kt") val data = hashMapOf<String, Any>("events" to events) template.process(data, writer)
  6. ActionEvent.ftl sealed class ActionEvent( val type: String, val value: String

    ) { <#assign currentType = ""> <#list events as event> <#if currentType != event.type> <#assign currentType = event.type> <#assign typeValues = events?filter(v -> v.type == currentType)> class ${currentType?cap_first} { <#typeValues as typeValue> object ${typeValue.value?cap_first} : ActionEvent("${typeValue.type}", "${typeValue.value}") </#list> } </#if> </#list> }
  7. ActionEvent.kt sealed class ActionEvent( val type: String, val value: String

    ) { class Foo { object On : ActionEvent("bar", "on") object Off : ActionEvent("bar", "off") } }