Skip to content

Commit 6c76491

Browse files
committed
Remove unused dependencyRulesApplied and make jackson object mapper lazy
1 parent 22693a8 commit 6c76491

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

src/main/kotlin/nebula/plugin/resolutionrules/alignRule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class AlignMatcher(val rule: AlignRule, groupPattern: Pattern, includesPatterns:
7070
}
7171

7272
//@CacheableRule
73-
open abstract class AlignedPlatformMetadataRule @Inject constructor(val rule: AlignRule) : ComponentMetadataRule, Serializable {
73+
abstract class AlignedPlatformMetadataRule @Inject constructor(val rule: AlignRule) : ComponentMetadataRule, Serializable {
7474
private val logger: Logger = Logging.getLogger(AlignedPlatformMetadataRule::class.java)
7575

7676
override fun execute(componentMetadataContext: ComponentMetadataContext) {

src/main/kotlin/nebula/plugin/resolutionrules/json.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package nebula.plugin.resolutionrules
1919
import com.fasterxml.jackson.databind.*
2020
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
2121

22-
fun objectMapper(): ObjectMapper {
23-
return jacksonObjectMapper()
24-
}
22+
/**
23+
* Lazily initialized, shared ObjectMapper instance for JSON deserialization.
24+
* Thread-safe singleton that is only created when first accessed.
25+
*/
26+
val objectMapper: ObjectMapper by lazy { jacksonObjectMapper() }

src/main/kotlin/nebula/plugin/resolutionrules/plugin.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ class ResolutionRulesPlugin : Plugin<Project> {
107107
return@configureEach
108108
}
109109

110-
var dependencyRulesApplied = false
111110
project.onExecute {
112111
val ruleSet = extension.ruleSet()
113112
when {
@@ -122,7 +121,6 @@ class ResolutionRulesPlugin : Plugin<Project> {
122121
ruleSet.dependencyRulesPartTwo().forEach { rule ->
123122
rule.apply(project, config, config.resolutionStrategy, extension)
124123
}
125-
dependencyRulesApplied = true
126124
}
127125
}
128126
}
@@ -150,7 +148,11 @@ class ResolutionRulesPlugin : Plugin<Project> {
150148
abstract class NebulaResolutionRulesService : BuildService<NebulaResolutionRulesService.Params> {
151149
companion object {
152150
private val Logger: Logger = Logging.getLogger(NebulaResolutionRulesService::class.java)
153-
private val Mapper = objectMapper()
151+
private val Mapper = objectMapper
152+
153+
private const val JSON_EXTENSION = "json"
154+
private const val JAR_EXTENSION = "jar"
155+
private const val ZIP_EXTENSION = "zip"
154156

155157
fun registerService(project: Project): Provider<NebulaResolutionRulesService> {
156158
return project.gradle.sharedServices.registerIfAbsent(
@@ -167,18 +169,18 @@ abstract class NebulaResolutionRulesService : BuildService<NebulaResolutionRules
167169
configuration.incoming.files.files.stream().use { stream ->
168170
return stream.flatMap { file ->
169171
when (file.extension) {
170-
"json" -> {
172+
JSON_EXTENSION -> {
171173
Logger.debug("nebula.resolution-rules uses: {}", file.name)
172174
Stream.of(file.absolutePath to file.readBytes())
173175
}
174-
"jar", "zip" -> {
176+
JAR_EXTENSION, ZIP_EXTENSION -> {
175177
Logger.info("nebula.resolution-rules is using ruleset: {}", file.name)
176178
val zipFile = ZipFile(file)
177179
Collections.list(zipFile.entries()).stream()
178180
.onClose(zipFile::close)
179181
.flatMap { entry ->
180182
val entryFile = File(entry.name)
181-
if (entryFile.extension == "json") {
183+
if (entryFile.extension == JSON_EXTENSION) {
182184
Stream.of("${file.absolutePath}!${entry.name}" to zipFile.getInputStream(entry).readBytes())
183185
} else Stream.empty()
184186
}
@@ -190,7 +192,7 @@ abstract class NebulaResolutionRulesService : BuildService<NebulaResolutionRules
190192
}
191193
}.parallel()
192194
.map { (path, bytes) ->
193-
val filePath = if (path.contains("!")) path.substringAfter("!") else path
195+
val filePath = path.substringAfterLast('!', path)
194196
val file = File(filePath)
195197
val ruleSetName = file.nameWithoutExtension
196198
Logger.debug("Using {} ({}) a dependency rules source", ruleSetName, path)

src/test/groovy/nebula/plugin/resolutionrules/RulesTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class RulesTest extends Specification {
7373
}
7474

7575
static RuleSet parseJsonText(String json) {
76-
def ruleSet = JsonKt.objectMapper().readValue(json, RuleSet)
76+
def ruleSet = JsonKt.getObjectMapper().readValue(json, RuleSet)
7777
return RulesKt.withName(ruleSet, "dummy")
7878
}
7979
}

0 commit comments

Comments
 (0)