示例#1
0
        public void TestBuild()
        {
            const string documentsBaseDir = "documents";
            const string outputBaseDir = "output";

            #region Prepare test data
            if (Directory.Exists(documentsBaseDir))
            {
                Directory.Delete(documentsBaseDir, true);
            }
            if (Directory.Exists(outputBaseDir))
            {
                Directory.Delete(outputBaseDir, true);
            }
            Directory.CreateDirectory(documentsBaseDir);
            Directory.CreateDirectory(documentsBaseDir + "/test");
            Directory.CreateDirectory(outputBaseDir);
            var conceptualFile = Path.Combine(documentsBaseDir, "test.md");
            var conceptualFile2 = Path.Combine(documentsBaseDir, "test/test.md");
            var resourceFile = Path.GetFileName(typeof(DocumentBuilderTest).Assembly.Location);
            var resourceMetaFile = resourceFile + ".meta";
            File.WriteAllLines(
                conceptualFile,
                new[]
                {
                    "---",
                    "uid: XRef1",
                    "a: b",
                    "b:",
                    "  c: e",
                    "---",
                    "# Hello World",
                    "Test XRef: @XRef1",
                    "Test link: [link text](test/test.md)",
                    "Test link: [link text 2](../" + resourceFile + ")",
                    "<p>",
                    "test",
                });
            File.WriteAllLines(
                conceptualFile2,
                new[]
                {
                    "---",
                    "uid: XRef2",
                    "a: b",
                    "b:",
                    "  c: e",
                    "---",
                    "# Hello World",
                    "Test XRef: @XRef2",
                    "Test link: [link text](../test.md)",
                    "<p>",
                    "test",
                });
            File.WriteAllText(resourceMetaFile, @"{ abc: ""xyz"", uid: ""r1"" }");
            File.WriteAllText(DfmEngineBuilder.MarkdownStyleCopFileName, @"[
    {
        tagNames: [""p""],
        behavior: ""Warning"",
        messageFormatter: ""Tag {0} is not valid."",
        openingTagOnly: true
    }
]");
            FileCollection files = new FileCollection(Environment.CurrentDirectory);
            files.Add(DocumentType.Article, new[] { conceptualFile, conceptualFile2 });
            files.Add(DocumentType.Article, new[] { "TestData/System.Console.csyml", "TestData/System.ConsoleColor.csyml" }, p => (((RelativePath)p) - (RelativePath)"TestData/").ToString());
            files.Add(DocumentType.Resource, new[] { resourceFile });
            #endregion

            Init(DocumentBuilder.PhaseName + "." + MarkdownRewriterBuilder.StyleCopPhaseName);
            try
            {
                using (var builder = new DocumentBuilder())
                {
                    builder.Build(
                    new DocumentBuildParameters
                    {
                        Files = files,
                        OutputBaseDir = Path.Combine(Environment.CurrentDirectory, outputBaseDir),
                        ExportRawModel = true,
                        Metadata = new Dictionary<string, object>
                        {
                            ["meta"] = "Hello world!",
                        }.ToImmutableDictionary()
                    });
                }
            }
            finally
            {
                Logger.UnregisterAllListeners();
            }

            {
                // check log for markdown stylecop.
                Assert.Equal(2, Listener.Items.Count);

                Assert.Equal("Tag p is not valid.", Listener.Items[0].Message);
                Assert.Equal(LogLevel.Warning, Listener.Items[0].LogLevel);
                Assert.Equal("documents/test.md", Listener.Items[0].File);

                Assert.Equal("Tag p is not valid.", Listener.Items[1].Message);
                Assert.Equal(LogLevel.Warning, Listener.Items[1].LogLevel);
                Assert.Equal("documents/test/test.md", Listener.Items[1].File);
            }

            {
                // check conceptual.
                Assert.True(File.Exists(Path.Combine(outputBaseDir, Path.ChangeExtension(conceptualFile, ".raw.model.json"))));
                var model = JsonUtility.Deserialize<Dictionary<string, object>>(Path.Combine(outputBaseDir, Path.ChangeExtension(conceptualFile, ".raw.model.json")));
                Assert.Equal(
                    "<h1 id=\"hello-world\">Hello World</h1>",
                    model["rawTitle"]);
                Assert.Equal(
                    "\n<p>Test XRef: <xref href=\"XRef1\"></xref>\n" +
                    "Test link: <a href=\"~/documents/test/test.md\">link text</a>\n" +
                    "Test link: <a href=\"~/" + resourceFile + "\">link text 2</a></p>\n" +
                    "<p><p>\n" +
                    "test</p>\n",
                    model["conceptual"]);
                Assert.Equal("Conceptual", model["type"]);
                Assert.Equal("Hello world!", model["meta"]);
                Assert.Equal("b", model["a"]);
            }

            {
                // check mref.
                Assert.True(File.Exists(Path.Combine(outputBaseDir, Path.ChangeExtension("System.Console.csyml", ".raw.model.json"))));
                Assert.True(File.Exists(Path.Combine(outputBaseDir, Path.ChangeExtension("System.ConsoleColor.csyml", ".raw.model.json"))));
            }

            {
                // check resource.
                Assert.True(File.Exists(Path.Combine(outputBaseDir, resourceFile)));
                Assert.True(File.Exists(Path.Combine(outputBaseDir, Path.ChangeExtension(resourceFile, ".raw.model.json"))));
                var meta = JsonUtility.Deserialize<Dictionary<string, object>>(Path.Combine(outputBaseDir, Path.ChangeExtension(resourceFile, ".raw.model.json")));
                Assert.Equal(3, meta.Count);
                Assert.True(meta.ContainsKey("meta"));
                Assert.Equal("Hello world!", meta["meta"]);
                Assert.True(meta.ContainsKey("abc"));
                Assert.Equal("xyz", meta["abc"]);
                Assert.True(meta.ContainsKey("uid"));
                Assert.Equal("r1", meta["uid"]);
            }

            // TODO: remove. Reason: intermediate files are no longer generated
            //{
            //    // check manifest file.
            //    var filepath = Path.Combine(outputBaseDir, DocumentBuildContext.ManifestFileName);
            //    Assert.True(File.Exists(filepath));
            //    var manifest = YamlUtility.Deserialize<List<Dictionary<string, object>>>(filepath);
            //    Assert.Equal(5, manifest.Count);
            //    Assert.Equal("Conceptual", manifest[0]["type"]);
            //    Assert.Equal(@"documents/test.json", manifest[0]["model"]);
            //    Assert.Equal("Conceptual", manifest[1]["type"]);
            //    Assert.Equal(@"documents/test/test.json", manifest[1]["model"]);
            //    Assert.Equal("ManagedReference", manifest[2]["type"]);
            //    Assert.Equal(@"System.Console.json", manifest[2]["model"]);
            //    Assert.Equal("ManagedReference", manifest[3]["type"]);
            //    Assert.Equal(@"System.ConsoleColor.json", manifest[3]["model"]);
            //    Assert.Equal("Resource", manifest[4]["type"]);
            //    Assert.Equal("Microsoft.DocAsCode.EntityModel.Tests.dll.json", manifest[4]["model"]);
            //    Assert.Equal("Microsoft.DocAsCode.EntityModel.Tests.dll", manifest[4]["resource"]);
            //}

            //{
            //    // check file map
            //    var filepath = Path.Combine(outputBaseDir, DocumentBuildContext.FileMapFileName);
            //    Assert.True(File.Exists(filepath));
            //    var filemap = YamlUtility.Deserialize<Dictionary<string, string>>(filepath);
            //    Assert.Equal(5, filemap.Count);
            //    Assert.Equal("~/documents/test.json", filemap["~/documents/test.md"]);
            //    Assert.Equal("~/documents/test/test.json", filemap["~/documents/test/test.md"]);
            //    Assert.Equal("~/System.Console.json", filemap["~/TestData/System.Console.csyml"]);
            //    Assert.Equal("~/System.ConsoleColor.json", filemap["~/TestData/System.ConsoleColor.csyml"]);
            //    Assert.Equal("~/Microsoft.DocAsCode.EntityModel.Tests.dll", filemap["~/Microsoft.DocAsCode.EntityModel.Tests.dll"]);
            //}

            //{
            //    // check external xref spec
            //    var filepath = Path.Combine(outputBaseDir, DocumentBuildContext.ExternalXRefSpecFileName);
            //    Assert.True(File.Exists(filepath));
            //    var xref = YamlUtility.Deserialize<List<XRefSpec>>(filepath);
            //    Assert.Equal(0, xref.Count);
            //}

            //{
            //    // check internal xref spec
            //    var filepath = Path.Combine(outputBaseDir, DocumentBuildContext.InternalXRefSpecFileName);
            //    Assert.True(File.Exists(filepath));
            //    var xref = YamlUtility.Deserialize<List<XRefSpec>>(filepath);
            //    Assert.Equal(70, xref.Count);
            //    Assert.NotNull(xref.Single(s => s.Uid == "System.Console"));
            //    Assert.NotNull(xref.Single(s => s.Uid == "System.Console.BackgroundColor"));
            //    Assert.NotNull(xref.Single(s => s.Uid == "System.Console.SetOut(System.IO.TextWriter)"));
            //    Assert.NotNull(xref.Single(s => s.Uid == "System.Console.WriteLine(System.Int32)"));
            //    Assert.NotNull(xref.Single(s => s.Uid == "System.ConsoleColor"));
            //    Assert.NotNull(xref.Single(s => s.Uid == "System.ConsoleColor.Black"));
            //}

            #region Cleanup
            Directory.Delete(documentsBaseDir, true);
            Directory.Delete(outputBaseDir, true);
            File.Delete(resourceMetaFile);
            #endregion
        }
