-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateApiDocs.py
80 lines (62 loc) · 2.62 KB
/
generateApiDocs.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import subprocess, os, shutil, sys, tempfile
from argparse import ArgumentParser
from os import path
if (__name__ == "__main__"):
argParser = ArgumentParser(description = "Generate API documentation")
argParser.add_argument("--docfx-path", help = "Path to docfx.exe")
argParser.add_argument("--output", help = "Path to generated API documentation", required = True)
parsedArgs = argParser.parse_args()
docfxPath = parsedArgs.docfx_path or shutil.which("docfx")
if (docfxPath is None):
raise RuntimeError("Unable to find docfx. Specify docfx.exe location with --docfx-path if not on PATH.")
tempDir = tempfile.mkdtemp()
try:
with open(path.join(tempDir, "docfx.json"), "w") as configFile:
# Create minimal docfx.json as we will pass all options as command line arguments
configFile.write('{"build": { }, "metadata": { }}')
with open(".gitignore", "r") as gitignoreFile:
gitignores = {
line
for line in (_line.strip() for _line in gitignoreFile.readlines())
if len(line) != 0 and line.find('/') == -1 and line.find('*') == -1
}
# Find all .csproj files
projects = []
for subdir in os.scandir("."):
if (subdir.name.endswith(".Tests") or subdir.name in gitignores):
# Exclude test projects and those in .gitignore
continue
csprojPath = path.join(subdir.name, subdir.name + ".csproj")
if (not path.exists(csprojPath)):
# Directory is not a project
continue
projects.append(path.abspath(csprojPath))
process = subprocess.run(
[docfxPath, "metadata", "--output", "__api", *projects],
shell = False,
stdout = sys.stdout,
stderr = sys.stderr,
check = True,
cwd = tempDir
)
shutil.move(path.join(tempDir, "__api", "__api"), path.join(tempDir, "api"))
process = subprocess.run(
[
docfxPath,
"build",
"--content", "api/**.yml",
"--template", "statictoc",
"--output", "site",
"--markdownEngineName", "markdig"
],
shell = False,
stdout = sys.stdout,
stderr = sys.stderr,
check = True,
cwd = tempDir
)
if (path.exists(parsedArgs.output)):
shutil.rmtree(parsedArgs.output)
shutil.move(path.join(tempDir, "site"), parsedArgs.output)
finally:
shutil.rmtree(tempDir)