From bc1674c98511270cecca7d8ef73eccd293afed0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martynas=20Petu=C5=A1ka?= Date: Thu, 2 Sep 2021 14:21:46 +0100 Subject: [PATCH] detekt fixes --- buildSrc/settings.gradle.kts | 2 +- buildSrc/src/main/kotlin/util.kt | 7 ++- sandbox/src/jsMain/kotlin/index.kt | 2 +- src/main/kotlin/delegate/ChainedProperty.kt | 17 +++++++ src/main/kotlin/delegate/FallbackDelegate.kt | 6 ++- src/main/kotlin/delegate/PropertyDelegate.kt | 4 +- src/main/kotlin/delegate/builders.kt | 42 +++++++---------- src/main/kotlin/dsl/NpmAccess.kt | 4 +- src/main/kotlin/dsl/NpmPublication.kt | 48 ++++++++++++++------ src/main/kotlin/dsl/NpmPublishExtension.kt | 14 ++++-- src/main/kotlin/dsl/NpmRepository.kt | 8 +++- src/main/kotlin/dsl/NpmShrinkwrapJson.kt | 10 +++- src/main/kotlin/dsl/PackageJson.kt | 3 +- 13 files changed, 108 insertions(+), 59 deletions(-) create mode 100644 src/main/kotlin/delegate/ChainedProperty.kt diff --git a/buildSrc/settings.gradle.kts b/buildSrc/settings.gradle.kts index d500e5ba..2003b055 100644 --- a/buildSrc/settings.gradle.kts +++ b/buildSrc/settings.gradle.kts @@ -6,4 +6,4 @@ pluginManagement { plugins { id("de.fayard.refreshVersions") -} \ No newline at end of file +} diff --git a/buildSrc/src/main/kotlin/util.kt b/buildSrc/src/main/kotlin/util.kt index dd7788b3..1e3d4cc7 100644 --- a/buildSrc/src/main/kotlin/util.kt +++ b/buildSrc/src/main/kotlin/util.kt @@ -1,7 +1,6 @@ -import groovy.lang.* -import org.gradle.api.provider.* -import java.io.* -import java.nio.charset.* +import groovy.lang.Closure +import org.gradle.api.provider.Property +import java.nio.charset.Charset typealias Lambda = R.() -> V diff --git a/sandbox/src/jsMain/kotlin/index.kt b/sandbox/src/jsMain/kotlin/index.kt index 9346f0b3..e5eebdc9 100644 --- a/sandbox/src/jsMain/kotlin/index.kt +++ b/sandbox/src/jsMain/kotlin/index.kt @@ -3,4 +3,4 @@ package sandbox import test.sandbox.sayHello @JsExport -fun sayWelcome(): String = sayHello() \ No newline at end of file +fun sayWelcome(): String = sayHello() diff --git a/src/main/kotlin/delegate/ChainedProperty.kt b/src/main/kotlin/delegate/ChainedProperty.kt new file mode 100644 index 00000000..dcfc0ca0 --- /dev/null +++ b/src/main/kotlin/delegate/ChainedProperty.kt @@ -0,0 +1,17 @@ +package dev.petuska.npm.publish.delegate + +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +internal class ChainedProperty( + private var main: ReadWriteProperty, + private val fallback: ReadWriteProperty +) : ReadWriteProperty { + override fun getValue(thisRef: R, property: KProperty<*>): V { + return main.getValue(thisRef, property) ?: fallback.getValue(thisRef, property) + } + + override fun setValue(thisRef: R, property: KProperty<*>, value: V) { + main.setValue(thisRef, property, value) + } +} diff --git a/src/main/kotlin/delegate/FallbackDelegate.kt b/src/main/kotlin/delegate/FallbackDelegate.kt index aa728eed..c00ee0c6 100644 --- a/src/main/kotlin/delegate/FallbackDelegate.kt +++ b/src/main/kotlin/delegate/FallbackDelegate.kt @@ -7,7 +7,11 @@ internal class FallbackDelegate( private val fallbackObj: F, private val projection: F.() -> V ) : ReadWriteProperty { - constructor(fallbackObj: F, default: V, projection: F.() -> V?) : this(fallbackObj, { fallbackObj.projection() ?: default }) + constructor( + fallbackObj: F, + default: V, + projection: F.() -> V? + ) : this(fallbackObj, { fallbackObj.projection() ?: default }) private var value: V? = null diff --git a/src/main/kotlin/delegate/PropertyDelegate.kt b/src/main/kotlin/delegate/PropertyDelegate.kt index 1ccfd1a2..31939790 100644 --- a/src/main/kotlin/delegate/PropertyDelegate.kt +++ b/src/main/kotlin/delegate/PropertyDelegate.kt @@ -2,6 +2,7 @@ package dev.petuska.npm.publish.delegate import dev.petuska.npm.publish.util.propertyOrNull import org.gradle.api.Project +import java.util.Locale import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty @@ -29,7 +30,8 @@ internal class PropertyDelegate( } private fun KProperty<*>.findEnv(): String? { - return System.getenv(buildPropertyKey().toUpperCase().replace("[.\\- ]".toRegex(), "_"))?.toString() + return System.getenv(buildPropertyKey().uppercase(Locale.getDefault()).replace("[.\\- ]".toRegex(), "_")) + ?.toString() } private fun KProperty<*>.buildPropertyKey() = diff --git a/src/main/kotlin/delegate/builders.kt b/src/main/kotlin/delegate/builders.kt index 010eaad4..e259dcb6 100644 --- a/src/main/kotlin/delegate/builders.kt +++ b/src/main/kotlin/delegate/builders.kt @@ -2,41 +2,31 @@ package dev.petuska.npm.publish.delegate import org.gradle.api.Project import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty import kotlin.reflect.KProperty1 -internal class ChainedProperty( - private var main: ReadWriteProperty, - private val fallback: ReadWriteProperty -) : ReadWriteProperty { - override fun getValue(thisRef: R, property: KProperty<*>): V { - return main.getValue(thisRef, property) ?: fallback.getValue(thisRef, property) - } +internal infix fun ReadWriteProperty.or(fallback: ReadWriteProperty) = + ChainedProperty(this, fallback) - override fun setValue(thisRef: R, property: KProperty<*>, value: V) { - main.setValue(thisRef, property, value) - } -} +internal fun R.fallbackDelegate(prop: KProperty1) = + FallbackDelegate(this) { prop.get(this) } -internal infix fun ReadWriteProperty.or(fallback: ReadWriteProperty) = ChainedProperty(this, fallback) +internal fun R.fallbackDelegate(prop: KProperty1, transform: P.() -> V) = + FallbackDelegate(this) { prop.get(this).transform() } -internal fun R.fallbackDelegate(prop: KProperty1) = FallbackDelegate(this) { - prop.get(this) -} +internal fun R.fallbackDelegate(prop: KProperty1, default: V) = + FallbackDelegate(this, default) { prop.get(this) } -internal fun R.fallbackDelegate(prop: KProperty1, transform: P.() -> V) = FallbackDelegate(this) { - prop.get(this).transform() -} +internal fun R.fallbackDelegate(projection: R.() -> V) = + FallbackDelegate(this, projection) -internal fun R.fallbackDelegate(prop: KProperty1, default: V) = FallbackDelegate(this, default) { - prop.get(this) -} +internal fun R.fallbackDelegate(default: V, projection: R.() -> V) = + FallbackDelegate(this, default, projection) -internal fun R.fallbackDelegate(projection: R.() -> V) = FallbackDelegate(this, projection) -internal fun R.fallbackDelegate(default: V, projection: R.() -> V) = FallbackDelegate(this, default, projection) +internal fun Project.propertyDelegate(prefix: String? = null, converter: (String) -> V?) = + propertyDelegate(prefix, null, converter) -internal fun Project.propertyDelegate(prefix: String? = null, converter: (String) -> V?) = propertyDelegate(prefix, null, converter) -internal fun Project.propertyDelegate(prefix: String? = null, default: V, converter: (String) -> V?) = PropertyDelegate(project, prefix, converter, default) +internal fun Project.propertyDelegate(prefix: String? = null, default: V, converter: (String) -> V?) = + PropertyDelegate(project, prefix, converter, default) internal inline fun Project.gradleNullableProperty(default: V? = null) = GradleProperty.Nullable(this, V::class, default) diff --git a/src/main/kotlin/dsl/NpmAccess.kt b/src/main/kotlin/dsl/NpmAccess.kt index 9a3eda03..1c3dae92 100644 --- a/src/main/kotlin/dsl/NpmAccess.kt +++ b/src/main/kotlin/dsl/NpmAccess.kt @@ -1,5 +1,7 @@ package dev.petuska.npm.publish.dsl +import java.util.Locale + /** * Enum representation of NPM repository access */ @@ -8,7 +10,7 @@ enum class NpmAccess { RESTRICTED; override fun toString(): String { - return name.toLowerCase() + return name.lowercase(Locale.getDefault()) } companion object { diff --git a/src/main/kotlin/dsl/NpmPublication.kt b/src/main/kotlin/dsl/NpmPublication.kt index 7f8b1b06..64a2d46a 100644 --- a/src/main/kotlin/dsl/NpmPublication.kt +++ b/src/main/kotlin/dsl/NpmPublication.kt @@ -37,19 +37,26 @@ class NpmPublication internal constructor( * Flag to bundle kotlin-only dependencies. * Defaults to true and only works for autogenerated kotlin publications. */ - var bundleKotlinDependencies: Boolean by project.propertyDelegate(propGroup) { it.notFalse() } or extension.fallbackDelegate(NpmPublishExtension::bundleKotlinDependencies) + var bundleKotlinDependencies: Boolean by project + .propertyDelegate(propGroup) { it.notFalse() } or extension.fallbackDelegate( + NpmPublishExtension::bundleKotlinDependencies + ) /** * Flag to add bundled dependencies to npm-shrinkwrap.json. * Defaults to true. */ - var shrinkwrapBundledDependencies: Boolean by project.propertyDelegate(propGroup) { it.notFalse() } or extension.fallbackDelegate(NpmPublishExtension::shrinkwrapBundledDependencies) + var shrinkwrapBundledDependencies: Boolean by project + .propertyDelegate(propGroup) { it.notFalse() } or extension.fallbackDelegate( + NpmPublishExtension::shrinkwrapBundledDependencies + ) /** * Optional npm scope. If set, package name will be constructed as `@{scope}/{moduleName}`. * Defaults to [NpmPublishExtension.organization]. */ - var scope: String? by project.propertyDelegate(propGroup) { it } or extension.fallbackDelegate(NpmPublishExtension::organization) + var scope: String? by project + .propertyDelegate(propGroup) { it } or extension.fallbackDelegate(NpmPublishExtension::organization) /** * NPM module name. @@ -61,7 +68,8 @@ class NpmPublication internal constructor( * NPM package version. * Defaults to [NpmPublishExtension.version]. */ - var version: String? by project.propertyDelegate(propGroup) { it } or extension.fallbackDelegate(NpmPublishExtension::version) + var version: String? by project + .propertyDelegate(propGroup) { it } or extension.fallbackDelegate(NpmPublishExtension::version) /** * Main js entry file. Can also be set via [packageJson] DSL. @@ -80,12 +88,16 @@ class NpmPublication internal constructor( * If set, the file will be moved to package assembly root and renamed to README.MD (regardless of the actual name). * Defaults to [NpmPublishExtension.readme] */ - var readme: File? by project.propertyDelegate(propGroup) { File(it) } or extension.fallbackDelegate(NpmPublishExtension::readme) + var readme: File? by project.propertyDelegate(propGroup) { File(it) } or extension.fallbackDelegate( + NpmPublishExtension::readme + ) /** - * Base NodeJS directory to be used when building and publishing the publication. Defaults to 'NODE_HOME' env variable. + * Base NodeJS directory to be used when building and publishing the publication. + * Defaults to 'NODE_HOME' env variable. */ - var nodeJsDir by project.propertyDelegate(propGroup, System.getenv("NODE_HOME")?.let(::File)) { File(it) } + var nodeJsDir by project + .propertyDelegate(propGroup, System.getenv("NODE_HOME")?.let(::File)) { File(it) } /** * Publication assembly directory. @@ -142,7 +154,8 @@ class NpmPublication internal constructor( var packageJsonTemplateFile by project.gradleNullableProperty() /** - * If [packageJsonFile] is not set and this property is set, ignore any further package.json generation configs produced by DSLs + * If [packageJsonFile] is not set and this property is set, + * ignore any further package.json generation configs produced by DSLs */ var packageJson by project.gradleNullableProperty<(PackageJson.() -> Unit)>() @@ -164,29 +177,34 @@ class NpmPublication internal constructor( * DSL builder to configure NPM dependencies for this publication. */ fun dependencies(config: MutableList.() -> Unit) = npmDependencies.config() - fun MutableList.dependency(name: String, version: String, scope: NpmDependency.Scope) = NpmDependency(project, name, version, scope, false).also { - add(it) - } + fun MutableList.dependency(name: String, version: String, scope: NpmDependency.Scope) = + NpmDependency(project, name, version, scope, false).also { + add(it) + } /** * Adds a [regular](https://docs.npmjs.com/files/package.json#dependencies) npm dependency. */ - fun MutableList.npm(name: String, version: String) = dependency(name, version, NpmDependency.Scope.NORMAL) + fun MutableList.npm(name: String, version: String) = + dependency(name, version, NpmDependency.Scope.NORMAL) /** * Adds a [dev](https://docs.npmjs.com/files/package.json#devdependencies) npm dependency. */ - fun MutableList.npmDev(name: String, version: String) = dependency(name, version, NpmDependency.Scope.DEV) + fun MutableList.npmDev(name: String, version: String) = + dependency(name, version, NpmDependency.Scope.DEV) /** * Adds an [optional](https://docs.npmjs.com/files/package.json#optionaldependencies) npm dependency. */ - fun MutableList.npmOptional(name: String, version: String) = dependency(name, version, NpmDependency.Scope.OPTIONAL) + fun MutableList.npmOptional(name: String, version: String) = + dependency(name, version, NpmDependency.Scope.OPTIONAL) /** * Adds a [peer](https://docs.npmjs.com/files/package.json#peerdependencies) npm dependency. */ - fun MutableList.npmPeer(name: String, version: String) = dependency(name, version, NpmDependency.Scope.PEER) + fun MutableList.npmPeer(name: String, version: String) = + dependency(name, version, NpmDependency.Scope.PEER) internal fun validate(alternativeNodeJsDir: File?): NpmPublication? { nodeJsDir = nodeJsDir ?: alternativeNodeJsDir diff --git a/src/main/kotlin/dsl/NpmPublishExtension.kt b/src/main/kotlin/dsl/NpmPublishExtension.kt index a3277739..f64b87c3 100644 --- a/src/main/kotlin/dsl/NpmPublishExtension.kt +++ b/src/main/kotlin/dsl/NpmPublishExtension.kt @@ -50,7 +50,8 @@ open class NpmPublishExtension(private val project: Project) : NpmPublishExtensi var shrinkwrapBundledDependencies: Boolean by project.propertyDelegate(default = true) { it.notFalse() } /** - * Specifies if a dry-run should be added to the npm command arguments. Dry run does all the normal run des except actual file uploading. + * Specifies if a dry-run should be added to the npm command arguments. + * Dry run does all the normal run des except actual file uploading. * Defaults to `npm.publish.dry` project property if set or `false` otherwise. */ var dry: Boolean by project.propertyDelegate(default = false) { it.notFalse() } @@ -137,9 +138,14 @@ open class NpmPublishExtension(private val project: Project) : NpmPublishExtensi * Will look for existing publication with the same name or create a new one before applying the configuration */ fun NpmPublicationContainer.publication(name: String, config: NpmPublication.() -> Unit): NpmPublication { - val pub = findByName(name) ?: NpmPublication(name, this@NpmPublishExtension.project, this@NpmPublishExtension).also { - add(it) - } + val pub = findByName(name) + ?: NpmPublication( + name = name, + project = this@NpmPublishExtension.project, + extension = this@NpmPublishExtension + ).also { + add(it) + } pub.apply(config) return pub } diff --git a/src/main/kotlin/dsl/NpmRepository.kt b/src/main/kotlin/dsl/NpmRepository.kt index d12fc35b..22663f86 100644 --- a/src/main/kotlin/dsl/NpmRepository.kt +++ b/src/main/kotlin/dsl/NpmRepository.kt @@ -24,7 +24,9 @@ class NpmRepository internal constructor( /** * Repository access. */ - var access: NpmAccess by project.propertyDelegate(propGroup) { NpmAccess.fromString(it) } or npmExtension.fallbackDelegate(NpmPublishExtension::access) + var access: NpmAccess by project.propertyDelegate(propGroup) { + NpmAccess.fromString(it) + } or npmExtension.fallbackDelegate(NpmPublishExtension::access) /** * NPM Registry uri to publish packages to. Should include schema domain and path if required @@ -44,7 +46,9 @@ class NpmRepository internal constructor( /** * Overrides [NpmPublishExtension.dry] option for this repository */ - var dry: Boolean by project.propertyDelegate(propGroup) { it.notFalse() } or npmExtension.fallbackDelegate(NpmPublishExtension::dry) + var dry: Boolean by project.propertyDelegate(propGroup) { it.notFalse() } or npmExtension.fallbackDelegate( + NpmPublishExtension::dry + ) internal fun validate(): NpmRepository? { return takeIf { diff --git a/src/main/kotlin/dsl/NpmShrinkwrapJson.kt b/src/main/kotlin/dsl/NpmShrinkwrapJson.kt index e0932dae..e7fc43cc 100644 --- a/src/main/kotlin/dsl/NpmShrinkwrapJson.kt +++ b/src/main/kotlin/dsl/NpmShrinkwrapJson.kt @@ -2,7 +2,12 @@ package dev.petuska.npm.publish.dsl import dev.petuska.npm.publish.util.npmFullName -class NpmShrinkwrapJson(name: String, version: String, scope: String? = null, config: NpmShrinkwrapJson.() -> Unit = {}) : JsonObject() { +class NpmShrinkwrapJson( + name: String, + version: String, + scope: String? = null, + config: NpmShrinkwrapJson.() -> Unit = {} +) : JsonObject() { /** * [name](https://docs.npmjs.com/files/package.json#name) */ @@ -31,7 +36,8 @@ class NpmShrinkwrapJson(name: String, version: String, scope: String? = null, co /** * Creates or extends shrinkwrap dependencies config. */ - fun dependencies(config: JsonObject.() -> Unit = {}) = (dependencies ?: JsonObject()).apply(config).also { dependencies = it } + fun dependencies(config: JsonObject.() -> Unit = {}) = + (dependencies ?: JsonObject()).apply(config).also { dependencies = it } /** * Creates and adds a shrinkwrap dependency. diff --git a/src/main/kotlin/dsl/PackageJson.kt b/src/main/kotlin/dsl/PackageJson.kt index 6bb0a655..f9420e7b 100644 --- a/src/main/kotlin/dsl/PackageJson.kt +++ b/src/main/kotlin/dsl/PackageJson.kt @@ -78,7 +78,8 @@ operator fun JsonObject.setValue(thisRef: JsonObject, property: KP } /** - * A class representing [package.json](https://docs.npmjs.com/files/package.json) schema. Custom fields can be added as regular map entries. + * A class representing [package.json](https://docs.npmjs.com/files/package.json) schema. + * Custom fields can be added as regular map entries. */ class PackageJson() : JsonObject() { constructor(name: String, version: String?, scope: String? = null, config: PackageJson.() -> Unit = {}) : this() {