diff --git a/.github/workflows/main.yml b/.github/workflows/native.yml similarity index 75% rename from .github/workflows/main.yml rename to .github/workflows/native.yml index 2ca6949..744b89a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/native.yml @@ -1,4 +1,4 @@ -name: Build release on tag +name: Build native release on tag on: push: tags: @@ -7,7 +7,7 @@ jobs: gradle: strategy: matrix: - os: [ubuntu-latest, macos-latest, windows-latest] + os: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 @@ -20,4 +20,4 @@ jobs: uses: gradle/gradle-build-action@v2 - name: Execute Gradle build - run: ./gradlew build + run: ./gradlew nativeBinaries \ No newline at end of file diff --git a/README.md b/README.md index 2be6e80..4b77cc0 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ Probably one could automate the setup using systemd and/or cronjobs, but so far ## Building It should be enough to do a simple: ```bash -./gradlew build +./gradlew nativeBinaries ``` You can then find the executable in `build/bin/native/hcReleaseExecutable`. diff --git a/build.gradle.kts b/build.gradle.kts index 0d1cfb7..3d4a7df 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,10 +1,18 @@ +val toolVersion: String by project +val koinVersion: String by project +val cliktVersion: String by project +val coroutinesVersion: String by project +val serializationVersion: String by project +val datetimeVersion: String by project +val ktorVersion: String by project + plugins { kotlin("multiplatform") version "1.6.10" kotlin("plugin.serialization") version "1.6.10" } group = "fi.schro" -version = "1.0.0" +version = toolVersion repositories { mavenCentral() @@ -12,12 +20,11 @@ repositories { kotlin { val hostOs = System.getProperty("os.name") - val isMingwX64 = hostOs.startsWith("Windows") val nativeTarget = when { hostOs == "Mac OS X" -> macosX64("native") hostOs == "Linux" -> linuxX64("native") - isMingwX64 -> mingwX64("native") + hostOs.startsWith("Windows") -> mingwX64("native") else -> throw GradleException("Host OS is not supported in Kotlin/Native.") } @@ -36,17 +43,19 @@ kotlin { } sourceSets { - val nativeMain by getting { + val commonMain by getting { dependencies { - implementation("io.insert-koin:koin-core:3.1.5") - implementation("com.github.ajalt.clikt:clikt:3.4.0") - implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2") - implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.2") - implementation("io.ktor:ktor-client-core:1.6.7") - implementation("io.ktor:ktor-client-cio:1.6.7") - implementation("io.ktor:ktor-client-serialization:1.6.7") + implementation(kotlin("stdlib")) + implementation("io.insert-koin:koin-core:$koinVersion") + implementation("com.github.ajalt.clikt:clikt:$cliktVersion") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion") + implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion") + implementation("org.jetbrains.kotlinx:kotlinx-datetime:$datetimeVersion") + implementation("io.ktor:ktor-client-core:$ktorVersion") + implementation("io.ktor:ktor-client-cio:$ktorVersion") } } - val nativeTest by getting + val commonTest by getting + val nativeMain by getting } -} +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 550aa4e..113459f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,11 @@ kotlin.code.style=official kotlin.mpp.enableGranularSourceSetsMetadata=true kotlin.native.enableDependencyPropagation=false -kotlin.native.cacheKind.linuxX64=none \ No newline at end of file +kotlin.native.cacheKind.linuxX64=none +toolVersion=1.0.0 +koinVersion=3.1.5 +cliktVersion=3.4.0 +coroutinesVersion=1.5.2-native +serializationVersion=1.3.2 +datetimeVersion=0.3.2 +ktorVersion=1.6.7 \ No newline at end of file diff --git a/src/nativeMain/kotlin/fi/schro/data/ConfigurationRepository.kt b/src/commonMain/kotlin/fi/schro/data/ConfigurationRepository.kt similarity index 94% rename from src/nativeMain/kotlin/fi/schro/data/ConfigurationRepository.kt rename to src/commonMain/kotlin/fi/schro/data/ConfigurationRepository.kt index 0b189b9..4c99944 100644 --- a/src/nativeMain/kotlin/fi/schro/data/ConfigurationRepository.kt +++ b/src/commonMain/kotlin/fi/schro/data/ConfigurationRepository.kt @@ -21,12 +21,13 @@ interface ConfigurationRepository { } class ConfigurationRepositoryImpl( - private val lightRepository: LightRepository + private val lightRepository: LightRepository, + private val fileUtil: FileUtil ): ConfigurationRepository { private val timezone = TimeZone.currentSystemDefault() override suspend fun applyConfiguration(configurationFilePath: String, lightAddress: String, port: Int?) { - val configString = FileUtil.readAllText(configurationFilePath) + val configString = fileUtil.readAllText(configurationFilePath) val configuration = Json.decodeFromString(configString) val statusToApply = determineCurrentStatus(configuration) diff --git a/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt b/src/commonMain/kotlin/fi/schro/data/ElgatoLightRepository.kt similarity index 99% rename from src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt rename to src/commonMain/kotlin/fi/schro/data/ElgatoLightRepository.kt index fcb0598..0017eba 100644 --- a/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt +++ b/src/commonMain/kotlin/fi/schro/data/ElgatoLightRepository.kt @@ -13,6 +13,7 @@ import fi.schro.data.ElgatoLightRepository.Companion.MIN_TEMPERATURE import fi.schro.ui.LightPowerStatus import fi.schro.util.clamp import io.ktor.client.* +import io.ktor.client.call.* import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* diff --git a/src/nativeMain/kotlin/fi/schro/data/LightRepository.kt b/src/commonMain/kotlin/fi/schro/data/LightRepository.kt similarity index 100% rename from src/nativeMain/kotlin/fi/schro/data/LightRepository.kt rename to src/commonMain/kotlin/fi/schro/data/LightRepository.kt diff --git a/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt b/src/commonMain/kotlin/fi/schro/ui/HappyCatCommand.kt similarity index 100% rename from src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt rename to src/commonMain/kotlin/fi/schro/ui/HappyCatCommand.kt diff --git a/src/commonMain/kotlin/fi/schro/util/FileUtil.kt b/src/commonMain/kotlin/fi/schro/util/FileUtil.kt new file mode 100644 index 0000000..f77348f --- /dev/null +++ b/src/commonMain/kotlin/fi/schro/util/FileUtil.kt @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2022 Florian Schrofner + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +package fi.schro.util + +interface FileUtil { + fun readAllText(filePath: String): String +} \ No newline at end of file diff --git a/src/nativeMain/kotlin/fi/schro/util/NumberExt.kt b/src/commonMain/kotlin/fi/schro/util/NumberExt.kt similarity index 100% rename from src/nativeMain/kotlin/fi/schro/util/NumberExt.kt rename to src/commonMain/kotlin/fi/schro/util/NumberExt.kt diff --git a/src/nativeMain/kotlin/fi/schro/util/TimeUtil.kt b/src/commonMain/kotlin/fi/schro/util/TimeUtil.kt similarity index 100% rename from src/nativeMain/kotlin/fi/schro/util/TimeUtil.kt rename to src/commonMain/kotlin/fi/schro/util/TimeUtil.kt diff --git a/src/nativeMain/kotlin/fi/schro/di/Modules.kt b/src/nativeMain/kotlin/fi/schro/di/Modules.kt index 85a7658..54583c0 100644 --- a/src/nativeMain/kotlin/fi/schro/di/Modules.kt +++ b/src/nativeMain/kotlin/fi/schro/di/Modules.kt @@ -16,9 +16,10 @@ import fi.schro.ui.ApplyCommand import fi.schro.ui.DaemonCommand import fi.schro.ui.GetCommand import fi.schro.ui.SetCommand +import fi.schro.util.FileUtil +import fi.schro.util.FileUtilImpl import io.ktor.client.* import io.ktor.client.engine.cio.* -import io.ktor.client.features.json.* import org.koin.dsl.module val commandModule = module { @@ -30,18 +31,21 @@ val commandModule = module { val dataModule = module { single { ElgatoLightRepository() } - single { ConfigurationRepositoryImpl(get()) } + single { ConfigurationRepositoryImpl(get(), get()) } } val networkModule = module { //HttpClient has to be recreated every time it is used - factory { HttpClient(CIO){ - install(JsonFeature) - }} + factory { HttpClient(CIO)} +} + +val fileModule = module { + single { FileUtilImpl() } } val mainModule = listOf( commandModule, dataModule, - networkModule + networkModule, + fileModule ) \ No newline at end of file diff --git a/src/nativeMain/kotlin/fi/schro/util/FileUtil.kt b/src/nativeMain/kotlin/fi/schro/util/FileUtil.kt index a1b9158..6042e25 100644 --- a/src/nativeMain/kotlin/fi/schro/util/FileUtil.kt +++ b/src/nativeMain/kotlin/fi/schro/util/FileUtil.kt @@ -16,8 +16,8 @@ import platform.posix.fclose import platform.posix.fgets import platform.posix.fopen -object FileUtil { - fun readAllText(filePath: String): String { +class FileUtilImpl: FileUtil { + override fun readAllText(filePath: String): String { val returnBuffer = StringBuilder() val file = fopen(filePath, "r") ?: throw IllegalArgumentException("Cannot open input file $filePath")