From a9c771bb9379e9d594bbe78e4772be885f733f62 Mon Sep 17 00:00:00 2001 From: Anton F Date: Fri, 9 Sep 2022 10:42:22 +0200 Subject: [PATCH] Multiplatform file reading --- .../kotlin/com/libermall/tnt/Utility.kt | 2 ++ .../com/libermall/tnt/command/MintCommand.kt | 6 ++--- .../com/libermall/tnt/readFileAsString.kt | 24 +++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 src/jvmMain/kotlin/com/libermall/tnt/readFileAsString.kt diff --git a/src/commonMain/kotlin/com/libermall/tnt/Utility.kt b/src/commonMain/kotlin/com/libermall/tnt/Utility.kt index 63f509f..c1ab5de 100644 --- a/src/commonMain/kotlin/com/libermall/tnt/Utility.kt +++ b/src/commonMain/kotlin/com/libermall/tnt/Utility.kt @@ -63,3 +63,5 @@ fun ContentData.flatten(): ByteArray = when (this) { is ContentData.Snake -> this.data.flatten() is ContentData.Chunks -> TODO("chunky content data") } + +expect fun readFileAsString(file: String): String diff --git a/src/commonMain/kotlin/com/libermall/tnt/command/MintCommand.kt b/src/commonMain/kotlin/com/libermall/tnt/command/MintCommand.kt index 016084c..9e7a3d9 100644 --- a/src/commonMain/kotlin/com/libermall/tnt/command/MintCommand.kt +++ b/src/commonMain/kotlin/com/libermall/tnt/command/MintCommand.kt @@ -31,14 +31,15 @@ import com.libermall.tnt.model.CollectionModel import com.libermall.tnt.model.MintableModel import com.libermall.tnt.model.SpecModel import com.libermall.tnt.model.StandaloneItemModel +import com.libermall.tnt.readFileAsString import com.libermall.tnt.toSafeBounceable import com.libermall.tnt.toSafeUnbounceable import kotlinx.coroutines.delay import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withTimeout import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json -import kotlinx.serialization.json.decodeFromStream import mu.KLogging import org.koin.core.component.KoinComponent import org.koin.core.component.inject @@ -47,7 +48,6 @@ import org.ton.cell.Cell import org.ton.cell.CellBuilder import org.ton.lite.client.LiteClient import org.ton.tlb.storeTlb -import java.io.File class MintCommand : CliktCommand( name = "mint", @@ -62,7 +62,7 @@ class MintCommand : CliktCommand( @OptIn(ExperimentalSerializationApi::class) val spec by argument().convert { try { - Json.decodeFromStream(File(it).inputStream()) + Json.decodeFromString(readFileAsString(it)) } catch (e: Exception) { logger.warn(e, {}) throw e diff --git a/src/jvmMain/kotlin/com/libermall/tnt/readFileAsString.kt b/src/jvmMain/kotlin/com/libermall/tnt/readFileAsString.kt new file mode 100644 index 0000000..e3d01f7 --- /dev/null +++ b/src/jvmMain/kotlin/com/libermall/tnt/readFileAsString.kt @@ -0,0 +1,24 @@ +/* + * (T)ON (N)FT (T)ool - all-in-one utility to work with NFTs on The Open Network + * Copyright (c) 2022 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.libermall.tnt + +import java.io.File + +actual fun readFileAsString(file: String): String = + File(file).readText()