Skip to content

Instantly share code, notes, and snippets.

@dotMorten
Last active September 14, 2016 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotMorten/b331d0fd377db2ba6784aeb6ee6b4d33 to your computer and use it in GitHub Desktop.
Save dotMorten/b331d0fd377db2ba6784aeb6ee6b4d33 to your computer and use it in GitHub Desktop.
Optimizes XML Doc for intellisense by stripping out internals and remarks from the XML doc
using Mono.Cecil;
using Mono.Cecil.Rocks;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xmlDoc = args[1];
string assemblyName = args[2];
string outFilename = args[3];
var xmldoc = new System.Xml.XmlDocument();
xmldoc.Load(xmlDoc);
using (var module = Mono.Cecil.ModuleDefinition.ReadModule(assemblyName))
{
//Build map of all public members
var publicTypes = new Dictionary<string, MemberReference>();
foreach (var item in module.GetTypes().Where(t => t.IsPublic))
{
publicTypes.Add(DocCommentId.GetDocCommentId(item), item);
foreach (var m in item.Methods.Where(m => m.IsPublic))
{
publicTypes.Add(DocCommentId.GetDocCommentId(m), m);
}
foreach (var p in item.Properties.Where(m => m.GetMethod != null && m.GetMethod.IsPublic || m.SetMethod != null && m.SetMethod.IsPublic))
{
publicTypes.Add(DocCommentId.GetDocCommentId(p), p);
}
foreach (var e in item.Events.Where(m => m.AddMethod != null && m.AddMethod.IsPublic || m.RemoveMethod != null && m.RemoveMethod.IsPublic))
{
publicTypes.Add(DocCommentId.GetDocCommentId(e), e);
}
foreach (var f in item.Fields.Where(m => m.IsPublic))
{
publicTypes.Add(DocCommentId.GetDocCommentId(f), f);
}
}
var membersNode = xmldoc.DocumentElement.SelectSingleNode("members");
var members = membersNode.SelectNodes("member");
foreach (var member in members.OfType<System.Xml.XmlNode>().ToArray())
{
var docCommentId = member.Attributes["name"].Value;
if (!publicTypes.ContainsKey(docCommentId))
{
//Not public. Remove
membersNode.RemoveChild(member);
continue;
}
//Strip out everything but summary, param, returns, exception, value
foreach (var child in member.ChildNodes.OfType<XmlNode>().ToArray())
{
string name = child.Name;
if (name == "summary" || name == "param" || name == "returns" || name == "exception" || name == "value")
{
if (child.InnerText != null)
{
var lines = child.InnerXml.Split(new char[] { '\n' });
child.InnerXml = string.Join(" ", lines.Select(l => l.Trim())).Trim();
}
continue;
}
member.RemoveChild(child);
}
}
}
XmlWriterSettings settings = new XmlWriterSettings()
{
NewLineChars = string.Empty, Indent = false, CheckCharacters = true
};
using (var writer = XmlWriter.Create(outFilename, settings))
{
xmldoc.Save(writer);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment