Fixed indentation on docs generator project

This commit is contained in:
EvilFactory
2023-02-04 14:20:55 -03:00
parent 81970b3fd4
commit 14c45e4900
2 changed files with 343 additions and 283 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ end_of_line = lf
[*.cs] [*.cs]
indent_style = space indent_style = space
indent_size = 2 indent_size = 4
csharp_prefer_braces = when_multiline:warning csharp_prefer_braces = when_multiline:warning
csharp_indent_case_contents_when_block = false csharp_indent_case_contents_when_block = false
# One True Brace style # One True Brace style
+342 -282
View File
@@ -6,190 +6,226 @@ using System.Text.RegularExpressions;
using Barotrauma; using Barotrauma;
using Barotrauma.Networking; using Barotrauma.Networking;
string NormalizeGenericTypeName(string s) { string NormalizeGenericTypeName(string s)
var idx = s.LastIndexOf('`'); {
var idx = s.LastIndexOf('`');
if (idx != -1) { if (idx != -1)
return s[..idx]; {
} return s[..idx];
}
return s; return s;
} }
string TypeToString(Type type, bool useLuaTypes = true) { string TypeToString(Type type, bool useLuaTypes = true)
// Return "T" for unresolved type params {
if (type.IsGenericParameter) { // Return "T" for unresolved type params
return type.Name; if (type.IsGenericParameter)
} {
return type.Name;
var genericType = type.IsGenericType
? type.GetGenericTypeDefinition()
: null;
if (type == typeof(bool)) {
return "bool";
}
if (type == typeof(string)) {
return "string";
}
if (useLuaTypes) {
if (type == typeof(sbyte)
|| type == typeof(byte)
|| type == typeof(short)
|| type == typeof(ushort)
|| type == typeof(int)
|| type == typeof(uint)
|| type == typeof(long)
|| type == typeof(ulong)
|| type == typeof(float)
|| type == typeof(double)) {
return "number";
} }
if (genericType == typeof(List<>) var genericType = type.IsGenericType
|| genericType == typeof(Dictionary<,>)) { ? type.GetGenericTypeDefinition()
return "table"; : null;
if (type == typeof(bool))
{
return "bool";
} }
if (genericType == typeof(Action<,>) if (type == typeof(string))
|| genericType == typeof(Func<,>)) { {
return "function"; return "string";
} }
}
var nsToRemove = new[] { if (useLuaTypes)
{
if (type == typeof(sbyte)
|| type == typeof(byte)
|| type == typeof(short)
|| type == typeof(ushort)
|| type == typeof(int)
|| type == typeof(uint)
|| type == typeof(long)
|| type == typeof(ulong)
|| type == typeof(float)
|| type == typeof(double))
{
return "number";
}
if (genericType == typeof(List<>)
|| genericType == typeof(Dictionary<,>))
{
return "table";
}
if (genericType == typeof(Action<,>)
|| genericType == typeof(Func<,>))
{
return "function";
}
}
var nsToRemove = new[] {
"Barotrauma", "Barotrauma",
"System", "System",
"System.Collections", "System.Collections",
"System.Collections.Generic", "System.Collections.Generic",
}; };
string Namespaced(string typeName) { string Namespaced(string typeName)
if (type.Namespace == null) { {
return typeName; if (type.Namespace == null)
} {
return typeName;
// Full namespace match
if (nsToRemove.Contains(type.Namespace)) {
return typeName;
}
// Partial namespace match
foreach (var ns in nsToRemove) {
if (ns == type.Namespace) {
return typeName;
}
if (type.Namespace.StartsWith(ns + ".")) {
var shortNs = type.Namespace.Remove(0, ns.Length + 1);
return $"{shortNs}.{typeName}";
}
}
return $"{type.Namespace}.{typeName}";
}
string Impl(string? ns) {
if (type.IsGenericType) {
var genericTypeDef = type.GetGenericTypeDefinition();
var genericTypeName = NormalizeGenericTypeName(genericTypeDef.Name);
var genericArgs = type.GetGenericArguments();
// Use the `T?` notation instead of Nullable<T>
if (genericTypeDef == typeof(Nullable<>)) {
// ldoc supports the "?string" notation, which expands to "?|nil|string"
if (useLuaTypes) {
return Namespaced("?" + TypeToString(genericArgs[0], useLuaTypes: false));
} else {
return Namespaced(TypeToString(genericArgs[0], useLuaTypes: false) + "?");
} }
}
var sb = new StringBuilder(); // Full namespace match
sb.Append(genericTypeName); if (nsToRemove.Contains(type.Namespace))
sb.Append("<"); {
foreach (var genericArgType in genericArgs) { return typeName;
sb.Append(TypeToString(genericArgType, useLuaTypes: false)); }
sb.Append(",");
}
// Remove the last separator
sb.Length--;
sb.Append(">");
return Namespaced(sb.ToString()); // Partial namespace match
foreach (var ns in nsToRemove)
{
if (ns == type.Namespace)
{
return typeName;
}
if (type.Namespace.StartsWith(ns + "."))
{
var shortNs = type.Namespace.Remove(0, ns.Length + 1);
return $"{shortNs}.{typeName}";
}
}
return $"{type.Namespace}.{typeName}";
} }
return Namespaced(type.Name); string Impl(string? ns)
} {
if (type.IsGenericType)
{
var genericTypeDef = type.GetGenericTypeDefinition();
var genericTypeName = NormalizeGenericTypeName(genericTypeDef.Name);
return Impl(type.Namespace); var genericArgs = type.GetGenericArguments();
}
static (bool Success, string Output, string Error) TryRunGitCommand(string args) { // Use the `T?` notation instead of Nullable<T>
static string? GetGitBinary() { if (genericTypeDef == typeof(Nullable<>))
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { {
return Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process) // ldoc supports the "?string" notation, which expands to "?|nil|string"
?.Split(';') if (useLuaTypes)
.Select(x => Path.Join(x, "git.exe")) {
.FirstOrDefault(File.Exists); return Namespaced("?" + TypeToString(genericArgs[0], useLuaTypes: false));
} else { }
return Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process) else
?.Split(':') {
.Select(x => Path.Join(x, "git")) return Namespaced(TypeToString(genericArgs[0], useLuaTypes: false) + "?");
.FirstOrDefault(File.Exists); }
}
var sb = new StringBuilder();
sb.Append(genericTypeName);
sb.Append("<");
foreach (var genericArgType in genericArgs)
{
sb.Append(TypeToString(genericArgType, useLuaTypes: false));
sb.Append(",");
}
// Remove the last separator
sb.Length--;
sb.Append(">");
return Namespaced(sb.ToString());
}
return Namespaced(type.Name);
} }
}
var gitBinary = GetGitBinary(); return Impl(type.Namespace);
if (gitBinary == null) {
throw new InvalidOperationException("Failed to find git binary in PATH");
}
using var process = Process.Start(new ProcessStartInfo(gitBinary, args) {
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
});
if (process == null)
throw new InvalidOperationException($"Failed to run git command: {args}");
process.Start();
var stdOut = process.StandardOutput.ReadToEndAsync();
var stdErr = process.StandardError.ReadToEndAsync();
Task.WhenAll(stdOut, stdErr).GetAwaiter().GetResult();
process.WaitForExit();
return (process.ExitCode == 0, stdOut.Result.TrimEnd('\r', '\n'), stdErr.Result);
} }
var gitDir = (new Func<String>(() => { static (bool Success, string Output, string Error) TryRunGitCommand(string args)
var (success, gitDir, error) = TryRunGitCommand("rev-parse --show-toplevel"); {
if (!success) { static string? GetGitBinary()
throw new InvalidDataException($"Failed to determine the root of the git repo: {error}"); {
} if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process)
?.Split(';')
.Select(x => Path.Join(x, "git.exe"))
.FirstOrDefault(File.Exists);
}
else
{
return Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Process)
?.Split(':')
.Select(x => Path.Join(x, "git"))
.FirstOrDefault(File.Exists);
}
}
return gitDir; var gitBinary = GetGitBinary();
if (gitBinary == null)
{
throw new InvalidOperationException("Failed to find git binary in PATH");
}
using var process = Process.Start(new ProcessStartInfo(gitBinary, args)
{
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
});
if (process == null)
throw new InvalidOperationException($"Failed to run git command: {args}");
process.Start();
var stdOut = process.StandardOutput.ReadToEndAsync();
var stdErr = process.StandardError.ReadToEndAsync();
Task.WhenAll(stdOut, stdErr).GetAwaiter().GetResult();
process.WaitForExit();
return (process.ExitCode == 0, stdOut.Result.TrimEnd('\r', '\n'), stdErr.Result);
}
var gitDir = (new Func<String>(() =>
{
var (success, gitDir, error) = TryRunGitCommand("rev-parse --show-toplevel");
if (!success)
{
throw new InvalidDataException($"Failed to determine the root of the git repo: {error}");
}
return gitDir;
}))(); }))();
void GenerateDocsImpl(Type type, string baseFile, string outFile, string? categoryName = null) { void GenerateDocsImpl(Type type, string baseFile, string outFile, string? categoryName = null)
categoryName ??= type.Name; {
var sb = new StringBuilder(); categoryName ??= type.Name;
var sb = new StringBuilder();
Console.WriteLine($"Generating docs for {type}"); Console.WriteLine($"Generating docs for {type}");
string baseLuaText; string baseLuaText;
try { try
baseLuaText = File.ReadAllText(baseFile); {
} catch (FileNotFoundException) { baseLuaText = File.ReadAllText(baseFile);
baseLuaText = @$"-- luacheck: ignore 111 }
catch (FileNotFoundException)
{
baseLuaText = @$"-- luacheck: ignore 111
--[[-- --[[--
{type.FullName} {type.FullName}
@@ -198,148 +234,172 @@ void GenerateDocsImpl(Type type, string baseFile, string outFile, string? catego
-- @pragma nostrip -- @pragma nostrip
local {type.Name} = {{}}".ReplaceLineEndings("\n"); local {type.Name} = {{}}".ReplaceLineEndings("\n");
File.WriteAllText(baseFile, baseLuaText); File.WriteAllText(baseFile, baseLuaText);
}
var removeTagPattern = new Regex("^-- @remove (.*)$", RegexOptions.Multiline);
var removed = new HashSet<string>();
var matches = removeTagPattern.Matches(baseLuaText);
foreach (var match in matches.Cast<Match>()) {
removed.Add(match.Value);
}
sb.Append(baseLuaText);
sb.AppendLine();
sb.AppendLine();
var members = type.GetMembers(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
foreach (var member in members) {
static string EscapeName(string n) {
return n switch {
"end" => "endparam",
_ => n
};
} }
switch (member.MemberType) { var removeTagPattern = new Regex("^-- @remove (.*)$", RegexOptions.Multiline);
case MemberTypes.Method: { var removed = new HashSet<string>();
var method = (MethodInfo)member; var matches = removeTagPattern.Matches(baseLuaText);
// Exclude property getters/setters foreach (var match in matches.Cast<Match>())
if (method.IsSpecialName) { {
continue; removed.Add(match.Value);
}
var paramNames = new StringBuilder();
foreach (var parameter in method.GetParameters()) {
paramNames.Append(EscapeName(parameter.Name!));
paramNames.Append(", ");
}
if (paramNames.Length > 0) {
// Remove the last separator
paramNames.Length -= 2;
}
string functionDecoration;
if (method.IsStatic) {
functionDecoration = $"function {type.Name}.{method.Name}({paramNames}) end";
} else {
functionDecoration = $"function {method.Name}({paramNames}) end";
}
if (removed.Contains(functionDecoration)) {
continue;
}
Console.WriteLine($" - METHOD: {method}");
sb.AppendLine($"--- {method.Name}");
sb.AppendLine("-- @realm shared");
foreach (var parameter in method.GetParameters()) {
sb.AppendLine($"-- @tparam {TypeToString(parameter.ParameterType)} {EscapeName(parameter.Name!)}");
}
if (method.ReturnType != typeof(void)) {
sb.AppendLine($"-- @treturn {TypeToString(method.ReturnType)}");
}
sb.AppendLine(functionDecoration);
sb.AppendLine();
break;
}
case MemberTypes.Field: {
var field = (FieldInfo)member;
var name = EscapeName(field.Name);
var returnName = TypeToString(field.FieldType);
if (field.IsStatic) {
name = type.Name + "." + field.Name;
}
if (removed.Contains(name)) {
continue;
}
Console.WriteLine($" - FIELD: {name}");
sb.AppendLine("---");
sb.Append("-- ");
sb.Append(name);
sb.AppendLine($", field of type {returnName}");
sb.AppendLine("-- @realm shared");
sb.AppendLine($"-- @field {name}");
sb.AppendLine();
break;
}
case MemberTypes.Property: {
var property = (PropertyInfo)member;
var name = EscapeName(property.Name);
var returnName = TypeToString(property.PropertyType);
if (property.GetGetMethod()?.IsStatic == true || property.GetSetMethod()?.IsStatic == true) {
name = type.Name + "." + property.Name;
}
if (removed.Contains(name)) {
continue;
}
Console.WriteLine($" - PROPERTY: {name}");
sb.AppendLine("---");
sb.Append("-- ");
sb.Append(name);
sb.AppendLine($", field of type {returnName}");
sb.AppendLine("-- @realm shared");
sb.AppendLine($"-- @field {name}");
sb.AppendLine();
break;
}
} }
}
new FileInfo(outFile).Directory.Create(); sb.Append(baseLuaText);
File.WriteAllText(outFile, sb.ToString()); sb.AppendLine();
sb.AppendLine();
var members = type.GetMembers(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
foreach (var member in members)
{
static string EscapeName(string n)
{
return n switch
{
"end" => "endparam",
_ => n
};
}
switch (member.MemberType)
{
case MemberTypes.Method:
{
var method = (MethodInfo)member;
// Exclude property getters/setters
if (method.IsSpecialName)
{
continue;
}
var paramNames = new StringBuilder();
foreach (var parameter in method.GetParameters())
{
paramNames.Append(EscapeName(parameter.Name!));
paramNames.Append(", ");
}
if (paramNames.Length > 0)
{
// Remove the last separator
paramNames.Length -= 2;
}
string functionDecoration;
if (method.IsStatic)
{
functionDecoration = $"function {type.Name}.{method.Name}({paramNames}) end";
}
else
{
functionDecoration = $"function {method.Name}({paramNames}) end";
}
if (removed.Contains(functionDecoration))
{
continue;
}
Console.WriteLine($" - METHOD: {method}");
sb.AppendLine($"--- {method.Name}");
sb.AppendLine("-- @realm shared");
foreach (var parameter in method.GetParameters())
{
sb.AppendLine($"-- @tparam {TypeToString(parameter.ParameterType)} {EscapeName(parameter.Name!)}");
}
if (method.ReturnType != typeof(void))
{
sb.AppendLine($"-- @treturn {TypeToString(method.ReturnType)}");
}
sb.AppendLine(functionDecoration);
sb.AppendLine();
break;
}
case MemberTypes.Field:
{
var field = (FieldInfo)member;
var name = EscapeName(field.Name);
var returnName = TypeToString(field.FieldType);
if (field.IsStatic)
{
name = type.Name + "." + field.Name;
}
if (removed.Contains(name))
{
continue;
}
Console.WriteLine($" - FIELD: {name}");
sb.AppendLine("---");
sb.Append("-- ");
sb.Append(name);
sb.AppendLine($", field of type {returnName}");
sb.AppendLine("-- @realm shared");
sb.AppendLine($"-- @field {name}");
sb.AppendLine();
break;
}
case MemberTypes.Property:
{
var property = (PropertyInfo)member;
var name = EscapeName(property.Name);
var returnName = TypeToString(property.PropertyType);
if (property.GetGetMethod()?.IsStatic == true || property.GetSetMethod()?.IsStatic == true)
{
name = type.Name + "." + property.Name;
}
if (removed.Contains(name))
{
continue;
}
Console.WriteLine($" - PROPERTY: {name}");
sb.AppendLine("---");
sb.Append("-- ");
sb.Append(name);
sb.AppendLine($", field of type {returnName}");
sb.AppendLine("-- @realm shared");
sb.AppendLine($"-- @field {name}");
sb.AppendLine();
break;
}
}
}
new FileInfo(outFile).Directory.Create();
File.WriteAllText(outFile, sb.ToString());
} }
var basePath = $"{gitDir}/luacs-docs/lua"; var basePath = $"{gitDir}/luacs-docs/lua";
var generatedDir = $"{basePath}/lua/generated"; var generatedDir = $"{basePath}/lua/generated";
var baseLuaDir = $"{basePath}/baseluadocs"; var baseLuaDir = $"{basePath}/baseluadocs";
void GenerateDocs(Type type, string file, string? categoryName = null) { void GenerateDocs(Type type, string file, string? categoryName = null)
GenerateDocsImpl(type, $"{baseLuaDir}/{file}", $"{generatedDir}/{file}", categoryName); {
GenerateDocsImpl(type, $"{baseLuaDir}/{file}", $"{generatedDir}/{file}", categoryName);
} }
try { try
Directory.Delete(generatedDir, true); {
} catch (DirectoryNotFoundException) { } Directory.Delete(generatedDir, true);
}
catch (DirectoryNotFoundException) { }
Directory.CreateDirectory(generatedDir); Directory.CreateDirectory(generatedDir);
Directory.CreateDirectory(baseLuaDir); Directory.CreateDirectory(baseLuaDir);