[Gradle] Rename usageScope to mavenScope of KotlinUsageContext

and remove includeDependenciesToMavenPublication flag as it not
needed anymore.
This commit is contained in:
Anton Lakotka
2022-11-30 12:39:35 +01:00
committed by teamcity
parent 3fa85bf7fd
commit b7dff37734
7 changed files with 36 additions and 46 deletions
@@ -22,9 +22,10 @@ import org.jetbrains.kotlin.gradle.InternalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsageContext.UsageScope.*
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsageContext.MavenScope.*
import org.jetbrains.kotlin.gradle.utils.dashSeparatedName
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
internal const val PRIMARY_SINGLE_COMPONENT_NAME = "kotlin"
@@ -77,7 +78,6 @@ abstract class AbstractKotlinTarget(
compilation = mainCompilation,
dependencyConfigurationName = sourcesElementsConfigurationName,
includeIntoProjectStructureMetadata = false,
includeDependenciesToMavenPublication = false,
)
}
@@ -200,9 +200,13 @@ internal fun Project.buildAdhocComponentsFromKotlinVariants(kotlinVariants: Set<
}
adhocVariant.addVariantsFromConfiguration(configuration) { configurationVariantDetails ->
val mavenScope = kotlinUsageContext.mavenDependenciesScope
val mavenScope = kotlinUsageContext.mavenScope
if (mavenScope != null) {
configurationVariantDetails.mapToMavenScope(mavenScope)
val mavenScopeString = when(mavenScope) {
COMPILE -> "compile"
RUNTIME -> "runtime"
}
configurationVariantDetails.mapToMavenScope(mavenScopeString)
}
}
}
@@ -60,7 +60,7 @@ abstract class KotlinSoftwareComponent(
this += DefaultKotlinUsageContext(
compilation = metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
usageScope = KotlinUsageContext.UsageScope.COMPILE,
mavenScope = KotlinUsageContext.MavenScope.COMPILE,
dependencyConfigurationName = metadataTarget.apiElementsConfigurationName,
overrideConfigurationArtifacts = project.setProperty { listOf(allMetadataArtifact) }
)
@@ -72,7 +72,7 @@ abstract class KotlinSoftwareComponent(
this += run {
DefaultKotlinUsageContext(
metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
KotlinUsageContext.UsageScope.COMPILE,
KotlinUsageContext.MavenScope.COMPILE,
/** this configuration is created by [KotlinMetadataTargetConfigurator.createCommonMainElementsConfiguration] */
COMMON_MAIN_ELEMENTS_CONFIGURATION_NAME
)
@@ -84,7 +84,6 @@ abstract class KotlinSoftwareComponent(
compilation = metadataTarget.compilations.getByName(MAIN_COMPILATION_NAME),
dependencyConfigurationName = metadataTarget.sourcesElementsConfigurationName,
includeIntoProjectStructureMetadata = false,
includeDependenciesToMavenPublication = false
)
}
}
@@ -127,29 +126,20 @@ interface KotlinUsageContext : UsageContext {
val compilation: KotlinCompilation<*>
val dependencyConfigurationName: String
val includeIntoProjectStructureMetadata: Boolean
val usageScope: UsageScope?
val includeDependenciesToMavenPublication: Boolean
val mavenScope: MavenScope?
enum class UsageScope {
enum class MavenScope {
COMPILE, RUNTIME;
}
}
val KotlinUsageContext.mavenDependenciesScope get() =
when (usageScope.takeIf { includeDependenciesToMavenPublication }) {
KotlinUsageContext.UsageScope.COMPILE -> "compile"
KotlinUsageContext.UsageScope.RUNTIME -> "runtime"
null -> null
}
class DefaultKotlinUsageContext(
override val compilation: KotlinCompilation<*>,
override val usageScope: KotlinUsageContext.UsageScope? = null,
override val mavenScope: KotlinUsageContext.MavenScope? = null,
override val dependencyConfigurationName: String,
internal val overrideConfigurationArtifacts: SetProperty<PublishArtifact>? = null,
internal val overrideConfigurationAttributes: AttributeContainer? = null,
override val includeIntoProjectStructureMetadata: Boolean = true,
override val includeDependenciesToMavenPublication: Boolean = true,
) : KotlinUsageContext {
private val kotlinTarget: KotlinTarget get() = compilation.target
private val project: Project get() = kotlinTarget.project
@@ -12,14 +12,14 @@ import org.gradle.api.XmlProvider
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.attributes.Usage
import org.gradle.api.internal.component.SoftwareComponentInternal
import org.gradle.api.internal.component.UsageContext
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilationToRunnableFiles
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetComponent
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsageContext.UsageScope
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinUsageContext.MavenScope
import org.jetbrains.kotlin.gradle.utils.getValue
internal data class ModuleCoordinates(
@@ -37,12 +37,13 @@ internal class PomDependenciesRewriter(
// Get the dependencies mapping according to the component's UsageContexts:
private val dependenciesMappingForEachUsageContext by project.provider {
(component as SoftwareComponentInternal).usages.mapNotNull { usage ->
if (usage is KotlinUsageContext && usage.includeDependenciesToMavenPublication)
associateDependenciesWithActualModuleDependencies(usage)
// We are only interested in dependencies that are mapped to some other dependencies:
.filter { (from, to) -> Triple(from.group, from.name, from.version) != Triple(to.group, to.name, to.version) }
else null
(component as SoftwareComponentInternal).usages.filterIsInstance<KotlinUsageContext>().mapNotNull { usage ->
// When maven scope is not set, we can shortcut immediately here, since no dependencies from that usage context
// will be present in maven pom, e.g. from sourcesElements
val mavenScope = usage.mavenScope ?: return@mapNotNull null
associateDependenciesWithActualModuleDependencies(usage.compilation, mavenScope)
// We are only interested in dependencies that are mapped to some other dependencies:
.filter { (from, to) -> Triple(from.group, from.name, from.version) != Triple(to.group, to.name, to.version) }
}
}
@@ -111,9 +112,9 @@ internal class PomDependenciesRewriter(
}
private fun associateDependenciesWithActualModuleDependencies(
usageContext: KotlinUsageContext
compilation: KotlinCompilation<*>,
mavenScope: MavenScope,
): Map<ModuleCoordinates, ModuleCoordinates> {
val compilation = usageContext.compilation
val project = compilation.target.project
val targetDependenciesConfiguration = project.configurations.getByName(
@@ -121,16 +122,14 @@ private fun associateDependenciesWithActualModuleDependencies(
is KotlinJvmAndroidCompilation -> {
// TODO handle Android configuration names in a general way once we drop AGP < 3.0.0
val variantName = compilation.name
when (usageContext.usageScope) {
UsageScope.COMPILE -> variantName + "CompileClasspath"
UsageScope.RUNTIME -> variantName + "RuntimeClasspath"
null -> error("Usage for usage context is undefined")
when (mavenScope) {
MavenScope.COMPILE -> variantName + "CompileClasspath"
MavenScope.RUNTIME -> variantName + "RuntimeClasspath"
}
}
else -> when (usageContext.usageScope) {
UsageScope.COMPILE -> compilation.compileDependencyConfigurationName
UsageScope.RUNTIME -> (compilation as KotlinCompilationToRunnableFiles).runtimeDependencyConfigurationName
null -> error("Usage for usage context is undefined")
else -> when (mavenScope) {
MavenScope.COMPILE -> compilation.compileDependencyConfigurationName
MavenScope.RUNTIME -> (compilation as KotlinCompilationToRunnableFiles).runtimeDependencyConfigurationName
}
}
)
@@ -214,13 +214,12 @@ abstract class KotlinAndroidTarget @Inject constructor(
compilation = compilation,
dependencyConfigurationName = sourcesElementsConfigurationName,
overrideConfigurationAttributes = sourcesElementsConfiguration.attributes.filterOutAndroidVariantAttributes(),
includeDependenciesToMavenPublication = false,
includeIntoProjectStructureMetadata = false
)
return listOf(
apiElementsConfigurationName to KotlinUsageContext.UsageScope.COMPILE,
runtimeElementsConfigurationName to KotlinUsageContext.UsageScope.RUNTIME,
apiElementsConfigurationName to KotlinUsageContext.MavenScope.COMPILE,
runtimeElementsConfigurationName to KotlinUsageContext.MavenScope.RUNTIME,
).mapTo(mutableSetOf()) { (dependencyConfigurationName, mavenScope) ->
val configuration = project.configurations.getByName(dependencyConfigurationName)
DefaultKotlinUsageContext(
@@ -83,10 +83,9 @@ constructor(
configureSourcesJarArtifact(mainCompilation, componentName, dashSeparatedName(targetName.toLowerCase()))
usageContexts += DefaultKotlinUsageContext(
compilation = mainCompilation,
usageScope = KotlinUsageContext.UsageScope.RUNTIME,
mavenScope = KotlinUsageContext.MavenScope.RUNTIME,
dependencyConfigurationName = sourcesElementsConfigurationName,
includeIntoProjectStructureMetadata = false,
includeDependenciesToMavenPublication = false,
)
val result = createKotlinVariant(componentName, mainCompilation, usageContexts)
@@ -103,7 +102,7 @@ constructor(
return usageContexts +
DefaultKotlinUsageContext(
compilation = compilations.getByName(MAIN_COMPILATION_NAME),
usageScope = KotlinUsageContext.UsageScope.COMPILE,
mavenScope = KotlinUsageContext.MavenScope.COMPILE,
dependencyConfigurationName = commonFakeApiElementsConfigurationName,
overrideConfigurationArtifacts = project.setProperty { emptyList() }
)
@@ -70,7 +70,7 @@ constructor(
return usageContexts +
DefaultKotlinUsageContext(
compilation = compilations.getByName(MAIN_COMPILATION_NAME),
usageScope = KotlinUsageContext.UsageScope.COMPILE,
mavenScope = KotlinUsageContext.MavenScope.COMPILE,
dependencyConfigurationName = commonFakeApiElementsConfigurationName,
overrideConfigurationArtifacts = project.setProperty { emptyList() }
)
@@ -93,7 +93,7 @@ abstract class KotlinNativeTarget @Inject constructor(
mutableUsageContexts.add(
DefaultKotlinUsageContext(
mainCompilation,
KotlinUsageContext.UsageScope.COMPILE,
KotlinUsageContext.MavenScope.COMPILE,
metadataConfiguration.name,
includeIntoProjectStructureMetadata = false
)
@@ -106,7 +106,6 @@ abstract class KotlinNativeTarget @Inject constructor(
compilation = mainCompilation,
dependencyConfigurationName = sourcesElementsConfigurationName,
includeIntoProjectStructureMetadata = false,
includeDependenciesToMavenPublication = false,
)
mutableUsageContexts += sourcesUsage