Created
March 12, 2023 08:09
-
-
Save madroidmaq/c1243312745f5d56f546b62968f41f44 to your computer and use it in GitHub Desktop.
Create a dependency diagram for the current project using the syntax of plantUML, with a result that looks like the following.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task projectDependencyGraph { | |
doLast { | |
def plantuml = new File(rootProject.buildDir, 'reports/dependency-graph/project.puml') | |
plantuml.parentFile.mkdirs() | |
plantuml.delete() | |
plantuml << '@startuml\n' | |
plantuml << "!define blue #00E489\n" | |
plantuml << "!define origin #FF8A65\n" | |
plantuml << "!define green #1A73E8\n" | |
plantuml << "!define dark #073042\n" | |
plantuml << "\npackage ${rootProject.name} {\n" | |
def rootProjects = [] | |
def queue = [rootProject] | |
while (!queue.isEmpty()) { | |
def project = queue.remove(0) | |
rootProjects.add(project) | |
queue.addAll(project.childProjects.values()) | |
} | |
def projects = new LinkedHashSet<Project>() | |
def dependencies = new LinkedHashMap<Tuple2<Project, Project>, List<String>>() | |
queue = [rootProject] | |
while (!queue.isEmpty()) { | |
def project = queue.remove(0) | |
queue.addAll(project.childProjects.values()) | |
project.configurations.all { config -> | |
if (!config.name.endsWith('implementation')) { | |
return | |
} | |
config.dependencies | |
.withType(ProjectDependency) | |
.collect { it.dependencyProject } | |
.each { dependency -> | |
projects.add(project) | |
projects.add(dependency) | |
rootProjects.remove(dependency) | |
def graphKey = new Tuple2<Project, Project>(project, dependency) | |
dependencies.computeIfAbsent(graphKey) { new ArrayList<String>() } | |
} | |
} | |
} | |
projects = projects.sort { it.path } | |
plantuml << '\n' | |
LinkedHashMap<String, LinkedHashSet<Project>> map = new LinkedHashMap<String, LinkedHashSet<Project>>() | |
projects.forEach { | |
def tempList = it.path.split(":") | |
if (tempList.length > 2) { | |
def groupName = tempList[1] | |
if (map[groupName] == null) { | |
map[groupName] = new LinkedHashSet<Project>() | |
} | |
map[groupName].add(it) | |
} else { | |
if (map[""] == null) { | |
map[""] = new LinkedHashSet<Project>() | |
} | |
map[""].add(it) | |
} | |
} | |
def colors = ["blue", "origin", "green", "dark"] | |
def colorIndex = 0 | |
map.forEach { key, values -> | |
if (key == "") { | |
values.forEach { | |
plantuml << " [${it.path}]\n" | |
} | |
} else { | |
def color = colors[colorIndex % colors.size()] | |
plantuml << " frame ${key} $color {\n" | |
values.forEach { | |
plantuml << " [${it.path}]\n" | |
} | |
plantuml << " }\n" | |
colorIndex++ | |
} | |
} | |
plantuml << "}\n" | |
plantuml << '\n' | |
dependencies.findAll { key, traits -> | |
return key.first.path != key.second.path | |
}.forEach { key, traits -> | |
plantuml << "[${key.first.path}] --> [${key.second.path}]" | |
plantuml << '\n' | |
} | |
plantuml << '@enduml\n' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
以 Now in Android 项目为例,效果图如下:

对应的 puml 文件内容如下: