@@ -77,6 +77,7 @@ data class RuleSet(
7777
7878class AlignedPlatformRule (alignRule : AlignRule , substituteRules : MutableList <SubstituteRule >) : Serializable {
7979 var substituteRules: List <SubstituteRule > = substituteRules
80+ var alignRule: AlignRule = alignRule
8081
8182 fun apply (project : Project , configuration : Configuration , resolutionStrategy : ResolutionStrategy , extension : NebulaResolutionRulesExtension , reasons : MutableSet <String >) {
8283 substituteRules.forEach {
@@ -93,24 +94,35 @@ class AlignedPlatformRule(alignRule: AlignRule, substituteRules: MutableList<Sub
9394 if (substitutedModule != null && shouldApplyRule(configuration, substitutedModule)) {
9495 firstLevelDependenciesRejectTheSubstitutedVersions(project, configuration, substitutedModule, withSelector, resolutionStrategy)
9596 transitiveDependenciesRejectTheSubstitutedVersions(project, substitutedModule, withSelector)
96- it.applyForAlignedGroup(project, configuration, configuration.resolutionStrategy, extension, reasons)
97+ it.applyForAlignedGroup(project, configuration, configuration.resolutionStrategy, extension, reasons, alignRule )
9798 }
9899 }
99100 }
100101
101102 private fun shouldApplyRule (configuration : Configuration , substitutedModule : ModuleComponentSelector ): Boolean {
102103 var shouldApplyRule = false
103- configuration.incoming.dependencies.forEach { dep ->
104+ var substitutedDependencyIsInDependencyGraph = false
105+
106+ val incomingDependencies = configuration.incoming.dependencies
107+
108+ incomingDependencies.forEach { dep ->
104109 if (dep.group == substitutedModule.group && dep.name == substitutedModule.module) {
105- shouldApplyRule = true // should apply only if substituted model is in the dependency graph
110+ substitutedDependencyIsInDependencyGraph = true
111+ }
112+ }
113+ if (substitutedDependencyIsInDependencyGraph) {
114+ incomingDependencies.forEach { dep ->
115+ if (alignRule.ruleMatches(dep.group ? : " " , dep.name)) {
116+ shouldApplyRule = true
117+ }
106118 }
107119 }
108120 return shouldApplyRule
109121 }
110122
111123 private fun transitiveDependenciesRejectTheSubstitutedVersions (project : Project , substitutedModule : ModuleComponentSelector , withSelector : ModuleComponentSelector ) {
112124 project.dependencies.components.all(TransitiveDependenciesSubstitutionMetadataRule ::class .java) {
113- it.params(substitutedModule.group, substitutedModule.module, substitutedModule.version, withSelector.version)
125+ it.params(alignRule, substitutedModule.group, substitutedModule.module, substitutedModule.version, withSelector.version)
114126 }
115127 }
116128
@@ -120,7 +132,7 @@ class AlignedPlatformRule(alignRule: AlignRule, substituteRules: MutableList<Sub
120132 configuration.incoming.beforeResolve { resolvableDependencies ->
121133 resolvableDependencies.dependencies.forEach { dep ->
122134 if (dep is ExternalModuleDependency ) {
123- if (dep.group !! .startsWith(substitutedModule .group)) {
135+ if (alignRule.ruleMatches(dep .group ? : " " , dep.name )) {
124136 val usingDependencyRecommendation = dep.version.isNullOrEmpty()
125137
126138 if (usingDependencyRecommendation) {
@@ -245,7 +257,8 @@ data class SubstituteRule(val module: String, val with: String, override var rul
245257 }
246258 }
247259
248- fun applyForAlignedGroup (project : Project , configuration : Configuration , resolutionStrategy : ResolutionStrategy , extension : NebulaResolutionRulesExtension , reasons : MutableSet <String >) {
260+ fun applyForAlignedGroup (project : Project , configuration : Configuration , resolutionStrategy : ResolutionStrategy ,
261+ extension : NebulaResolutionRulesExtension , reasons : MutableSet <String >, alignRule : AlignRule ) {
249262 val substitution = resolutionStrategy.dependencySubstitution
250263 val selector = substitution.module(module)
251264 val withModuleId = ModuleVersionIdentifier .valueOf(with)
@@ -254,16 +267,15 @@ data class SubstituteRule(val module: String, val with: String, override var rul
254267 }
255268 val withSelector = substitution.module(withModuleId.toString()) as ModuleComponentSelector
256269
257- // TODO: only do this if the alignment rule applies to this one via includes and excludes
258270 if (selector is ModuleComponentSelector ) {
259271 resolutionStrategy.dependencySubstitution.all(action {
260272 if (requested is ModuleComponentSelector ) {
261273 val requestedSelector = requested as ModuleComponentSelector
262274 val requestedWithSubstitutedVersionFromAlignedModule = ModuleVersionIdentifier .valueOf(" ${requestedSelector.group} :${requestedSelector.module} :${withSelector.version} " )
263275
264- val hasSameGroup = requestedSelector.group == selector.group
276+ val matches = alignRule.ruleMatches( requestedSelector.group ? : " " , requestedSelector.module)
265277 val notTheOriginatingDependency = requestedSelector.module != selector.module
266- if (hasSameGroup && notTheOriginatingDependency) {
278+ if (matches && notTheOriginatingDependency) {
267279 val versionSelector = VersionWithSelector (selector.version).asSelector()
268280 if (versionSelector.accept(requestedSelector.version)) {
269281 val message = " substitution from aligned dependency '$selector ' to '$withSelector ' because '$reason '\n " +
@@ -282,13 +294,15 @@ data class SubstituteRule(val module: String, val with: String, override var rul
282294
283295class TransitiveDependenciesSubstitutionMetadataRule : ComponentMetadataRule , Serializable {
284296 private val logger: Logger = Logging .getLogger(TransitiveDependenciesSubstitutionMetadataRule ::class .java)
297+ val alignRule: AlignRule
285298 val substitutionGroup: String
286299 val substitutionModuleName: String
287300 val substitutionVersion: String
288301 val withSelectorVersion: String
289302
290303 @Inject
291- constructor (substitutionGroup: String , substitutionModuleName: String , substitutionVersion: String , withSelectorVersion: String ) {
304+ constructor (alignRule: AlignRule , substitutionGroup: String , substitutionModuleName: String , substitutionVersion: String , withSelectorVersion: String ) {
305+ this .alignRule = alignRule
292306 this .substitutionGroup = substitutionGroup
293307 this .substitutionModuleName = substitutionModuleName
294308 this .substitutionVersion = substitutionVersion
@@ -304,7 +318,7 @@ class TransitiveDependenciesSubstitutionMetadataRule : ComponentMetadataRule, Se
304318 details.allVariants {
305319 it.withDependencies { deps ->
306320 deps.forEach { dep ->
307- if (dep.group.startsWith(substitutionGroup )) {
321+ if (alignRule.ruleMatches( dep.group ? : " " , dep.name )) {
308322 dep.version {
309323 it.reject(substitutionVersion)
310324 logger.debug(" Rejection of transitive dependency ${dep.group} :${dep.name} version(s) '$substitutionVersion ' from aligned dependency '$substitutionGroup :$substitutionModuleName '" )
0 commit comments