Skip to content

Commit e08a74c

Browse files
committed
refactor: format files to comply with detekt
1 parent 9a274d1 commit e08a74c

1 file changed

Lines changed: 112 additions & 100 deletions

File tree

gradle-plugins/react/brownfield/src/main/kotlin/com/callstack/react/brownfield/expo/ExpoPublishingHelper.kt

Lines changed: 112 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ open class ExpoPublishingHelper(val brownfieldAppProject: Project, val expoProje
9999
* @param discoveredExpoTransitiveDependencies Set of DependencyInfo
100100
* representing Expo transitive dependencies to add.
101101
*/
102+
@Suppress("LongMethod")
102103
protected fun reconfigureGradleModuleJSON(discoveredExpoTransitiveDependencies: VersionMediatingDependencySet) {
103104
val removeDependenciesFromModuleFileTask =
104105
brownfieldAppProject.tasks.register("removeDependenciesFromModuleFile")
@@ -182,6 +183,7 @@ open class ExpoPublishingHelper(val brownfieldAppProject: Project, val expoProje
182183
* @param discoveredExpoTransitiveDependencies Set of DependencyInfo
183184
* representing Expo transitive dependencies to add.
184185
*/
186+
@Suppress("LongMethod")
185187
protected fun reconfigurePOM(discoveredExpoTransitiveDependencies: VersionMediatingDependencySet) {
186188
brownfieldAppProject.pluginManager.withPlugin("maven-publish") {
187189
brownfieldAppProject.extensions.configure(PublishingExtension::class.java) { publishing ->
@@ -289,7 +291,7 @@ open class ExpoPublishingHelper(val brownfieldAppProject: Project, val expoProje
289291
fun discoverExpoTransitiveDependenciesForPublication(expoGPProjection: ExpoGradleProjectProjection): VersionMediatingDependencySet? {
290292
val publication =
291293
getPublishingInfo(expoGPProjection)
292-
?: throw IllegalStateException(
294+
?: error(
293295
"Cannot configure publishing for Expo project " +
294296
"${expoGPProjection.name} - could not determine publishing info",
295297
)
@@ -314,76 +316,23 @@ open class ExpoPublishingHelper(val brownfieldAppProject: Project, val expoProje
314316
)
315317

316318
val dependencies = VersionMediatingDependencySet()
317-
var depsDiscoverySource = ""
319+
var depsDiscoverySource: String
318320

319321
if (pomFile.exists()) {
320322
// firstly, try reading transitive dependencies from the POM file
321323
val xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(pomFile)
322-
val dependenciesNodes = xml.getElementsByTagName("dependencies")
323324

324325
depsDiscoverySource = "POM file"
325326

326-
dependenciesNodes.forEach { depNodeList ->
327-
depNodeList.childNodes.forEach { depNode ->
328-
// below: some nodes are not dependencies, but pure text, in which case their name is '#text'
329-
if (depNode.nodeName == "dependency") {
330-
val groupId = depNode.getChildNodeByName("groupId")!!.textContent
331-
val maybeArtifactId = depNode.getChildNodeByName("artifactId")
332-
333-
val artifactId = maybeArtifactId!!.textContent
334-
val version = depNode.getChildNodeByName("version")?.textContent
335-
val scope = depNode.getChildNodeByName("scope")?.textContent
336-
val optional = depNode.getChildNodeByName("optional")?.textContent
337-
338-
val dependencyInfo =
339-
DependencyInfo(
340-
groupId = groupId,
341-
artifactId = artifactId,
342-
version = version,
343-
scope = scope ?: "compile",
344-
optional = optional?.toBoolean() ?: false,
345-
)
346-
347-
dependencies.add(dependencyInfo)
348-
}
349-
}
350-
}
327+
appendExpoTransitiveDependenciesFromMavenPOM(
328+
xml.getElementsByTagName("dependencies"),
329+
dependencies,
330+
)
351331
} else if (pkgProject != null) {
352332
// as fallback, iterate Gradle's resolved dependencies for the Expo project
353333
depsDiscoverySource = "Gradle resolved dependencies"
354334

355-
pkgProject.configurations
356-
.filter { cfg ->
357-
setOf(
358-
"test",
359-
"androidTest",
360-
"kapt",
361-
"annotationProcessor",
362-
"lint",
363-
"detached",
364-
).none {
365-
cfg.name.contains(it, ignoreCase = true)
366-
}
367-
}
368-
.filter { cfg ->
369-
setOf("compile", "implementation", "api", "runtime").any {
370-
cfg.name.contains(it, ignoreCase = true)
371-
}
372-
}
373-
.forEach { cfg ->
374-
cfg.dependencies
375-
.filter { dep ->
376-
dep.group != null
377-
}.forEach { dep ->
378-
dependencies.add(
379-
DependencyInfo.fromGradleDep(
380-
groupId = dep.group!!,
381-
artifactId = dep.name,
382-
version = dep.version,
383-
),
384-
)
385-
}
386-
}
335+
appendExpoTransitiveDependenciesFromGradle(pkgProject, dependencies)
387336
} else {
388337
// if no POM & no Gradle project have been resolved, there is no source for the data
389338

@@ -413,18 +362,83 @@ open class ExpoPublishingHelper(val brownfieldAppProject: Project, val expoProje
413362
return dependencies
414363
}
415364

365+
protected fun appendExpoTransitiveDependenciesFromMavenPOM(
366+
dependenciesNodes: org.w3c.dom.NodeList,
367+
dependencies: VersionMediatingDependencySet,
368+
) {
369+
dependenciesNodes.forEach { depNodeList ->
370+
depNodeList.childNodes.forEach { depNode ->
371+
// below: some nodes are not dependencies, but pure text, in which case their name is '#text'
372+
if (depNode.nodeName == "dependency") {
373+
val groupId = depNode.getChildNodeByName("groupId")!!.textContent
374+
val maybeArtifactId = depNode.getChildNodeByName("artifactId")
375+
376+
val artifactId = maybeArtifactId!!.textContent
377+
val version = depNode.getChildNodeByName("version")?.textContent
378+
val scope = depNode.getChildNodeByName("scope")?.textContent
379+
val optional = depNode.getChildNodeByName("optional")?.textContent
380+
381+
val dependencyInfo =
382+
DependencyInfo(
383+
groupId = groupId,
384+
artifactId = artifactId,
385+
version = version,
386+
scope = scope ?: "compile",
387+
optional = optional?.toBoolean() ?: false,
388+
)
389+
390+
dependencies.add(dependencyInfo)
391+
}
392+
}
393+
}
394+
}
395+
396+
protected fun appendExpoTransitiveDependenciesFromGradle(
397+
pkgProject: Project,
398+
dependencies: VersionMediatingDependencySet,
399+
) {
400+
pkgProject.configurations
401+
.filter { cfg ->
402+
setOf(
403+
"test",
404+
"androidTest",
405+
"kapt",
406+
"annotationProcessor",
407+
"lint",
408+
"detached",
409+
).none {
410+
cfg.name.contains(it, ignoreCase = true)
411+
}
412+
}
413+
.filter { cfg ->
414+
setOf("compile", "implementation", "api", "runtime").any {
415+
cfg.name.contains(it, ignoreCase = true)
416+
}
417+
}
418+
.forEach { cfg ->
419+
cfg.dependencies
420+
.filter { dep ->
421+
dep.group != null
422+
}.forEach { dep ->
423+
dependencies.add(
424+
DependencyInfo.fromGradleDep(
425+
groupId = dep.group!!,
426+
artifactId = dep.name,
427+
version = dep.version,
428+
),
429+
)
430+
}
431+
}
432+
}
433+
416434
/**
417435
* Discovers Expo projects in the current brownfield app project that are marked for publication.
418436
* @return List of ExpoGradleProjectProjection representing the discoverable Expo projects.
419437
*/
420438
protected fun getDiscoverableExpoProjects(): List<ExpoGradleProjectProjection> {
421439
val expoExtension =
422-
(
423-
brownfieldAppProject.rootProject.gradle.extensions.findByType(Class.forName("expo.modules.plugin.ExpoGradleExtension"))
424-
?: throw IllegalStateException(
425-
"Expo Gradle extension not found. This should never happen in an Expo project.",
426-
)
427-
)
440+
brownfieldAppProject.rootProject.gradle.extensions.findByType(Class.forName("expo.modules.plugin.ExpoGradleExtension"))
441+
?: error("Expo Gradle extension not found. This should never happen in an Expo project.")
428442

429443
// expoExtension.config
430444
val config =
@@ -460,42 +474,40 @@ open class ExpoPublishingHelper(val brownfieldAppProject: Project, val expoProje
460474
}
461475

462476
fun getPublishingInfo(expoGPProjection: ExpoGradleProjectProjection): BrownfieldPublishingInfo? {
463-
return (
464-
expoGPProjection.publication?.let {
465-
BrownfieldPublishingInfo(
466-
groupId = it.groupId,
467-
artifactId = it.artifactId,
468-
version = it.version,
469-
)
470-
} ?: run {
471-
val targetProject =
472-
brownfieldAppProject.rootProject.allprojects.firstOrNull {
473-
it.projectDir.absoluteFile.path == expoGPProjection.sourceDir
474-
}
475-
476-
if (targetProject == null) {
477-
return null
477+
return expoGPProjection.publication?.let {
478+
BrownfieldPublishingInfo(
479+
groupId = it.groupId,
480+
artifactId = it.artifactId,
481+
version = it.version,
482+
)
483+
} ?: run {
484+
val targetProject =
485+
brownfieldAppProject.rootProject.allprojects.firstOrNull {
486+
it.projectDir.absoluteFile.path == expoGPProjection.sourceDir
478487
}
479488

480-
val targetProjectAndroidLibExt =
481-
targetProject.extensions.getByType(LibraryExtension::class.java)
482-
483-
val packagePieces = targetProjectAndroidLibExt.namespace!!.split(".")
484-
val artifactId = packagePieces.last()
485-
// below: remove the trailing artifactId component -> leaves only the groupId components
486-
val groupId = packagePieces.dropLast(1).joinToString(".")
487-
488-
(
489-
BrownfieldPublishingInfo(
490-
groupId = groupId,
491-
artifactId = artifactId,
492-
version = (
493-
targetProjectAndroidLibExt.defaultConfig.versionName
494-
?: targetProject.version.toString()
495-
),
496-
)
497-
)
489+
if (targetProject == null) {
490+
return null
498491
}
499-
)
492+
493+
val targetProjectAndroidLibExt =
494+
targetProject.extensions.getByType(LibraryExtension::class.java)
495+
496+
val packagePieces = targetProjectAndroidLibExt.namespace!!.split(".")
497+
val artifactId = packagePieces.last()
498+
// below: remove the trailing artifactId component -> leaves only the groupId components
499+
val groupId = packagePieces.dropLast(1).joinToString(".")
500+
501+
(
502+
BrownfieldPublishingInfo(
503+
groupId = groupId,
504+
artifactId = artifactId,
505+
version = (
506+
targetProjectAndroidLibExt.defaultConfig.versionName
507+
?: targetProject.version.toString()
508+
),
509+
)
510+
)
511+
}
500512
}
501513
}

0 commit comments

Comments
 (0)