Skip to content

Commit

Permalink
Switch to project logger
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielThomas committed May 17, 2016
1 parent 7978de5 commit 44c6d1b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
Expand Up @@ -21,14 +21,11 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolutionStrategy
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import java.util.zip.ZipEntry
import java.util.zip.ZipFile

class ResolutionRulesPlugin implements Plugin<Project> {
private static final Logger LOGGER = LoggerFactory.getLogger(ResolutionRulesPlugin)
private static final String CONFIGURATION_NAME = "resolutionRules"
private static final String JSON_EXT = ".json"
private static final String JAR_EXT = ".jar"
Expand All @@ -50,7 +47,7 @@ class ResolutionRulesPlugin implements Plugin<Project> {
return
}
if (config.state != Configuration.State.UNRESOLVED) {
LOGGER.warn("Configuration '{}' has been resolved. Dependency resolution rules will not be applied", config.name)
project.logger.warn("Configuration '{}' has been resolved. Dependency resolution rules will not be applied", config.name)
return
}

Expand Down Expand Up @@ -80,19 +77,20 @@ class ResolutionRulesPlugin implements Plugin<Project> {
return rules
}

private static Rules rulesFromConfiguration(Configuration configuration, NebulaResolutionRulesExtension extension) {
private Rules rulesFromConfiguration(Configuration configuration, NebulaResolutionRulesExtension extension) {
List<Rules> rules = new ArrayList<Rules>();
Set<File> files = configuration.resolve()
def logger = project.logger
if (files.isEmpty()) {
LOGGER.warn("No resolution rules have been added to the '{}' configuration", configuration.name)
logger.warn("No resolution rules have been added to the '{}' configuration", configuration.name)
}
for (file in files) {
if (isIncludedRuleFile(file.name, extension)) {
ResolutionJsonValidator.validateJsonFile(file)
LOGGER.info("Using $file as a dependency rules source")
logger.info("Using $file as a dependency rules source")
rules.add(parseJsonFile(file))
} else if (file.name.endsWith(JAR_EXT) || file.name.endsWith(ZIP_EXT)) {
LOGGER.info("Using $file as a dependency rules source")
logger.info("Using $file as a dependency rules source")
ZipFile zip = new ZipFile(file)
try {
Enumeration<? extends ZipEntry> entries = zip.entries()
Expand All @@ -107,7 +105,7 @@ class ResolutionRulesPlugin implements Plugin<Project> {
zip.close()
}
} else {
LOGGER.debug("Unsupported rules file extension for $file")
logger.debug("Unsupported rules file extension for $file")
}
}
return flattenRules(rules)
Expand Down
30 changes: 13 additions & 17 deletions src/main/groovy/nebula/plugin/resolutionrules/Rules.groovy
Expand Up @@ -27,8 +27,6 @@ import org.gradle.api.internal.artifacts.ivyservice.ivyresolve.strategy.VersionS
import org.gradle.api.specs.Specs
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.slf4j.Logger
import org.slf4j.LoggerFactory

public class Rules {
List<ReplaceRule> replace
Expand All @@ -43,15 +41,15 @@ public class Rules {
}

public List<ConfigurationRule> configurationRules() {
return [deny, exclude].flatten()
return [deny].flatten()
}

public List<ResolutionRule> resolutionRules() {
return [substitute, reject].flatten()
}

public List<ProjectConfigurationRule> projectConfigurationRules() {
return [new AlignRules(aligns: align)]
return [new AlignRules(aligns: align), exclude]
}
}

Expand Down Expand Up @@ -212,8 +210,6 @@ class AlignRule extends BaseRule {
}

class AlignRules implements ProjectConfigurationRule {
private static final Logger LOGGER = LoggerFactory.getLogger(AlignRules)

List<AlignRule> aligns

@Override
Expand All @@ -222,14 +218,15 @@ class AlignRules implements ProjectConfigurationRule {
return
}

def logger = project.logger
def copy = configuration.copyRecursive()
// Hacky workaround to prevent Gradle from attempting to resolve a project dependency as an external dependency
copy.exclude group: project.group.toString(), module: project.name
Set<ResolvedArtifact> artifacts
def resolvedConfiguration = copy.resolvedConfiguration
if (resolvedConfiguration.hasError()) {
def lenientConfiguration = resolvedConfiguration.lenientConfiguration
project.logger.info("Resolution rules could not resolve all dependencies to align in configuration '${configuration.name}' should also fail to resolve")
logger.info("Resolution rules could not resolve all dependencies to align in configuration '${configuration.name}' should also fail to resolve")
artifacts = lenientConfiguration.getArtifacts(Specs.SATISFIES_ALL)
} else {
artifacts = resolvedConfiguration.resolvedArtifacts
Expand All @@ -247,7 +244,7 @@ class AlignRules implements ProjectConfigurationRule {
aligns.each { AlignRule align ->
def matches = moduleVersions.findAll { ResolvedModuleVersion dep -> align.resolvedMatches(dep) }
if (matches) {
selectedVersion[align] = alignedVersion(align, matches, configuration, scheme, comparator.asStringComparator())
selectedVersion[align] = alignedVersion(project, align, matches, configuration, scheme, comparator.asStringComparator())
}
}

Expand All @@ -259,7 +256,7 @@ class AlignRules implements ProjectConfigurationRule {
def rule = foundMatch.key
def version = foundMatch.value
if (version != details.requested.version) {
LOGGER.info("Resolution rules ruleset ${rule.ruleSet} rule $rule aligning ${details.requested.group}:${details.requested.name} to $version")
logger.info("Resolution rules ruleset ${rule.ruleSet} rule $rule aligning ${details.requested.group}:${details.requested.name} to $version")
details.useVersion version
}
}
Expand All @@ -268,7 +265,7 @@ class AlignRules implements ProjectConfigurationRule {
}

private
static String alignedVersion(AlignRule rule, List<ResolvedModuleVersion> moduleVersions, Configuration configuration,
static String alignedVersion(Project project, AlignRule rule, List<ResolvedModuleVersion> moduleVersions, Configuration configuration,
VersionSelectorScheme scheme, Comparator<String> comparator) {
List<ModuleVersionSelector> forced = moduleVersions.findResults { moduleVersion ->
configuration.resolutionStrategy.forcedModules.find {
Expand All @@ -282,23 +279,22 @@ class AlignRules implements ProjectConfigurationRule {
def selector = scheme.parseSelector(version)
selector.dynamic
}
def logger = project.logger
if (!dynamicVersions.isEmpty()) {
LOGGER.warn("Resolution rules ruleset ${rule.ruleSet} align rule $rule is unable to honor forced versions $dynamicVersions. For a force to take precedence on an align rule, it must use a static version")
logger.warn("Resolution rules ruleset ${rule.ruleSet} align rule $rule is unable to honor forced versions $dynamicVersions. For a force to take precedence on an align rule, it must use a static version")
}
if (!staticVersions.isEmpty()) {
return staticVersions.min { String a, String b -> comparator.compare(a, b) }
} else {
LOGGER.warn("No static forces found for ruleset ${rule.ruleSet} align rule $rule. Falling back to default alignment logic")
logger.warn("No static forces found for ruleset ${rule.ruleSet} align rule $rule. Falling back to default alignment logic")
}
}
def versions = moduleVersions.collect { ResolvedModuleVersion dep -> dep.id.version }.toUnique()
return versions.max { String a, String b -> comparator.compare(a, b) }
}
}

class ExcludeRule extends BaseRule implements ConfigurationRule {
private static final Logger LOGGER = LoggerFactory.getLogger(ExcludeRule)

class ExcludeRule extends BaseRule implements ProjectConfigurationRule {
ModuleIdentifier moduleId

ExcludeRule(String ruleSet, Map map) {
Expand All @@ -307,8 +303,8 @@ class ExcludeRule extends BaseRule implements ConfigurationRule {
}

@Override
public void apply(Configuration configuration) {
LOGGER.info("Resolution rule ruleset $ruleSet excluding ${moduleId.organization}:${moduleId.name}")
void apply(Project project, ResolutionStrategy rs, Configuration configuration, NebulaResolutionRulesExtension extension) {
project.logger.info("Resolution rule ruleset $ruleSet excluding ${moduleId.organization}:${moduleId.name}")
configuration.exclude(group: moduleId.organization, module: moduleId.name)
}
}
Expand Down

0 comments on commit 44c6d1b

Please sign in to comment.