Skip to content

Commit

Permalink
Merge branch 'main' into common-gradle-dependencies-release
Browse files Browse the repository at this point in the history
common-gradle-dependencies v0.7.1-20231111 release
  • Loading branch information
ShreckYe committed Jan 30, 2024
2 parents d28c4aa + eca744e commit f2ca225
Show file tree
Hide file tree
Showing 24 changed files with 478 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ configurations.all {
## Developer notices

1. IntelliJ IDEA doesn't work well with applying plugins to script plugins in project sources. If a script plugin's code does not resolve, try restarting IntelliJ IDEA.
1. `./gradlew build` (and tasks depending on it) somehow has to run twice to work. I haven't identified the cause yet.
7 changes: 6 additions & 1 deletion architecture-common-gradle-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ dependencies {
/* This project depends on a specific version of the Maven dependency of "common-gradle-dependencies"
since now they are developed together in the same branch `main`,
enabling it to always depend on a release version. */
implementation("com.huanshankeji:common-gradle-dependencies:$pluginProjectDependentStableCommonGradleDependenciesVersion")

implementation(commonGradleClasspathDependencies.composeMultiplatform.gradlePlugin.pluginProject())
}
Expand Down Expand Up @@ -70,5 +69,11 @@ gradlePlugin {
description = "Generate webroot from a Kotlin/JS subproject with browser target for Vert.x Web"
}
}

