66using System . Reflection ;
77using System . Text . Json ;
88using Elastic . Documentation . Legacy ;
9+ using Elastic . Documentation . Links ;
910using Elastic . Documentation . Serialization ;
1011using Elastic . Documentation . State ;
1112using Elastic . Markdown . Exporters ;
@@ -27,6 +28,11 @@ public interface IDocumentationFileOutputProvider
2728 IFileInfo ? OutputFile ( DocumentationSet documentationSet , IFileInfo defaultOutputFile , string relativePath ) ;
2829}
2930
31+ public record GenerationResult
32+ {
33+ public IReadOnlyDictionary < string , LinkRedirect > Redirects { get ; set ; } = new Dictionary < string , LinkRedirect > ( ) ;
34+ }
35+
3036public class DocumentationGenerator
3137{
3238 private readonly IDocumentationFileOutputProvider ? _documentationFileOutputProvider ;
@@ -87,14 +93,23 @@ public async Task ResolveDirectoryTree(Cancel ctx)
8793 _logger . LogInformation ( "Resolved tree" ) ;
8894 }
8995
90- public async Task GenerateAll ( Cancel ctx )
96+ public async Task < GenerationResult > GenerateAll ( Cancel ctx )
9197 {
92- var generationState = GetPreviousGenerationState ( ) ;
93- if ( ! Context . SkipMetadata && ( Context . Force || generationState == null ) )
98+ var result = new GenerationResult ( ) ;
99+
100+ HashSet < string > offendingFiles = [ ] ;
101+ var outputSeenChanges = DateTimeOffset . MinValue ;
102+ if ( Context . SkipDocumentationState )
94103 DocumentationSet . ClearOutputDirectory ( ) ;
104+ else
105+ {
106+ var generationState = GetPreviousGenerationState ( ) ;
107+ if ( Context . Force || generationState == null )
108+ DocumentationSet . ClearOutputDirectory ( ) ;
95109
96- if ( CompilationNotNeeded ( generationState , out var offendingFiles , out var outputSeenChanges ) )
97- return ;
110+ if ( CompilationNotNeeded ( generationState , out offendingFiles , out outputSeenChanges ) )
111+ return result ;
112+ }
98113
99114 _logger . LogInformation ( $ "Fetching external links") ;
100115 _ = await Resolver . FetchLinks ( ctx ) ;
@@ -107,14 +122,20 @@ public async Task GenerateAll(Cancel ctx)
107122
108123 await ExtractEmbeddedStaticResources ( ctx ) ;
109124
110- if ( Context . SkipMetadata )
111- return ;
112-
113- _logger . LogInformation ( $ "Generating documentation compilation state" ) ;
114- await GenerateDocumentationState ( ctx ) ;
125+ if ( ! Context . SkipDocumentationState )
126+ {
127+ _logger . LogInformation ( $ "Generating documentation compilation state" ) ;
128+ await GenerateDocumentationState ( ctx ) ;
129+ }
115130
116131 _logger . LogInformation ( $ "Generating links.json") ;
117- await GenerateLinkReference ( ctx ) ;
132+ var linkReference = await GenerateLinkReference ( ctx ) ;
133+
134+ // ReSharper disable once WithExpressionModifiesAllMembers
135+ return result with
136+ {
137+ Redirects = linkReference . Redirects ?? [ ]
138+ } ;
118139 }
119140
120141 private async Task ProcessDocumentationFiles ( HashSet < string > offendingFiles , DateTimeOffset outputSeenChanges , Cancel ctx )
@@ -254,13 +275,13 @@ private bool CompilationNotNeeded(GenerationState? generationState, out HashSet<
254275 return false ;
255276 }
256277
257- private async Task GenerateLinkReference ( Cancel ctx )
278+ private async Task < LinkReference > GenerateLinkReference ( Cancel ctx )
258279 {
259280 var file = DocumentationSet . LinkReferenceFile ;
260281 var state = DocumentationSet . CreateLinkReference ( ) ;
261-
262282 var bytes = JsonSerializer . SerializeToUtf8Bytes ( state , SourceGenerationContext . Default . LinkReference ) ;
263283 await DocumentationSet . OutputDirectory . FileSystem . File . WriteAllBytesAsync ( file . FullName , bytes , ctx ) ;
284+ return state ;
264285 }
265286
266287 private async Task GenerateDocumentationState ( Cancel ctx )
0 commit comments