Skip to content

Commit

Permalink
Add support for rootProject as a dependency (#233)
Browse files Browse the repository at this point in the history
* Add support for `rootProject` as a dependency

* Replace `allprojects` with `subprojects` + `rootProject` for backward compatibility

* Add FullProjectRootGradleTest

---------

Co-authored-by: Bruno Wieczorek <bruno.wieczorek@gomotive.com>
  • Loading branch information
burnoo and Bruno Wieczorek authored Sep 3, 2023
1 parent e41771c commit 2bce780
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object GradleDependencyGraphFactory {
}

private fun Project.listAllDependencies(configurationsToLook: Set<String>): List<Pair<String, List<String>>> {
return rootProject.subprojects
return (rootProject.subprojects + rootProject)
.map { project ->
project.moduleDisplayName() to project.configurations
.filter { configurationsToLook.contains(it.name) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import org.gradle.api.Project

object GradleModuleAliasExtractor {
fun extractModuleAliases(project: Project): Map<String, String> {
return project.rootProject.subprojects
val rootProject = project.rootProject
return (rootProject.subprojects + rootProject)
.mapNotNull { alias(it) }
.toMap()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.jraska.module.graph.assertion

import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File

class FullProjectRootGradleTest {
@get:Rule
val testProjectDir: TemporaryFolder = TemporaryFolder()

@Test
fun printsCorrectStatisticsForRootProjectWithDependency() {
testProjectDir.newFile("settings.gradle")
.writeText("include ':core'")

createRoot(content = """
plugins {
id 'com.jraska.module.graph.assertion'
}
apply plugin: 'java-library'
moduleGraphAssert {
maxHeight = 1
}
dependencies {
implementation project(":core")
}
""".trimIndent())

createModule(
"core", content = """
apply plugin: 'java-library'
"""
)

val output =
runGradleAssertModuleGraph(testProjectDir.root, "generateModulesGraphStatistics").output

assert(output.contains(("> Task :generateModulesGraphStatistics\n.*" +
"GraphStatistics\\(modulesCount=2, edgesCount=1, height=1, longestPath=\'root.* -> :core\'\\)").toRegex()))
}

@Test
fun printsCorrectStatisticsForIndependentRootProject() {
testProjectDir.newFile("settings.gradle")
.writeText("include ':app'")

createRoot(content = """
plugins {
id 'com.jraska.module.graph.assertion'
}
apply plugin: 'java-library'
moduleGraphAssert {
maxHeight = 0
}
""".trimIndent())

createModule(
"app", content = """
plugins {
id 'com.jraska.module.graph.assertion'
}
apply plugin: 'java-library'
moduleGraphAssert {
maxHeight = 0
}
"""
)

val output =
runGradleAssertModuleGraph(testProjectDir.root, "generateModulesGraphStatistics").output

assert(output.contains(("> Task :generateModulesGraphStatistics\n.*" +
"GraphStatistics\\(modulesCount=1, edgesCount=0, height=0, longestPath=\'root.*\'\\)\n\n" +
"> Task :app:generateModulesGraphStatistics\n+" +
"GraphStatistics\\(modulesCount=1, edgesCount=0, height=0, longestPath=\':app\'\\)").toRegex()))
}

private fun createRoot(content: String) {
File(testProjectDir.root, "build.gradle").writeText(content)
}

private fun createModule(dir: String, content: String) {
val newFolder = testProjectDir.newFolder(dir)
File(newFolder, "build.gradle").writeText(content)
}
}

0 comments on commit 2bce780

Please sign in to comment.