public override IEnumerable <DocSample> CreateSamplesFromSourceFile(string filePath)
        {
            var result = new List <DocSample>();

            var xmlDoc = new HtmlDocument();

            xmlDoc.LoadHtml(File.ReadAllText(filePath));

            var samples = new List <HtmlNode>();

            var htmlSamples = (xmlDoc.DocumentNode.SelectNodes("//div[@doc-sample]"));
            var jsSamples   = (xmlDoc.DocumentNode.SelectNodes("//script[@doc-sample]"));

            if (htmlSamples != null)
            {
                samples.AddRange(htmlSamples);
            }

            if (jsSamples != null)
            {
                samples.AddRange(jsSamples);
            }

            foreach (var sample in samples)
            {
                var docSample = new DocSample();

                var methodName = sample.GetAttributeValue("doc-method-name", string.Empty);

                if (string.IsNullOrEmpty(methodName))
                {
                    throw new ArgumentException("doc-method-name attr must be present");
                }

                if (sample.Name == "script")
                {
                    docSample.Language = "js";
                }
                else
                {
                    docSample.Language = "html";
                }

                docSample.SourceFileName = Path.GetFileName(filePath);
                docSample.SourceFileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);

                docSample.SourceFileFolder = Path.GetDirectoryName(filePath);
                docSample.SourceFilePath   = filePath;

                docSample.ClassName = Path.GetFileNameWithoutExtension(filePath);

                docSample.MethodName = methodName;
                docSample.MethodBody = sample.InnerHtml;

                result.Add(docSample);
            }

            return(result);
        }
Exemple #2
0
        private string GetSampleInstanceAsString(DocSample sample)
        {
            var classXmlString = StringUtils.ToLiteral(sample.ToXml());

            return(string.Format("{0}.FromXml(\"{1}\")",
                                 sample.GetType().Namespace + "." + sample.GetType().Name,
                                 classXmlString));
        }
Exemple #3
0
        public static List <string> GetSampleTagValues(DocSample sample, string tagName, List <string> defaultValues)
        {
            var tag = sample.Tags.FirstOrDefault(t => t.Name == tagName);

            if (tag != null && tag.Values.Any())
            {
                return(tag.Values);
            }

            return(defaultValues);
        }
Exemple #4
0
        public static bool HasTagValue(DocSample sample, string tagName, string value)
        {
            var tag = sample.Tags.FirstOrDefault(t => t.Name == tagName);

            if (tag == null)
            {
                return(false);
            }

            if (!tag.Values.Any())
            {
                return(false);
            }

            return(tag.Values.Contains(value));
        }
        public override IEnumerable <DocSample> CreateSamplesFromSourceFile(string filePath)
        {
            var result = new List <DocSample>();

            var xmlDoc = new HtmlDocument();

            xmlDoc.LoadHtml(File.ReadAllText(filePath));

            var samples = xmlDoc.DocumentNode.Descendants("Sample");

            foreach (var sample in samples)
            {
                var docSample = new DocSample();

                var methodName = sample.GetAttributeValue("MethodName", string.Empty);

                if (string.IsNullOrEmpty(methodName))
                {
                    throw new ArgumentException("methodName attr must be present");
                }

                docSample.SourceFileName = Path.GetFileName(filePath);
                docSample.SourceFileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);

                docSample.SourceFileFolder = Path.GetDirectoryName(filePath);
                docSample.SourceFilePath   = filePath;

                docSample.Language = "xml";

                docSample.ClassName = Path.GetFileNameWithoutExtension(filePath);

                docSample.MethodName = methodName;
                docSample.MethodBody = sample.InnerHtml;

                result.Add(docSample);
            }

            //var samples = xmlDoc.DescendantNodes("");

            return(result);
        }
Exemple #6
0
        public static bool HasTag(DocSample sample, string tagName)
        {
            var tag = sample.Tags.FirstOrDefault(t => t.Name == tagName);

            return(tag != null);
        }
