From e3829c54259adfe89a59ef2d25df7f8404dee8e9 Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Sun, 5 Sep 2021 20:26:37 +0200 Subject: [PATCH 1/6] Add basic compilation and execution POSIX scripts --- compile.sh | 3 +++ run.sh | 3 +++ 2 files changed, 6 insertions(+) create mode 100755 compile.sh create mode 100755 run.sh diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..2ea97e4 --- /dev/null +++ b/compile.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +kotlinc app/src/main/kotlin/de/jotoho/waituntil/start.kt diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..62dc8d1 --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +kotlin de.jotoho.waituntil.StartKt $* -- 2.45.2 From 532cbc89a93d907f68868d5b3d4a7e2e2a5fcfe6 Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Sun, 5 Sep 2021 20:26:59 +0200 Subject: [PATCH 2/6] Basic implementation --- .../main/kotlin/de/jotoho/waituntil/start.kt | 80 ++++++++++++++++++- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/de/jotoho/waituntil/start.kt b/app/src/main/kotlin/de/jotoho/waituntil/start.kt index 3b90fbc..54bc9ce 100644 --- a/app/src/main/kotlin/de/jotoho/waituntil/start.kt +++ b/app/src/main/kotlin/de/jotoho/waituntil/start.kt @@ -1,9 +1,64 @@ package de.jotoho.waituntil +import java.util.Locale; +import java.util.Locale.LanguageRange; +import java.text.DateFormat; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.util.Date; +import java.util.TimeZone +import java.time.Instant; +import java.time.LocalTime; +import java.time.LocalDate; +import java.time.ZonedDateTime; +import java.time.temporal.ChronoUnit + // This file contains the main function and other utility function necessary for interpreting the terminal arguments. // See README.md and LICENSE.md for license information // Author: Jonas Tobias Hopusch (@jotoho) +val langGerman: String = Locale.GERMAN.getLanguage(); +val applicationOutputLanguage: String = if (Locale.getDefault().getLanguage().equals(Locale.GERMAN.getLanguage())) + Locale.GERMAN.getLanguage() + else Locale.ENGLISH.getLanguage(); + +fun waitUntilTimeStamp(timestamp: ZonedDateTime) { + Thread.sleep( + Math.max(Instant.now().until(timestamp, ChronoUnit.MILLIS), 0) + ); + + val formattedTimeStamp: String = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG) + .withZone(TimeZone.getDefault().toZoneId()) + .format(Instant.now()); + + when (applicationOutputLanguage) { + langGerman -> System.err.println("Erfolgreich bis $formattedTimeStamp gewartet!"); + else -> { + System.err.println("Successfully waited until $formattedTimeStamp"); + } + } +} + +fun calculateAndAnnounceTargetTime(userTimeInputRaw: String): ZonedDateTime { + val userTimeInputRelative = LocalTime.parse(userTimeInputRaw); + val userTimeInputAbsolute = ZonedDateTime.of(LocalDate.now(), userTimeInputRelative, TimeZone.getDefault().toZoneId()); + + val userTimeInputFinal = if (Instant.now().isBefore(userTimeInputAbsolute.toInstant())) userTimeInputAbsolute else userTimeInputAbsolute.plusDays(1); + + val formattedTimeStamp = userTimeInputFinal.format( + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG) + ); + + when (applicationOutputLanguage) { + langGerman -> System.err.println("Dieses Program wird bis zum $formattedTimeStamp warten."); + else -> { + println("WaitUntil will suspend until $formattedTimeStamp"); + } + } + + return userTimeInputFinal; +} + fun main(args: Array) { val optionDictionary = mapOf(Pair("-h", "--help")); @@ -25,8 +80,25 @@ fun main(args: Array) { words.add(arg); } - println("Content of options:"); - println(options.toString()); - println("Content of words:"); - println(words.toString()); + if (options.contains("help")) { + when (applicationOutputLanguage) { + langGerman -> println("Hilfe kommt noch. (Nicht implementiert)"); + else -> { + println("Help is yet to come. (Not implemented)"); + } + } + } + else if (words.size == 1) { + val target = calculateAndAnnounceTargetTime(words.iterator().next()); + waitUntilTimeStamp(target); + } + else { + when (applicationOutputLanguage) { + langGerman -> System.err.println("FATAL: Es wurde exact ein nicht-flag Argument erwartet. (${words.size} erhalten)"); + else -> { + System.err.println("FATAL: Expected one non-flag argument. (Got ${words.size})"); + } + } + System.exit(1); + } } -- 2.45.2 From 874a8dacc096daf4d70bcd9b968dfc71c4539309 Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Sun, 5 Sep 2021 20:31:52 +0200 Subject: [PATCH 3/6] Reduce and reformat imports --- .../main/kotlin/de/jotoho/waituntil/start.kt | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/de/jotoho/waituntil/start.kt b/app/src/main/kotlin/de/jotoho/waituntil/start.kt index 54bc9ce..32f4911 100644 --- a/app/src/main/kotlin/de/jotoho/waituntil/start.kt +++ b/app/src/main/kotlin/de/jotoho/waituntil/start.kt @@ -1,16 +1,13 @@ package de.jotoho.waituntil -import java.util.Locale; -import java.util.Locale.LanguageRange; -import java.text.DateFormat; -import java.time.format.DateTimeFormatter; -import java.time.format.FormatStyle; -import java.util.Date; +import java.util.Locale +import java.time.format.DateTimeFormatter +import java.time.format.FormatStyle import java.util.TimeZone -import java.time.Instant; -import java.time.LocalTime; -import java.time.LocalDate; -import java.time.ZonedDateTime; +import java.time.Instant +import java.time.LocalTime +import java.time.LocalDate +import java.time.ZonedDateTime import java.time.temporal.ChronoUnit // This file contains the main function and other utility function necessary for interpreting the terminal arguments. -- 2.45.2 From a8247c8ca2e2062eecaea9cf1ae18613ccd25eff Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Sun, 5 Sep 2021 20:40:27 +0200 Subject: [PATCH 4/6] Scripts: Change scripts to generate portable jar-file and run JRE directly --- compile.sh | 2 +- run.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compile.sh b/compile.sh index 2ea97e4..4066495 100755 --- a/compile.sh +++ b/compile.sh @@ -1,3 +1,3 @@ #!/bin/sh -kotlinc app/src/main/kotlin/de/jotoho/waituntil/start.kt +kotlinc app/src/main/kotlin/de/jotoho/waituntil/start.kt -include-runtime -d waituntil.jar diff --git a/run.sh b/run.sh index 62dc8d1..ece8c9a 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,3 @@ #!/bin/sh -kotlin de.jotoho.waituntil.StartKt $* +java -jar waituntil.jar $* -- 2.45.2 From 1044147ffe0f137fa93202fb713ce20b95f2180b Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Sun, 5 Sep 2021 21:11:28 +0200 Subject: [PATCH 5/6] gradle: Always use newest version of kotlin plugin --- app/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 81f3487..9fd91b0 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -8,7 +8,7 @@ plugins { // Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin. - id("org.jetbrains.kotlin.jvm") version "1.5.0" + id("org.jetbrains.kotlin.jvm") version "1.+" // Apply the application plugin to add support for building a CLI application in Java. application -- 2.45.2 From 267c2877778503613f0f7165baaa458c859571fa Mon Sep 17 00:00:00 2001 From: Jonas Tobias Hopusch Date: Sun, 5 Sep 2021 21:12:28 +0200 Subject: [PATCH 6/6] Always target Java 16 during compilation --- app/build.gradle.kts | 13 +++++++++++++ compile.sh | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 9fd91b0..061be2a 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -36,6 +36,19 @@ dependencies { testImplementation("org.jetbrains.kotlin:kotlin-test-junit") } +java { + sourceCompatibility = JavaVersion.VERSION_16 + targetCompatibility = JavaVersion.VERSION_16 +} + +tasks { + compileKotlin { + kotlinOptions { + jvmTarget = "16" + } + } +} + application { // Define the main class for the application. mainClass.set("de.jotoho.waituntil.StartKt") diff --git a/compile.sh b/compile.sh index 4066495..94bbae6 100755 --- a/compile.sh +++ b/compile.sh @@ -1,3 +1,3 @@ #!/bin/sh -kotlinc app/src/main/kotlin/de/jotoho/waituntil/start.kt -include-runtime -d waituntil.jar +kotlinc app/src/main/kotlin/de/jotoho/waituntil/start.kt -jvm-target 16 -include-runtime -d waituntil.jar -- 2.45.2