Post-Implementation refactoring of application #9
11 changed files with 176 additions and 173 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -38,3 +38,4 @@ build
|
||||||
# Ignore manual compilation results
|
# Ignore manual compilation results
|
||||||
/de/jotoho/
|
/de/jotoho/
|
||||||
/META-INF
|
/META-INF
|
||||||
|
/.idea
|
||||||
|
|
|
@ -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")
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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
49
build.gradle.kts
Normal 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")
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
org.gradle.daemon=false
|
org.gradle.daemon=false
|
||||||
|
kotlin.code.style=official
|
||||||
|
|
|
@ -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")
|
|
||||||
|
|
24
src/main/kotlin/de/jotoho/waituntil/sleeping.kt
Normal file
24
src/main/kotlin/de/jotoho/waituntil/sleeping.kt
Normal 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
56
src/main/kotlin/de/jotoho/waituntil/start.kt
Normal file
56
src/main/kotlin/de/jotoho/waituntil/start.kt
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
41
src/main/kotlin/de/jotoho/waituntil/timecalc.kt
Normal file
41
src/main/kotlin/de/jotoho/waituntil/timecalc.kt
Normal 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
|
||||||
|
}
|
Loading…
Reference in a new issue