Exemple #7
0
 public async void DocReportOk()
 {
     SummitReportSettings.Instance.ConnectionString = "data source=summittest.database.windows.net;initial catalog=MARS;user=simsa;password=D3n^3r#$";
     var rpt         = new DocSample();
     var genFileName = await rpt.GenerateAsync(6670);
 }
Exemple #8
0
        public override IEnumerable <DocSample> CreateSamplesFromSourceFile(string filePath)
        {
            var result = new List <DocSample>();

            var tree     = CSharpSyntaxTree.ParseText(File.ReadAllText(filePath));
            var treeRoot = tree.GetRoot() as CompilationUnitSyntax;

            var csClasses = tree.GetRoot()
                            .DescendantNodes()
                            .OfType <ClassDeclarationSyntax>()
                            .ToList();

            foreach (var csClass in csClasses)
            {
                var className    = csClass.Identifier.ToString();
                var classComment = string.Empty;

                var trivia = csClass.GetLeadingTrivia();

                if (trivia != null)
                {
                    var commentXml = trivia.ToString();

                    try
                    {
                        classComment = XElement.Parse(trivia.ToString()
                                                      .Replace(@"///", string.Empty)
                                                      .Trim())
                                       .FirstNode
                                       .ToString()
                                       .Trim()
                                       .Replace("     ", "");
                    }
                    catch (Exception)
                    {
                    }
                }

                var namespaceName = (csClass.Parent as NamespaceDeclarationSyntax).Name.ToString();

                var sample = new DocSample();

                sample.IsClass  = true;
                sample.IsMethod = false;

                // namespace
                sample.Namespace = namespaceName;
                sample.Language  = "cs";

                // class level
                sample.ClassName     = className;
                sample.ClassFullName = namespaceName + "." + className;

                sample.ClassComment = classComment;


                var classBody = "    " + csClass.ToString();

                // cleaning up attributes
                foreach (var classAttr in csClass.AttributeLists.ToList())
                {
                    classBody = classBody.Replace(classAttr.ToString(), string.Empty);
                }

                // method
                sample.MethodBodyWithFunction = classBody;
                sample.MethodBody             = classBody;

                sample.MethodName     = className + "Class";
                sample.MethodFullName = "Class" + sample.MethodName;

                sample.SourceFileName = Path.GetFileName(filePath);
                sample.SourceFileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);

                sample.SourceFileFolder = Path.GetDirectoryName(filePath);
                sample.SourceFilePath   = filePath;

                // TODO, metadata
                sample.Title       = string.Empty;
                sample.Description = string.Empty;

                // load from SampleMetadata attr
                var instanceType = Type.GetType(sample.ClassFullName);
                var method       = instanceType;

                if (method != null)
                {
                    var methodMetadata = method.GetCustomAttributes(typeof(SampleMetadataAttribute), false)
                                         .FirstOrDefault() as SampleMetadataAttribute;

                    if (methodMetadata != null)
                    {
                        sample.Title       = methodMetadata.Title;
                        sample.Description = methodMetadata.Description;
                    }
                    else
                    {
                        // fallbak on the method name
                        sample.Title = method.Name;
                    }

                    // tags


                    var sampleTags = (method.GetCustomAttributes(typeof(SampleMetadataTagAttribute), false)
                                      as SampleMetadataTagAttribute[]).ToList();


                    // addint top-class tags
                    sampleTags.AddRange(instanceType.GetCustomAttributes(typeof(SampleMetadataTagAttribute), false)
                                        as SampleMetadataTagAttribute[]);


                    foreach (var tagNames in sampleTags.GroupBy(tag => tag.Name))
                    {
                        var newTag = new DocSampleTag
                        {
                            Name   = tagNames.Key,
                            Values = tagNames.Select(t => t.Value).ToList()
                        };

                        sample.Tags.Add(newTag);
                    }
                }

                result.Add(sample);
                //}
            }


            return(result);
        }