commit 713061803860a6bec8fbe9c0e528ddc3cc0c7f6a Author: Minecon724 Date: Fri Oct 3 17:37:59 2025 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b1dff0d --- /dev/null +++ b/.gitignore @@ -0,0 +1,45 @@ +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/**/build/ +!**/src/test/**/build/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Kotlin ### +.kotlin + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..a0ccf77 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,5 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Environment-dependent path to Maven home directory +/mavenHomeManager.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..caae4ab --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +bsk2 \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1bec35e --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..2a65317 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,17 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml new file mode 100644 index 0000000..3efb2d8 --- /dev/null +++ b/.idea/kotlinc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7d6e415 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..785eb46 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +### Building + +Build a native binary for the current architecture using GraalVM: +```shell +./gradlew nativeCompile +ls -l build/native/nativeCompile/bsk2 +``` + +Build an uber JAR: +```shell +./gradlew build +ls -l build/libs/bsk2-*-all.jar +``` \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..b91d088 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,46 @@ +plugins { + application + alias(libs.plugins.kotlin.jvm) + alias(libs.plugins.kotlin.serialization) + alias(libs.plugins.graalvm.buildtools.native) + alias(libs.plugins.shadow) +} + +group = "eu.m724" +version = "1.0-SNAPSHOT" + +repositories { + mavenCentral() +} + +dependencies { + //implementation("org.eclipse.jgit:org.eclipse.jgit:7.3.0.202506031305-r") + /// implementation("io.pebbletemplates:pebble:3.2.4") + implementation(libs.clikt) // command line argument parsing + implementation(libs.kaml) // YAML configuration parsing + + testImplementation(kotlin("test")) + testImplementation(libs.mockk) +} + +tasks.test { + useJUnitPlatform() +} + +tasks.shadowJar { + minimize() +} + +graalvmNative { + toolchainDetection = true + + // -Os? --emit build-report? pgo? +} + +kotlin { + jvmToolchain(21) +} + +application { + mainClass = "eu.m724.bsk2.MainKt" +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..7fc6f1f --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..33b5798 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,18 @@ +[versions] +kotlin = "2.2.20" +graalvm-buildtools-native = "0.11.1" +shadow = "9.2.2" +clikt = "5.0.3" +kaml = "0.97.0" +mockk = "1.14.5" + +[libraries] +clikt = { group = "com.github.ajalt.clikt", name = "clikt", version.ref = "clikt" } +kaml = { group = "com.charleskorn.kaml", name = "kaml", version.ref = "kaml" } +mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" } + +[plugins] +kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } +kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } +graalvm-buildtools-native = { id = "org.graalvm.buildtools.native", version.ref = "graalvm-buildtools-native" } +shadow = { id = "com.gradleup.shadow", version.ref = "shadow" } \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..249e583 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..2c6fe59 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Sep 24 14:33:20 CEST 2025 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..107acd3 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..34e0755 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1,4 @@ +plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0" +} +rootProject.name = "bsk2" \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/Main.kt b/src/main/kotlin/eu/m724/bsk2/Main.kt new file mode 100644 index 0000000..5a77e64 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/Main.kt @@ -0,0 +1,50 @@ +package eu.m724.bsk2 + +import com.github.ajalt.clikt.core.CliktCommand +import com.github.ajalt.clikt.core.Context +import com.github.ajalt.clikt.core.main +import com.github.ajalt.clikt.parameters.options.defaultLazy +import com.github.ajalt.clikt.parameters.options.option +import com.github.ajalt.clikt.parameters.options.required +import com.github.ajalt.clikt.parameters.types.path +import java.nio.file.Path +import kotlin.io.path.div +import kotlin.time.TimeSource + +class Main : CliktCommand() { + private val sourceDirectory: Path by option("-i", help = "Your workspace directory") + .path(mustExist = true, canBeFile = false) + .required() + + private val outputDirectory: Path by option("-o", help = "The output directory") + .path(canBeDir = false, canBeFile = false, canBeSymlink = false) // I don't know if there's a cleaner way to require non existing? + .defaultLazy( + defaultForHelp = "source-directory/output", + value = { sourceDirectory / "output" } + ) + + override fun help(context: Context): String = "Render a blog" + + override fun run() { + println() + printArguments() + println() + + println("Rendering now...") + val startMark = TimeSource.Monotonic.markNow() + + // TODO + + val renderingTime = startMark.elapsedNow() + println("Render successful, took ${renderingTime.inWholeMilliseconds} ms") + } + + private fun printArguments() { + println("Read arguments:") + println(" Source directory: $sourceDirectory") + println(" Output directory: $outputDirectory") + } +} + +fun main(args: Array) = + Main().main(args) \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/FileSystem.kt b/src/main/kotlin/eu/m724/bsk2/builder/FileSystem.kt new file mode 100644 index 0000000..5d79884 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/FileSystem.kt @@ -0,0 +1,7 @@ +package eu.m724.bsk2.builder + +import java.nio.file.Path + +interface FileSystem { + fun readText(path: Path): String +} \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/PathUtils.kt b/src/main/kotlin/eu/m724/bsk2/builder/PathUtils.kt new file mode 100644 index 0000000..94e9cac --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/PathUtils.kt @@ -0,0 +1,10 @@ +package eu.m724.bsk2.builder + +import java.nio.file.Path + +// TODO needs a test or two +fun Path.isParentOf(child: Path): Boolean = + child.toFile().canonicalPath.startsWith(this.toFile().canonicalPath) + +fun Path.contains(other: Path): Boolean = + isParentOf(other) \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/config/data/RenderConfiguration.kt b/src/main/kotlin/eu/m724/bsk2/builder/config/data/RenderConfiguration.kt new file mode 100644 index 0000000..05b1989 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/config/data/RenderConfiguration.kt @@ -0,0 +1,19 @@ +package eu.m724.bsk2.builder.config.data + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class RenderConfiguration( + val templatePosts: Boolean = false, + val compress: List = listOf(CompressionMethod.GZIP, CompressionMethod.ZSTD) +) { + @Serializable + enum class CompressionMethod { + @SerialName("gzip") + GZIP, + + @SerialName("zstd") + ZSTD + } +} \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/config/data/SiteConfiguration.kt b/src/main/kotlin/eu/m724/bsk2/builder/config/data/SiteConfiguration.kt new file mode 100644 index 0000000..656ff0b --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/config/data/SiteConfiguration.kt @@ -0,0 +1,9 @@ +package eu.m724.bsk2.builder.config.data + +import kotlinx.serialization.Serializable + +@Serializable +data class SiteConfiguration( + val name: String, + val baseUrl: String = "/" +) \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/config/data/WorkspaceConfiguration.kt b/src/main/kotlin/eu/m724/bsk2/builder/config/data/WorkspaceConfiguration.kt new file mode 100644 index 0000000..fff4e91 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/config/data/WorkspaceConfiguration.kt @@ -0,0 +1,9 @@ +package eu.m724.bsk2.builder.config.data + +import kotlinx.serialization.Serializable + +@Serializable +data class WorkspaceConfiguration( + val site: SiteConfiguration, + val render: RenderConfiguration = RenderConfiguration() +) \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/config/parser/ConfigurationParser.kt b/src/main/kotlin/eu/m724/bsk2/builder/config/parser/ConfigurationParser.kt new file mode 100644 index 0000000..e004c24 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/config/parser/ConfigurationParser.kt @@ -0,0 +1,7 @@ +package eu.m724.bsk2.builder.config.parser + +import eu.m724.bsk2.builder.config.data.WorkspaceConfiguration + +interface ConfigurationParser { + fun parse(text: String): WorkspaceConfiguration +} \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/config/parser/YamlConfigurationParser.kt b/src/main/kotlin/eu/m724/bsk2/builder/config/parser/YamlConfigurationParser.kt new file mode 100644 index 0000000..f525d48 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/config/parser/YamlConfigurationParser.kt @@ -0,0 +1,16 @@ +package eu.m724.bsk2.builder.config.parser + +import com.charleskorn.kaml.Yaml +import com.charleskorn.kaml.YamlConfiguration +import eu.m724.bsk2.builder.config.data.WorkspaceConfiguration +import kotlinx.serialization.decodeFromString + +class YamlConfigurationParser ( + private val yaml: Yaml = Yaml( + configuration = YamlConfiguration(strictMode = false) + ) +) : ConfigurationParser { + override fun parse(text: String): WorkspaceConfiguration { + return yaml.decodeFromString(text) + } +} \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/post/data/Post.kt b/src/main/kotlin/eu/m724/bsk2/builder/post/data/Post.kt new file mode 100644 index 0000000..d87bdf2 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/post/data/Post.kt @@ -0,0 +1,16 @@ +package eu.m724.bsk2.builder.post.data + +/** + * This class represents a post with various attributes including metadata and content. + * + * @param id The unique identifier of this post, also the slug. + * @param metadata The metadata of this post. + * @param history The changes of this post, `null` if not a Git object. + * @param content The content of this post, `null` if not loaded. + */ +data class Post( + val id: String, + val metadata: PostMeta, + val history: PostHistory?, + val content: String? +) \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/post/data/PostHistory.kt b/src/main/kotlin/eu/m724/bsk2/builder/post/data/PostHistory.kt new file mode 100644 index 0000000..afcc47e --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/post/data/PostHistory.kt @@ -0,0 +1,14 @@ +package eu.m724.bsk2.builder.post.data + +/** + * Contains information about important change events of this post. + * + * @param revisions The amount of revisions this post had. + * @param creationEvent The first (creation) event of this post. + * @param lastModification The most recent event of this post. + */ +data class PostHistory( + val revisions: Int, + val creationEvent: PostModificationEvent, + val lastModification: PostModificationEvent +) \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/post/data/PostMeta.kt b/src/main/kotlin/eu/m724/bsk2/builder/post/data/PostMeta.kt new file mode 100644 index 0000000..e5582f8 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/post/data/PostMeta.kt @@ -0,0 +1,20 @@ +package eu.m724.bsk2.builder.post.data + +import kotlinx.serialization.Serializable + +/** + * This class represents user-specified post metadata. + * It does not contain things that generally do not belong to the metadata file. + * + * @param title The post title. + * @param summary The post summary. + * @param draft Whether the post is a draft. Usually combined with [hidden]. + * @param hidden Whether the post is hidden from the all posts list. + */ +@Serializable +data class PostMeta( + val title: String, + val summary: String, + val draft: Boolean = false, + val hidden: Boolean = false, +) diff --git a/src/main/kotlin/eu/m724/bsk2/builder/post/data/PostModificationEvent.kt b/src/main/kotlin/eu/m724/bsk2/builder/post/data/PostModificationEvent.kt new file mode 100644 index 0000000..9e68ec8 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/post/data/PostModificationEvent.kt @@ -0,0 +1,16 @@ +package eu.m724.bsk2.builder.post.data + +import java.time.ZonedDateTime + +/** + * Represents a single post modification event, usually means a modification. + * + * @param author The author of this modification. + * @param datetime The datetime this modification was made. + * @param description The description of this modification. + */ +data class PostModificationEvent( + val author: String, + val datetime: ZonedDateTime, + val description: String +) \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/post/loader/FileSystemPostLoader.kt b/src/main/kotlin/eu/m724/bsk2/builder/post/loader/FileSystemPostLoader.kt new file mode 100644 index 0000000..e539d43 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/post/loader/FileSystemPostLoader.kt @@ -0,0 +1,42 @@ +package eu.m724.bsk2.builder.post.loader + +import com.charleskorn.kaml.Yaml +import com.charleskorn.kaml.YamlConfiguration +import eu.m724.bsk2.builder.FileSystem +import eu.m724.bsk2.builder.isParentOf +import eu.m724.bsk2.builder.post.data.PostMeta +import kotlinx.serialization.decodeFromString +import java.nio.file.Path +import kotlin.io.path.div +import kotlin.io.path.readText + +/** + * A post loader that loads from a file system + * + * @param postRoot The posts directory (usually `workspace/posts`) + * @param fileSystem The file system reader, defaults to reading from disk + */ +class FileSystemPostLoader ( + private val postRoot: Path, + private val fileSystem: FileSystem = object : FileSystem { + override fun readText(path: Path): String = path.readText() + }, + private val yaml: Yaml = Yaml( + configuration = YamlConfiguration(strictMode = false) + ) +) : PostLoader { + private val cache = mutableMapOf() + + override fun getMeta(name: String): PostMeta { + val path = postRoot / name / "meta.yml" + + if (!postRoot.isParentOf(path)) { + throw IllegalArgumentException("Path traversal ($path not a child of $postRoot)") + } + + return cache.getOrPut(name) { + val text = fileSystem.readText(path) + yaml.decodeFromString(text) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/post/loader/PostLoader.kt b/src/main/kotlin/eu/m724/bsk2/builder/post/loader/PostLoader.kt new file mode 100644 index 0000000..a96f8c9 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/post/loader/PostLoader.kt @@ -0,0 +1,7 @@ +package eu.m724.bsk2.builder.post.loader + +import eu.m724.bsk2.builder.post.data.PostMeta + +interface PostLoader { + fun getMeta(name: String): PostMeta +} \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/template/loader/FileSystemTemplateLoader.kt b/src/main/kotlin/eu/m724/bsk2/builder/template/loader/FileSystemTemplateLoader.kt new file mode 100644 index 0000000..c83e000 --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/template/loader/FileSystemTemplateLoader.kt @@ -0,0 +1,35 @@ +package eu.m724.bsk2.builder.template.loader + +import eu.m724.bsk2.builder.FileSystem +import eu.m724.bsk2.builder.isParentOf +import java.nio.file.Path +import kotlin.io.path.div +import kotlin.io.path.readText + +/** + * A template loader that loads from a file system + * + * @param templateRoot The root path of the template (usually `workspace/template`) + * @param fileSystem The file system reader, defaults to reading from disk + */ +class FileSystemTemplateLoader ( + templateRoot: Path, + private val fileSystem: FileSystem = object : FileSystem { + override fun readText(path: Path): String = path.readText() + } +) : TemplateLoader { + private val cache = mutableMapOf() + private val htmlRoot = templateRoot / "html" + + override fun getHtml(name: String): String { + val path = htmlRoot / "$name.html" + + if (!htmlRoot.isParentOf(path)) { + throw IllegalArgumentException("Path traversal ($path not a child of $htmlRoot)") + } + + return cache.getOrPut(name) { + fileSystem.readText(path) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/eu/m724/bsk2/builder/template/loader/TemplateLoader.kt b/src/main/kotlin/eu/m724/bsk2/builder/template/loader/TemplateLoader.kt new file mode 100644 index 0000000..5861cea --- /dev/null +++ b/src/main/kotlin/eu/m724/bsk2/builder/template/loader/TemplateLoader.kt @@ -0,0 +1,5 @@ +package eu.m724.bsk2.builder.template.loader + +interface TemplateLoader { + fun getHtml(name: String): String +} \ No newline at end of file diff --git a/src/test/kotlin/eu/m724/bsk2/builder/config/parser/YamlConfigurationParserTest.kt b/src/test/kotlin/eu/m724/bsk2/builder/config/parser/YamlConfigurationParserTest.kt new file mode 100644 index 0000000..2057233 --- /dev/null +++ b/src/test/kotlin/eu/m724/bsk2/builder/config/parser/YamlConfigurationParserTest.kt @@ -0,0 +1,95 @@ +package eu.m724.bsk2.builder.config.parser + +import eu.m724.bsk2.builder.config.data.RenderConfiguration +import eu.m724.bsk2.builder.config.data.SiteConfiguration +import eu.m724.bsk2.builder.config.data.WorkspaceConfiguration +import eu.m724.bsk2.builder.config.parser.YamlConfigurationParser +import kotlinx.serialization.SerializationException +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.BeforeEach +import kotlin.test.Test +import kotlin.test.assertEquals + +class YamlConfigurationParserTest { + private lateinit var parser: YamlConfigurationParser + + @BeforeEach + fun setupTest() { + parser = YamlConfigurationParser() + } + + @Test + fun `test configuration loading`() { + val expectedConfiguration = WorkspaceConfiguration( + site = SiteConfiguration( + name = "My Blog", + baseUrl = "https://blog.example.com" + ), + render = RenderConfiguration( + templatePosts = true, + compress = listOf(RenderConfiguration.CompressionMethod.GZIP, RenderConfiguration.CompressionMethod.ZSTD) + ) + ) + + val sourceText = """ + # Site options + site: + name: My Blog + # This is used only for RSS feed + baseUrl: https://blog.example.com + surplus: value which should be ignored + + # Render options + render: + templatePosts: true + + compress: + - gzip + - zstd + + surplus: value which should be ignored + """.trimIndent() + + assertEquals(expectedConfiguration, parser.parse(sourceText)) + } + + @Test + fun `test defaults`() { + val expectedConfiguration = WorkspaceConfiguration( + site = SiteConfiguration( + name = "My Blog" + ), + render = RenderConfiguration() + ) + + val sourceText = """ + site: + name: My Blog + """.trimIndent() + + assertEquals(expectedConfiguration, parser.parse(sourceText)) + } + + @Test + fun `test invalid yaml`() { + val sourceText = """ + i am totally invalid + """.trimIndent() + + assertThrows(SerializationException::class.java) { + parser.parse(sourceText) + } + } + + @Test + fun `test invalid value type`() { + val sourceText = """ + site: + name: [] + """.trimIndent() + + assertThrows(SerializationException::class.java) { + parser.parse(sourceText) + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/eu/m724/bsk2/builder/post/loader/PostLoaderTest.kt b/src/test/kotlin/eu/m724/bsk2/builder/post/loader/PostLoaderTest.kt new file mode 100644 index 0000000..3592afd --- /dev/null +++ b/src/test/kotlin/eu/m724/bsk2/builder/post/loader/PostLoaderTest.kt @@ -0,0 +1,107 @@ +package eu.m724.bsk2.builder.post.loader + +import com.charleskorn.kaml.YamlException +import eu.m724.bsk2.builder.FileSystem +import eu.m724.bsk2.builder.post.data.PostMeta +import io.mockk.* +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach +import java.nio.file.Path +import java.nio.file.Paths +import kotlin.io.path.div +import kotlin.test.Test +import kotlin.test.assertEquals + +class PostLoaderTest { + private companion object { + val ROOT_PATH: Path = Paths.get("workspace", "posts") + + fun String.toMetaYmlPath() = ROOT_PATH / this / "meta.yml" + } + + private lateinit var fileSystem: FileSystem + private lateinit var loader: PostLoader + + @BeforeEach + fun setupTest() { + fileSystem = mockk() + + loader = FileSystemPostLoader( + postRoot = ROOT_PATH, + fileSystem = fileSystem + ) + } + + @AfterEach + fun cleanupTest() { + checkUnnecessaryStub(fileSystem) + unmockkAll() + } + + @Test + fun `getPost should cache the result on success`() { + everyMetaYmlRead("first") returns """ + title: Hello, world! + summary: My first post + draft: true + hidden: true + """ + + // default values + everyMetaYmlRead("second") returns """ + title: Hello, universe! + summary: My second post + """ + + val expectedFirst = PostMeta( + title = "Hello, world!", + summary = "My first post", + draft = true, + hidden = true + ) + + val expectedSecond = PostMeta( + title = "Hello, universe!", + summary = "My second post" + ) + + repeat(3) { assertEquals(expectedFirst, loader.getMeta("first")) } + repeat(3) { assertEquals(expectedSecond, loader.getMeta("second")) } + + // ensure that cached + verify(exactly = 1) { fileSystem.readText("first".toMetaYmlPath()) } + verify(exactly = 1) { fileSystem.readText("second".toMetaYmlPath()) } + } + + @Test + fun `getPost should throw YamlException on invalid meta YML file and not cache the failure`() { + everyMetaYmlRead("invalid") returns "Totally invalid" + + repeat(3) { Assertions.assertThrows(YamlException::class.java) { loader.getMeta("invalid") } } + + // ensure not cached + verify(exactly = 3) { fileSystem.readText("invalid".toMetaYmlPath()) } + } + + @Test + fun `getPost should throw NoSuchFileException and not cache the failure`() { + everyMetaYmlRead("non existent") throws NoSuchFileException("non existent".toMetaYmlPath().toFile()) + + repeat(3) { Assertions.assertThrows(NoSuchFileException::class.java) { loader.getMeta("non existent") } } + + // ensure not cached + verify(exactly = 3) { fileSystem.readText("non existent".toMetaYmlPath()) } + } + + @Test + fun `getPost should throw IllegalArgumentException for path traversal attempts`() { + Assertions.assertThrows(IllegalArgumentException::class.java) { loader.getMeta("../traversed") } + + // while not necessary, here for explicitness + verify(exactly = 0) { fileSystem.readText(any()) } + } + + private fun everyMetaYmlRead(name: String) = + every { fileSystem.readText(name.toMetaYmlPath()) } +} \ No newline at end of file diff --git a/src/test/kotlin/eu/m724/bsk2/builder/template/loader/TemplateLoaderTest.kt b/src/test/kotlin/eu/m724/bsk2/builder/template/loader/TemplateLoaderTest.kt new file mode 100644 index 0000000..24f7230 --- /dev/null +++ b/src/test/kotlin/eu/m724/bsk2/builder/template/loader/TemplateLoaderTest.kt @@ -0,0 +1,88 @@ +package eu.m724.bsk2.builder.template.loader + +import eu.m724.bsk2.builder.FileSystem +import io.mockk.* +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeEach +import java.nio.file.Path +import java.nio.file.Paths +import kotlin.io.path.div +import kotlin.test.Test +import kotlin.test.assertEquals + +class emplateLoaderTest { + private companion object { + val ROOT_PATH: Path = Paths.get("workspace", "template") + const val POST_TEMPLATE_TEXT = "This is the post template" + const val INDEX_TEMPLATE_TEXT = "This is the index template" + + fun String.toHtmlTemplatePath() = (ROOT_PATH / "html" / "$this.html") + } + + private lateinit var fileSystem: FileSystem + private lateinit var loader: TemplateLoader + + @BeforeEach + fun setupTest() { + fileSystem = mockk() + + loader = FileSystemTemplateLoader( + templateRoot = ROOT_PATH, + fileSystem = fileSystem + ) + } + + @AfterEach + fun cleanupTest() { + checkUnnecessaryStub(fileSystem) + unmockkAll() + } + + @Test + fun `getHtml should return file content on success`() { + everyHtmlTemplateRead("post") returns POST_TEMPLATE_TEXT + everyHtmlTemplateRead("index") returns INDEX_TEMPLATE_TEXT + + assertEquals(POST_TEMPLATE_TEXT, loader.getHtml("post")) + assertEquals(INDEX_TEMPLATE_TEXT, loader.getHtml("index")) + } + + @Test + fun `getHtml should cache the result on success`() { + everyHtmlTemplateRead("post") returns POST_TEMPLATE_TEXT + everyHtmlTemplateRead("index") returns INDEX_TEMPLATE_TEXT + + repeat(3) { assertEquals(POST_TEMPLATE_TEXT, loader.getHtml("post")) } + repeat(3) { assertEquals(INDEX_TEMPLATE_TEXT, loader.getHtml("index")) } + + // ensure the reads are cached + verify(exactly = 1) { fileSystem.readText("post".toHtmlTemplatePath()) } + verify(exactly = 1) { fileSystem.readText("index".toHtmlTemplatePath()) } + } + + @Test + fun `getHtml should throw NoSuchFileException and not cache the failure`() { + everyHtmlTemplateRead("non existent") throws NoSuchFileException("non existent".toHtmlTemplatePath().toFile()) + + Assertions.assertThrows(NoSuchFileException::class.java) { loader.getHtml("non existent") } + + // ensure that the result is not cached + Assertions.assertThrows(NoSuchFileException::class.java) { loader.getHtml("non existent") } + + verify(exactly = 2) { fileSystem.readText("non existent".toHtmlTemplatePath()) } + } + + @Test + fun `getHtml should throw IllegalArgumentException for path traversal attempts`() { + Assertions.assertThrows(IllegalArgumentException::class.java) { + loader.getHtml("../traversed") + } + + // while not necessary, here for explicitness + verify(exactly = 0) { fileSystem.readText(any()) } + } + + private fun everyHtmlTemplateRead(name: String) = + every { fileSystem.readText(name.toHtmlTemplatePath()) } +} \ No newline at end of file diff --git a/src/test/resources/test-blog/assets/another_asset b/src/test/resources/test-blog/assets/another_asset new file mode 100644 index 0000000..5a7cfbf --- /dev/null +++ b/src/test/resources/test-blog/assets/another_asset @@ -0,0 +1 @@ +this is an asset as well \ No newline at end of file diff --git a/src/test/resources/test-blog/assets/asset.txt b/src/test/resources/test-blog/assets/asset.txt new file mode 100644 index 0000000..9fbbc37 --- /dev/null +++ b/src/test/resources/test-blog/assets/asset.txt @@ -0,0 +1 @@ +hello this is asset \ No newline at end of file diff --git a/src/test/resources/test-blog/assets/more/nested_asset.txt b/src/test/resources/test-blog/assets/more/nested_asset.txt new file mode 100644 index 0000000..5fe7994 --- /dev/null +++ b/src/test/resources/test-blog/assets/more/nested_asset.txt @@ -0,0 +1 @@ +hello this is nested asset \ No newline at end of file diff --git a/src/test/resources/test-blog/blog.yml b/src/test/resources/test-blog/blog.yml new file mode 100644 index 0000000..d839657 --- /dev/null +++ b/src/test/resources/test-blog/blog.yml @@ -0,0 +1,20 @@ +# Site options +site: + name: My Blog + # This is used only for RSS feed + baseUrl: https://blog.example.com + + # Custom setting! + hello: "World" + +# Render options +render: + templatePosts: true + + compress: + - gzip + - zstd + +# Template options, if any +template: + some_template_setting: "indeed" \ No newline at end of file diff --git a/src/test/resources/test-blog/posts/simple/content.html b/src/test/resources/test-blog/posts/simple/content.html new file mode 100644 index 0000000..dbdea4e --- /dev/null +++ b/src/test/resources/test-blog/posts/simple/content.html @@ -0,0 +1,2 @@ +

