Ejemplo n.º 1
0
        public ParsedMember(MemberDeclarationSyntax member, DocumentationCommentTriviaSyntax documentation)
            : base(documentation)
        {
            Member = member;
            ParsedMemberType mt = ParsedMemberType.None;

            if (Member is ConstructorDeclarationSyntax)
            {
                mt = ParsedMemberType.Constructor;
            }
            if (Member is EventDeclarationSyntax)
            {
                mt = ParsedMemberType.Event;
            }
            if (Member is PropertyDeclarationSyntax)
            {
                mt = ParsedMemberType.Property;
            }
            if (Member is MethodDeclarationSyntax)
            {
                mt = ParsedMemberType.Method;
            }
            if (Member is EnumMemberDeclarationSyntax)
            {
                mt = ParsedMemberType.EnumValue;
            }
            MemberType = mt;
        }
Ejemplo n.º 2
0
        static string MembersAsJsonArray(ParsedType type, ParsedMemberType filter, bool asJavascript = true)
        {
            if (type.Members == null)
            {
                return(null);
            }
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("[");
            bool memberAdded = false;

            foreach (var member in type.Members)
            {
                if (filter != member.MemberType)
                {
                    continue;
                }
                if (memberAdded)
                {
                    sb.AppendLine(",");
                }
                sb.AppendLine("      {");
                sb.Append(KeyValString(8, "signature", member.Signature(false), asJavascript));
                //sb.Append($"        signature: '{member.Signature(false)}'");
                string summary = member.Summary();
                if (!string.IsNullOrWhiteSpace(summary))
                {
                    sb.AppendLine(",");
                    sb.Append(KeyValString(8, "summary", summary, asJavascript));
                    //sb.Append($"        summary: {JsonQuote(summary)}");
                }
                string since = member.Since;
                if (!string.IsNullOrWhiteSpace(since) && double.TryParse(since, out double sinceValue))
                {
                    sb.AppendLine(",");
                    sb.Append(KeyValString(8, "since", since, asJavascript));
                    //sb.Append($"        since: '{since}'");
                }
                string deprecated = member.Deprecated;
                if (!string.IsNullOrWhiteSpace(deprecated) && double.TryParse(deprecated, out double deprecatedValue))
                {
                    sb.AppendLine(",");
                    sb.Append(KeyValString(8, "deprecated", deprecated, asJavascript));
                    //sb.Append($"        deprecated: '{deprecated}'");
                }

                var parameters = member.GetParameters();
                if (parameters != null)
                {
                    // for now, just skip items that have ALL undocumented parameters
                    bool writeParameters = false;
                    for (int i = 0; i < parameters.Length; i++)
                    {
                        if (!string.IsNullOrWhiteSpace(parameters[i].DocString))
                        {
                            writeParameters = true;
                            break;
                        }
                    }

                    if (writeParameters)
                    {
                        sb.AppendLine(",");
                        if (asJavascript)
                        {
                            sb.AppendLine($"        parameters: [");
                        }
                        else
                        {
                            sb.AppendLine($"        \"parameters\": [");
                        }
                        for (int i = 0; i < parameters.Length; i++)
                        {
                            if (i > 0)
                            {
                                sb.AppendLine(",");
                            }
                            sb.AppendLine("          {");
                            sb.AppendLine(KeyValString(12, "name", parameters[i].Name, asJavascript) + ",");
                            //sb.AppendLine($"            name: {JsonQuote(parameters[i].Name)},");
                            // Not sure if we really need type as it is easy to resolve in javascript
                            // sb.AppendLine($"            type: {JsonQuote(parameters[i].Type)},");
                            sb.AppendLine(KeyValString(12, "summary", parameters[i].DocString, asJavascript));
                            //sb.AppendLine($"            summary: {JsonQuote(parameters[i].DocString)}");
                            sb.Append("          }");
                        }
                        sb.AppendLine();
                        sb.Append("        ]");
                    }
                }

                if (member.MemberType == ParsedMemberType.Method)
                {
                    string returns = member.ReturnDocString();
                    if (!string.IsNullOrWhiteSpace(returns))
                    {
                        sb.AppendLine(",");
                        sb.Append(KeyValString(8, "returns", returns, asJavascript));
                    }
                }

                if (member.MemberType == ParsedMemberType.Property)
                {
                    bool get, set;
                    if (member.PropertyType(out get, out set))
                    {
                        sb.AppendLine(",");
                        string s = get ? "['get'" : "[";
                        if (set)
                        {
                            if (get)
                            {
                                s += ", ";
                            }
                            s += "'set'";
                        }
                        s += "]";
                        if (!asJavascript)
                        {
                            s = s.Replace("'", "\"");
                        }
                        if (asJavascript)
                        {
                            sb.Append($"        property: {s}");
                        }
                        else
                        {
                            sb.Append($"        \"property\": {s}");
                        }
                    }
                }
                sb.AppendLine();
                sb.Append("      }");
                memberAdded = true;
            }
            sb.AppendLine();
            sb.Append("    ]");

            return(memberAdded ? sb.ToString() : null);
        }
