Last active
January 14, 2016 17:52
-
-
Save stanch/12a180aa814bca7b5898 to your computer and use it in GitHub Desktop.
A quick-n-dirty way to convert Scala case class definitions into http://yuml.me/diagram/plain/class/draw class diagrams
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
#!/usr/bin/env python3 | |
import os | |
import re | |
import sys | |
import fnmatch | |
entities = {} | |
relations = {} | |
parents = {} | |
for root, dirnames, filenames in os.walk(sys.argv[1]): | |
for filename in fnmatch.filter(filenames, '*.scala'): | |
with open(os.path.join(root, filename)) as f: | |
content = f.read() | |
for m in re.finditer(r'trait (\w+)\s*(\{([^}]+)\})?\s+(extends (\w+))?', content, re.DOTALL): | |
name = m.group(1) | |
parent = m.group(5) | |
if parent: | |
parents[name] = parent | |
for m in re.finditer(r'case object (\w+)\s*(extends (\w+))?', content, re.DOTALL): | |
name = m.group(1) | |
parent = m.group(3) | |
if parent: | |
parents[name] = parent | |
for m in re.finditer(r'case class (\w+)\s*\(([^)]+)\)\s+(extends (\w+))?', content, re.DOTALL): | |
name = m.group(1) | |
body = re.sub(',\s+', ';', re.sub('//.+', '', m.group(2))).replace('[', '-').replace(']', '').strip() | |
parent = m.group(4) | |
entities[name] = body | |
relations[name] = set() | |
for n in re.finditer(r'[ -]([A-Z]\w+)[^.]', body): | |
relations[name].add(n.group(1)) | |
if parent: | |
parents[name] = parent | |
for k, v in entities.items(): | |
print("[{}|{}]".format(k, v)) | |
for k, v in parents.items(): | |
print("[{}]-^[{}]".format(k, v)) | |
for k, v in relations.items(): | |
for x in v: | |
if x in entities: | |
print("[{}]+->[{}]".format(k, x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment