From a73d46dba51e363ce6c98f1c30a52cda4c490d49 Mon Sep 17 00:00:00 2001 From: btwonion Date: Thu, 4 Apr 2024 19:17:04 +0200 Subject: [PATCH] refactor code and add linter --- .editorconfig | 40 +++++++++++++++++++ .github/workflows/build-commit.yml | 39 ++++++++++++++++++ .github/workflows/ktlint-commit.yml | 22 ++++++++++ .github/workflows/ktlint-pull-request.yml | 20 ++++++++++ build.gradle.kts | 2 - .../kotlin/dev/nyon/konfig/config/Config.kt | 34 ++++++++++------ .../dev/nyon/konfig/config/ConfigFile.kt | 2 +- .../dev/nyon/konfig/config/ConfigSettings.kt | 6 ++- .../dev/nyon/konfig/config/Migration.kt | 2 +- .../dev/nyon/konfig/internal/Annotations.kt | 12 ------ .../nyon/konfig/internal/InternalKonfigApi.kt | 13 ++++++ 11 files changed, 162 insertions(+), 30 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/workflows/build-commit.yml create mode 100644 .github/workflows/ktlint-commit.yml create mode 100644 .github/workflows/ktlint-pull-request.yml delete mode 100644 src/main/kotlin/dev/nyon/konfig/internal/Annotations.kt create mode 100644 src/main/kotlin/dev/nyon/konfig/internal/InternalKonfigApi.kt diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..4e0d1ae --- /dev/null +++ b/.editorconfig @@ -0,0 +1,40 @@ +# https://editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 2 +max_line_length = 140 + +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.{java,kt,kts,scala,rs,xml,kt.spec,kts.spec}] +indent_size = 4 + +[*.{kt,kts}] +ktlint_code_style = ktlint_official +ktlint_ignore_back_ticked_identifier = true +ij_kotlin_allow_trailing_comma_on_call_site = false +ij_kotlin_allow_trailing_comma = false + +ktlint_standard = enabled + +# Don't allow any wildcard imports +ij_kotlin_packages_to_use_import_on_demand = unset + +# Prevent wildcard imports +ij_kotlin_name_count_to_use_star_import = 99 +ij_kotlin_name_count_to_use_star_import_for_members = 99 + +[*.md] +trim_trailing_whitespace = false +max_line_length = unset + +[gradle/verification-metadata.xml] +indent_size = 3 + +[*.yml] +ij_yaml_spaces_within_brackets = false \ No newline at end of file diff --git a/.github/workflows/build-commit.yml b/.github/workflows/build-commit.yml new file mode 100644 index 0000000..621dc22 --- /dev/null +++ b/.github/workflows/build-commit.yml @@ -0,0 +1,39 @@ +# Used when a commit is pushed to the repository +# This makes use of caching for faster builds and uploads the resulting artifacts +name: build-commit + +on: [ push ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Extract current branch name + shell: bash + # bash pattern expansion to grab branch name without slashes + run: ref="${GITHUB_REF#refs/heads/}" && echo "branch=${ref////-}" >> $GITHUB_OUTPUT + id: ref + - name: Checkout sources + uses: actions/checkout@v4 + - name: Set up JDK + uses: actions/setup-java@v4 + with: + distribution: 'temurin' + java-version: 17 + - name: Initialize caches + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/loom-cache + ~/.gradle/wrapper + key: ${{ runner.os }}-build-commit-${{ hashFiles('gradle/wrapper/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-build-commit- + - name: Build artifacts + run: ./gradlew build + - name: Upload artifacts + uses: actions/upload-artifact@v4 + with: + name: konfig-artifacts-${{ steps.ref.outputs.branch }} + path: build/libs/*.jar \ No newline at end of file diff --git a/.github/workflows/ktlint-commit.yml b/.github/workflows/ktlint-commit.yml new file mode 100644 index 0000000..de1327a --- /dev/null +++ b/.github/workflows/ktlint-commit.yml @@ -0,0 +1,22 @@ +name: ktlint-commit + +on: [ push ] + +jobs: + ktlint: + name: Check Code Quality + runs-on: ubuntu-latest + + steps: + - name: Clone repo + uses: actions/checkout@v4 + with: + fetch-depth: 1 + - name: ktlint + uses: ScaCap/action-ktlint@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + reporter: github-check + filter_mode: nofilter + level: info + fail_on_error: true \ No newline at end of file diff --git a/.github/workflows/ktlint-pull-request.yml b/.github/workflows/ktlint-pull-request.yml new file mode 100644 index 0000000..0d3a987 --- /dev/null +++ b/.github/workflows/ktlint-pull-request.yml @@ -0,0 +1,20 @@ +name: ktlint-pull-request + +on: [ pull_request ] + +jobs: + ktlint: + name: Check Code Quality + runs-on: ubuntu-latest + + steps: + - name: Clone repo + uses: actions/checkout@v4 + with: + fetch-depth: 1 + - name: ktlint + uses: ScaCap/action-ktlint@master + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + reporter: github-pr-review + level: info \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 7bd8666..3eff400 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,6 +1,4 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile -import java.nio.file.Path -import kotlin.io.path.notExists import kotlin.io.path.readText plugins { diff --git a/src/main/kotlin/dev/nyon/konfig/config/Config.kt b/src/main/kotlin/dev/nyon/konfig/config/Config.kt index 4c6dd30..9081199 100644 --- a/src/main/kotlin/dev/nyon/konfig/config/Config.kt +++ b/src/main/kotlin/dev/nyon/konfig/config/Config.kt @@ -8,7 +8,11 @@ import kotlinx.serialization.json.JsonBuilder import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive import java.nio.file.Path -import kotlin.io.path.* +import kotlin.io.path.createDirectories +import kotlin.io.path.createFile +import kotlin.io.path.notExists +import kotlin.io.path.readText +import kotlin.io.path.writeText @InternalKonfigApi var configFiles: MutableList> = mutableListOf() @@ -29,12 +33,13 @@ inline fun config( crossinline jsonBuilder: JsonBuilder.() -> Unit = {}, noinline migration: Migration ) { - val json = Json { - prettyPrint = true - ignoreUnknownKeys = true - encodeDefaults = true - jsonBuilder() - } + val json = + Json { + prettyPrint = true + ignoreUnknownKeys = true + encodeDefaults = true + jsonBuilder() + } configFiles.add(ConfigFile(T::class, ConfigSettings(path, currentVersion, migration), defaultConfig, json)) } @@ -68,10 +73,15 @@ inline fun loadConfig(): @Serializable T? { saveConfig(defaultInstance) return defaultInstance } - val config = file.settings.migration.invoke( - if (version == null) jsonTree - else jsonTree.jsonObject["config"] ?: jsonTree, version - ) as? T + val config = + file.settings.migration.invoke( + if (version == null) { + jsonTree + } else { + jsonTree.jsonObject["config"] ?: jsonTree + }, + version + ) as? T saveConfig(config ?: defaultInstance) return config } @@ -95,4 +105,4 @@ inline fun saveConfig(config: @Serializable T) { @InternalKonfigApi @Suppress("SpellCheckingInspection") @Serializable -data class Konfig(val version: Int, val config: @Serializable T) \ No newline at end of file +data class Konfig(val version: Int, val config: @Serializable T) diff --git a/src/main/kotlin/dev/nyon/konfig/config/ConfigFile.kt b/src/main/kotlin/dev/nyon/konfig/config/ConfigFile.kt index 8d35d9d..144d9e3 100644 --- a/src/main/kotlin/dev/nyon/konfig/config/ConfigFile.kt +++ b/src/main/kotlin/dev/nyon/konfig/config/ConfigFile.kt @@ -5,4 +5,4 @@ import kotlinx.serialization.json.Json import kotlin.reflect.KClass @InternalKonfigApi -data class ConfigFile(val type: KClass, val settings: ConfigSettings, val defaultInstance: T, val json: Json) \ No newline at end of file +data class ConfigFile(val type: KClass, val settings: ConfigSettings, val defaultInstance: T, val json: Json) diff --git a/src/main/kotlin/dev/nyon/konfig/config/ConfigSettings.kt b/src/main/kotlin/dev/nyon/konfig/config/ConfigSettings.kt index 52b6a66..095f873 100644 --- a/src/main/kotlin/dev/nyon/konfig/config/ConfigSettings.kt +++ b/src/main/kotlin/dev/nyon/konfig/config/ConfigSettings.kt @@ -5,5 +5,7 @@ import java.nio.file.Path @InternalKonfigApi data class ConfigSettings( - val path: Path, val currentVersion: Int, val migration: Migration<*> -) \ No newline at end of file + val path: Path, + val currentVersion: Int, + val migration: Migration<*> +) diff --git a/src/main/kotlin/dev/nyon/konfig/config/Migration.kt b/src/main/kotlin/dev/nyon/konfig/config/Migration.kt index 6289e2d..c04d2fd 100644 --- a/src/main/kotlin/dev/nyon/konfig/config/Migration.kt +++ b/src/main/kotlin/dev/nyon/konfig/config/Migration.kt @@ -10,4 +10,4 @@ import kotlinx.serialization.json.JsonElement * @param version the saved config's version. if Int is null the config has the old config system (will be removed in Minecraft version 1.21) * @param T the encoded config class or null if the default instance should be applied */ -typealias Migration = (jsonTree: JsonElement, version: Int?) -> @Serializable T? \ No newline at end of file +typealias Migration = (jsonTree: JsonElement, version: Int?) -> @Serializable T? diff --git a/src/main/kotlin/dev/nyon/konfig/internal/Annotations.kt b/src/main/kotlin/dev/nyon/konfig/internal/Annotations.kt deleted file mode 100644 index 638b0af..0000000 --- a/src/main/kotlin/dev/nyon/konfig/internal/Annotations.kt +++ /dev/null @@ -1,12 +0,0 @@ -package dev.nyon.konfig.internal - -@Retention(value = AnnotationRetention.BINARY) -@Target(AnnotationTarget.TYPEALIAS, AnnotationTarget.CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION) -@RequiresOptIn( - message = "This is an internal Konfig API that " + - "should not be used from the outside. No compatibility guarantees are provided. " + - "It is recommended to report your use-case of internal API to the konfig developer.", - level = RequiresOptIn.Level.ERROR -) -@Suppress("SpellCheckingInspection") -annotation class InternalKonfigApi \ No newline at end of file diff --git a/src/main/kotlin/dev/nyon/konfig/internal/InternalKonfigApi.kt b/src/main/kotlin/dev/nyon/konfig/internal/InternalKonfigApi.kt new file mode 100644 index 0000000..658a8d5 --- /dev/null +++ b/src/main/kotlin/dev/nyon/konfig/internal/InternalKonfigApi.kt @@ -0,0 +1,13 @@ +package dev.nyon.konfig.internal + +@Retention(value = AnnotationRetention.BINARY) +@Target(AnnotationTarget.TYPEALIAS, AnnotationTarget.CLASS, AnnotationTarget.PROPERTY, AnnotationTarget.FUNCTION) +@RequiresOptIn( + message = + "This is an internal Konfig API that " + + "should not be used from the outside. No compatibility guarantees are provided. " + + "It is recommended to report your use-case of internal API to the konfig developer.", + level = RequiresOptIn.Level.ERROR +) +@Suppress("SpellCheckingInspection") +annotation class InternalKonfigApi