scriptConventionsPlugin(
"jvm.native.osandarch.register-default-supported-feature-variants",
"Register the OS and architecture feature variants",
"Registers feature variants for different operating systems (Linux, Windows, macOS) and CPU architectures."
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.huanshankeji.jvm.native.osandarch


object DefaultSupported {
object ArchsByOs {
val linux = listOf(CpuArchitecture.X8664, CpuArchitecture.Aarch64)
val windows = listOf(CpuArchitecture.X8664)
val macos = listOf(CpuArchitecture.X8664, CpuArchitecture.Aarch64)
}

object OsAndArchs {
val linux = ArchsByOs.linux.map { OsAndArch(Os.Linux, it) }
val windows = ArchsByOs.windows.map { OsAndArch(Os.Windows, it) }
val macos = ArchsByOs.macos.map { OsAndArch(Os.Macos, it) }

val all = listOf(linux, windows, macos).flatten()
val allFeatureVariantNames = all.map { it.featureVariantName }

// TODO use `entries` when the language version is bumped to 1.9
val futureAll = Os.values().flatMap { os ->
CpuArchitecture.values().map { arch ->
OsAndArch(os, arch)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.huanshankeji.jvm.native.osandarch

// not implemented yet
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.huanshankeji.jvm.native.osandarch

import com.huanshankeji.*
import com.huanshankeji.SourceSetType.Main
import com.huanshankeji.SourceSetType.RegisterSeparate
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.kotlin.dsl.DependencyHandlerScope
import org.gradle.kotlin.dsl.accessors.runtime.addExternalModuleDependencyTo
import org.gradle.kotlin.dsl.get

val OsAndArch.featureVariantName get() = camelCaseIdentifier


fun JavaPluginExtension.registerDefaultSupportedFeatureVariants(sourceSetType: SourceSetType) {
when (sourceSetType) {
Main -> {
val mainSourceSet = sourceSets["main"]
for (osAndArch in DefaultSupported.OsAndArchs.all)
registerFeatureVariantWithSourceSet(osAndArch.featureVariantName, mainSourceSet)
}

RegisterSeparate ->
for (osAndArch in DefaultSupported.OsAndArchs.all)
registerFeatureVariantWithNewSourceSet(osAndArch.featureVariantName)
}
}


/**
* @param identifier usually in kebab case (sometimes mixed with snake case, for example "X86_64")
*/
data class FeatureVariantDependencyConfig(val osAndArch: OsAndArch, val identifier: String)

fun DependencyHandlerScope.addDependencyToFeatureVariants(
osAndArchs: List<OsAndArch>, targetConfigurationType: String, dependencyNotation: Any
) =
addDependencyToFeatureVariants(
osAndArchs.map { it.featureVariantName }, targetConfigurationType, dependencyNotation
)


// TODO some functions related to feature variants can be extracted to a separate feature variant package

/**
* @param osAndArchs use a predefined one in [DefaultSupported.OsAndArchs] or make your own with [FeatureVariantDependencyConfig]
* @param targetConfigurationType the type of the dependency configuration
* (see https://docs.gradle.org/current/userguide/declaring_dependencies.html#sec:what-are-dependency-configurations
* and https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph)
*/
fun DependencyHandlerScope.addDependenciesToFeatureVariantsWithIdentifiersInNameSuffixes(
osAndArchs: List<FeatureVariantDependencyConfig>,
targetConfigurationType: String,
group: String, namePrefix: String, version: String? = null
) {
for ((osAndArch, dependencyIdentifier) in osAndArchs)
addExternalModuleDependencyTo(
this,
osAndArch.featureVariantName camelCaseConcat targetConfigurationType,
group, "$namePrefix-$dependencyIdentifier", version,
null, null, null, null
)
}

/** @see addDependenciesToFeatureVariantsWithIdentifiersInNameSuffixes */
fun DependencyHandlerScope.addDependenciesToFeatureVariantsWithIdentifiersInNameSuffixes(
osAndArchs: List<OsAndArch>, getIdentifier: (OsAndArch) -> String,
targetConfigurationType: String,
group: String, namePrefix: String, version: String? = null
) =
addDependenciesToFeatureVariantsWithIdentifiersInNameSuffixes(osAndArchs.map {
FeatureVariantDependencyConfig(it, getIdentifier(it))
}, targetConfigurationType, group, namePrefix, version)

/**
* @param configs use a predefined one in [DefaultSupported.OsAndArchs] or make your own with [FeatureVariantDependencyConfig]
* @param targetConfigurationType the type of the dependency configuration
* (see https://docs.gradle.org/current/userguide/declaring_dependencies.html#sec:what-are-dependency-configurations
* and https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph)
*/
fun DependencyHandlerScope.addDependenciesToFeatureVariantsWithIdentifiersInClassifiers(
configs: List<FeatureVariantDependencyConfig>,
targetConfigurationType: String,
group: String, name: String, version: String? = null
) {
for ((osAndArch, dependencyIdentifier) in configs)
addExternalModuleDependencyTo(
this,
osAndArch.featureVariantName camelCaseConcat targetConfigurationType,
group, name, version,
null, dependencyIdentifier, null, null
)
}

/** @see addDependenciesToFeatureVariantsWithIdentifiersInClassifiers */
fun DependencyHandlerScope.addDependenciesToFeatureVariantsWithIdentifiersInClassifiers(
osAndArchs: List<OsAndArch>, getIdentifier: (OsAndArch) -> String,
targetConfigurationType: String,
group: String, name: String, version: String? = null
) =
addDependenciesToFeatureVariantsWithIdentifiersInClassifiers(osAndArchs.map {
FeatureVariantDependencyConfig(it, getIdentifier(it))
}, targetConfigurationType, group, name, version)


fun getCapabilityNotation(group: String, name: String, featureVariantName: String) =
"$group:$name-${featureVariantName.camelCaseToKebabCase()}"

private inline fun DependencyHandlerScope.addDependencyWithFeatureVariantCapabilityDependencies(
featureVariantNames: List<String>, targetConfiguration: (featureVariantName: String?) -> String,
group: String, name: String, version: String? = null
) {
addExternalModuleDependencyTo(this, targetConfiguration(null), group, name, version, null, null, null, null)
for (featureVariantName in featureVariantNames)
addExternalModuleDependencyTo(
this,
targetConfiguration(featureVariantName),
group, name, version,
null, null, null
) {
capabilities {
requireCapability(getCapabilityNotation(group, name, featureVariantName))
}
}
}

fun DependencyHandlerScope.addDependencyWithFeatureVariantTransitiveCapabilityDependencies(
featureVariantNames: List<String>, targetConfigurationType: String,
group: String, name: String, version: String? = null
) =
addDependencyWithFeatureVariantCapabilityDependencies(
featureVariantNames,
{ featureVariantName ->
featureVariantName?.camelCaseConcat(targetConfigurationType) ?: targetConfigurationType
},
group, name, version
)

/**
* Adds all the feature variant dependencies to the main variant.
* Use this function without the `register-feature-variants` plugin.
*/
fun DependencyHandlerScope.addDependencyWithFeatureVariantCapabilityDependenciesToOneConfiguration(
featureVariantNames: List<String>, targetConfiguration: String,
group: String, name: String, version: String? = null
) =
addDependencyWithFeatureVariantCapabilityDependencies(
featureVariantNames,
{ _ -> targetConfiguration },
group, name, version
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.huanshankeji.jvm.native.osandarch

import com.huanshankeji.camelCaseConcat
import com.huanshankeji.jvm.native.osandarch.CpuArchitecture.Aarch64
import com.huanshankeji.jvm.native.osandarch.CpuArchitecture.X8664
import com.huanshankeji.jvm.native.osandarch.Os.*
import com.huanshankeji.kebabCaseConcat
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform

// TODO possibly use feature variant name
enum class Os(val identifier: String) {
Linux("linux"), Windows("windows"), Macos("osx")
}

enum class CpuArchitecture(val identifier: String) {
X8664("x8664"), Aarch64("aarch64")
}

data class OsAndArch(val os: Os, val arch: CpuArchitecture) {
val kebabCaseIdentifier = os.identifier kebabCaseConcat arch.identifier
val camelCaseIdentifier = os.identifier camelCaseConcat arch.identifier
}

fun getCurrentOsAndArch(): OsAndArch {
val currentOperatingSystem = DefaultNativePlatform.getCurrentOperatingSystem()
val os = when {
currentOperatingSystem.isLinux -> Linux
currentOperatingSystem.isWindows -> Windows
currentOperatingSystem.isMacOsX -> Macos
else -> throw IllegalArgumentException("Huanshankeji architecture common unsupported operating system: $currentOperatingSystem")
}
val currentArchitecture = DefaultNativePlatform.getCurrentArchitecture()
val arch = when {
currentArchitecture.isAmd64 -> X8664
currentArchitecture.isArm64 -> Aarch64
else -> throw IllegalArgumentException("Huanshankeji architecture common unsupported CPU architecture: $currentOperatingSystem")
}
return OsAndArch(os, arch)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.huanshankeji.jvm.native.osandarch

import com.huanshankeji.SourceSetType

plugins {
java
}

interface Extension {
val sourceSetType: Property<SourceSetType>
}

// TODO put in `afterEvaluate`?

val extension = extensions.create<Extension>("registerOsAndArchFeatureVariants")

java.registerDefaultSupportedFeatureVariants(extension.sourceSetType.getOrElse(SourceSetType.Main))
7 changes: 4 additions & 3 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-sam-with-receiver:1.8.0")
}
*/
//implementation(kotlin("gradle-plugin", "1.8.10")) // for Compose 1.3.1
// for `KotlinCompilationTask` and the version is for Compose 1.5.1
implementation(kotlin("gradle-plugin", "1.9.20"))
implementation("org.gradle.kotlin:gradle-kotlin-dsl-plugins:4.1.2") // This version has to be used for Gradle 8.4.

implementation("com.gradle.publish:plugin-publish-plugin:1.2.0")
implementation("com.gradle.publish:plugin-publish-plugin:1.2.1")

// This is a bootstrapping dependency (cross-version self-dependency). Try not to update its version unless necessary.
implementation("com.huanshankeji.team:gradle-plugins:0.3.0") { exclude("org.jetbrains.kotlin") }
// This is also a bootstrapping dependency.
implementation("com.huanshankeji:common-gradle-dependencies:0.5.0-20230310") { exclude("org.jetbrains.kotlin") }
implementation("com.huanshankeji:common-gradle-dependencies:0.7.0-20231111") { exclude("org.jetbrains.kotlin") }
}
7 changes: 3 additions & 4 deletions buildSrc/src/main/kotlin/VersionsAndDependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ val alignedPluginVersion = "0.5.0-SNAPSHOT"

// "x.y.z" indicates the version of the way of organizing the code,
// and the date indicates the version when the dependency versions are updated.
val commonGradleDependenciesVersion = "0.7.0-20231111"
val commonGradleDependenciesVersion = "0.7.1-20231111"

// This is the source dependency version. There is another build source dependency in "buildSrc/build.gradle.kts".
val pluginProjectDependentStableCommonGradleDependenciesVersion = "0.7.0-20231111-SNAPSHOT".apply {
// TODO: temporararily commented out for debugging purposes
//require(!endsWith("SNAPSHOT"))
val pluginProjectDependentStableCommonGradleDependenciesVersion = "0.7.0-20231111".apply {
require(!endsWith("SNAPSHOT"))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask

plugins {
id("conventions")
}

version = alignedPluginVersion

dependencies {
implementation("com.huanshankeji:common-gradle-dependencies:$pluginProjectDependentStableCommonGradleDependenciesVersion")
}

tasks.named<KotlinCompilationTask<*>>("compileKotlin").configure {
compilerOptions.freeCompilerArgs.add("-opt-in=com.huanshankeji.InternalApi")
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CommonGradleClasspathDependencies(val versions: CommonVersions) {

val plugin = Plugin()

@Deprecated("Use `plugin.serialization` instead.")
@Deprecated("Use `plugin.serialization` instead.", ReplaceWith("plugin.serialization"))
val serializationPlugin = plugin.serialization
}

Expand Down Expand Up @@ -54,6 +54,9 @@ class CommonGradleClasspathDependencies(val versions: CommonVersions) {
val defaultVersion = versions.kotlinxBenchmark
fun PluginDependenciesSpec.applyPluginWithVersion(version: String = defaultVersion) =
id("org.jetbrains.kotlinx.benchmark").version(defaultVersion)

fun pluginProject(version: String = defaultVersion) =
"org.jetbrains.kotlinx:kotlinx-benchmark-plugin:$version"
}

val benchmark = Benchmark()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CommonVersions @JvmOverloads constructor(
val ktor: String = "2.3.6",
val composeMultiplatform: String = "1.5.10", // this is usually only used in classpath dependencies

val vertx: String = "4.4.6",
val vertx: String = "4.4.6", // TODO bump to "4.5.0". There are some breaking changes however. See https://github.com/vert-x3/wiki/wiki/4.5.0-Deprecations-and-breaking-changes.
val arrow: String = "1.2.1",
val orgJunit: String = "5.10.1",
val kotest: String = "5.8.0",
Expand Down
19 changes: 19 additions & 0 deletions kotlin-common-gradle-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ plugins {
dependencies {
//implementation("io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.30.0")

implementation("org.jetbrains.kotlinx:kotlinx-benchmark-plugin:0.4.9")
// TODO
//implementation(commonGradleClasspathDependencies.kotlinx.benchmark.pluginProject())
implementation("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion")

testImplementation(kotlin("test"))
}

Expand Down Expand Up @@ -87,5 +92,19 @@ gradlePlugin {
"JVM test common feature variant",
"Adds a JVM test common feature variant with a source set that depends on `main`."
)

run {
scriptConventionsPlugin(
"benchmark.kotlinx-benchmark-jvm-conventions",
"kotlinx-benchmark conventions for Kotlin JVM",
"Applies the kotlinx-benchmark and `allopen` plugins, adds the kotlinx-benchmark dependencies, " +
"and registers a separate `benchmarks` source set that depends on `main` by default."
)
scriptConventionsPlugin(
"benchmark.kotlinx-benchmark-multiplatform-conventions",
"kotlinx-benchmark conventions for Kotlin Multiplatform",
"Applies the kotlinx-benchmark and `allopen` plugins and adds the koltinx-benchmark dependencies."
)
}
}
}
Loading

0 comments on commit f2ca225

Please sign in to comment.