diff --git a/.github/workflows/jar.yml b/.github/workflows/jar.yml new file mode 100644 index 0000000..739778e --- /dev/null +++ b/.github/workflows/jar.yml @@ -0,0 +1,23 @@ +name: Build jar release on tag +on: + push: + tags: + - '*' +jobs: + gradle: + strategy: + matrix: + os: [ubuntu-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-java@v2 + with: + distribution: temurin + java-version: 11 + + - name: Setup Gradle + uses: gradle/gradle-build-action@v2 + + - name: Execute Gradle build + run: ./gradlew shadowJar \ No newline at end of file diff --git a/README.md b/README.md index 4b77cc0..a437c17 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,11 @@ Change your light settings easily inside scripts or use configuration files to a Please report issues, bugs or feature requests [here](https://codeberg.org/schrofi/happy-cat/issues). +## Running +Download the newest release for your system from the [releases page](https://codeberg.org/schrofi/happy-cat/releases). +On Linux and Mac OS you can run the native binaries by simply executing `./hc`. +On all other systems you have to resort to java by running the jar like `java -jar ./hc.jar` + ## Usage Happy cat is split up into multiple subcommands, each of which have their own parameters. To find out more about each command, check out the help pages by appending `--help` after the command. @@ -79,11 +84,19 @@ Timeframes can cross midnight, but *must not* overlap. If there are overlapping Probably one could automate the setup using systemd and/or cronjobs, but so far I didn't get to that. ## Building -It should be enough to do a simple: +### Linux and Mac OS +To build native binaries run: ```bash ./gradlew nativeBinaries ``` You can then find the executable in `build/bin/native/hcReleaseExecutable`. +### Windows and others +To build the Java package run: +```bash +./gradlew shadowJar +``` +You can then find the jar in `build/libs`. + ## License This software is licensed under the MPL 2.0, see the [LICENSE](LICENSE) file. diff --git a/build.gradle.kts b/build.gradle.kts index 3d4a7df..e79a9d4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,16 +9,28 @@ val ktorVersion: String by project plugins { kotlin("multiplatform") version "1.6.10" kotlin("plugin.serialization") version "1.6.10" + id("com.github.johnrengelman.shadow") version "7.1.2" + application } group = "fi.schro" version = toolVersion +application { + mainClass.set("fi.schro.Main") +} + repositories { mavenCentral() } kotlin { + //java compilation + jvm { + withJava() + } + + //native compilation val hostOs = System.getProperty("os.name") val nativeTarget = when { @@ -53,9 +65,11 @@ kotlin { implementation("org.jetbrains.kotlinx:kotlinx-datetime:$datetimeVersion") implementation("io.ktor:ktor-client-core:$ktorVersion") implementation("io.ktor:ktor-client-cio:$ktorVersion") + implementation("io.ktor:ktor-client-serialization:$ktorVersion") } } val commonTest by getting val nativeMain by getting + val jvmMain by getting } } \ No newline at end of file diff --git a/src/jvmMain/kotlin/fi/schro/Main.kt b/src/jvmMain/kotlin/fi/schro/Main.kt new file mode 100644 index 0000000..9cf1da0 --- /dev/null +++ b/src/jvmMain/kotlin/fi/schro/Main.kt @@ -0,0 +1,25 @@ +/* + * 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 + +import fi.schro.di.jvmModules +import fi.schro.ui.HappyCatCommand +import org.koin.core.context.startKoin + +object Main { + @JvmStatic + fun main(args: Array) { + startKoin { + modules(jvmModules) + } + + HappyCatCommand() + .main(args) + } +} \ No newline at end of file diff --git a/src/nativeMain/kotlin/fi/schro/di/Modules.kt b/src/jvmMain/kotlin/fi/schro/di/JvmModules.kt similarity index 79% rename from src/nativeMain/kotlin/fi/schro/di/Modules.kt rename to src/jvmMain/kotlin/fi/schro/di/JvmModules.kt index 54583c0..232b59e 100644 --- a/src/nativeMain/kotlin/fi/schro/di/Modules.kt +++ b/src/jvmMain/kotlin/fi/schro/di/JvmModules.kt @@ -8,6 +8,9 @@ package fi.schro.di +import fi.schro.util.FileUtil +import fi.schro.util.FileUtilImpl +import org.koin.dsl.module import fi.schro.data.ConfigurationRepository import fi.schro.data.ConfigurationRepositoryImpl import fi.schro.data.ElgatoLightRepository @@ -16,17 +19,15 @@ 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 org.koin.dsl.module +import io.ktor.client.features.json.* val commandModule = module { - single{ ApplyCommand(get()) } - single{ DaemonCommand(get()) } - single{ SetCommand(get()) } - single{ GetCommand(get()) } + single { ApplyCommand(get()) } + single { DaemonCommand(get()) } + single { SetCommand(get()) } + single { GetCommand(get()) } } val dataModule = module { @@ -36,14 +37,18 @@ val dataModule = module { val networkModule = module { //HttpClient has to be recreated every time it is used - factory { HttpClient(CIO)} + factory { + HttpClient(CIO) { + install(JsonFeature) + } + } } val fileModule = module { single { FileUtilImpl() } } -val mainModule = listOf( +val jvmModules = listOf( commandModule, dataModule, networkModule, diff --git a/src/jvmMain/kotlin/fi/schro/util/FileUtil.kt b/src/jvmMain/kotlin/fi/schro/util/FileUtil.kt new file mode 100644 index 0000000..462d831 --- /dev/null +++ b/src/jvmMain/kotlin/fi/schro/util/FileUtil.kt @@ -0,0 +1,19 @@ +/* + * 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 + +import java.io.File + +class FileUtilImpl: FileUtil { + override fun readAllText(filePath: String): String { + return File(filePath).inputStream().use { + it.readBytes().toString(Charsets.UTF_8) + } + } +} \ No newline at end of file diff --git a/src/nativeMain/kotlin/fi/schro/Main.kt b/src/nativeMain/kotlin/fi/schro/Main.kt index e2adfc9..7f8fbb5 100644 --- a/src/nativeMain/kotlin/fi/schro/Main.kt +++ b/src/nativeMain/kotlin/fi/schro/Main.kt @@ -8,13 +8,13 @@ package fi.schro -import fi.schro.di.mainModule +import fi.schro.di.nativeModules import fi.schro.ui.HappyCatCommand import org.koin.core.context.startKoin fun main(args: Array) { startKoin { - modules(mainModule) + modules(nativeModules) } HappyCatCommand() diff --git a/src/nativeMain/kotlin/fi/schro/di/NativeModules.kt b/src/nativeMain/kotlin/fi/schro/di/NativeModules.kt new file mode 100644 index 0000000..18babac --- /dev/null +++ b/src/nativeMain/kotlin/fi/schro/di/NativeModules.kt @@ -0,0 +1,57 @@ +/* + * 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.di + +import fi.schro.util.FileUtil +import fi.schro.util.FileUtilImpl +import org.koin.dsl.module +import fi.schro.data.ConfigurationRepository +import fi.schro.data.ConfigurationRepositoryImpl +import fi.schro.data.ElgatoLightRepository +import fi.schro.data.LightRepository +import fi.schro.ui.ApplyCommand +import fi.schro.ui.DaemonCommand +import fi.schro.ui.GetCommand +import fi.schro.ui.SetCommand +import io.ktor.client.* +import io.ktor.client.engine.cio.* +import io.ktor.client.features.json.* +import org.koin.dsl.module + +val commandModule = module { + single { ApplyCommand(get()) } + single { DaemonCommand(get()) } + single { SetCommand(get()) } + single { GetCommand(get()) } +} + +val dataModule = module { + single { ElgatoLightRepository() } + single { ConfigurationRepositoryImpl(get(), get()) } +} + +val networkModule = module { + //HttpClient has to be recreated every time it is used + factory { + HttpClient(CIO) { + install(JsonFeature) + } + } +} + +val fileModule = module { + single { FileUtilImpl() } +} + +val nativeModules = listOf( + commandModule, + dataModule, + networkModule, + fileModule +) \ No newline at end of file