From c3c5cd04cc5e876e8e07409fe8601f0bd693c9ea Mon Sep 17 00:00:00 2001 From: Taryn Hill Date: Sat, 2 Aug 2014 20:31:08 -0500 Subject: [PATCH] Write trait requirements in documentation. --- OpenRA.Utility/Command.cs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/OpenRA.Utility/Command.cs b/OpenRA.Utility/Command.cs index 820a5726f4..2ac401676f 100644 --- a/OpenRA.Utility/Command.cs +++ b/OpenRA.Utility/Command.cs @@ -323,6 +323,25 @@ namespace OpenRA.Utility foreach (var line in traitDescLines) doc.AppendLine(line); + var requires = RequiredTraitTypes(t); + var reqCount = requires.Length; + if (reqCount > 0) + { + if (t.HasAttribute()) + doc.AppendLine("\n"); + + doc.Append("Requires trait{0}: ".F(reqCount > 1 ? "s" : "")); + + var i = 0; + foreach (var require in requires) + { + var n = require.Name; + var name = n.EndsWith("Info") ? n.Remove(n.Length - 4, 4) : n; + doc.Append("`{0}`{1}".F(name, i + 1 == reqCount ? ".\n" : ", ")); + i++; + } + } + var infos = FieldLoader.GetTypeLoadInfo(t); if (!infos.Any()) continue; @@ -362,6 +381,16 @@ namespace OpenRA.Utility .ToArray(); } + static Type[] RequiredTraitTypes(Type t) + { + return t.GetInterfaces() + .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(Requires<>)) + .SelectMany(i => i.GetGenericArguments()) + .Where(i => !i.IsInterface && !t.IsSubclassOf(i)) + .OrderBy(i => i.Name) + .ToArray(); + } + [Desc("MOD", "Generate Lua API documentation in MarkDown format.")] public static void ExtractLuaDocs(string[] args) {