Skip to content

Commit 555bc12

Browse files
committed
refactor(gradle-plugin): Drop another when in favor of early returns
Signed-off-by: Frank Viernau <frank.viernau@gmail.com>
1 parent 32a3f6f commit 555bc12

1 file changed

Lines changed: 70 additions & 74 deletions

File tree

plugins/package-managers/gradle-plugin/src/main/kotlin/OrtModelBuilder.kt

Lines changed: 70 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -203,87 +203,83 @@ internal class OrtModelBuilder : ToolingModelBuilder {
203203
return ortDependencyCache[selected]
204204
}
205205

206-
when (id) {
207-
is ModuleComponentIdentifier -> {
208-
val pomFile = selected.getPomFile()
209-
210-
val modelBuildingResult = poms[id.toString()]
211-
if (modelBuildingResult == null) {
212-
val message = "No POM found for component '$id'."
213-
logger.warn(message)
214-
warnings += message
215-
}
216-
217-
// Check if we have scanned the dependencies of this subtree before, and if so, reuse them.
218-
val dependencies = globalDependencySubtrees.getOrPut(id.displayName) {
219-
selected.dependencies.toOrtDependencies(poms, visited + id)
220-
}
221-
222-
return OrtDependencyImpl(
223-
groupId = id.group,
224-
artifactId = id.module,
225-
version = id.version,
226-
classifier = "",
227-
extension = modelBuildingResult?.effectiveModel?.packaging.orEmpty(),
228-
variants = selected.variants.associate {
229-
it.displayName to it.attributes.keySet().associate { key ->
230-
key.name to it.attributes.getAttribute(key)?.toString().orEmpty()
231-
}
232-
},
233-
dependencies = dependencies,
234-
error = null,
235-
warning = null,
236-
pomFile = pomFile,
237-
mavenModel = modelBuildingResult?.run {
238-
OrtMavenModelImpl(
239-
licenses = effectiveModel.collectLicenses(),
240-
authors = effectiveModel.collectAuthors(),
241-
description = effectiveModel.description.orEmpty(),
242-
homepageUrl = effectiveModel.url.orEmpty(),
243-
vcs = getVcsModel()
244-
)
245-
},
246-
localPath = null
247-
).also {
248-
ortDependencyCache[selected] = it
249-
}
206+
if (id is ModuleComponentIdentifier) {
207+
val pomFile = selected.getPomFile()
208+
209+
val modelBuildingResult = poms[id.toString()]
210+
if (modelBuildingResult == null) {
211+
val message = "No POM found for component '$id'."
212+
logger.warn(message)
213+
warnings += message
250214
}
251215

252-
is ProjectComponentIdentifier -> {
253-
val moduleId = selected.moduleVersion ?: return null
254-
val dependencies = selected.dependencies.toOrtDependencies(poms, visited + id)
255-
256-
return OrtDependencyImpl(
257-
groupId = moduleId.group,
258-
artifactId = moduleId.name,
259-
version = moduleId.version.takeUnless { it == "unspecified" }.orEmpty(),
260-
classifier = "",
261-
extension = "",
262-
variants = selected.variants.associate {
263-
it.displayName to it.attributes.keySet().associate { key ->
264-
key.name to it.attributes.getAttribute(key)?.toString().orEmpty()
265-
}
266-
},
267-
dependencies = dependencies,
268-
error = null,
269-
warning = null,
270-
pomFile = null,
271-
mavenModel = null,
272-
localPath = id.projectPath
273-
).also {
274-
ortDependencyCache[selected] = it
275-
}
216+
// Check if we have scanned the dependencies of this subtree before, and if so, reuse them.
217+
val dependencies = globalDependencySubtrees.getOrPut(id.displayName) {
218+
selected.dependencies.toOrtDependencies(poms, visited + id)
276219
}
277220

278-
else -> {
279-
val message = "Unhandled dependency result type '$this' in '$from'."
280-
281-
logger.error(message)
282-
errors += message
221+
return OrtDependencyImpl(
222+
groupId = id.group,
223+
artifactId = id.module,
224+
version = id.version,
225+
classifier = "",
226+
extension = modelBuildingResult?.effectiveModel?.packaging.orEmpty(),
227+
variants = selected.variants.associate {
228+
it.displayName to it.attributes.keySet().associate { key ->
229+
key.name to it.attributes.getAttribute(key)?.toString().orEmpty()
230+
}
231+
},
232+
dependencies = dependencies,
233+
error = null,
234+
warning = null,
235+
pomFile = pomFile,
236+
mavenModel = modelBuildingResult?.run {
237+
OrtMavenModelImpl(
238+
licenses = effectiveModel.collectLicenses(),
239+
authors = effectiveModel.collectAuthors(),
240+
description = effectiveModel.description.orEmpty(),
241+
homepageUrl = effectiveModel.url.orEmpty(),
242+
vcs = getVcsModel()
243+
)
244+
},
245+
localPath = null
246+
).also {
247+
ortDependencyCache[selected] = it
248+
}
249+
}
283250

284-
return null
251+
if (id is ProjectComponentIdentifier) {
252+
val moduleId = selected.moduleVersion ?: return null
253+
val dependencies = selected.dependencies.toOrtDependencies(poms, visited + id)
254+
255+
return OrtDependencyImpl(
256+
groupId = moduleId.group,
257+
artifactId = moduleId.name,
258+
version = moduleId.version.takeUnless { it == "unspecified" }.orEmpty(),
259+
classifier = "",
260+
extension = "",
261+
variants = selected.variants.associate {
262+
it.displayName to it.attributes.keySet().associate { key ->
263+
key.name to it.attributes.getAttribute(key)?.toString().orEmpty()
264+
}
265+
},
266+
dependencies = dependencies,
267+
error = null,
268+
warning = null,
269+
pomFile = null,
270+
mavenModel = null,
271+
localPath = id.projectPath
272+
).also {
273+
ortDependencyCache[selected] = it
285274
}
286275
}
276+
277+
val message = "Unhandled component identifier type $id."
278+
279+
logger.error(message)
280+
errors += message
281+
282+
return null
287283
}
288284

289285
private fun ResolvedComponentResult.getPomFile(): String? {

0 commit comments

Comments
 (0)