Skip to content

Commit

Permalink
spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetuska committed Oct 23, 2021
1 parent bc1674c commit 3313694
Show file tree
Hide file tree
Showing 23 changed files with 815 additions and 996 deletions.
54 changes: 21 additions & 33 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import de.fayard.refreshVersions.core.versionFor
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
if (System.getenv("CI") == null) {
id("plugin.git-hooks")
}
kotlin("jvm") version "1.4.31"
id("com.gradle.plugin-publish")
id("org.jetbrains.dokka")
id("com.github.jakemarsden.git-hooks")
id("org.jlleitschuh.gradle.ktlint")
id("io.github.gradle-nexus.publish-plugin")
id("com.diffplug.spotless")
`java-gradle-plugin`
`maven-publish`
signing
idea
}

description = """
description =
"""
A maven-publish alternative for NPM package publishing.
Integrates with kotlin JS/MPP plugins (if applied) to automatically
setup publishing to NPM repositories for all JS targets.
Expand All @@ -27,9 +29,9 @@ idea {
}
}

ktlint {
version by versionFor("version.ktlint")
additionalEditorconfigFile.set(rootDir.resolve(".editorconfig"))
spotless {
kotlin { ktfmt() }
kotlinGradle { ktfmt() }
}

gradleEnterprise {
Expand All @@ -39,15 +41,6 @@ gradleEnterprise {
}
}

gitHooks {
setHooks(
mapOf(
"pre-commit" to "ktlintFormat",
"pre-push" to "ktlintCheck"
)
)
}

repositories {
mavenLocal()
mavenCentral()
Expand All @@ -58,11 +51,14 @@ kotlin {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:_")
implementation("com.google.code.gson:gson:_")
api("dev.petuska:kon:_")
testImplementation("io.kotest:kotest-runner-junit5:_")
testImplementation("dev.petuska:klip:_")
}
}

val pluginId = "dev.petuska.npm.publish"

gradlePlugin {
plugins {
create(name) {
Expand Down Expand Up @@ -105,13 +101,7 @@ signing {
}
}

tasks {
withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "${JavaVersion.VERSION_11}"
}
}
}
tasks { withType<KotlinCompile> { kotlinOptions { jvmTarget = "${JavaVersion.VERSION_11}" } } }

publishing {
publications {
Expand Down Expand Up @@ -159,17 +149,15 @@ afterEvaluate {
tasks {
withType<Jar> {
manifest {
attributes += sortedMapOf(
"Built-By" to System.getProperty("user.name"),
"Build-Jdk" to System.getProperty("java.version"),
"Implementation-Version" to project.version,
"Created-By" to "Gradle v${GradleVersion.current()}",
"Created-From" to Git.headCommitHash
)
attributes +=
sortedMapOf(
"Built-By" to System.getProperty("user.name"),
"Build-Jdk" to System.getProperty("java.version"),
"Implementation-Version" to project.version,
"Created-By" to "Gradle v${GradleVersion.current()}",
"Created-From" to Git.headCommitHash)
}
}
withType<Test> {
useJUnitPlatform()
}
withType<Test> { useJUnitPlatform() }
}
}
4 changes: 4 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ repositories {
jcenter()
mavenCentral()
}

dependencies {
implementation("com.github.jakemarsden:git-hooks-gradle-plugin:_")
}
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/plugin.git-hooks.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins {
id("com.github.jakemarsden.git-hooks")
}

gitHooks { setHooks(mapOf("pre-commit" to "spotlessApply", "pre-push" to "spotlessCheck")) }
205 changes: 108 additions & 97 deletions src/main/kotlin/NpmPublishPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsSetupTask
import org.jetbrains.kotlin.gradle.targets.js.npm.NpmDependency

/**
* Main entry point for npm-publish plugin
*/
/** Main entry point for npm-publish plugin */
class NpmPublishPlugin : Plugin<Project> {
override fun apply(project: Project) {
val extension = project.createExtension()
Expand All @@ -45,119 +43,130 @@ class NpmPublishPlugin : Plugin<Project> {
private const val KOTLIN_MPP_PLUGIN = "org.jetbrains.kotlin.multiplatform"
private const val MAVEN_PUBLISH_PLUGIN = "org.gradle.maven-publish"

private fun Project.createExtension() = extensions.findByType(NpmPublishExtension::class.java) ?: extensions.create(
EXTENSION_NAME,
NpmPublishExtension::class.java,
this@createExtension
)
private fun Project.createExtension() =
extensions.findByType(NpmPublishExtension::class.java)
?: extensions.create(
EXTENSION_NAME, NpmPublishExtension::class.java, this@createExtension)

private fun Project.configureExtension(extension: NpmPublishExtension, target: KotlinJsTargetDsl) {
private fun Project.configureExtension(
extension: NpmPublishExtension,
target: KotlinJsTargetDsl
) {
target.binaries.find { it.mode == KotlinJsBinaryMode.PRODUCTION }?.let { binary ->
val deps = binary.compilation.relatedConfigurationNames.flatMap { conf ->
val mainName = "${target.name}Main${conf.substringAfter(target.name)}"
val normDeps = configurations.findByName(conf)?.dependencies?.toSet() ?: setOf()
val mainDeps = configurations.findByName(mainName)?.dependencies?.toSet() ?: setOf()
(normDeps + mainDeps).filterIsInstance<NpmDependency>()
}
val deps =
binary.compilation.relatedConfigurationNames.flatMap { conf ->
val mainName = "${target.name}Main${conf.substringAfter(target.name)}"
val normDeps = configurations.findByName(conf)?.dependencies?.toSet() ?: setOf()
val mainDeps = configurations.findByName(mainName)?.dependencies?.toSet() ?: setOf()
(normDeps + mainDeps).filterIsInstance<NpmDependency>()
}

extension.apply {
publications(0) {
publication(target.name) {
this.binary = binary
this.main = compileKotlinTask?.outputFileProperty?.orNull?.name
dependencies {
addAll(deps)
}
packageJson {
bundledDependencies {
-"kotlin-test.*".toRegex()
}
}
dependencies { addAll(deps) }
packageJson { bundledDependencies { -"kotlin-test.*".toRegex() } }
}
}
}
}
}

private fun Project.configureTasks(extension: NpmPublishExtension) {
val nodeJsSetupTask = project.rootProject.tasks.findByName("kotlinNodeJsSetup") as NodeJsSetupTask?
val nodeJsSetupTask =
project.rootProject.tasks.findByName("kotlinNodeJsSetup") as NodeJsSetupTask?

val publishTask = tasks.findByName("publish") ?: tasks.create("publish") {
it.group = "publishing"
it.enabled = false
}
val publishTask =
tasks.findByName("publish")
?: tasks.create("publish") {
it.group = "publishing"
it.enabled = false
}
val assembleTask = tasks.findByName("assemble")
val packTask = tasks.findByName("pack") ?: tasks.create("pack") {
it.group = "build"
it.enabled = false
}

val publications = with(extension) {
pubConfigs.forEach {
publications.configure(it)
}
publications.mapNotNull { pub ->
val needsNode = pub.nodeJsDir == null
pub.validate(nodeJsSetupTask?.destination)?.let {
it to nodeJsSetupTask?.takeIf { needsNode }
} ?: null.also { logger.warn("NPM Publication [${pub.name}] is invalid. Skipping...") }
}
}

val repositories = with(extension) {
repoConfigs.forEach {
repositories.configure(it)
}
repositories.mapNotNull { repo ->
repo.validate() ?: null.also { logger.warn("NPM Repository [${repo.name}] is invalid. Skipping...") }
}
}
val packTask =
tasks.findByName("pack")
?: tasks.create("pack") {
it.group = "build"
it.enabled = false
}

val pubTasks = publications.flatMap { (pub, nodeJsTask) ->
val processResourcesTask = pub.binary?.compilation?.let {
val processResourcesTask = project.tasks.findByName(it.processResourcesTaskName) as Copy
pub.kotlinDestinationDir?.let { kotlinDestinationDir ->
pub.files {
from(kotlinDestinationDir)
from(processResourcesTask.destinationDir)
val publications =
with(extension) {
pubConfigs.forEach { publications.configure(it) }
publications.mapNotNull { pub ->
val needsNode = pub.nodeJsDir == null
pub.validate(nodeJsSetupTask?.destination)?.let {
it to nodeJsSetupTask?.takeIf { needsNode }
}
?: null.also {
logger.warn("NPM Publication [${pub.name}] is invalid. Skipping...")
}
}
}
processResourcesTask
}
val upperName = GUtil.toCamelCase(pub.name)

val assembleTaskName = "assemble${upperName}NpmPublication"
val packTaskName = "pack${upperName}NpmPublication"
val assemblePackageTask = tasks.findByName(assembleTaskName) as NpmPackageAssembleTask?
?: tasks.create(assembleTaskName, NpmPackageAssembleTask::class.java, pub).also { task ->
task.onlyIf { pub.compileKotlinTask?.outputFileProperty?.orNull?.exists() ?: true }
task.dependsOn(
*listOfNotNull(
processResourcesTask,
pub.kotlinMainTask,
nodeJsTask
).toTypedArray()
)
assembleTask?.dependsOn(task)
}
val npmPackTask = tasks.findByName(packTaskName) as NpmPackTask?
?: tasks.create(packTaskName, NpmPackTask::class.java, pub).also {
it.onlyIf { assemblePackageTask.didWork }
it.dependsOn(assemblePackageTask)
val repositories =
with(extension) {
repoConfigs.forEach { repositories.configure(it) }
repositories.mapNotNull { repo ->
repo.validate()
?: null.also {
logger.warn("NPM Repository [${repo.name}] is invalid. Skipping...")
}
}
}
packTask.dependsOn(npmPackTask)
packTask.enabled = true
repositories.map { repo ->
val upperRepoName = GUtil.toCamelCase(repo.name)
val publishTaskName = "publish${upperName}NpmPublicationTo$upperRepoName"
tasks.findByName(publishTaskName)
?: tasks.create(publishTaskName, NpmPublishTask::class.java, pub, repo).also { task ->
task.onlyIf { assemblePackageTask.didWork }
task.dependsOn(assemblePackageTask)
publishTask.dependsOn(task)

val pubTasks =
publications.flatMap { (pub, nodeJsTask) ->
val processResourcesTask =
pub.binary?.compilation?.let {
val processResourcesTask =
project.tasks.findByName(it.processResourcesTaskName) as Copy
pub.kotlinDestinationDir?.let { kotlinDestinationDir ->
pub.files {
from(kotlinDestinationDir)
from(processResourcesTask.destinationDir)
}
}
processResourcesTask
}
val upperName = GUtil.toCamelCase(pub.name)

val assembleTaskName = "assemble${upperName}NpmPublication"
val packTaskName = "pack${upperName}NpmPublication"
val assemblePackageTask =
tasks.findByName(assembleTaskName) as NpmPackageAssembleTask?
?: tasks.create(assembleTaskName, NpmPackageAssembleTask::class.java, pub)
.also { task ->
task.onlyIf {
pub.compileKotlinTask?.outputFileProperty?.orNull?.exists() ?: true
}
task.dependsOn(
*listOfNotNull(processResourcesTask, pub.kotlinMainTask, nodeJsTask)
.toTypedArray())
assembleTask?.dependsOn(task)
}
val npmPackTask =
tasks.findByName(packTaskName) as NpmPackTask?
?: tasks.create(packTaskName, NpmPackTask::class.java, pub).also {
it.onlyIf { assemblePackageTask.didWork }
it.dependsOn(assemblePackageTask)
}
packTask.dependsOn(npmPackTask)
packTask.enabled = true
repositories.map { repo ->
val upperRepoName = GUtil.toCamelCase(repo.name)
val publishTaskName = "publish${upperName}NpmPublicationTo$upperRepoName"
tasks.findByName(publishTaskName)
?: tasks.create(publishTaskName, NpmPublishTask::class.java, pub, repo).also {
task ->
task.onlyIf { assemblePackageTask.didWork }
task.dependsOn(assemblePackageTask)
publishTask.dependsOn(task)
}
}
}
}
}
if (pubTasks.isNotEmpty()) {
publishTask.enabled = true
}
Expand All @@ -166,8 +175,10 @@ class NpmPublishPlugin : Plugin<Project> {
}

internal val Project.npmPublishing: NpmPublishExtension
get() = extensions.getByName(EXTENSION_NAME) as? NpmPublishExtension
?: throw IllegalStateException("$EXTENSION_NAME is not of the correct type")
get() =
extensions.getByName(EXTENSION_NAME) as? NpmPublishExtension
?: throw IllegalStateException("$EXTENSION_NAME is not of the correct type")

internal fun Project.npmPublishing(config: NpmPublishExtension.() -> Unit = {}): NpmPublishExtension =
npmPublishing.apply(config)
internal fun Project.npmPublishing(
config: NpmPublishExtension.() -> Unit = {}
): NpmPublishExtension = npmPublishing.apply(config)
4 changes: 2 additions & 2 deletions src/main/kotlin/delegate/ChainedProperty.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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>
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)
Expand Down
Loading

0 comments on commit 3313694

Please sign in to comment.