Skip to content

Commit

Permalink
add a python script to generate a dependency graph for the readme (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
nisrulz authored Feb 17, 2024
1 parent c5d3bab commit 5b24ed1
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ Icon
Network Trash Folder
Temporary Items
.apdisk

# Dependency Graph
dependency_graph
6 changes: 6 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ An Offline first Android app to consume the SpaceX Backend API [`https://github.
- [Gradle Convention Plugins](https://docs.gradle.org/current/samples/sample_convention_plugins.html) | PR [#5](https://github.com/nisrulz/android-spacex-app/pull/5) [#7](https://github.com/nisrulz/android-spacex-app/pull/7)
- [Github Actions](https://docs.github.com/en/actions) | [Workflows used](.github/workflows)

## Dependency Graph

<img src="dependency_graph.svg" alt="Dependency Graph" />

> To generate this graph, simply run `python3 generate_dependency_graph.py` in the root directory.
## Project Requirements

- Java 11+
Expand Down
147 changes: 147 additions & 0 deletions dependency_graph.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions generate_dependency_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
from pathlib import Path
import re
from graphviz import Digraph

# Root directory of the Android project i.e Current Directory
root_dir = os.getcwd()

# Find all build.gradle files in the project directory and its subdirectories
build_files = [str(f) for f in Path(root_dir).rglob("build.gradle.kts")]

# Create a dictionary to store the dependencies graph
dependencies_graph = {}

# Define a function to extract dependencies from a build.gradle file
def extract_dependencies(file_path):
with open(file_path, 'r') as file:
content = file.read()
dependencies_block_match = re.search(r'dependencies\s*{([^}]*)}', content)
if dependencies_block_match:
dependencies_block = dependencies_block_match.group(1)
dependencies = re.findall(r'\(([^)]*)\)', dependencies_block)
return [dep.replace("projects.", "") for dep in dependencies if dep.startswith("projects.")]
else:
return []

# Populate the dependencies graph
for build_file in build_files:
module = os.path.dirname(build_file).replace(root_dir, '').lstrip('/').replace('/', '.')
dependencies_graph[module] = extract_dependencies(build_file)

# Create a Digraph object
dot = Digraph(format='svg')
dot.attr('node', shape='box3d', style='filled', fillcolor='lightblue')
dot.attr('graph', splines='polyline')

# Add nodes and edges to the graph
for module, deps in dependencies_graph.items():
for dep in deps:
dot.edge(module, dep)

# Save the graph to a SVG file
dot.render('dependency_graph')

0 comments on commit 5b24ed1

Please sign in to comment.