Lorem ipsum

+

Lorem ipsum dolor sit amet

\ No newline at end of file diff --git a/src/test/resources/test-blog/posts/simple/cover.thumbnail.webp b/src/test/resources/test-blog/posts/simple/cover.thumbnail.webp new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/test-blog/posts/simple/cover.webp b/src/test/resources/test-blog/posts/simple/cover.webp new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/test-blog/posts/simple/meta.yml b/src/test/resources/test-blog/posts/simple/meta.yml new file mode 100644 index 0000000..3c730f7 --- /dev/null +++ b/src/test/resources/test-blog/posts/simple/meta.yml @@ -0,0 +1,2 @@ +title: This is a post title +summary: Summary of this post \ No newline at end of file diff --git a/src/test/resources/test-blog/template/html/index.html b/src/test/resources/test-blog/template/html/index.html new file mode 100644 index 0000000..21475b2 --- /dev/null +++ b/src/test/resources/test-blog/template/html/index.html @@ -0,0 +1,2 @@ +{{ site.name }} +{{ static('style.css') \ No newline at end of file diff --git a/src/test/resources/test-blog/template/html/post.html b/src/test/resources/test-blog/template/html/post.html new file mode 100644 index 0000000..9c49c57 --- /dev/null +++ b/src/test/resources/test-blog/template/html/post.html @@ -0,0 +1,8 @@ +
    +
  • site name: {{ site.name }}
  • +
  • style.css: {{ static('style.css') }}
  • +
+ +
    + +
\ No newline at end of file diff --git a/src/test/resources/test-blog/template/static/style.css b/src/test/resources/test-blog/template/static/style.css new file mode 100644 index 0000000..c7ca687 --- /dev/null +++ b/src/test/resources/test-blog/template/static/style.css @@ -0,0 +1 @@ +Contents of style.css \ No newline at end of file diff --git a/src/test/resources/test-blog/template/template.yml b/src/test/resources/test-blog/template/template.yml new file mode 100644 index 0000000..ed2a694 --- /dev/null +++ b/src/test/resources/test-blog/template/template.yml @@ -0,0 +1,5 @@ +# Incremental template specification version used by the compiler for backwards compatibility and forwards incompatibility +specification_version: 1 + +# Configuration is not here as template directories aren't meant to be customized. +# TODO figure out how to neatly configure templates \ No newline at end of file