Skip to content

Commit 6f0e6a9

Browse files
committed
Mimic folderstructure rather than namespace for files and include full namespace for structure
1 parent 5e26d73 commit 6f0e6a9

4 files changed

Lines changed: 234 additions & 113 deletions

File tree

SchemaDocumentationGenerator/SchemaDocumentationGenerator/TypeGeneration/DocumentationLink.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static string GetNameAsLinkNonGeneric(this Type type, bool includeNamesp
5454
if (!includeNamespace || type.IsSystemNamespace())
5555
return nameWithLink;
5656

57-
string fullName = type.BHoMTypeFullName();
57+
string fullName = type.FullName;
5858
string[] fullNameSplit = fullName.Split('.');
5959
fullNameSplit[fullNameSplit.Length - 1] = nameWithLink;
6060
return string.Join(".", fullNameSplit);
@@ -91,17 +91,18 @@ private static string GetNameAsLink(this Type type)
9191

9292
/***************************************************/
9393

94-
private static string GetDocumentLink(this Type type)
94+
public static string GetDocumentLink(this Type type)
9595
{
9696
if (m_docLinks.ContainsKey(type))
9797
return m_docLinks[type];
9898

9999
if (type.Namespace.StartsWith("BH.oM"))
100100
{
101101
string categoryPath;
102-
if (Settings.AssemblyCategory.TryGetValue(type.Assembly.GetName().Name, out categoryPath))
102+
if (Settings.AssemblyCategory.TryGetValue(type.Assembly.GetName().Name, out categoryPath) && TryGetFileLink(type, out string fileLink))
103103
{
104-
string link = $"/api/oM/{categoryPath}/{type.BHoMTypeFullName().Replace("`", "%60").Replace(".", "/")}";
104+
string partLink = fileLink.Split('.')[0].Replace('\\', '/');
105+
string link = $"/api/oM/{categoryPath}/{type.BHoMMainNameSpace()}/{partLink}";
105106
m_docLinks[type] = link;
106107
return link;
107108
}
@@ -120,6 +121,22 @@ private static string GetDocumentLink(this Type type)
120121

121122
/***************************************************/
122123

124+
private static string BHoMMainNameSpace(this Type type)
125+
{
126+
string fullName = type.Namespace;
127+
string[] split = fullName.Split('.');
128+
if (split.Length < 3)
129+
return "";
130+
131+
IEnumerable<string> parts;
132+
if (split[2] == "Adapters")
133+
return $"{split[2]}.{split[3]}";
134+
else
135+
return split[2];
136+
}
137+
138+
/***************************************************/
139+
123140
private static bool IsSystemNamespace(this Type type)
124141
{
125142
return type.Namespace == "System" || type.Namespace.StartsWith("System.");

SchemaDocumentationGenerator/SchemaDocumentationGenerator/TypeGeneration/FileGeneration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ private static string GetFilePath(this Type type, string basePath)
2020
{
2121
if (type.Namespace.StartsWith("BH.oM"))
2222
{
23-
string categoryPath;
24-
if (Settings.AssemblyCategory.TryGetValue(type.Assembly.GetName().Name, out categoryPath))
23+
string link = type.GetDocumentLink();
24+
if (!string.IsNullOrEmpty(link))
2525
{
26-
return @$"{basePath}\oM\{categoryPath}\{type.BHoMTypeFullName().Replace(".", "\\")}.md";
26+
return $"{link.Replace("/api/", basePath).Replace("/", "\\")}.md";
2727
}
2828
}
2929

SchemaDocumentationGenerator/SchemaDocumentationGenerator/TypeGeneration/GithubLink.cs

Lines changed: 50 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -19,144 +19,88 @@ public static partial class TypeToMarkdown
1919

2020
private static string Links(this Type type)
2121
{
22-
string link = type.GithubLink();
23-
if (string.IsNullOrEmpty(link))
24-
return "";
25-
26-
string classWord = type.IsEnum ? "enum" : type.IsInterface ? "interface" : "class";
22+
string classWord = type.TypeWord();
2723

2824
string markdown = "## Code and Schema\n\n";
2925
markdown += "### C# implementation\n\n";
30-
markdown += $"The {classWord} is defined in C#. The class definition is available on github:\n\n";
31-
markdown += $"- {link}\n";
32-
markdown += "\nAll history and changes of the class can be found by inspection the history.\n";
3326

34-
return markdown;
35-
}
27+
markdown += "``` C# title=\"C#\"\n";
3628

37-
/***************************************************/
38-
39-
private static string GithubLink(this Type type)
40-
{
29+
markdown += $"public {classWord} {type.Name}";
30+
List<Type> baseTypes = type.GetBaseTypes();
31+
if (baseTypes.Count > 0)
32+
{
33+
List<string> names = baseTypes.Select(x => x.TypeNameBaseTypes()).ToList();
34+
int charCount = names.Sum(x => x.Length);
35+
string split = charCount > 120? ",\n" : ", ";
36+
markdown += $" : {string.Join(split, names)}";
37+
}
38+
markdown += "\n";
39+
markdown += "```\n\n";
40+
markdown += $"Assembly: {type.Assembly.GetName().Name}.dll\n\n";
4141

42-
if (Settings.AssemblyLink.TryGetValue(type.Assembly.GetName().Name, out string repoLink) && TryCheckObjectLink(type, out string objectLink))
42+
string link = type.GithubLink();
43+
if (!string.IsNullOrEmpty(link))
4344
{
44-
string link = $"{repoLink}/blob/develop/{type.Assembly.GetName().Name}/{objectLink}"; //Skip the first three parts of the namespace
45-
return $"[{type.Name.Split('`')[0]}.cs]({link})";
45+
markdown += $"The {classWord} is defined in C#. The class definition is available on github:\n\n";
46+
markdown += $"- {link}\n";
47+
markdown += "\nAll history and changes of the class can be found by inspection the history.\n";
4648
}
47-
return "";
49+
return markdown;
50+
51+
markdown += "### JSON Schema implementation\n\n";
52+
markdown += "The object is defined as a JSON schema, available on github:";
4853
}
4954

5055
/***************************************************/
5156

52-
private static bool TryCheckObjectLink(this Type type, out string objectLink)
57+
private static string TypeWord(this Type type)
5358
{
54-
//Method to find/validate the relative link to the file on github
59+
if (type.IsEnum)
60+
return "enum";
61+
if (type.IsInterface)
62+
return "interface";
63+
if (type.IsAbstract)
64+
return "abstract class";
65+
66+
return "class";
67+
}
5568

56-
//Get the assumed relative link based on type name and namespace
57-
string relativeLink = type.GetBaseLink();
58-
if (string.IsNullOrEmpty(relativeLink))
59-
{
60-
Console.WriteLine($"Unable to generate relative link for type {type.Name}");
61-
objectLink = "";
62-
return false;
63-
}
69+
/***************************************************/
6470

65-
string folder;
66-
//If cant find folder to check against, for now, assume ok and return relative link
67-
if (!m_AssemblyFolder.TryGetValue(type.Assembly, out folder))
71+
private static string TypeNameBaseTypes(this Type type)
72+
{
73+
if (type.IsGenericType)
6874
{
69-
objectLink = relativeLink;
70-
return true;
71-
}
75+
string genType = type.GetGenericTypeDefinition().FullName.Split('`')[0];
7276

73-
//Check if the assumed place that it for msot cases should be (namespace matching folder path)
74-
if (File.Exists(Path.Combine(folder, relativeLink)))
75-
{
76-
objectLink = relativeLink;
77-
return true;
78-
}
77+
List<string> args = new List<string>();
7978

80-
//If not, try scan folders matching the namespace, starting from inner subfolder and going up a level each time
81-
string[] relPathSplit = relativeLink.Split(Path.DirectorySeparatorChar);
82-
string fileName = relPathSplit.Last();
83-
for (int i = relPathSplit.Length - 1; i >= 0; i--)
84-
{
85-
string dir = Path.Combine(folder, Path.Combine(relPathSplit.Take(i).ToArray()));
86-
if (Directory.Exists(dir))
87-
{
88-
string[] files = Directory.GetFiles(dir, fileName, SearchOption.AllDirectories); //All directories to check if contained in subfolder. Checking for file amtching type name exactly. Should be the case and required compliance check
89-
if (files.Length == 1) //Single file found -> return
90-
{
91-
string[] fileSplit = files[0].Split(Path.DirectorySeparatorChar);
92-
objectLink = string.Join("/", fileSplit.Skip(folder.Split(Path.DirectorySeparatorChar).Length)); //Get rid of part leading up to the oM folder, and return the rest after. Link generation above handles all before.
93-
return true;
94-
}
79+
foreach (Type t in type.GetGenericArguments())
80+
{
81+
args.Add(t.TypeNameBaseTypes());
9582
}
96-
}
9783

98-
string[] allFiles = Directory.GetFiles(folder, fileName, SearchOption.AllDirectories); //Most likely not required and should be handled by final iteration above, but final scanning of all files in the folder
99-
if (allFiles.Length == 1)
100-
{
101-
string[] fileSplit = allFiles[0].Split(Path.DirectorySeparatorChar);
102-
objectLink = string.Join("/", fileSplit.Skip(folder.Split(Path.DirectorySeparatorChar).Length));
103-
return true;
84+
return $"{genType}<{string.Join(", ", args)}>";
10485
}
105-
106-
107-
Console.WriteLine($"Unable to find valid github link for {type.Name}.");
108-
109-
objectLink = "";
110-
return false;
111-
}
112-
113-
/***************************************************/
114-
115-
private static string GetBaseLink(this Type type)
116-
{
117-
string fullName = type.FullName.Split('`')[0];
118-
string[] split = fullName.Split('.');
119-
if (split.Length < 4)
120-
return "";
121-
122-
IEnumerable<string> parts;
123-
if (split[3] == "Adapters")
124-
parts = split.Skip(4);
12586
else
126-
parts = split.Skip(3);
127-
128-
return string.Join(Path.DirectorySeparatorChar, parts) + ".cs";
87+
return type.FullName;
12988
}
13089

13190
/***************************************************/
13291

133-
public static void SetupAssemblyFolders(string gitFolderPath, List<Assembly> assemblies)
134-
{
135-
List<Assembly> assembliesCopy = assemblies.ToList();
136-
137-
foreach (string repo in Directory.GetDirectories(gitFolderPath))
138-
{
139-
foreach (string projFolder in Directory.GetDirectories(repo))
140-
{
141-
string dirName = Path.GetFileName(projFolder);
142-
Assembly assembly = assemblies.FirstOrDefault(a => a.GetName().Name == dirName);
143-
144-
if (assembly != null)
145-
m_AssemblyFolder[assembly] = projFolder;
146-
}
147-
}
92+
private static string GithubLink(this Type type)
93+
{
14894

149-
if (m_AssemblyFolder.Count != assemblies.Count)
95+
if (Settings.AssemblyLink.TryGetValue(type.Assembly.GetName().Name, out string repoLink) && TryGetFileLink(type, out string objectLink))
15096
{
151-
Console.WriteLine($"Unable to find a filepath for git link checking for {string.Join(", ", assemblies.Except(m_AssemblyFolder.Keys))}.");
97+
string link = $"{repoLink}/blob/develop/{type.Assembly.GetName().Name}/{objectLink}"; //Skip the first three parts of the namespace
98+
return $"[{type.Name.Split('`')[0]}.cs]({link})";
15299
}
153-
100+
return "";
154101
}
155102

156103
/***************************************************/
157104

158-
private static Dictionary<Assembly, string> m_AssemblyFolder = new Dictionary<Assembly, string>();
159-
160-
/***************************************************/
161105
}
162106
}

0 commit comments

Comments
 (0)