diff --git a/src/main/kotlin/NpmPublishPlugin.kt b/src/main/kotlin/NpmPublishPlugin.kt index 506c9895..ba3d830e 100644 --- a/src/main/kotlin/NpmPublishPlugin.kt +++ b/src/main/kotlin/NpmPublishPlugin.kt @@ -1,12 +1,15 @@ package dev.petuska.npm.publish +import dev.petuska.npm.publish.dsl.NpmPublication import dev.petuska.npm.publish.dsl.NpmPublishExtension import dev.petuska.npm.publish.dsl.NpmPublishExtension.Companion.EXTENSION_NAME +import dev.petuska.npm.publish.dsl.NpmRepository import dev.petuska.npm.publish.task.NpmPackTask import dev.petuska.npm.publish.task.NpmPackageAssembleTask import dev.petuska.npm.publish.task.NpmPublishTask import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.Task import org.gradle.api.tasks.Copy import org.gradle.util.internal.GUtil import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension @@ -17,6 +20,7 @@ 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 */ +@Suppress("unused") class NpmPublishPlugin : Plugin { override fun apply(project: Project) { val extension = project.createExtension() @@ -76,102 +80,110 @@ class NpmPublishPlugin : Plugin { } 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 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 publications = configurePublications(extension, nodeJsSetupTask) + val repositories = configureRepositories(extension) + val pubTasks = createPublishTasks(publications, repositories, assembleTask, packTask, publishTask) + if (pubTasks.isNotEmpty()) { + publishTask.enabled = true + } + } + + private fun Project.configurePublications( + extension: NpmPublishExtension, + nodeJsSetupTask: NodeJsSetupTask? + ) = 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...") - } + private fun Project.configureRepositories(extension: NpmPublishExtension) = with(extension) { + repoConfigs.forEach { repositories.configure(it) } + repositories.mapNotNull { repo -> + repo.validate() + ?: null.also { + logger.warn("NPM Repository [${repo.name}] is invalid. Skipping...") } - } + } + } - val pubTasks = - publications.flatMap { (pub, nodeJsTask) -> + @Suppress("UNCHECKED_CAST") + private fun Project.createPublishTasks( + publications: List>, + repositories: List, + assembleTask: Task?, + packTask: Task, + publishTask: Task + ): List = publications.flatMap { (pub, nodeJsTask) -> + val processResourcesTask = + pub.binary?.compilation?.let { 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 + project.tasks.findByName(it.processResourcesTaskName) as Copy + pub.kotlinDestinationDir?.let { kotlinDestinationDir -> + pub.files { + from(kotlinDestinationDir) + from(processResourcesTask.destinationDir) } - val upperName = GUtil.toCamelCase(pub.name) + } + 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) + 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 } - } + } as List } }