-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (40 loc) · 1.88 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
# extract the data we need to build a usage/implementation graph of a typescript project
def extractImports(lines, includeFunctions=False):
imports = []
for line in lines:
if line.startswith("import") and "{" in line and "}" in line:
imported = [s.strip() for s in line.split("{")[1].split("}")[0].split(",")]
imported = [s for s in imported if includeFunctions or (len(s) > 0) and s[0].isupper()]
imports.extend(imported)
return imports
def genericInheritanceExtractor(lines, keyword):
implementations = []
for line in lines:
if "class" in line and keyword in line:
segments = line.split(" ")
className = segments[segments.index("class") + 1]
interfaceNames = [s.strip() for s in line.split(keyword)[1].split(",")]
if "{" in interfaceNames[-1]:
interfaceNames[-1] = interfaceNames[-1].split("{")[0].strip()
implementations.append((className, interfaceNames))
return implementations
def extractImplementations(lines):
return genericInheritanceExtractor(lines, "implements")
def extractExtends(lines):
return genericInheritanceExtractor(lines, "extends")
def recursivelyGetAllSourceFiles(rootFolder):
sourceFiles = []
for root, dirs, files in os.walk(rootFolder):
for file in files:
if file.endswith(".ts"):
sourceFiles.append(os.path.join(root, file))
return sourceFiles
files = recursivelyGetAllSourceFiles(os.path.join(os.getcwd(), "src"))
for file in files:
fileContent = open(file, "r", encoding="utf8").readlines()
imports = extractImports(fileContent)
implementations = extractImplementations(fileContent)
extends = extractExtends(fileContent)
if len(implementations) > 0:
print(file, "\nIMPORT", imports, "\nIMPLEMENT", implementations, "\nEXTENDS", extends, "\n")