rj-action-library-godot-dev.../Scripts/Doc-Generator/DocGenerator.cs

112 lines
2.4 KiB
C#

using Godot;
using Rokojori;
using System.Collections.Generic;
using System;
namespace Rokojori.DocGenerator
{
public class ClassTypeEntry
{
public Type type;
public string path;
}
public class DocGenerator
{
string path = "";
string outputPath = "";
List<string> icons = new List<string>();
public List<FilePath> classFiles = new List<FilePath>();
public List<ClassTypeEntry> classTypes = new List<ClassTypeEntry>();
public void Generate( string path, string outputPath, List<string> icons )
{
this.path = path;
this.outputPath = outputPath;
this.icons = icons;
GetFiles();
CreateTypesFromFiles();
IncludeDefaultTypes();
GenerateDocsFromTypes();
}
void GetFiles()
{
classFiles = FilesSync.GetFiles( path, f => f.fileExtension == ".cs", true );
}
void IncludeDefaultTypes()
{
var defaultTypes = new List<Type>()
{
typeof( RJNetworkNode ),
typeof( RJAction ),
typeof( RJSensor ),
typeof( RJSelector ),
typeof( RJSensor )
};
defaultTypes.ForEach(
( dt )=>
{
var entry = new ClassTypeEntry();
entry.type = dt;
entry.path = null;
classTypes.Add( entry);
}
);
}
void CreateTypesFromFiles()
{
var genericType = new EventSlot<int>().GetType();
var namespaceLabel = "Rokojori";
var assembly = genericType.Assembly;
classFiles.ForEach(
( cf )=>
{
var name = cf.fileName;
var type = ReflectionHelper.GetTypeByName( namespaceLabel + "." + name );
if ( type == null )
{
type = ReflectionHelper.GetTypeByNameFromAssembly( name, assembly );
}
if ( type != null )
{
var entry = new ClassTypeEntry();
entry.type = type;
entry.path = cf.fullPath;
classTypes.Add( entry);
}
}
);
}
void GenerateDocsFromTypes()
{
classTypes.ForEach(
( c )=>
{
var cdg = new ClassDocGenerator();
var cinfo = cdg.Create( c.path, c.type, icons );
var outputPath = FilePath.Join( this.outputPath, cinfo.name + ".json" );
FilesSync.SaveJSON( outputPath, cinfo );
}
);
}
}
}