Compare commits

..

18 commits

Author SHA1 Message Date
4238605180
Merge pull request 'Post-Implementation refactoring of application' (#9) from refactoring into master
Reviewed-on: https://gitea.jotoho.de/jotoho/waituntil/pulls/9
2021-09-19 20:40:48 +02:00
5a80ac7169
Fix compile script using old source path 2021-09-19 20:27:26 +02:00
4ab78f4028
Avoid unnecessary temporary variable in short-hand translation 2021-09-19 20:23:26 +02:00
a48f122296
Delete unneeded comment from build config 2021-09-19 20:13:14 +02:00
5919c26c10
Apply formatting suggestion for build config 2021-09-19 20:10:36 +02:00
9aba3f156a
Apply IntelliJ code formatting and alternatives suggestions 2021-09-19 20:08:50 +02:00
ad869f7346
Back to Java version 16
Gradle and Kotlin don't yet support Java 17, apparently. :(

This commit reverts 7f725b345c
2021-09-19 19:58:37 +02:00
92a226dd02
Specify dependencies with kotlin helper function 2021-09-19 19:52:14 +02:00
c8dbcc3f49
Use latest release for all dependencies 2021-09-19 19:42:56 +02:00
365a0c2a17
Restructure folders 2021-09-19 19:33:39 +02:00
7f725b345c
Upgrade project to Java 17 2021-09-19 19:25:59 +02:00
3fe9ed5b93
Use improved syntax for setting Java Version 2021-09-19 19:24:20 +02:00
8059cccc50
Specifiy group id in build gradle file instead of settings 2021-09-19 19:23:45 +02:00
090616d05d
Move functions into separate kotlin files 2021-09-19 19:19:06 +02:00
adc0b75405
Make kotlinc use all .kt files in root package 2021-09-19 19:19:06 +02:00
6c795316d6
Switch to unversioned kotlin-stdlib and specify Java 16 for testing 2021-09-19 19:19:06 +02:00
4718ff7379
Follow official kotlin code style 2021-09-19 19:18:24 +02:00
0ade9ee9bf
Ignore .idea folder in root dir 2021-09-19 19:18:11 +02:00
11 changed files with 176 additions and 173 deletions

1
.gitignore vendored
View file

@ -38,3 +38,4 @@ build
# Ignore manual compilation results # Ignore manual compilation results
/de/jotoho/ /de/jotoho/
/META-INF /META-INF
/.idea

View file

@ -1,55 +0,0 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Kotlin application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/7.2/userguide/building_java_projects.html
*/
plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
id("org.jetbrains.kotlin.jvm") version "1.+"
// Apply the application plugin to add support for building a CLI application in Java.
application
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Align versions of all Kotlin components
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
// Use the Kotlin JDK 8 standard library.
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// This dependency is used by the application.
implementation("com.google.guava:guava:30.1.1-jre")
// Use the Kotlin test library.
testImplementation("org.jetbrains.kotlin:kotlin-test")
// Use the Kotlin JUnit integration.
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")
}

View file

@ -1,101 +0,0 @@
package de.jotoho.waituntil
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.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<String>) {
val optionDictionary = mapOf(Pair("-h", "--help"));
val options = HashSet<String>();
val words = HashSet<String>();
for (arg in args) {
if (arg.startsWith("--")) {
options.add(arg.substring(startIndex=2))
}
else if (arg.startsWith('-')) {
val translation = optionDictionary.get(arg);
if (translation != null)
options.add(translation.substring(startIndex=2));
else
System.err.println("Short-hand '$arg' does not exist. Ignoring!");
}
else
words.add(arg);
}
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);
}
}

View file

@ -1,14 +0,0 @@
/*
* This Kotlin source file was generated by the Gradle 'init' task.
*/
package de.jotoho.waituntil
import kotlin.test.Test
import kotlin.test.assertNotNull
class AppTest {
@Test fun appHasAGreeting() {
val classUnderTest = App()
assertNotNull(classUnderTest.greeting, "app should have a greeting")
}
}

49
build.gradle.kts Normal file
View file

@ -0,0 +1,49 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
kotlin("jvm") version "latest.release"
// Apply the application plugin to add support for building a CLI application in Java.
application
}
group = "de.jotoho"
version = "0.1.0"
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Align versions of all Kotlin components
implementation(platform(kotlin("bom", "latest.release")))
// Use the Kotlin standard library.
implementation(kotlin("stdlib", "latest.release"))
// Use the Kotlin test library.
testImplementation(kotlin("test", "latest.release"))
// Use the Kotlin JUnit integration.
testImplementation(kotlin("test-junit", "latest.release"))
}
java {
sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "16"
}
application {
// Define the main class for the application.
mainClass.set("de.jotoho.waituntil.StartKt")
}

View file

@ -1,3 +1,5 @@
#!/bin/sh #!/bin/sh
kotlinc app/src/main/kotlin/de/jotoho/waituntil/start.kt -jvm-target 16 -include-runtime -d waituntil.jar # shellcheck disable=SC2046
# Word splitting in find results is intentional!
kotlinc $(find src/main -type f -iname '*.kt') -jvm-target 16 -include-runtime -d waituntil.jar

View file

@ -1 +1,2 @@
org.gradle.daemon=false org.gradle.daemon=false
kotlin.code.style=official

View file

@ -7,5 +7,4 @@
* in the user manual at https://docs.gradle.org/7.2/userguide/multi_project_builds.html * in the user manual at https://docs.gradle.org/7.2/userguide/multi_project_builds.html
*/ */
rootProject.name = "de.jotoho.waituntil" rootProject.name = "waituntil"
include("app")

View file

@ -0,0 +1,24 @@
package de.jotoho.waituntil
import java.time.Instant
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.time.temporal.ChronoUnit
import java.util.*
fun waitUntilTimeStamp(timestamp: ZonedDateTime) {
Thread.sleep(Instant.now().until(timestamp, ChronoUnit.MILLIS).coerceAtLeast(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")
}
}
}

View file

@ -0,0 +1,56 @@
package de.jotoho.waituntil
import java.util.*
import kotlin.system.exitProcess
// 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.language
val applicationOutputLanguage: String = if (Locale.getDefault().language.equals(Locale.GERMAN.language))
Locale.GERMAN.language
else Locale.ENGLISH.language
fun main(args: Array<String>) {
val optionDictionary = mapOf(Pair("-h", "--help"))
val options = HashSet<String>()
val words = HashSet<String>()
for (arg in args) {
if (arg.startsWith("--")) {
options.add(arg.substring(startIndex=2))
}
else if (arg.startsWith('-')) {
if (optionDictionary.containsKey(arg))
options.add(optionDictionary[arg]!!.substring(startIndex=2))
else
System.err.println("Short-hand '$arg' does not exist. Ignoring!")
}
else
words.add(arg)
}
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})")
}
}
exitProcess(1)
}
}

View file

@ -0,0 +1,41 @@
package de.jotoho.waituntil
import java.time.Instant
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.TimeZone
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
}