From 1f18f31f9b584e06f9e7351b2690179b0a0f00de Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Fri, 7 Jul 2023 09:59:36 -0700 Subject: [PATCH] Expose apply ruleSet functionality to allow manually apply resolution rules --- .../ResolutionRulesPluginSpec.groovy | 29 ++++++++ .../nebula/plugin/resolutionrules/plugin.kt | 69 +++++++++++-------- 2 files changed, 70 insertions(+), 28 deletions(-) diff --git a/src/integTest/groovy/nebula/plugin/resolutionrules/ResolutionRulesPluginSpec.groovy b/src/integTest/groovy/nebula/plugin/resolutionrules/ResolutionRulesPluginSpec.groovy index 45b0479..736a47f 100644 --- a/src/integTest/groovy/nebula/plugin/resolutionrules/ResolutionRulesPluginSpec.groovy +++ b/src/integTest/groovy/nebula/plugin/resolutionrules/ResolutionRulesPluginSpec.groovy @@ -333,6 +333,35 @@ class ResolutionRulesPluginSpec extends IntegrationSpec { result.standardOutput.contains '\\--- org.slf4j:slf4j-api:1.7.21\n' } + def 'manually apply optional rules'() { + given: + buildFile << """ + dependencies { + resolutionRules files("$optionalRulesJsonFile") + + implementation 'log4j:log4j:1.2.17' + implementation 'org.slf4j:jcl-over-slf4j:1.7.0' + } + + afterEvaluate { + project.configurations.all {c -> + project.plugins.getPlugin("com.netflix.nebula.resolution-rules").applyRuleSet(["optional-$moduleName"], c) + } + } + + """.stripIndent() + + + when: + def result = runTasksSuccessfully('dependencies', '--configuration', 'compileClasspath') + + then: + result.standardOutput.contains '+--- log4j:log4j:1.2.17 -> org.slf4j:log4j-over-slf4j:1.7.21\n' + result.standardOutput.contains '| \\--- org.slf4j:slf4j-api:1.7.21\n' + result.standardOutput.contains '\\--- org.slf4j:jcl-over-slf4j:1.7.0 -> 1.7.21\n' + result.standardOutput.contains '\\--- org.slf4j:slf4j-api:1.7.21\n' + } + def 'only included rules are applied'() { given: def otherRulesFile = new File(projectDir, "other-${moduleName}.json") diff --git a/src/main/kotlin/nebula/plugin/resolutionrules/plugin.kt b/src/main/kotlin/nebula/plugin/resolutionrules/plugin.kt index 6564e18..de9ecb9 100644 --- a/src/main/kotlin/nebula/plugin/resolutionrules/plugin.kt +++ b/src/main/kotlin/nebula/plugin/resolutionrules/plugin.kt @@ -19,7 +19,6 @@ package nebula.plugin.resolutionrules import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import com.netflix.nebula.interop.onExecute -import com.netflix.nebula.interop.onResolve import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.Configuration @@ -97,31 +96,36 @@ class ResolutionRulesPlugin : Plugin { } project.configurations.all { config -> - if (ignoredConfigurationPrefixes.any { config.name.startsWith(it) }) { - return@all + project.onExecute { + val ruleSet = extension.ruleSet() + applyRuleSet(ruleSet, config) } + } + } - if (ignoredConfigurationSuffixes.any { config.name.endsWith(it) }) { - return@all - } + fun applyRuleSet(ruleNames: ArrayList, config: Configuration) { + val ruleSet = extension.ruleSetByNames(ruleNames) + applyRuleSet(ruleSet, config) + } + private fun applyRuleSet(ruleSet: RuleSet, config: Configuration) { + if (ignoredConfigurationPrefixes.any { config.name.startsWith(it) }) { + return + } - var dependencyRulesApplied = false - project.onExecute { - val ruleSet = extension.ruleSet() - when { - config.state != Configuration.State.UNRESOLVED || config.getObservedState() != Configuration.State.UNRESOLVED -> Logger.warn( - "Dependency resolution rules will not be applied to $config, it was resolved before the project was executed" - ) - else -> { - ruleSet.dependencyRulesPartOne().forEach { rule -> - rule.apply(project, config, config.resolutionStrategy, extension) - } + if (ignoredConfigurationSuffixes.any { config.name.endsWith(it) }) { + return + } + when { + config.state != Configuration.State.UNRESOLVED || config.getObservedState() != Configuration.State.UNRESOLVED -> Logger.warn( + "Dependency resolution rules will not be applied to $config, it was resolved before the project was executed" + ) + else -> { + ruleSet.dependencyRulesPartOne().forEach { rule -> + rule.apply(project, config, config.resolutionStrategy, extension) + } - ruleSet.dependencyRulesPartTwo().forEach { rule -> - rule.apply(project, config, config.resolutionStrategy, extension) - } - dependencyRulesApplied = true - } + ruleSet.dependencyRulesPartTwo().forEach { rule -> + rule.apply(project, config, config.resolutionStrategy, extension) } } } @@ -207,12 +211,7 @@ open class NebulaResolutionRulesExtension @Inject constructor(private val projec var exclude = ArrayList() fun ruleSet(): RuleSet { - val service = NebulaResolutionRulesService.registerService(project).get() - @Suppress("UnstableApiUsage") val rulesByFile = service.parameters - .getResolutionRules() - .get() - .byFile - return rulesByFile.filterKeys { ruleSet -> + return getAllRuleSets().filterKeys { ruleSet -> when { ruleSet.startsWith(ResolutionRulesPlugin.OPTIONAL_PREFIX) -> { val ruleSetWithoutPrefix = ruleSet.substring(ResolutionRulesPlugin.OPTIONAL_PREFIX.length) @@ -223,4 +222,18 @@ open class NebulaResolutionRulesExtension @Inject constructor(private val projec } }.values.flatten() } + + fun ruleSetByNames(names: ArrayList): RuleSet { + return getAllRuleSets().filterKeys { ruleSet -> + names.contains(ruleSet) + }.values.flatten() + } + + private fun getAllRuleSets(): Map { + val service = NebulaResolutionRulesService.registerService(project).get() + return service.parameters + .getResolutionRules() + .get() + .byFile + } }