Skip to content

Commit

Permalink
Switch to a new Ktlint Formatter (#705)
Browse files Browse the repository at this point in the history
* Switch to new Ktlint plugin

* Add ktlintCheck to PR builds

* Run formatter

* Put ktlint version in libs toml

* Fix lint

* Use Zip4Java from libs.toml
  • Loading branch information
Syer10 authored Oct 7, 2023
1 parent 3cd3cb0 commit 849acfc
Show file tree
Hide file tree
Showing 277 changed files with 6,660 additions and 5,041 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[*.{kt,kts}]
indent_size=4
insert_final_newline=true
ij_kotlin_allow_trailing_comma=true
ij_kotlin_allow_trailing_comma_on_call_site=true
ij_kotlin_name_count_to_use_star_import=2147483647
ij_kotlin_name_count_to_use_star_import_for_members=2147483647

ktlint_standard_discouraged-comment-location=disabled
ktlint_standard_if-else-wrapping=disabled
ktlint_standard_no-consecutive-comments=disabled
2 changes: 1 addition & 1 deletion .github/workflows/build_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ jobs:
uses: gradle/gradle-build-action@v2
with:
build-root-directory: master
arguments: :server:shadowJar --stacktrace
arguments: ktlintCheck :server:shadowJar --stacktrace

5 changes: 2 additions & 3 deletions AndroidCompat/Config/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
id(libs.plugins.kotlin.jvm.get().pluginId)
id(libs.plugins.kotlin.serialization.get().pluginId)
id(libs.plugins.kotlinter.get().pluginId)
id(libs.plugins.ktlint.get().pluginId)
}

