Skip to content

Commit

Permalink
detekt fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetuska committed Sep 2, 2021
1 parent 9841e24 commit bc1674c
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 59 deletions.
2 changes: 1 addition & 1 deletion buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ pluginManagement {

plugins {
id("de.fayard.refreshVersions")
}
}
7 changes: 3 additions & 4 deletions buildSrc/src/main/kotlin/util.kt
Original file line number Diff line number Diff line change
@@ -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> = R.() -> V

Expand Down
2 changes: 1 addition & 1 deletion sandbox/src/jsMain/kotlin/index.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package sandbox
import test.sandbox.sayHello

@JsExport
fun sayWelcome(): String = sayHello()
fun sayWelcome(): String = sayHello()
17 changes: 17 additions & 0 deletions src/main/kotlin/delegate/ChainedProperty.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.petuska.npm.publish.delegate

import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

internal class ChainedProperty<R, V>(
private var main: ReadWriteProperty<R, V?>,
private val fallback: ReadWriteProperty<R, V>
) : ReadWriteProperty<R, V> {
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)
}
}
6 changes: 5 additions & 1 deletion src/main/kotlin/delegate/FallbackDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ internal class FallbackDelegate<V, F>(
private val fallbackObj: F,
private val projection: F.() -> V
) : ReadWriteProperty<Any, V> {
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

Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/delegate/PropertyDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -29,7 +30,8 @@ internal class PropertyDelegate<V>(
}

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() =
Expand Down
42 changes: 16 additions & 26 deletions src/main/kotlin/delegate/builders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<R, V>(
private var main: ReadWriteProperty<R, V?>,
private val fallback: ReadWriteProperty<R, V>
) : ReadWriteProperty<R, V> {
override fun getValue(thisRef: R, property: KProperty<*>): V {
return main.getValue(thisRef, property) ?: fallback.getValue(thisRef, property)
}
internal infix fun <R, V> ReadWriteProperty<R, V?>.or(fallback: ReadWriteProperty<R, V>) =
ChainedProperty(this, fallback)

override fun setValue(thisRef: R, property: KProperty<*>, value: V) {
main.setValue(thisRef, property, value)
}
}
internal fun <R, V> R.fallbackDelegate(prop: KProperty1<R, V>) =
FallbackDelegate(this) { prop.get(this) }

internal infix fun <R, V> ReadWriteProperty<R, V?>.or(fallback: ReadWriteProperty<R, V>) = ChainedProperty(this, fallback)
internal fun <R, P, V> R.fallbackDelegate(prop: KProperty1<R, P>, transform: P.() -> V) =
FallbackDelegate(this) { prop.get(this).transform() }

internal fun <R, V> R.fallbackDelegate(prop: KProperty1<R, V>) = FallbackDelegate(this) {
prop.get(this)
}
internal fun <R, V> R.fallbackDelegate(prop: KProperty1<R, V?>, default: V) =
FallbackDelegate(this, default) { prop.get(this) }

internal fun <R, P, V> R.fallbackDelegate(prop: KProperty1<R, P>, transform: P.() -> V) = FallbackDelegate(this) {
prop.get(this).transform()
}
internal fun <R, V> R.fallbackDelegate(projection: R.() -> V) =
FallbackDelegate(this, projection)

internal fun <R, V> R.fallbackDelegate(prop: KProperty1<R, V?>, default: V) = FallbackDelegate(this, default) {
prop.get(this)
}
internal fun <R, V> R.fallbackDelegate(default: V, projection: R.() -> V) =
FallbackDelegate(this, default, projection)

internal fun <R, V> R.fallbackDelegate(projection: R.() -> V) = FallbackDelegate(this, projection)
internal fun <R, V> R.fallbackDelegate(default: V, projection: R.() -> V) = FallbackDelegate(this, default, projection)
internal fun <V> Project.propertyDelegate(prefix: String? = null, converter: (String) -> V?) =
propertyDelegate(prefix, null, converter)

internal fun <V> Project.propertyDelegate(prefix: String? = null, converter: (String) -> V?) = propertyDelegate(prefix, null, converter)
internal fun <V> Project.propertyDelegate(prefix: String? = null, default: V, converter: (String) -> V?) = PropertyDelegate(project, prefix, converter, default)
internal fun <V> Project.propertyDelegate(prefix: String? = null, default: V, converter: (String) -> V?) =
PropertyDelegate(project, prefix, converter, default)

internal inline fun <reified V : Any> Project.gradleNullableProperty(default: V? = null) =
GradleProperty.Nullable(this, V::class, default)
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/dsl/NpmAccess.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.petuska.npm.publish.dsl

import java.util.Locale

/**
* Enum representation of NPM repository access
*/
Expand All @@ -8,7 +10,7 @@ enum class NpmAccess {
RESTRICTED;

override fun toString(): String {
return name.toLowerCase()
return name.lowercase(Locale.getDefault())
}

companion object {
Expand Down
48 changes: 33 additions & 15 deletions src/main/kotlin/dsl/NpmPublication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -142,7 +154,8 @@ class NpmPublication internal constructor(
var packageJsonTemplateFile by project.gradleNullableProperty<File>()

/**
* 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)>()

Expand All @@ -164,29 +177,34 @@ class NpmPublication internal constructor(
* DSL builder to configure NPM dependencies for this publication.
*/
fun dependencies(config: MutableList<NpmDependency>.() -> Unit) = npmDependencies.config()
fun MutableList<NpmDependency>.dependency(name: String, version: String, scope: NpmDependency.Scope) = NpmDependency(project, name, version, scope, false).also {
add(it)
}
fun MutableList<NpmDependency>.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<NpmDependency>.npm(name: String, version: String) = dependency(name, version, NpmDependency.Scope.NORMAL)
fun MutableList<NpmDependency>.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<NpmDependency>.npmDev(name: String, version: String) = dependency(name, version, NpmDependency.Scope.DEV)
fun MutableList<NpmDependency>.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<NpmDependency>.npmOptional(name: String, version: String) = dependency(name, version, NpmDependency.Scope.OPTIONAL)
fun MutableList<NpmDependency>.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<NpmDependency>.npmPeer(name: String, version: String) = dependency(name, version, NpmDependency.Scope.PEER)
fun MutableList<NpmDependency>.npmPeer(name: String, version: String) =
dependency(name, version, NpmDependency.Scope.PEER)

internal fun validate(alternativeNodeJsDir: File?): NpmPublication? {
nodeJsDir = nodeJsDir ?: alternativeNodeJsDir
Expand Down
14 changes: 10 additions & 4 deletions src/main/kotlin/dsl/NpmPublishExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/dsl/NpmRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/main/kotlin/dsl/NpmShrinkwrapJson.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any>() {
class NpmShrinkwrapJson(
name: String,
version: String,
scope: String? = null,
config: NpmShrinkwrapJson.() -> Unit = {}
) : JsonObject<Any>() {
/**
* [name](https://docs.npmjs.com/files/package.json#name)
*/
Expand Down Expand Up @@ -31,7 +36,8 @@ class NpmShrinkwrapJson(name: String, version: String, scope: String? = null, co
/**
* Creates or extends shrinkwrap dependencies config.
*/
fun dependencies(config: JsonObject<ShrinkwrapDependency>.() -> Unit = {}) = (dependencies ?: JsonObject()).apply(config).also { dependencies = it }
fun dependencies(config: JsonObject<ShrinkwrapDependency>.() -> Unit = {}) =
(dependencies ?: JsonObject()).apply(config).also { dependencies = it }

/**
* Creates and adds a shrinkwrap dependency.
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/dsl/PackageJson.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ operator fun <R> JsonObject<Any>.setValue(thisRef: JsonObject<Any>, 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<Any>() {
constructor(name: String, version: String?, scope: String? = null, config: PackageJson.() -> Unit = {}) : this() {
Expand Down

0 comments on commit bc1674c

Please sign in to comment.