Skip to content

Commit beaa494

Browse files
authored
Error handling for Assembly & Namespace mismatch
1 parent 7dc6e2a commit beaa494

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

Editor/MarkdownGenerator.cs

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@
88
using UnityEngine;
99
using UnityEditor;
1010

11-
/*
12-
MIT License
13-
14-
Copyright (c) 2023 Kitbashery
15-
16-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
17-
18-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
19-
20-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21-
*/
22-
2311
/// <summary>
2412
/// Generates Github Markdown from a script.
2513
/// </summary>
@@ -38,30 +26,39 @@ private static void GenerateMarkdownDocumentation()
3826
string scriptName = Path.GetFileNameWithoutExtension(scriptPath);
3927
string scriptText = AssetDatabase.LoadAssetAtPath<TextAsset>(scriptPath).text;
4028
Assembly scriptAssembly = GetScriptAssembly(scriptPath);
29+
string fullTypeName = scriptAssembly.GetName().Name + "." + scriptName;
30+
Type scriptType = scriptAssembly.GetType(fullTypeName);
4131

42-
string markdown = "# " + scriptName + ":\n";
43-
// Get class summary.
44-
markdown += GetClassSummary(scriptText, scriptName) + "\n";
45-
46-
// Get class namespace.
47-
string scriptNamespace = scriptAssembly.GetType(scriptName).Namespace;
48-
if (!string.IsNullOrEmpty(scriptNamespace))
32+
if (scriptType != null)
4933
{
50-
markdown += "### Namespace:\n";
51-
markdown += scriptNamespace + "\n";
52-
}
34+
string markdown = "# " + scriptName + ":\n";
35+
// Get class summary.
36+
markdown += GetClassSummary(scriptText, scriptName) + "\n";
5337

54-
markdown += "## Properties:\n\n";
55-
markdown += GetPropertiesTable(scriptAssembly, scriptName, scriptText) + "\n\n";
38+
// Get class namespace.
39+
string scriptNamespace = scriptType.Namespace;
40+
if (!string.IsNullOrEmpty(scriptNamespace))
41+
{
42+
markdown += "### Namespace:\n";
43+
markdown += scriptNamespace + "\n\n";
44+
}
45+
46+
markdown += "## Properties:\n\n";
47+
markdown += GetPropertiesTable(scriptType, scriptName, scriptText) + "\n\n";
5648

57-
markdown += "## Methods:\n\n";
58-
markdown += GetMethodTable(scriptAssembly, scriptName, scriptText) + "\n";
49+
markdown += "## Methods:\n\n";
50+
markdown += GetMethodTable(scriptType, scriptName, scriptText) + "\n";
5951

60-
string outputPath = Path.GetDirectoryName(scriptPath) + "/" + scriptName + ".md";
61-
File.WriteAllText(outputPath, markdown);
62-
AssetDatabase.Refresh();
52+
string outputPath = Path.GetDirectoryName(scriptPath) + "/" + scriptName + ".md";
53+
File.WriteAllText(outputPath, markdown);
54+
AssetDatabase.Refresh();
6355

64-
Debug.Log("Markdown documentation generated at " + outputPath);
56+
Debug.Log("Markdown documentation generated at " + outputPath);
57+
}
58+
else
59+
{
60+
Debug.LogWarningFormat("Script type: {0} not found in {1} make sure the assembly name matches the namespace containing the class.", scriptName, scriptAssembly.FullName);
61+
}
6562
}
6663

6764
public static string GetClassSummary(string scriptText, string className)
@@ -135,10 +132,9 @@ public static Assembly GetScriptAssembly(string assetPath)
135132
return null;
136133
}
137134

138-
public static string GetPropertiesTable(Assembly assembly, string className, string scriptText)
135+
public static string GetPropertiesTable(Type scriptType, string className, string scriptText)
139136
{
140-
var type = assembly.GetType(className);
141-
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
137+
var properties = scriptType.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
142138

143139
var markdownTable = "| Type | Property Name | Summary | Default Value |\n| --- | --- | --- | --- |\n";
144140
foreach (var property in properties)
@@ -164,7 +160,7 @@ public static string GetPropertiesTable(Assembly assembly, string className, str
164160
markdownTable += $"| {propertyType} | {propertyName} | {summary} | {defaultValue} |\n";
165161
}
166162

167-
var fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
163+
var fields = scriptType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
168164
foreach (var field in fields)
169165
{
170166
if (field.IsPrivate || field.IsInitOnly)
@@ -193,13 +189,10 @@ public static string GetPropertiesTable(Assembly assembly, string className, str
193189
return markdownTable;
194190
}
195191

196-
public static string GetMethodTable(Assembly assembly, string className, string scriptText)
192+
public static string GetMethodTable(Type scriptType, string className, string scriptText)
197193
{
198-
// Get class type
199-
Type type = assembly.GetType(className);
200-
201194
// Get methods
202-
MethodInfo[] methods = type.GetMethods();
195+
MethodInfo[] methods = scriptType.GetMethods();
203196

204197
// Build table header
205198
StringBuilder table = new StringBuilder("| Method | Summary | Parameters | Returns |\n");

0 commit comments

Comments
 (0)