Skip to content

Commit

Permalink
refactor code and add linter
Browse files Browse the repository at this point in the history
  • Loading branch information
btwonion committed Apr 4, 2024
1 parent 321b2e7 commit a73d46d
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 30 deletions.
40 changes: 40 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions .github/workflows/build-commit.yml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions .github/workflows/ktlint-commit.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions .github/workflows/ktlint-pull-request.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
34 changes: 22 additions & 12 deletions src/main/kotlin/dev/nyon/konfig/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfigFile<*>> = mutableListOf()
Expand All @@ -29,12 +33,13 @@ inline fun <reified T : Any> config(
crossinline jsonBuilder: JsonBuilder.() -> Unit = {},
noinline migration: Migration<T>
) {
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))
}

Expand Down Expand Up @@ -68,10 +73,15 @@ inline fun <reified T : Any> 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
}
Expand All @@ -95,4 +105,4 @@ inline fun <reified T : Any> saveConfig(config: @Serializable T) {
@InternalKonfigApi
@Suppress("SpellCheckingInspection")
@Serializable
data class Konfig<T>(val version: Int, val config: @Serializable T)
data class Konfig<T>(val version: Int, val config: @Serializable T)
2 changes: 1 addition & 1 deletion src/main/kotlin/dev/nyon/konfig/config/ConfigFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import kotlinx.serialization.json.Json
import kotlin.reflect.KClass

@InternalKonfigApi
data class ConfigFile<T : Any>(val type: KClass<T>, val settings: ConfigSettings, val defaultInstance: T, val json: Json)
data class ConfigFile<T : Any>(val type: KClass<T>, val settings: ConfigSettings, val defaultInstance: T, val json: Json)
6 changes: 4 additions & 2 deletions src/main/kotlin/dev/nyon/konfig/config/ConfigSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ import java.nio.file.Path

@InternalKonfigApi
data class ConfigSettings(
val path: Path, val currentVersion: Int, val migration: Migration<*>
)
val path: Path,
val currentVersion: Int,
val migration: Migration<*>
)
2 changes: 1 addition & 1 deletion src/main/kotlin/dev/nyon/konfig/config/Migration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = (jsonTree: JsonElement, version: Int?) -> @Serializable T?
typealias Migration<T> = (jsonTree: JsonElement, version: Int?) -> @Serializable T?
12 changes: 0 additions & 12 deletions src/main/kotlin/dev/nyon/konfig/internal/Annotations.kt

This file was deleted.

13 changes: 13 additions & 0 deletions src/main/kotlin/dev/nyon/konfig/internal/InternalKonfigApi.kt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a73d46d

Please sign in to comment.