Skip to content

Commit

Permalink
Using ZeroBrane's "inherits" feature to get rid of redundancies
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSWolf committed Mar 27, 2014
1 parent bf902bf commit da0e7c5
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions MoaiUtils/DocExport/Exporters/ZeroBraneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@
namespace MoaiUtils.DocExport.Exporters {
public class ZeroBraneExporter : IApiExporter {
public void Export(MoaiClass[] classes, string header, DirectoryInfo outputDirectory) {
// Select classes to export
var scriptableClasses = classes
.Where(moaiClass => moaiClass.IsScriptable)
.ToArray();
var ancestorClasses = scriptableClasses
.SelectMany(c => c.AncestorClasses);
var exportClasses = scriptableClasses.Concat(ancestorClasses)
.Distinct();

// Create contents
LuaTable typeListTable = CreateTypeListTable(classes.Where(moaiClass => moaiClass.IsScriptable));
LuaTable typeListTable = CreateTypeListTable(exportClasses);

// Write to file
var targetFileInfo = outputDirectory.GetFileInfo("moai.lua");
Expand All @@ -24,7 +33,6 @@ public void Export(MoaiClass[] classes, string header, DirectoryInfo outputDirec
private LuaTable CreateTypeListTable(IEnumerable<MoaiClass> classes) {
var typeListTable = new LuaTable();
foreach (MoaiClass moaiClass in classes.OrderBy(c => c.Name)) {
typeListTable.Add(new LuaComment(moaiClass.Signature, blankLineBefore: typeListTable.Any()));
typeListTable.Add(moaiClass.Name, CreateTypeTable(moaiClass));
}
return typeListTable;
Expand All @@ -33,6 +41,7 @@ private LuaTable CreateTypeListTable(IEnumerable<MoaiClass> classes) {
private LuaTable CreateTypeTable(MoaiClass moaiClass) {
return new LuaTable {
{ "type", "class" },
{ "inherits", moaiClass.BaseClasses.Any() ? moaiClass.BaseClasses.Select(c => c.Name).Join(" ") : null },
{ "description", ConvertString(moaiClass.Description) },
{ "childs" /* sic */, CreateMemberListTable(moaiClass) }
};
Expand All @@ -41,24 +50,13 @@ private LuaTable CreateTypeTable(MoaiClass moaiClass) {
private LuaTable CreateMemberListTable(MoaiClass moaiClass) {
var memberListTable = new LuaTable();

memberListTable.Add(new LuaComment("Direct members"));
IEnumerable<ClassMember> directMembers = moaiClass.Members
.OrderBy(member => member.GetType().Name) // Attribute, then Constant, Flag, Method
.ThenBy(member => member.Name);
foreach (var member in directMembers) {
memberListTable.Add(member.Name, CreateMemberTable((dynamic) member));
}

if (moaiClass.InheritedMembers.Any()) {
memberListTable.Add(new LuaComment("Inherited members", blankLineBefore: true));
var inheritedMembers = moaiClass.InheritedMembers
.OrderBy(member => member.GetType().Name)
.ThenBy(member => member.Name);
foreach (var member in inheritedMembers) {
memberListTable.Add(member.Name, CreateMemberTable((dynamic) member));
}
}

return memberListTable;
}

Expand Down

0 comments on commit da0e7c5

Please sign in to comment.