From 02d2971b79233d8d9fc9886a4a97a938af7b18b4 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Wed, 11 Aug 2021 13:12:41 +0300 Subject: [PATCH] Refactor: explicit API, publishing extension, code style --- build.gradle.kts | 4 +- .../mipt/npm/gradle/KScienceCommonPlugin.kt | 22 ++- .../ru/mipt/npm/gradle/KScienceExtension.kt | 66 ++++----- .../ru/mipt/npm/gradle/KScienceJSPlugin.kt | 11 +- .../ru/mipt/npm/gradle/KScienceJVMPlugin.kt | 12 +- .../ru/mipt/npm/gradle/KScienceMPPlugin.kt | 9 +- .../mipt/npm/gradle/KScienceNativePlugin.kt | 69 ++++------ .../ru/mipt/npm/gradle/KScienceNodePlugin.kt | 18 +-- .../mipt/npm/gradle/KScienceProjectPlugin.kt | 90 +++++++++---- .../npm/gradle/KScienceReadmeExtension.kt | 51 ++++--- .../ru/mipt/npm/gradle/KScienceVersions.kt | 28 ++-- .../mipt/npm/gradle/SerializationTargets.kt | 87 ++++++------ .../ru/mipt/npm/gradle/internal/common.kt | 7 +- .../mipt/npm/gradle/internal/dependencies.kt | 4 +- .../kotlin/ru/mipt/npm/gradle/internal/fx.kt | 5 +- .../ru/mipt/npm/gradle/internal/publishing.kt | 126 +++++++++--------- 16 files changed, 308 insertions(+), 301 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 97c5cfd..5c1a564 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -22,6 +22,8 @@ repositories { java.targetCompatibility = JavaVersion.VERSION_11 +kotlin.explicitApiWarning() + dependencies { api(libs.kotlin.gradle) implementation(libs.atomicfu.gradle) @@ -158,7 +160,7 @@ afterEvaluate { } if (plugins.findPlugin("signing") == null) { - plugins.apply("signing") + apply() } repositories.maven { diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceCommonPlugin.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceCommonPlugin.kt index 63d2c37..70b06c4 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceCommonPlugin.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceCommonPlugin.kt @@ -6,6 +6,7 @@ import org.gradle.api.plugins.JavaPluginExtension import org.gradle.api.tasks.Copy import org.gradle.api.tasks.testing.Test import org.gradle.kotlin.dsl.* +import org.jetbrains.dokka.gradle.DokkaPlugin import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension @@ -15,7 +16,7 @@ import ru.mipt.npm.gradle.internal.applySettings import ru.mipt.npm.gradle.internal.fromJsDependencies @Suppress("UNUSED_VARIABLE") -open class KScienceCommonPlugin : Plugin { +public open class KScienceCommonPlugin : Plugin { override fun apply(project: Project): Unit = project.run { //Common configuration registerKScienceExtension() @@ -50,10 +51,8 @@ open class KScienceCommonPlugin : Plugin { targetCompatibility = KScienceVersions.JVM_TARGET } - tasks.apply { - withType { - useJUnitPlatform() - } + tasks.withType { + useJUnitPlatform() } } @@ -112,7 +111,7 @@ open class KScienceCommonPlugin : Plugin { } } - sourceSets.invoke { + sourceSets { val commonMain by getting { dependencies { api(project.dependencies.platform("org.jetbrains.kotlin-wrappers:kotlin-wrappers-bom:${KScienceVersions.jsBom}")) @@ -128,7 +127,7 @@ open class KScienceCommonPlugin : Plugin { val jvmTest by getting { dependencies { implementation(kotlin("test-junit5")) - implementation("org.junit.jupiter:junit-jupiter:5.6.1") + implementation("org.junit.jupiter:junit-jupiter:5.7.2") } } val jsMain by getting @@ -155,18 +154,15 @@ open class KScienceCommonPlugin : Plugin { targetCompatibility = KScienceVersions.JVM_TARGET } - tasks.apply { - withType { - useJUnitPlatform() - } + tasks.withType { + useJUnitPlatform() } } } // apply dokka for all projects if (!plugins.hasPlugin("org.jetbrains.dokka")) { - plugins.apply("org.jetbrains.dokka") + apply() } - } } diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceExtension.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceExtension.kt index dee6d7d..2b71104 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceExtension.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceExtension.kt @@ -1,8 +1,12 @@ package ru.mipt.npm.gradle +import kotlinx.atomicfu.plugin.gradle.AtomicFUGradlePlugin import org.gradle.api.Project import org.gradle.api.plugins.ApplicationPlugin +import org.gradle.kotlin.dsl.apply +import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.findByType +import org.gradle.kotlin.dsl.withType import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension @@ -11,7 +15,7 @@ import ru.mipt.npm.gradle.internal.defaultPlatform import ru.mipt.npm.gradle.internal.useCommonDependency import ru.mipt.npm.gradle.internal.useFx -enum class FXModule(val artifact: String, vararg val dependencies: FXModule) { +public enum class FXModule(public val artifact: String, public vararg val dependencies: FXModule) { BASE("javafx-base"), GRAPHICS("javafx-graphics", BASE), CONTROLS("javafx-controls", GRAPHICS, BASE), @@ -21,30 +25,29 @@ enum class FXModule(val artifact: String, vararg val dependencies: FXModule) { WEB("javafx-web", CONTROLS, GRAPHICS, BASE) } -enum class FXPlatform(val id: String) { +public enum class FXPlatform(public val id: String) { WINDOWS("win"), LINUX("linux"), MAC("mac") } -enum class DependencyConfiguration { +public enum class DependencyConfiguration { API, IMPLEMENTATION, - COMPILE_ONLY + COMPILE_ONLY, } -enum class DependencySourceSet(val setName: String, val suffix: String) { +public enum class DependencySourceSet(public val setName: String, public val suffix: String) { MAIN("main", "Main"), TEST("test", "Test") } -class KScienceExtension(val project: Project) { - +public class KScienceExtension(public val project: Project) { /** * Use coroutines-core with default version or [version] */ - fun useCoroutines( + public fun useCoroutines( version: String = KScienceVersions.coroutinesVersion, sourceSet: DependencySourceSet = DependencySourceSet.MAIN, configuration: DependencyConfiguration = DependencyConfiguration.API, @@ -55,14 +58,14 @@ class KScienceExtension(val project: Project) { ) /** - * Use kotlinx-atmicfu plugin and library + * Use kotlinx-atomicfu plugin and library */ - fun useAtomic( + public fun useAtomic( version: String = KScienceVersions.atomicVersion, sourceSet: DependencySourceSet = DependencySourceSet.MAIN, configuration: DependencyConfiguration = DependencyConfiguration.IMPLEMENTATION, ): Unit = project.run { - plugins.apply("kotlinx-atomicfu") + apply() useCommonDependency( "org.jetbrains.kotlinx:atomicfu:$version", dependencySourceSet = sourceSet, @@ -73,13 +76,13 @@ class KScienceExtension(val project: Project) { /** * Use core serialization library and configure targets */ - fun useSerialization( + public fun useSerialization( version: String = KScienceVersions.serializationVersion, sourceSet: DependencySourceSet = DependencySourceSet.MAIN, configuration: DependencyConfiguration = DependencyConfiguration.API, block: SerializationTargets.() -> Unit = {}, ): Unit = project.run { - plugins.apply("org.jetbrains.kotlin.plugin.serialization") + apply("org.jetbrains.kotlin.plugin.serialization") val artifactName = if (version.startsWith("0")) { "kotlinx-serialization-runtime" } else { @@ -90,23 +93,23 @@ class KScienceExtension(val project: Project) { dependencySourceSet = sourceSet, dependencyConfiguration = configuration ) - SerializationTargets(sourceSet, configuration).apply(block) + SerializationTargets(sourceSet, configuration).block() } /** * Add platform-specific JavaFX dependencies with given list of [FXModule]s */ - fun useFx( + public fun useFx( vararg modules: FXModule, configuration: DependencyConfiguration = DependencyConfiguration.COMPILE_ONLY, version: String = "11", platform: FXPlatform = defaultPlatform, - ) = project.useFx(modules.toList(), configuration, version, platform) + ): Unit = project.useFx(modules.toList(), configuration, version, platform) /** * Add dependency on kotlinx-html library */ - fun useHtml( + public fun useHtml( version: String = KScienceVersions.htmlVersion, sourceSet: DependencySourceSet = DependencySourceSet.MAIN, configuration: DependencyConfiguration = DependencyConfiguration.API, @@ -119,35 +122,33 @@ class KScienceExtension(val project: Project) { /** * Use kotlinx-datetime library with default version or [version] */ - fun useDateTime( + public fun useDateTime( version: String = KScienceVersions.dateTimeVersion, sourceSet: DependencySourceSet = DependencySourceSet.MAIN, configuration: DependencyConfiguration = DependencyConfiguration.API, - ) { - project.useCommonDependency( - "org.jetbrains.kotlinx:kotlinx-datetime:$version", - dependencySourceSet = sourceSet, - dependencyConfiguration = configuration - ) - } + ): Unit = project.useCommonDependency( + "org.jetbrains.kotlinx:kotlinx-datetime:$version", + dependencySourceSet = sourceSet, + dependencyConfiguration = configuration + ) /** * Apply jupyter plugin */ - fun useJupyter() { - project.plugins.apply("org.jetbrains.kotlin.jupyter.api") + public fun useJupyter() { + project.apply("org.jetbrains.kotlin.jupyter.api") } /** * Mark this module as an application module. JVM application should be enabled separately */ - fun application() { + public fun application() { project.extensions.findByType()?.apply { explicitApi = null } project.pluginManager.withPlugin("org.jetbrains.kotlin.jvm") { - project.plugins.apply(ApplicationPlugin::class.java) + project.apply() } project.extensions.findByType()?.apply { @@ -160,8 +161,9 @@ class KScienceExtension(val project: Project) { js { binaries.executable() } - targets.filterIsInstance().forEach { - it.binaries.executable() + + targets.withType { + binaries.executable() } } } @@ -171,4 +173,4 @@ internal fun Project.registerKScienceExtension() { if (extensions.findByType() == null) { extensions.add("kscience", KScienceExtension(this)) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceJSPlugin.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceJSPlugin.kt index 75de0f4..b44574a 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceJSPlugin.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceJSPlugin.kt @@ -4,13 +4,14 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.apply -open class KScienceJSPlugin : Plugin { +public open class KScienceJSPlugin : Plugin { override fun apply(project: Project): Unit = project.run { - if (plugins.findPlugin("org.jetbrains.kotlin.js") == null) { - pluginManager.apply("org.jetbrains.kotlin.js") + if (!plugins.hasPlugin("org.jetbrains.kotlin.js")) { + apply("org.jetbrains.kotlin.js") } else { logger.info("Kotlin JS plugin is already present") } - plugins.apply(KScienceCommonPlugin::class) + + apply() } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceJVMPlugin.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceJVMPlugin.kt index f496817..fcfccf9 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceJVMPlugin.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceJVMPlugin.kt @@ -4,13 +4,13 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.apply -open class KScienceJVMPlugin : Plugin { +public open class KScienceJVMPlugin : Plugin { override fun apply(project: Project): Unit = project.run { - if (plugins.findPlugin("org.jetbrains.kotlin.jvm") == null) { - pluginManager.apply("org.jetbrains.kotlin.jvm") - } else { + if (!plugins.hasPlugin("org.jetbrains.kotlin.jvm")) + apply("org.jetbrains.kotlin.jvm") + else logger.info("Kotlin JVM plugin is already present") - } - plugins.apply(KScienceCommonPlugin::class) + + apply() } } diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceMPPlugin.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceMPPlugin.kt index 167d9b9..c4a39f1 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceMPPlugin.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceMPPlugin.kt @@ -4,13 +4,14 @@ import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.apply -open class KScienceMPPlugin : Plugin { +public open class KScienceMPPlugin : Plugin { override fun apply(project: Project): Unit = project.run { - if (plugins.findPlugin("org.jetbrains.kotlin.multiplatform") == null) { - pluginManager.apply("org.jetbrains.kotlin.multiplatform") + if (!plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) { + apply("org.jetbrains.kotlin.multiplatform") } else { logger.info("Kotlin MPP plugin is already present") } - plugins.apply(KScienceCommonPlugin::class) + + apply() } } diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceNativePlugin.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceNativePlugin.kt index 6080de4..12ce3c8 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceNativePlugin.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceNativePlugin.kt @@ -1,68 +1,57 @@ package ru.mipt.npm.gradle -import org.gradle.api.Action -import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.kotlin.dsl.apply -import org.gradle.kotlin.dsl.configure -import org.gradle.kotlin.dsl.findPlugin +import org.gradle.kotlin.dsl.* import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension -import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet -private fun KotlinMultiplatformExtension.sourceSets(configure: Action>): Unit = - (this as org.gradle.api.plugins.ExtensionAware).extensions.configure("sourceSets", configure) - -class KScienceNativePlugin : Plugin { - override fun apply(project: Project) = project.run { +public class KScienceNativePlugin : Plugin { + override fun apply(project: Project): Unit = project.run { //Apply multiplatform plugin is not applied, apply it - if (plugins.findPlugin("org.jetbrains.kotlin.multiplatform") == null) { + if (!plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) { logger.info("Kotlin multiplatform plugin is not resolved. Adding it automatically") - pluginManager.apply("org.jetbrains.kotlin.multiplatform") + apply("org.jetbrains.kotlin.multiplatform") } - if (plugins.findPlugin(KScienceCommonPlugin::class) == null) { + + if (!plugins.hasPlugin(KScienceCommonPlugin::class)) { logger.info("KScience plugin is not resolved. Adding it automatically") - pluginManager.apply(KScienceCommonPlugin::class) + apply() } configure { - val hostOs = System.getProperty("os.name") - - val isLinux = hostOs == "Linux" - val isMinGw = hostOs.startsWith("Windows") - val isMacOs = hostOs == "Mac OS X" - - if (isLinux || isMinGw) { - linuxX64() - } - if (isMinGw) { - mingwX64() - } - if (isMacOs) { - macosX64() - } + val nativeTargets = setOf( + linuxX64(), + mingwX64(), + macosX64(), + ) sourceSets { val commonMain = findByName("commonMain")!! val commonTest = findByName("commonTest")!! - val nativeMain = create("nativeMain").apply { + val nativeMain by creating { dependsOn(commonMain) } - val nativeTest = create("nativeTest").apply { + val nativeTest by creating { + dependsOn(nativeMain) dependsOn(commonTest) } - findByName("linuxX64Main")?.dependsOn(nativeMain) - findByName("linuxX64Test")?.dependsOn(nativeTest) + configure(nativeTargets) { + compilations["main"]?.apply { + configure(kotlinSourceSets) { + dependsOn(nativeMain) + } + } - findByName("mingwX64Main")?.dependsOn(nativeMain) - findByName("mingwX64Test")?.dependsOn(nativeTest) - - findByName("macosX64Main")?.dependsOn(nativeMain) - findByName("macosX64Test")?.dependsOn(nativeTest) + compilations["test"]?.apply { + configure(kotlinSourceSets) { + dependsOn(nativeTest) + } + } + } } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceNodePlugin.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceNodePlugin.kt index b321012..0e3e0cc 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceNodePlugin.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceNodePlugin.kt @@ -1,31 +1,27 @@ package ru.mipt.npm.gradle -import org.gradle.api.Action -import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.findPlugin +import org.gradle.kotlin.dsl.invoke import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension -import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet - -private fun KotlinMultiplatformExtension.sourceSets(configure: Action>): Unit = - (this as org.gradle.api.plugins.ExtensionAware).extensions.configure("sourceSets", configure) /** * Create a separate target for node */ -class KScienceNodePlugin : Plugin { - override fun apply(target: Project) = target.run { +public class KScienceNodePlugin : Plugin { + override fun apply(target: Project): Unit = target.run { //Apply multiplatform plugin is not applied, apply it if (plugins.findPlugin("org.jetbrains.kotlin.multiplatform") == null) { logger.info("Kotlin multiplatform plugin is not resolved. Adding it automatically") - pluginManager.apply("org.jetbrains.kotlin.multiplatform") + apply("org.jetbrains.kotlin.multiplatform") } + if (plugins.findPlugin(KScienceCommonPlugin::class) == null) { logger.info("KScience plugin is not resolved. Adding it automatically") - pluginManager.apply(KScienceCommonPlugin::class) + apply() } configure { @@ -58,4 +54,4 @@ class KScienceNodePlugin : Plugin { } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceProjectPlugin.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceProjectPlugin.kt index 70568f3..23bde44 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceProjectPlugin.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceProjectPlugin.kt @@ -17,13 +17,41 @@ import ru.mipt.npm.gradle.internal.* private fun Project.allTasks(): Set = allprojects.flatMapTo(HashSet()) { it.tasks } @Suppress("unused") -class KSciencePublishingExtension(val project: Project) { - private var initializedFlag = false +public class KSciencePublishingExtension(public val project: Project) { + private var isVcsInitialized = false - fun vcs(vcsUrl: String) { - if (!initializedFlag) { - project.setupPublication(vcsUrl) - initializedFlag = true + @Deprecated("Use git function and report an issue if other VCS is used.") + public fun vcs(vcsUrl: String) { + if (!isVcsInitialized) { + project.setupPublication { + url.set(vcsUrl) + scm { url.set(vcsUrl) } + } + + isVcsInitialized = true + } + } + + /** + * Configures Git repository for the publication. + * + * @param vcsUrl URL of the repository's web interface. + * @param connectionUrl URL of the Git repository. + * @param developerConnectionUrl URL of the Git repository for developers. + */ + public fun git(vcsUrl: String, connectionUrl: String? = null, developerConnectionUrl: String? = connectionUrl) { + if (!isVcsInitialized) { + project.setupPublication { + url.set(vcsUrl) + + scm { + url.set(vcsUrl) + connectionUrl?.let { connection.set("scm:git:$it") } + developerConnectionUrl?.let { developerConnection.set("scm:git:$it") } + } + } + + isVcsInitialized = true } } @@ -34,17 +62,19 @@ class KSciencePublishingExtension(val project: Project) { } /** - * github publishing - * @param publish include github packages in release publishing. By default - false + * Adds GitHub as VCS and adds GitHub Packages Maven repository to publishing. + * + * @param githubProject the GitHub project. + * @param githubOrg the GitHub user or organization. + * @param released whether publish packages in the `release` task to the GitHub repository. */ - fun github(githubProject: String, githubOrg: String = "mipt-npm", publish: Boolean = false) { - //automatically initialize vcs using github - if (!initializedFlag) { - vcs("https://github.com/$githubOrg/$githubProject") - } + public fun github(githubProject: String, githubOrg: String = "mipt-npm", released: Boolean = false, published: Boolean = true) { + // Automatically initialize VCS using GitHub + if (!isVcsInitialized) + git("https://github.com/$githubOrg/${githubProject}", "https://github.com/$githubOrg/${githubProject}.git") - project.addGithubPublishing(githubOrg, githubProject) - if (publish) linkPublicationsToReleaseTask("github") + if (published) project.addGithubPublishing(githubOrg, githubProject) + if (released) linkPublicationsToReleaseTask("github") } private val releaseTask by lazy { @@ -52,13 +82,16 @@ class KSciencePublishingExtension(val project: Project) { } /** - * Space publishing + * Adds Space Packages Maven repository to publishing. + * + * @param spaceRepo the repository URL. + * @param released whether publish packages in the `release` task to the Space repository. */ - fun space(spaceRepo: String = "https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven", publish: Boolean = false) { - require(initializedFlag) { "The project vcs is not set up use 'vcs' method to do so" } + public fun space(spaceRepo: String = "https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven", released: Boolean = false) { + require(isVcsInitialized) { "The project vcs is not set up use 'vcs' method to do so" } project.addSpacePublishing(spaceRepo) - if (publish) linkPublicationsToReleaseTask("space") + if (released) linkPublicationsToReleaseTask("space") } // // Bintray publishing @@ -68,20 +101,23 @@ class KSciencePublishingExtension(val project: Project) { // var bintrayRepo: String? by project.extra /** - * Sonatype publishing + * Adds Sonatype Maven repository to publishing. + * + * @param released whether publish packages in the `release` task to the Sonatype repository. */ - fun sonatype(publish: Boolean = true) { - require(initializedFlag) { "The project vcs is not set up use 'vcs' method to do so" } + public fun sonatype(released: Boolean = true) { + require(isVcsInitialized) { "The project vcs is not set up use 'vcs' method to do so" } project.addSonatypePublishing() - if (publish) linkPublicationsToReleaseTask("sonatype") + if (released) linkPublicationsToReleaseTask("sonatype") } } /** - * Apply extension and repositories + * Applies third-party plugins (Dokka, Changelog, binary compatibility validator); configures Maven publishing, README + * generation. */ -open class KScienceProjectPlugin : Plugin { +public open class KScienceProjectPlugin : Plugin { override fun apply(target: Project): Unit = target.run { apply() @@ -219,7 +255,7 @@ open class KScienceProjectPlugin : Plugin { } } - companion object { - const val RELEASE_GROUP = "release" + public companion object { + public const val RELEASE_GROUP: String = "release" } } diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceReadmeExtension.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceReadmeExtension.kt index 874a166..4edb9ca 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceReadmeExtension.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceReadmeExtension.kt @@ -3,10 +3,10 @@ package ru.mipt.npm.gradle import groovy.text.SimpleTemplateEngine import kotlinx.validation.ApiValidationExtension import org.gradle.api.Project -import org.gradle.kotlin.dsl.getByType +import org.gradle.kotlin.dsl.* import java.io.File -enum class Maturity { +public enum class Maturity { PROTOTYPE, EXPERIMENTAL, DEVELOPMENT, @@ -14,16 +14,17 @@ enum class Maturity { } -class KScienceReadmeExtension(val project: Project) { - var description: String = project.description ?: "" - var maturity: Maturity = Maturity.EXPERIMENTAL +public class KScienceReadmeExtension(public val project: Project) { + public var description: String = project.description ?: "" + + public var maturity: Maturity = Maturity.EXPERIMENTAL set(value) { field = value val projectName = project.name if (value == Maturity.EXPERIMENTAL || value == Maturity.PROTOTYPE) { project.rootProject.run { plugins.withId("org.jetbrains.kotlinx.binary-compatibility-validator") { - extensions.getByType().apply { + extensions.findByType()?.apply { project.logger.warn("$value project $projectName is excluded from API validation") ignoredProjects.add(projectName) } @@ -32,18 +33,18 @@ class KScienceReadmeExtension(val project: Project) { } } - var readmeTemplate: File = project.file("docs/README-TEMPLATE.md") + public var readmeTemplate: File = project.file("docs/README-TEMPLATE.md") - data class Feature(val id: String, val description: String, val ref: String?, val name: String = id) + public data class Feature(val id: String, val description: String, val ref: String?, val name: String = id) - val features: MutableList = ArrayList() + public val features: MutableList = ArrayList() - @Deprecated("Use lambda builder instead") - fun feature(id: String, description: String, ref: String? = null, name: String = id) { + @Deprecated("Use lambda builder instead.") + public fun feature(id: String, description: String, ref: String? = null, name: String = id) { features += Feature(id, description, ref, name) } - fun feature(id: String, ref: String? = null, name: String = id, description: () -> String) { + public fun feature(id: String, ref: String? = null, name: String = id, description: () -> String) { features += Feature(id, description(), ref, name) } @@ -54,25 +55,25 @@ class KScienceReadmeExtension(val project: Project) { "features" to { featuresString() } ) - val actualizedProperties + public val actualizedProperties: Map get() = properties.mapValues { (_, value) -> value() } - fun property(key: String, value: Any?) { + public fun property(key: String, value: Any?) { properties[key] = { value } } - fun property(key: String, value: () -> Any?) { + public fun property(key: String, value: () -> Any?) { properties[key] = value } - fun propertyByTemplate(key: String, template: String) { + public fun propertyByTemplate(key: String, template: String) { val actual = actualizedProperties properties[key] = { SimpleTemplateEngine().createTemplate(template).make(actual).toString() } } internal val additionalFiles = ArrayList() - fun propertyByTemplate(key: String, template: File) { + public fun propertyByTemplate(key: String, template: File) { val actual = actualizedProperties properties[key] = { SimpleTemplateEngine().createTemplate(template).make(actual).toString() } additionalFiles += template @@ -81,7 +82,7 @@ class KScienceReadmeExtension(val project: Project) { /** * Generate a markdown string listing features */ - fun featuresString(itemPrefix: String = " - ", pathPrefix: String = "") = buildString { + public fun featuresString(itemPrefix: String = " - ", pathPrefix: String = ""): String = buildString { features.forEach { appendLine("$itemPrefix[${it.name}]($pathPrefix${it.ref ?: "#"}) : ${it.description}") } @@ -90,12 +91,10 @@ class KScienceReadmeExtension(val project: Project) { /** * Generate a readme string from the stub */ - fun readmeString(): String? { - return if (readmeTemplate.exists()) { - val actual = actualizedProperties - SimpleTemplateEngine().createTemplate(readmeTemplate).make(actual).toString() - } else { - null - } + public fun readmeString(): String? = if (readmeTemplate.exists()) { + val actual = actualizedProperties + SimpleTemplateEngine().createTemplate(readmeTemplate).make(actual).toString() + } else { + null } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/mipt/npm/gradle/KScienceVersions.kt b/src/main/kotlin/ru/mipt/npm/gradle/KScienceVersions.kt index b8acdb4..84002fe 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/KScienceVersions.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/KScienceVersions.kt @@ -5,21 +5,21 @@ import org.gradle.api.JavaVersion /** * Build constants */ -object KScienceVersions { - const val kotlinVersion = "1.5.21" - const val kotlinxNodeVersion = "0.0.7" - const val coroutinesVersion = "1.5.1" - const val serializationVersion = "1.2.2" - const val atomicVersion = "0.16.2" - const val ktorVersion = "1.6.1" - const val htmlVersion = "0.7.3" - const val dateTimeVersion = "0.2.1" - const val jsBom = "0.0.1-pre.216-kotlin-1.5.20" +public object KScienceVersions { + public const val kotlinVersion: String = "1.5.21" + public const val kotlinxNodeVersion: String = "0.0.7" + public const val coroutinesVersion: String = "1.5.1" + public const val serializationVersion: String = "1.2.2" + public const val atomicVersion: String = "0.16.2" + public const val ktorVersion: String = "1.6.1" + public const val htmlVersion: String = "0.7.3" + public const val dateTimeVersion: String = "0.2.1" + public const val jsBom: String = "0.0.1-pre.216-kotlin-1.5.20" - val JVM_TARGET = JavaVersion.VERSION_11 + public val JVM_TARGET: JavaVersion = JavaVersion.VERSION_11 - object Serialization{ - const val xmlVersion = "0.82.0" - const val yamlKtVersion = "0.10.0" + public object Serialization { + public const val xmlVersion: String = "0.82.0" + public const val yamlKtVersion: String = "0.10.0" } } diff --git a/src/main/kotlin/ru/mipt/npm/gradle/SerializationTargets.kt b/src/main/kotlin/ru/mipt/npm/gradle/SerializationTargets.kt index 62873c0..1ef2351 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/SerializationTargets.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/SerializationTargets.kt @@ -3,58 +3,47 @@ package ru.mipt.npm.gradle import org.gradle.api.Project import ru.mipt.npm.gradle.internal.useCommonDependency -class SerializationTargets( - val sourceSet: DependencySourceSet, - val configuration: DependencyConfiguration +public class SerializationTargets( + public val sourceSet: DependencySourceSet, + public val configuration: DependencyConfiguration, ) { + public fun Project.json( + version: String = KScienceVersions.serializationVersion, + ): Unit = useCommonDependency( + "org.jetbrains.kotlinx:kotlinx-serialization-json:$version", + dependencySourceSet = sourceSet, + dependencyConfiguration = configuration, + ) - fun Project.json( - version: String = KScienceVersions.serializationVersion - ) { - useCommonDependency( - "org.jetbrains.kotlinx:kotlinx-serialization-json:$version", - dependencySourceSet = sourceSet, - dependencyConfiguration = configuration - ) - } + public fun Project.cbor( + version: String = KScienceVersions.serializationVersion, + ): Unit = useCommonDependency( + "org.jetbrains.kotlinx:kotlinx-serialization-cbor:$version", + dependencySourceSet = sourceSet, + dependencyConfiguration = configuration, + ) - fun Project.cbor( - version: String = KScienceVersions.serializationVersion - ) { - useCommonDependency( - "org.jetbrains.kotlinx:kotlinx-serialization-cbor:$version", - dependencySourceSet = sourceSet, - dependencyConfiguration = configuration - ) - } + public fun Project.protobuf( + version: String = KScienceVersions.serializationVersion, + ): Unit = useCommonDependency( + "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:$version", + dependencySourceSet = sourceSet, + dependencyConfiguration = configuration, + ) - fun Project.protobuf( - version: String = KScienceVersions.serializationVersion - ) { - useCommonDependency( - "org.jetbrains.kotlinx:kotlinx-serialization-protobuf:$version", - dependencySourceSet = sourceSet, - dependencyConfiguration = configuration - ) - } + public fun Project.xml( + version: String = KScienceVersions.Serialization.xmlVersion, + ): Unit = useCommonDependency( + "io.github.pdvrieze.xmlutil:serialization:$version", + dependencySourceSet = sourceSet, + dependencyConfiguration = configuration, + ) - fun Project.xml( - version: String = KScienceVersions.Serialization.xmlVersion - ) { - useCommonDependency( - "io.github.pdvrieze.xmlutil:serialization:$version", - dependencySourceSet = sourceSet, - dependencyConfiguration = configuration - ) - } - - fun Project.yamlKt( - version: String = KScienceVersions.Serialization.yamlKtVersion - ) { - useCommonDependency( - "net.mamoe.yamlkt:yamlkt:$version", - dependencySourceSet = sourceSet, - dependencyConfiguration = configuration - ) - } + public fun Project.yamlKt( + version: String = KScienceVersions.Serialization.yamlKtVersion, + ): Unit = useCommonDependency( + "net.mamoe.yamlkt:yamlkt:$version", + dependencySourceSet = sourceSet, + dependencyConfiguration = configuration, + ) } diff --git a/src/main/kotlin/ru/mipt/npm/gradle/internal/common.kt b/src/main/kotlin/ru/mipt/npm/gradle/internal/common.kt index 07b01e8..f4d8096 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/internal/common.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/internal/common.kt @@ -11,7 +11,7 @@ import org.gradle.language.jvm.tasks.ProcessResources import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension import org.jetbrains.kotlin.gradle.plugin.LanguageSettingsBuilder -internal fun LanguageSettingsBuilder.applySettings(): Unit { +internal fun LanguageSettingsBuilder.applySettings() { languageVersion = "1.5" apiVersion = "1.5" progressiveMode = true @@ -23,7 +23,7 @@ internal fun LanguageSettingsBuilder.applySettings(): Unit { useExperimentalAnnotation("kotlin.js.ExperimentalJsExport") } -internal fun RepositoryHandler.applyRepos(): Unit { +internal fun RepositoryHandler.applyRepos() { mavenCentral() maven("https://repo.kotlin.link") } @@ -50,8 +50,7 @@ internal fun Copy.fromJsDependencies(configurationName: String) = project.run { } } - -fun KotlinMultiplatformExtension.bundleJsBinaryAsResource(bundleName: String = "js/bundle.js"){ +internal fun KotlinMultiplatformExtension.bundleJsBinaryAsResource(bundleName: String = "js/bundle.js"){ js { binaries.executable() browser { diff --git a/src/main/kotlin/ru/mipt/npm/gradle/internal/dependencies.kt b/src/main/kotlin/ru/mipt/npm/gradle/internal/dependencies.kt index eaf260b..9280846 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/internal/dependencies.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/internal/dependencies.kt @@ -87,7 +87,6 @@ internal fun Project.useCommonDependency( } } - withPlugin("org.jetbrains.kotlin.jvm") { configure { sourceSets.findByName(dependencySourceSet.setName)?.apply { @@ -102,6 +101,7 @@ internal fun Project.useCommonDependency( } } } + withPlugin("org.jetbrains.kotlin.js") { configure { sourceSets.findByName(dependencySourceSet.setName)?.apply { @@ -116,4 +116,4 @@ internal fun Project.useCommonDependency( } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/mipt/npm/gradle/internal/fx.kt b/src/main/kotlin/ru/mipt/npm/gradle/internal/fx.kt index 83c07df..3420cb3 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/internal/fx.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/internal/fx.kt @@ -2,6 +2,7 @@ package ru.mipt.npm.gradle.internal import org.apache.tools.ant.taskdefs.condition.Os import org.gradle.api.Project +import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.findByType import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension @@ -10,7 +11,7 @@ import ru.mipt.npm.gradle.DependencyConfiguration import ru.mipt.npm.gradle.FXModule import ru.mipt.npm.gradle.FXPlatform -val defaultPlatform: FXPlatform = when { +internal val defaultPlatform: FXPlatform = when { Os.isFamily(Os.FAMILY_WINDOWS) -> FXPlatform.WINDOWS Os.isFamily(Os.FAMILY_MAC) -> FXPlatform.MAC Os.isFamily(Os.FAMILY_UNIX) -> FXPlatform.LINUX @@ -58,4 +59,4 @@ internal fun Project.useFx( } } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/ru/mipt/npm/gradle/internal/publishing.kt b/src/main/kotlin/ru/mipt/npm/gradle/internal/publishing.kt index 94eccc8..7185bec 100644 --- a/src/main/kotlin/ru/mipt/npm/gradle/internal/publishing.kt +++ b/src/main/kotlin/ru/mipt/npm/gradle/internal/publishing.kt @@ -2,10 +2,12 @@ package ru.mipt.npm.gradle.internal import org.gradle.api.Project import org.gradle.api.publish.PublishingExtension +import org.gradle.api.publish.maven.MavenPom import org.gradle.api.publish.maven.MavenPublication import org.gradle.api.tasks.bundling.Jar import org.gradle.kotlin.dsl.* import org.gradle.plugins.signing.SigningExtension +import org.gradle.plugins.signing.SigningPlugin import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension @@ -16,27 +18,27 @@ private fun Project.requestProperty(propertyName: String): String = requestPrope ?: error("Property $propertyName not defined") -internal fun Project.setupPublication(vcs: String) = allprojects { +internal fun Project.setupPublication(mavenPomConfiguration: MavenPom.() -> Unit = {}) = allprojects { plugins.withId("maven-publish") { configure { plugins.withId("org.jetbrains.kotlin.js") { val kotlin: KotlinJsProjectExtension = extensions.findByType()!! - val sourcesJar: Jar by tasks.creating(Jar::class) { + val sourcesJar by tasks.creating(Jar::class) { archiveClassifier.set("sources") - kotlin.sourceSets.forEach{ - from(it.kotlin) + + kotlin.sourceSets.all { + from(kotlin) } } afterEvaluate { - publications { - create("js", MavenPublication::class) { - kotlin.js().components.forEach { - from(it) - } - artifact(sourcesJar) + publications.create("js") { + kotlin.js().components.forEach { + from(it) } + + artifact(sourcesJar) } } } @@ -44,24 +46,23 @@ internal fun Project.setupPublication(vcs: String) = allprojects { plugins.withId("org.jetbrains.kotlin.jvm") { val kotlin = extensions.findByType()!! - val sourcesJar: Jar by tasks.creating(Jar::class) { + val sourcesJar by tasks.creating(Jar::class) { archiveClassifier.set("sources") - kotlin.sourceSets.forEach{ + kotlin.sourceSets.forEach { from(it.kotlin) } } - publications { - create("jvm", MavenPublication::class) { - kotlin.target.components.forEach { - from(it) - } - artifact(sourcesJar) + publications.create("jvm") { + kotlin.target.components.forEach { + from(it) } + + artifact(sourcesJar) } } - val dokkaJar: Jar by tasks.creating(Jar::class) { + val dokkaJar by tasks.creating(Jar::class) { group = "documentation" archiveClassifier.set("javadoc") from(tasks.findByName("dokkaHtml")) @@ -69,35 +70,35 @@ internal fun Project.setupPublication(vcs: String) = allprojects { // Process each publication we have in this project afterEvaluate { - publications.withType().forEach { publication -> - publication.artifact(dokkaJar) - publication.pom { + publications.withType { + artifact(dokkaJar) + + pom { name.set(project.name) description.set(project.description ?: project.name) - url.set(vcs) licenses { license { name.set("The Apache Software License, Version 2.0") - url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") + url.set("https://www.apache.org/licenses/LICENSE-2.0.txt") distribution.set("repo") } } + developers { developer { id.set("MIPT-NPM") name.set("MIPT nuclear physics methods laboratory") organization.set("MIPT") - organizationUrl.set("http://npm.mipt.ru") + organizationUrl.set("https://npm.mipt.ru") } - } + scm { - url.set(vcs) tag.set(project.version.toString()) - //developerConnection = "scm:git:[fetch=]/*ВАША ССЫЛКА НА .git файл*/[push=]/*Повторить предыдущую ссылку*/" } + mavenPomConfiguration() } } } @@ -105,7 +106,7 @@ internal fun Project.setupPublication(vcs: String) = allprojects { } } -internal fun Project.isSnapshot() = version.toString().contains("dev") || version.toString().endsWith("SNAPSHOT") +internal fun Project.isSnapshot() = "dev" in version.toString() || version.toString().endsWith("SNAPSHOT") internal val Project.publicationTarget: String get() { @@ -137,14 +138,14 @@ internal fun Project.addGithubPublishing( plugins.withId("maven-publish") { configure { logger.info("Adding github publishing to project [${project.name}]") - repositories { - maven { - name = "github" - url = uri("https://maven.pkg.github.com/$githubOrg/$githubProject/") - credentials { - username = githubUser - password = githubToken - } + + repositories.maven { + name = "github" + url = uri("https://maven.pkg.github.com/$githubOrg/$githubProject/") + + credentials { + username = githubUser + password = githubToken } } } @@ -157,6 +158,7 @@ internal fun Project.addSpacePublishing(spaceRepo: String) { logger.info("Skipping github publishing because publishing is disabled") return } + if (requestPropertyOrNull("publishing.space") == "false") { logger.info("Skipping space publishing because `publishing.space == false`") return @@ -169,15 +171,14 @@ internal fun Project.addSpacePublishing(spaceRepo: String) { plugins.withId("maven-publish") { configure { project.logger.info("Adding mipt-npm Space publishing to project [${project.name}]") - repositories { - maven { - name = "space" - url = uri(spaceRepo) - credentials { - username = spaceUser - password = spaceToken - } + repositories.maven { + name = "space" + url = uri(spaceRepo) + + credentials { + username = spaceUser + password = spaceToken } } } @@ -186,14 +187,16 @@ internal fun Project.addSpacePublishing(spaceRepo: String) { } internal fun Project.addSonatypePublishing() { - if(requestPropertyOrNull("publishing.enabled")!="true"){ + if (requestPropertyOrNull("publishing.enabled") != "true") { logger.info("Skipping github publishing because publishing is disabled") return } + if (isSnapshot()) { logger.info("Sonatype publishing skipped for dev version") return } + if (requestPropertyOrNull("publishing.sonatype") == "false") { logger.info("Skipping sonatype publishing because `publishing.sonatype == false`") return @@ -206,40 +209,33 @@ internal fun Project.addSonatypePublishing() { allprojects { plugins.withId("maven-publish") { configure { - val sonatypeRepo: String = "https://oss.sonatype.org/service/local/staging/deploy/maven2" + val sonatypeRepo = "https://oss.sonatype.org/service/local/staging/deploy/maven2" - if (plugins.findPlugin("signing") == null) { - plugins.apply("signing") + if (!plugins.hasPlugin("signing") ) { + apply() } + extensions.configure("signing") { if (!signingId.isNullOrBlank()) { val signingKey: String = requestProperty("publishing.signing.key") val signingPassphrase: String = requestProperty("publishing.signing.passPhrase") - //if key is provided, use it - @Suppress("UnstableApiUsage") + // if key is provided, use it useInMemoryPgpKeys(signingId, signingKey, signingPassphrase) } // else use file signing sign(publications) } - repositories { - maven { - name = "sonatype" - url = uri(sonatypeRepo) - credentials { - username = sonatypeUser - password = sonatypePassword - } + repositories.maven { + name = "sonatype" + url = uri(sonatypeRepo) + + credentials { + username = sonatypeUser + password = sonatypePassword } } } } } } - -//internal val Project.bintrayPublish: Boolean -// get() = (findProperty("publishing.bintray.publish") as? String)?.toBoolean() ?: false -//internal val Project.bintrayOrg: String? get() = findProperty("publishing.bintray.org") as? String -//internal val Project.bintrayUser: String? get() = findProperty("publishing.bintray.user") as? String -//internal val Project.bintrayApiKey: String? get() = findProperty("publishing.bintray.apiKey") as? String