Ejemplo n.º 3
0
        public static void WriteTypes(Dictionary <string, ParsedType> types, string outputDirectory)
        {
            var    di      = new System.IO.DirectoryInfo(outputDirectory);
            string apibase = di.Name;

            var parallelOptions = new ParallelOptions {
                MaxDegreeOfParallelism = 100
            };

            Parallel.ForEach(types, parallelOptions, (keyValue) =>
            {
                ParsedType basetype = keyValue.Value;
                if (!basetype.IsPublic)
                {
                    return;
                }

                var content = new StringBuilder();
                content.AppendLine("---");
                content.AppendLine($"title: \"{basetype.Name}\"");
                content.AppendLine($"date: {DateTime.Now.ToString("u")}");
                content.AppendLine("draft: false");
                content.AppendLine("---");
                content.AppendLine();

                content.AppendLine($"*Namespace: [{basetype.Namespace}](../)*");
                content.AppendLine();
                string baseTypeSummary = basetype.Summary();
                if (!string.IsNullOrEmpty(baseTypeSummary))
                {
                    content.AppendLine(baseTypeSummary);
                }

                if (basetype.IsClass)
                {
                    content.AppendLine("```cs");
                    string[] attributes = basetype.GetAttributes();
                    for (int i = 0; i < attributes.Length; i++)
                    {
                        content.AppendLine(attributes[i]);
                    }

                    content.Append($"public class {basetype.Name}");
                    string[] baseList = basetype.GetBaseList(null);
                    if (baseList != null)
                    {
                        for (int i = 0; i < baseList.Length; i++)
                        {
                            if (i == 0)
                            {
                                content.Append($": {baseList[i]}");
                            }
                            else
                            {
                                content.Append($", {baseList[i]}");
                            }
                        }
                    }
                    content.AppendLine();
                    content.AppendLine("```");
                }

                if (basetype.Members == null)
                {
                    return; // TODO: Figure out this case
                }
                ParsedMemberType state = ParsedMemberType.None;
                foreach (var item in basetype.Members)
                {
                    if (item.IsEvent && state != ParsedMemberType.Event)
                    {
                        content.AppendLine("## Events");
                        state = ParsedMemberType.Event;
                    }
                    if (item.IsProperty && state != ParsedMemberType.Property)
                    {
                        content.AppendLine("## Properties");
                        state = ParsedMemberType.Property;
                    }
                    if (item.IsMethod && state != ParsedMemberType.Method)
                    {
                        content.AppendLine("## Methods");
                        state = ParsedMemberType.Method;
                    }
                    if (item.IsConstructor && state != ParsedMemberType.Constructor)
                    {
                        content.AppendLine("## Constructors");
                        state = ParsedMemberType.Constructor;
                    }
                    content.AppendLine();
                    string signature = item.Signature(false);
                    var returntype   = item.ReturnType(types.Values);
                    if (returntype != null && returntype != item.ParentType)
                    {
                        string rn   = returntype.Name;
                        int index   = signature.IndexOf(rn);
                        string link = ParsedTypePath(apibase, returntype);
                        string s    = "";
                        if (index > 0)
                        {
                            s = signature.Substring(0, index);
                        }
                        s        += $"[{signature.Substring(index, rn.Length)}]({link}){signature.Substring(index + rn.Length)}";
                        signature = s;
                    }
                    content.AppendLine(signature);
                    content.AppendLine($": {item.Summary()}");
                    string returnString = item.ReturnDocString();
                    if (!string.IsNullOrWhiteSpace(returnString))
                    {
                        content.AppendLine($": Returns - {returnString}");
                    }
                    if (!item.Since.Equals("5.0")) // no need to add since tags initial items
                    {
                        content.AppendLine($": since {item.Since}");
                    }
                }

                string name      = basetype.Name;
                string directory = OutputDirectoryFromNamespace(outputDirectory, basetype.Namespace);
                string path      = System.IO.Path.Combine(directory, name.ToLower() + ".md");
                if (WriteContent(content, path, true))
                {
                    Console.WriteLine($"(write) {name}");
                }
                else
                {
                    Console.WriteLine($"(no change) {name}");
                }
            });
        }