dependencies {
// Shared
implementation(libs.bundles.shared)
testImplementation(libs.bundles.sharedTest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ val ApplicationRootDir: String
get(): String {
return System.getProperty(
"$CONFIG_PREFIX.server.rootDir",
AppDirsFactory.getInstance().getUserDataDir("Tachidesk", null, null)
AppDirsFactory.getInstance().getUserDataDir("Tachidesk", null, null),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import org.kodein.di.bind
import org.kodein.di.singleton

class ConfigKodeinModule {
fun create() = DI.Module("ConfigManager") {
// Config module
bind<ConfigManager>() with singleton { GlobalConfigManager }
}
fun create() =
DI.Module("ConfigManager") {
// Config module
bind<ConfigManager>() with singleton { GlobalConfigManager }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,21 @@ open class ConfigManager {
val baseConfig =
ConfigFactory.parseMap(
mapOf(
"androidcompat.rootDir" to "$ApplicationRootDir/android-compat" // override AndroidCompat's rootDir
)
// override AndroidCompat's rootDir
"androidcompat.rootDir" to "$ApplicationRootDir/android-compat",
),
)

// Load user config
val userConfig = getUserConfig()

val config = ConfigFactory.empty()
.withFallback(baseConfig)
.withFallback(userConfig)
.withFallback(compatConfig)
.withFallback(serverConfig)
.resolve()
val config =
ConfigFactory.empty()
.withFallback(baseConfig)
.withFallback(userConfig)
.withFallback(compatConfig)
.withFallback(serverConfig)
.resolve()

// set log level early
if (debugLogsEnabled(config)) {
Expand All @@ -95,14 +97,20 @@ open class ConfigManager {
}
}

private fun updateUserConfigFile(path: String, value: ConfigValue) {
private fun updateUserConfigFile(
path: String,
value: ConfigValue,
) {
val userConfigDoc = ConfigDocumentFactory.parseFile(userConfigFile)
val updatedConfigDoc = userConfigDoc.withValue(path, value)
val newFileContent = updatedConfigDoc.render()
userConfigFile.writeText(newFileContent)
}

suspend fun updateValue(path: String, value: Any) {
suspend fun updateValue(
path: String,
value: Any,
) {
mutex.withLock {
val configValue = ConfigValueFactory.fromAnyRef(value)

Expand Down Expand Up @@ -140,10 +148,16 @@ open class ConfigManager {
return
}

logger.debug { "user config is out of date, updating... (missingSettings= $hasMissingSettings, outdatedSettings= $hasOutdatedSettings" }
logger.debug {
"user config is out of date, updating... (missingSettings= $hasMissingSettings, outdatedSettings= $hasOutdatedSettings"
}

var newUserConfigDoc: ConfigDocument = resetUserConfig(false)
userConfig.entrySet().filter { serverConfig.hasPath(it.key) }.forEach { newUserConfigDoc = newUserConfigDoc.withValue(it.key, it.value) }
userConfig.entrySet().filter {
serverConfig.hasPath(
it.key,
)
}.forEach { newUserConfigDoc = newUserConfigDoc.withValue(it.key, it.value) }

userConfigFile.writeText(newUserConfigDoc.render())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ abstract class SystemPropertyOverridableConfigModule(getConfig: () -> Config, mo

/** Defines a config property that is overridable with jvm `-D` commandline arguments prefixed with [CONFIG_PREFIX] */
class SystemPropertyOverrideDelegate(val getConfig: () -> Config, val moduleName: String) {
inline operator fun <R, reified T> getValue(thisRef: R, property: KProperty<*>): T {
inline operator fun <R, reified T> getValue(
thisRef: R,
property: KProperty<*>,
): T {
val configValue: T = getConfig().getValue(thisRef, property)

val combined = System.getProperty(
"$CONFIG_PREFIX.$moduleName.${property.name}",
configValue.toString()
)
val combined =
System.getProperty(
"$CONFIG_PREFIX.$moduleName.${property.name}",
configValue.toString(),
)

return when (T::class.simpleName) {
"Int" -> combined.toInt()
Expand Down
68 changes: 39 additions & 29 deletions AndroidCompat/Config/src/main/java/xyz/nulldev/ts/config/Logging.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,37 @@ import mu.KotlinLogging
import org.slf4j.Logger
import org.slf4j.LoggerFactory

private fun createRollingFileAppender(logContext: LoggerContext, logDirPath: String): RollingFileAppender<ILoggingEvent> {
private fun createRollingFileAppender(
logContext: LoggerContext,
logDirPath: String,
): RollingFileAppender<ILoggingEvent> {
val logFilename = "application"

val logEncoder = PatternLayoutEncoder().apply {
pattern = "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n"
context = logContext
start()
}

val appender = RollingFileAppender<ILoggingEvent>().apply {
name = "FILE"
context = logContext
encoder = logEncoder
file = "$logDirPath/$logFilename.log"
}

val rollingPolicy = SizeAndTimeBasedRollingPolicy<ILoggingEvent>().apply {
context = logContext
setParent(appender)
fileNamePattern = "$logDirPath/${logFilename}_%d{yyyy-MM-dd}_%i.log.gz"
setMaxFileSize(FileSize.valueOf("10mb"))
maxHistory = 14
setTotalSizeCap(FileSize.valueOf("1gb"))
start()
}
val logEncoder =
PatternLayoutEncoder().apply {
pattern = "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n"
context = logContext
start()
}

val appender =
RollingFileAppender<ILoggingEvent>().apply {
name = "FILE"
context = logContext
encoder = logEncoder
file = "$logDirPath/$logFilename.log"
}

val rollingPolicy =
SizeAndTimeBasedRollingPolicy<ILoggingEvent>().apply {
context = logContext
setParent(appender)
fileNamePattern = "$logDirPath/${logFilename}_%d{yyyy-MM-dd}_%i.log.gz"
setMaxFileSize(FileSize.valueOf("10mb"))
maxHistory = 14
setTotalSizeCap(FileSize.valueOf("1gb"))
start()
}

appender.rollingPolicy = rollingPolicy
appender.start()
Expand Down Expand Up @@ -72,12 +78,16 @@ fun initLoggerConfig(appRootPath: String) {

const val BASE_LOGGER_NAME = "_BaseLogger"

fun setLogLevelFor(name: String, level: Level) {
val logger = if (name == BASE_LOGGER_NAME) {
getBaseLogger()
} else {
getLogger(name)
}
fun setLogLevelFor(
name: String,
level: Level,
) {
val logger =
if (name == BASE_LOGGER_NAME) {
getBaseLogger()
} else {
getLogger(name)
}

logger.level = level
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package xyz.nulldev.ts.config.util

import com.typesafe.config.Config

operator fun Config.get(key: String) = getString(key)
?: throw IllegalStateException("Could not find value for config entry: $key!")
operator fun Config.get(key: String) =
getString(key)
?: throw IllegalStateException("Could not find value for config entry: $key!")
3 changes: 1 addition & 2 deletions AndroidCompat/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
id(libs.plugins.kotlin.jvm.get().pluginId)
id(libs.plugins.kotlin.serialization.get().pluginId)
id(libs.plugins.kotlinter.get().pluginId)
id(libs.plugins.ktlint.get().pluginId)
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PreferenceManager {
fun getDefaultSharedPreferences(context: Context) =
context.getSharedPreferences(
context.applicationInfo.packageName,
Context.MODE_PRIVATE
Context.MODE_PRIVATE,
)!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.kodein.di.instance
import xyz.nulldev.androidcompat.androidimpl.CustomContext

class AndroidCompat {

val context: CustomContext by DI.global.instance()

fun startApp(application: Application) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ class AndroidCompatInitializer {
GlobalConfigManager.registerModules(
FilesConfigModule.register(GlobalConfigManager.config),
ApplicationInfoConfigModule.register(GlobalConfigManager.config),
SystemConfigModule.register(GlobalConfigManager.config)
SystemConfigModule.register(GlobalConfigManager.config),
)

// Set some properties extensions use
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
System.setProperty(
"http.agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ import xyz.nulldev.androidcompat.service.ServiceSupport
*/

class AndroidCompatModule {
fun create() = DI.Module("AndroidCompat") {
bind<AndroidFiles>() with singleton { AndroidFiles() }
fun create() =
DI.Module("AndroidCompat") {
bind<AndroidFiles>() with singleton { AndroidFiles() }

bind<ApplicationInfoImpl>() with singleton { ApplicationInfoImpl() }
bind<ApplicationInfoImpl>() with singleton { ApplicationInfoImpl() }

bind<ServiceSupport>() with singleton { ServiceSupport() }
bind<ServiceSupport>() with singleton { ServiceSupport() }

bind<FakePackageManager>() with singleton { FakePackageManager() }
bind<FakePackageManager>() with singleton { FakePackageManager() }

bind<PackageController>() with singleton { PackageController() }
bind<PackageController>() with singleton { PackageController() }

// Context
bind<CustomContext>() with singleton { CustomContext() }
bind<Context>() with singleton {
val context: Context by DI.global.instance<CustomContext>()
context
// Context
bind<CustomContext>() with singleton { CustomContext() }
bind<Context>() with
singleton {
val context: Context by DI.global.instance<CustomContext>()
context
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ApplicationInfoConfigModule(getConfig: () -> Config) : ConfigModule(getCon
val debug: Boolean by getConfig()

companion object {
fun register(config: Config) =
ApplicationInfoConfigModule { config.getConfig("android.app") }
fun register(config: Config) = ApplicationInfoConfigModule { config.getConfig("android.app") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class FilesConfigModule(getConfig: () -> Config) : ConfigModule(getConfig) {
val packageDir: String by getConfig()

companion object {
fun register(config: Config) =
FilesConfigModule { config.getConfig("android.files") }
fun register(config: Config) = FilesConfigModule { config.getConfig("android.files") }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ class SystemConfigModule(val getConfig: () -> Config) : ConfigModule(getConfig)
val propertyPrefix = "properties."

fun getStringProperty(property: String) = getConfig().getString("$propertyPrefix$property")!!

fun getIntProperty(property: String) = getConfig().getInt("$propertyPrefix$property")

fun getLongProperty(property: String) = getConfig().getLong("$propertyPrefix$property")

fun getBooleanProperty(property: String) = getConfig().getBoolean("$propertyPrefix$property")

fun hasProperty(property: String) = getConfig().hasPath("$propertyPrefix$property")

companion object {
fun register(config: Config) =
SystemConfigModule { config.getConfig("android.system") }
fun register(config: Config) = SystemConfigModule { config.getConfig("android.system") }
}
}
Loading

0 comments on commit 849acfc

Please sign in to comment.