From fecc1df6684147a9c29c7cba9b973283525a007e Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Thu, 17 Dec 2020 06:34:24 +0100 Subject: [PATCH] Create days with examples without manually creating an input --- .../aoc/utils/input/MicronautExtensions.kt | 13 +++++++++++++ .../vandewalleh/aoc/utils/input/ResourceLoader.kt | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/MicronautExtensions.kt b/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/MicronautExtensions.kt index e58205b..2bbdaee 100644 --- a/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/MicronautExtensions.kt +++ b/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/MicronautExtensions.kt @@ -6,6 +6,7 @@ import io.micronaut.core.annotation.AnnotationMetadataDelegate import io.micronaut.core.annotation.AnnotationMetadataProvider import io.micronaut.core.annotation.AnnotationValue import io.micronaut.core.beans.BeanIntrospection +import java.io.File import java.util.* internal inline fun getIntrospection(): BeanIntrospection = @@ -21,3 +22,15 @@ internal inline fun AnnotationMetadataProvider.hasAnnot findAnnotation(T::class.java).isPresent inline fun createDay() = BeanContext.run().getBean() + +// A custom resourceLoader that returns a string should be more appropriate but ¯\_(ツ)_/¯ +fun BeanContext.registerExampleLoader(example: String) { + registerSingleton(TempFileResourceLoader(File.createTempFile("aoc-example", ".txt").apply { + writeText(example) + })) +} + +inline fun createDay(example: String): T = BeanContext.build() + .apply { registerExampleLoader(example) } + .start() + .getBean() diff --git a/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/ResourceLoader.kt b/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/ResourceLoader.kt index 18f0adb..9ebd69b 100644 --- a/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/ResourceLoader.kt +++ b/utils/src/main/kotlin/be/vandewalleh/aoc/utils/input/ResourceLoader.kt @@ -1,5 +1,6 @@ package be.vandewalleh.aoc.utils.input +import java.io.File import java.nio.file.Path import javax.inject.Singleton @@ -19,3 +20,7 @@ class ResourceLoaderImpl : ResourceLoader { return Path.of(url.toURI()) } } + +class TempFileResourceLoader(private val tempFile: File) : ResourceLoader { + override fun ofDay(day: Int) = Path.of(tempFile.path) +}