示例#2
0
        public void TestBuild()
        {
            const string documentsBaseDir = "db.documents";
            const string outputBaseDir = "db.output";

            #region Prepare test data
            if (Directory.Exists(documentsBaseDir))
            {
                Directory.Delete(documentsBaseDir, true);
            }
            if (Directory.Exists(outputBaseDir))
            {
                Directory.Delete(outputBaseDir, true);
            }
            Directory.CreateDirectory(documentsBaseDir);
            Directory.CreateDirectory(documentsBaseDir + "/test");
            Directory.CreateDirectory(outputBaseDir);
            var conceptualFile = Path.Combine(documentsBaseDir, "test.md");
            var conceptualFile2 = Path.Combine(documentsBaseDir, "test/test.md");
            var resourceFile = Path.GetFileName(typeof(DocumentBuilderTest).Assembly.Location);
            var resourceMetaFile = resourceFile + ".meta";
            File.WriteAllLines(
                conceptualFile,
                new[]
                {
                    "---",
                    "uid: XRef1",
                    "a: b",
                    "b:",
                    "  c: e",
                    "---",
                    "# Hello World",
                    "Test XRef: @XRef1",
                    "Test link: [link text](test/test.md)",
                    "Test link: [link text 2](../" + resourceFile + ")",
                    "<p>",
                    "test",
                });
            File.WriteAllLines(
                conceptualFile2,
                new[]
                {
                    "---",
                    "uid: XRef2",
                    "a: b",
                    "b:",
                    "  c: e",
                    "---",
                    "# Hello World",
                    "Test XRef: @XRef2",
                    "Test link: [link text](../test.md)",
                    "<p>",
                    "test",
                });
            File.WriteAllText(resourceMetaFile, @"{ abc: ""xyz"", uid: ""r1"" }");
            File.WriteAllText(MarkdownSytleConfig.MarkdownStyleFileName, @"{
rules : [
    ""foo"",
    { name: ""bar"", disable: true}
],
tagRules : [
    {
        tagNames: [""p""],
        behavior: ""Warning"",
        messageFormatter: ""Tag {0} is not valid."",
        openingTagOnly: true
    }
]
}");
            FileCollection files = new FileCollection(Environment.CurrentDirectory);
            files.Add(DocumentType.Article, new[] { conceptualFile, conceptualFile2 });
            files.Add(DocumentType.Article, new[] { "TestData/System.Console.csyml", "TestData/System.ConsoleColor.csyml" }, p => (((RelativePath)p) - (RelativePath)"TestData/").ToString());
            files.Add(DocumentType.Resource, new[] { resourceFile });
            #endregion

            Init(string.Join(".", nameof(DocumentBuilderTest), DocumentBuilder.PhaseName, MarkdownValidatorBuilder.MarkdownValidatePhaseName));
            try
            {
                using (new LoggerPhaseScope(nameof(DocumentBuilderTest)))
                using (var builder = new DocumentBuilder())
                {
                    builder.Build(
                        new DocumentBuildParameters
                        {
                            Files = files,
                            OutputBaseDir = Path.Combine(Environment.CurrentDirectory, outputBaseDir),
                            ExportRawModel = true,
                            Metadata = new Dictionary<string, object>
                            {
                                ["meta"] = "Hello world!",
                            }.ToImmutableDictionary()
                        });
                }

                {
                    // check log for markdown stylecop.
                    Assert.Equal(2, Listener.Items.Count);

                    Assert.Equal("Tag p is not valid.", Listener.Items[0].Message);
                    Assert.Equal(LogLevel.Warning, Listener.Items[0].LogLevel);
                    Assert.Equal(documentsBaseDir + "/test.md", Listener.Items[0].File);

                    Assert.Equal("Tag p is not valid.", Listener.Items[1].Message);
                    Assert.Equal(LogLevel.Warning, Listener.Items[1].LogLevel);
                    Assert.Equal(documentsBaseDir + "/test/test.md", Listener.Items[1].File);
                }

                {
                    // check conceptual.
                    Assert.True(File.Exists(Path.Combine(outputBaseDir, Path.ChangeExtension(conceptualFile, ".raw.model.json"))));
                    var model = JsonUtility.Deserialize<Dictionary<string, object>>(Path.Combine(outputBaseDir, Path.ChangeExtension(conceptualFile, ".raw.model.json")));
                    Assert.Equal(
                        "<h1 id=\"hello-world\">Hello World</h1>",
                        model["rawTitle"]);
                    Assert.Equal(
                        "\n<p>Test XRef: <xref href=\"XRef1\"></xref>\n" +
                        "Test link: <a href=\"~/db.documents/test/test.md\">link text</a>\n" +
                        "Test link: <a href=\"~/" + resourceFile + "\">link text 2</a></p>\n" +
                        "<p><p>\n" +
                        "test</p>\n",
                        model["conceptual"]);
                    Assert.Equal("Conceptual", model["type"]);
                    Assert.Equal("Hello world!", model["meta"]);
                    Assert.Equal("b", model["a"]);
                }

                {
                    // check mref.
                    Assert.True(File.Exists(Path.Combine(outputBaseDir, Path.ChangeExtension("System.Console.csyml", ".raw.model.json"))));
                    Assert.True(File.Exists(Path.Combine(outputBaseDir, Path.ChangeExtension("System.ConsoleColor.csyml", ".raw.model.json"))));
                }

                {
                    // check resource.
                    Assert.True(File.Exists(Path.Combine(outputBaseDir, resourceFile)));
                    Assert.True(File.Exists(Path.Combine(outputBaseDir, Path.ChangeExtension(resourceFile, ".raw.model.json"))));
                    var meta = JsonUtility.Deserialize<Dictionary<string, object>>(Path.Combine(outputBaseDir, Path.ChangeExtension(resourceFile, ".raw.model.json")));
                    Assert.Equal(3, meta.Count);
                    Assert.True(meta.ContainsKey("meta"));
                    Assert.Equal("Hello world!", meta["meta"]);
                    Assert.True(meta.ContainsKey("abc"));
                    Assert.Equal("xyz", meta["abc"]);
                    Assert.True(meta.ContainsKey("uid"));
                    Assert.Equal("r1", meta["uid"]);
                }
            }
            finally
            {
                CleanUp();
                Directory.Delete(documentsBaseDir, true);
                Directory.Delete(outputBaseDir, true);
                File.Delete(resourceMetaFile);
            }
        }
示例#3
0
            public static void BuildDocument(BuildJsonConfig config, string baseDirectory, string outputDirectory, string pluginDirectory)
            {
                try
                {
                    var templateManager = new TemplateManager(typeof(DocumentBuilderWrapper).Assembly, "Template", config.Templates, config.Themes, config.BaseDirectory);

                    using (var builder = new DocumentBuilder(LoadPluginAssemblies(pluginDirectory)))
                    {
                        var parameters = ConfigToParameter(config, baseDirectory, outputDirectory);
                        if (parameters.Files.Count == 0)
                        {
                            Logger.LogWarning("No files found, nothing is to be generated");
                            return;
                        }

                        using (var processor = templateManager.GetTemplateProcessor())
                        {
                            parameters.TemplateCollection = processor.Templates;
                            processor.ProcessDependencies(parameters.OutputBaseDir);
                            builder.Build(parameters);
                        }
                    }
                }
                catch (DocumentException ex)
                {
                    Logger.LogWarning("Document error occurs:" + ex.Message);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex.ToString());
                }
            }