|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using net.adamec.dev.markupdoc.CodeModel; |
| 5 | +using net.adamec.dev.markupdoc.CodeModel.Builder; |
| 6 | +using net.adamec.dev.markupdoc.Markup; |
| 7 | +using net.adamec.dev.markupdoc.MsApiDoc; |
| 8 | +using net.adamec.dev.markupdoc.Options; |
| 9 | + |
| 10 | +namespace net.adamec.dev.markupdoc |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Main application class - encapsulates the documentation building logic |
| 14 | + /// </summary> |
| 15 | + public class Application |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Output configuration |
| 19 | + /// </summary> |
| 20 | + public OutputOptions OutputOptions { get; } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// CTOR with output configuration |
| 24 | + /// </summary> |
| 25 | + /// <param name="outputOptions">Output configuration</param> |
| 26 | + public Application(OutputOptions outputOptions) |
| 27 | + { |
| 28 | + OutputOptions = outputOptions; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Runs the documentation building logic |
| 33 | + /// </summary> |
| 34 | + /// <param name="projectFile">Project file of C# project to build the documentation for</param> |
| 35 | + /// <returns>Async task</returns> |
| 36 | + public async Task RunAsync(string projectFile) |
| 37 | + { |
| 38 | + if (string.IsNullOrEmpty(projectFile)) throw new ArgumentNullException(nameof(projectFile)); |
| 39 | + if (!File.Exists(projectFile)) throw new ArgumentException($"Project file {projectFile} doesn't exist"); |
| 40 | + |
| 41 | + var projectDir = new FileInfo(projectFile).DirectoryName; |
| 42 | + if (projectDir == null) throw new Exception($"Can't get the project directory from {projectFile}"); |
| 43 | + |
| 44 | + //Read MS API local documentation to get the links to MS API online documentation |
| 45 | + MsApiDocEngine.ReadLocalDoc(); |
| 46 | + |
| 47 | + //Build code model |
| 48 | + var root = await ModelBuilder.BuildFromProjectSourcesAsync(projectFile); |
| 49 | + |
| 50 | + //Prepare for the output |
| 51 | + var targetBase = OutputOptions.Target; //full name w/o extension |
| 52 | + if (string.IsNullOrEmpty(targetBase)) targetBase = Path.Combine(projectDir, "doc"); //if not set, proj dir will be used and file doc.html/doc.md |
| 53 | + |
| 54 | + var targetDir = new FileInfo(targetBase).DirectoryName; |
| 55 | + var targetFileBase = new FileInfo(targetBase).Name; |
| 56 | + |
| 57 | + if (OutputOptions.SplitNs) root.ProcessingInfo.SplitFileType = SplitTypeEnum.Namespace; |
| 58 | + if (OutputOptions.SplitType) root.ProcessingInfo.SplitFileType = SplitTypeEnum.Type; |
| 59 | + |
| 60 | + root.ProcessingInfo.BaseMainFile = targetFileBase; |
| 61 | + |
| 62 | + //Generate markup outputs |
| 63 | + if (OutputOptions.Markdown) |
| 64 | + { |
| 65 | + var writer = new MarkupGenerator( |
| 66 | + // ReSharper disable once AssignNullToNotNullAttribute |
| 67 | + Path.Combine(targetDir, $"{targetFileBase}.{OutputOptions.MarkdownExtension}"), root, |
| 68 | + new MarkdownMarkupProvider(OutputOptions)); |
| 69 | + await writer.WriteModelAsync(OutputOptions.Title); |
| 70 | + } |
| 71 | + |
| 72 | + if (OutputOptions.Html) |
| 73 | + { |
| 74 | + var writer = new MarkupGenerator( |
| 75 | + // ReSharper disable once AssignNullToNotNullAttribute |
| 76 | + Path.Combine(targetDir, $"{targetFileBase}.{OutputOptions.HtmlExtension}"), root, |
| 77 | + new HtmlMarkupProvider(OutputOptions)); |
| 78 | + |
| 79 | + await writer.WriteModelAsync(OutputOptions.Title); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments