Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose apply ruleSet functionality to allow manually apply resolution… #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
69 changes: 41 additions & 28 deletions src/main/kotlin/nebula/plugin/resolutionrules/plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,31 +96,36 @@ class ResolutionRulesPlugin : Plugin<Project> {
}

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<String>, 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)
}
}
}
Expand Down Expand Up @@ -207,12 +211,7 @@ open class NebulaResolutionRulesExtension @Inject constructor(private val projec
var exclude = ArrayList<String>()

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)
Expand All @@ -223,4 +222,18 @@ open class NebulaResolutionRulesExtension @Inject constructor(private val projec
}
}.values.flatten()
}

fun ruleSetByNames(names: ArrayList<String>): RuleSet {
return getAllRuleSets().filterKeys { ruleSet ->
names.contains(ruleSet)
}.values.flatten()
}

private fun getAllRuleSets(): Map<String, RuleSet> {
val service = NebulaResolutionRulesService.registerService(project).get()
return service.parameters
.getResolutionRules()
.get()
.byFile
}
}