예제 #1
0
        private static async Task WriteExamples(IExamples examples)
        {
            var targetPath = examples.Path;

            if (!Directory.Exists(targetPath))
            {
                Console.WriteLine($"Createing directory {targetPath}");
                Directory.CreateDirectory(targetPath);
            }

            var messages = examples.Build();

            var markdownPath = Path.Combine(targetPath, "Examples.md");

            using (var fs = File.Open(markdownPath, FileMode.Create, FileAccess.Write)) {
                using (var sw = new StreamWriter(fs, Encoding.UTF8)) {
                    await sw.WriteLineAsync("| File | Json | Binary |");

                    await sw.WriteLineAsync("|------|------|--------|");

                    foreach (var kvp in messages)
                    {
                        var jsonPath = Path.Combine(targetPath, $"{kvp.Key}.json");
                        kvp.Value.WriteJson(jsonPath, true).Wait();

                        long jsonSize;
                        using (var ms = new MemoryStream()) {
                            kvp.Value.WriteJson(ms, false).Wait();
                            jsonSize = ms.Length;
                        }
                        Console.WriteLine($"Wrote {jsonPath}");

                        var binPath = Path.Combine(targetPath, $"{kvp.Key}.bin");
                        kvp.Value.WriteBinary(binPath);
                        var binSize = new FileInfo(binPath).Length;
                        Console.WriteLine($"Wrote {binPath}");

                        await sw.WriteLineAsync($"| {kvp.Key} | [{BytesConverter.ToReadableString(jsonSize)}]({jsonPath.Substring(DocsPath.Length + 1).Replace("\\", "/")} ':ignore') | [{BytesConverter.ToReadableString(binSize)}]({binPath.Substring(DocsPath.Length + 1).Replace("\\", "/")} ':ignore') |");
                    }
                }
            }

            Console.WriteLine($"Wrote {markdownPath}");
        }