11using System ;
22using System . Collections . Generic ;
3- using System . Diagnostics ;
43using System . IO ;
54using System . Linq ;
65using System . Reflection ;
7- using System . Reflection . PortableExecutable ;
86using System . Text ;
97using System . Text . RegularExpressions ;
10- using System . Threading ;
11- using System . Threading . Tasks ;
128using AsciiDocNet ;
13- using DocGenerator . Walkers ;
149using DocGenerator . XmlDocs ;
1510using Elasticsearch . Net ;
1611using Microsoft . CodeAnalysis ;
17- using Microsoft . CodeAnalysis . CSharp ;
1812using Nest ;
1913using NuDoq ;
2014using Container = AsciiDocNet . Container ;
@@ -29,15 +23,15 @@ namespace DocGenerator.AsciiDoc
2923 /// </summary>
3024 public class GeneratedAsciidocVisitor : NoopVisitor
3125 {
32- private static readonly Dictionary < string , string > Ids = new Dictionary < string , string > ( ) ;
33-
34- private readonly FileInfo _source ;
26+ private static readonly Dictionary < string , string > Ids = new Dictionary < string , string > ( ) ;
3527 private readonly FileInfo _destination ;
3628 private readonly Dictionary < string , Project > _projects ;
37- private int _topSectionTitleLevel ;
29+
30+ private readonly FileInfo _source ;
3831 private Document _document ;
3932 private Document _newDocument ;
4033 private bool _topLevel = true ;
34+ private int _topSectionTitleLevel ;
4135
4236 public GeneratedAsciidocVisitor ( FileInfo source , FileInfo destination , Dictionary < string , Project > projects )
4337 {
@@ -61,35 +55,22 @@ public override void VisitDocument(Document document)
6155 DocType = document . DocType
6256 } ;
6357
64- foreach ( var authorInfo in document . Authors )
65- {
66- _newDocument . Authors . Add ( authorInfo ) ;
67- }
58+ foreach ( var authorInfo in document . Authors ) _newDocument . Authors . Add ( authorInfo ) ;
6859
6960 RemoveDocDirectoryAttribute ( _newDocument ) ;
7061 RemoveDocDirectoryAttribute ( document ) ;
7162
72- foreach ( var attributeEntry in document . Attributes )
73- {
74- _newDocument . Attributes . Add ( attributeEntry ) ;
75- }
63+ foreach ( var attributeEntry in document . Attributes ) _newDocument . Attributes . Add ( attributeEntry ) ;
7664
7765 if ( document . Attributes . All ( a => a . Name != "ref_current" ) )
78- {
7966 _newDocument . Attributes . Add ( new AttributeEntry ( "ref_current" ,
8067 $ "https://www.elastic.co/guide/en/elasticsearch/reference/{ Program . DocVersion } ") ) ;
81- }
8268
8369 var github = "https://github.com/elastic/elasticsearch-net" ;
84- if ( document . Attributes . All ( a => a . Name != "github" ) )
85- {
86- _newDocument . Attributes . Add ( new AttributeEntry ( "github" , github ) ) ;
87- }
70+ if ( document . Attributes . All ( a => a . Name != "github" ) ) _newDocument . Attributes . Add ( new AttributeEntry ( "github" , github ) ) ;
8871
8972 if ( document . Attributes . All ( a => a . Name != "nuget" ) )
90- {
9173 _newDocument . Attributes . Add ( new AttributeEntry ( "nuget" , "https://www.nuget.org/packages" ) ) ;
92- }
9374
9475 var originalFile = Regex . Replace ( _source . FullName . Replace ( "\\ " , "/" ) , @"^(.*Tests/)" ,
9576 $ "{ github } /tree/{ Program . BranchName } /src/Tests/") ;
@@ -98,15 +79,15 @@ public override void VisitDocument(Document document)
9879 {
9980 Style = CommentStyle . MultiLine ,
10081 Text = $ "IMPORTANT NOTE\r \n ==============\r \n This file has been generated from { originalFile } . \r \n " +
101- "If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,\r \n " +
102- "please modify the original csharp file found at the link and submit the PR with that change. Thanks!"
82+ "If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,\r \n " +
83+ "please modify the original csharp file found at the link and submit the PR with that change. Thanks!"
10384 } ) ;
10485
10586 _topSectionTitleLevel = _source . Directory . Name . Equals ( "request" , StringComparison . OrdinalIgnoreCase ) &&
106- _source . Directory . Parent != null &&
107- _source . Directory . Parent . Name . Equals ( "search" , StringComparison . OrdinalIgnoreCase )
108- ? 2
109- : 3 ;
87+ _source . Directory . Parent != null &&
88+ _source . Directory . Parent . Name . Equals ( "search" , StringComparison . OrdinalIgnoreCase )
89+ ? 2
90+ : 3 ;
11091
11192 // see if the document has some kind of top level title and add one with an anchor if not.
11293 // Used to add titles to *Usage test files
@@ -118,7 +99,7 @@ public override void VisitDocument(Document document)
11899 if ( sectionTitle != null && sectionTitle . Level <= 3 )
119100 _topSectionTitleLevel = sectionTitle . Level ;
120101
121- if ( sectionTitle == null || ( sectionTitle . Level > 3 ) )
102+ if ( sectionTitle == null || sectionTitle . Level > 3 )
122103 {
123104 var id = Path . GetFileNameWithoutExtension ( _destination . Name ) ;
124105 var title = id . LowercaseHyphenToPascal ( ) ;
@@ -136,18 +117,15 @@ public override void VisitContainer(Container elements)
136117 if ( _topLevel )
137118 {
138119 _topLevel = false ;
139- for ( int index = 0 ; index < elements . Count ; index ++ )
120+ for ( var index = 0 ; index < elements . Count ; index ++ )
140121 {
141122 var element = elements [ index ] ;
142123 var source = element as Source ;
143124
144125 if ( source != null )
145126 {
146127 // remove empty source blocks
147- if ( string . IsNullOrWhiteSpace ( source . Text ) )
148- {
149- continue ;
150- }
128+ if ( string . IsNullOrWhiteSpace ( source . Text ) ) continue ;
151129
152130 var method = source . Attributes . OfType < NamedAttribute > ( ) . FirstOrDefault ( a => a . Name == "method" ) ;
153131 if ( method == null )
@@ -176,9 +154,7 @@ public override void VisitContainer(Container elements)
176154 case "queryfluent" :
177155 case "fluentaggs" :
178156 if ( ! LastSectionTitleMatches ( text => text . StartsWith ( "Fluent DSL" , StringComparison . OrdinalIgnoreCase ) ) )
179- {
180157 _newDocument . Add ( CreateSubsectionTitle ( "Fluent DSL example" ) ) ;
181- }
182158
183159 _newDocument . Add ( source ) ;
184160 break ;
@@ -191,9 +167,7 @@ public override void VisitContainer(Container elements)
191167 case "expectresponse" :
192168 // Don't add the Handlng Response section title if it was the last title (it might be defined in the doc already)
193169 if ( ! LastSectionTitleMatches ( text => text . Equals ( "Handling responses" , StringComparison . OrdinalIgnoreCase ) ) )
194- {
195170 _newDocument . Add ( CreateSubsectionTitle ( "Handling Responses" ) ) ;
196- }
197171 _newDocument . Add ( source ) ;
198172 break ;
199173 default :
@@ -202,9 +176,7 @@ public override void VisitContainer(Container elements)
202176 }
203177 }
204178 else
205- {
206179 _newDocument . Add ( element ) ;
207- }
208180 }
209181 }
210182
@@ -216,10 +188,7 @@ public override void VisitSource(Source source)
216188 // remove method attributes as the elastic doc generation doesn't like them; it
217189 // expects a linenumbering in the index 2 position of a source block
218190 var methodAttribute = source . Attributes . FirstOrDefault ( a => a . Name == "method" ) ;
219- if ( methodAttribute != null )
220- {
221- source . Attributes . Remove ( methodAttribute ) ;
222- }
191+ if ( methodAttribute != null ) source . Attributes . Remove ( methodAttribute ) ;
223192
224193 // Replace tabs with spaces and remove C# comment escaping from callouts
225194 // (elastic docs generation does not like this callout format)
@@ -231,13 +200,10 @@ public override void VisitSource(Source source)
231200 public override void VisitSectionTitle ( SectionTitle sectionTitle )
232201 {
233202 // Generate an anchor for all top level section titles
234- if ( this . _document . IndexOf ( sectionTitle ) == 0 && ! sectionTitle . Attributes . HasAnchor )
203+ if ( _document . IndexOf ( sectionTitle ) == 0 && ! sectionTitle . Attributes . HasAnchor )
235204 {
236205 var builder = new StringBuilder ( ) ;
237- using ( var writer = new AsciiDocVisitor ( new StringWriter ( builder ) ) )
238- {
239- writer . VisitInlineContainer ( sectionTitle ) ;
240- }
206+ using ( var writer = new AsciiDocVisitor ( new StringWriter ( builder ) ) ) writer . VisitInlineContainer ( sectionTitle ) ;
241207
242208 var title = builder . ToString ( ) . PascalToHyphen ( ) ;
243209 sectionTitle . Attributes . Add ( new Anchor ( title ) ) ;
@@ -248,9 +214,7 @@ public override void VisitSectionTitle(SectionTitle sectionTitle)
248214 // Check for duplicate ids across documents
249215 var key = sectionTitle . Attributes . Anchor . Id ;
250216 if ( Ids . TryGetValue ( key , out var existingFile ) )
251- {
252217 throw new Exception ( $ "duplicate id { key } in { _destination . FullName } . Id already exists in { existingFile } ") ;
253- }
254218
255219 Ids . Add ( key , _destination . FullName ) ;
256220 }
@@ -261,6 +225,7 @@ public override void VisitSectionTitle(SectionTitle sectionTitle)
261225 public override void VisitAttributeEntry ( AttributeEntry attributeEntry )
262226 {
263227 if ( attributeEntry . Name != "xml-docs" ) return ;
228+
264229 //true when running from the IDE, build/output might have not been created
265230 string configuration = null ;
266231 if ( Program . BuildOutputPath . Contains ( "src" ) )
@@ -274,6 +239,7 @@ string XmlFile(string project)
274239 {
275240 if ( configuration == null )
276241 return Path . Combine ( Program . BuildOutputPath , project , "netstandard2.0" , $ "{ project } .XML") ;
242+
277243 return Path . Combine ( Program . BuildOutputPath , project , "bin" , configuration , "netstandard2.0" ,
278244 $ "{ project } .XML") ;
279245 }
@@ -328,10 +294,7 @@ string XmlFile(string project)
328294 diagnostic . Severity == DiagnosticSeverity . Error ) ;
329295
330296 var builder = new StringBuilder ( $ "Unable to emit compilation for: { assemblyName } ") ;
331- foreach ( var diagnostic in failures )
332- {
333- builder . AppendLine ( $ "{ diagnostic . Id } : { diagnostic . GetMessage ( ) } ") ;
334- }
297+ foreach ( var diagnostic in failures ) builder . AppendLine ( $ "{ diagnostic . Id } : { diagnostic . GetMessage ( ) } ") ;
335298
336299 builder . AppendLine ( new string ( '-' , 30 ) ) ;
337300
@@ -348,10 +311,7 @@ string XmlFile(string project)
348311 if ( visitor . LabeledListItems . Any ( ) )
349312 {
350313 var labeledList = new LabeledList ( ) ;
351- foreach ( var item in visitor . LabeledListItems . OrderBy ( l => l . Label ) )
352- {
353- labeledList . Items . Add ( item ) ;
354- }
314+ foreach ( var item in visitor . LabeledListItems . OrderBy ( l => l . Label ) ) labeledList . Items . Add ( item ) ;
355315
356316 _newDocument . Insert ( _newDocument . IndexOf ( attributeEntry ) , labeledList ) ;
357317 }
@@ -360,10 +320,7 @@ string XmlFile(string project)
360320 private void RemoveDocDirectoryAttribute ( Document document )
361321 {
362322 var directoryAttribute = document . Attributes . FirstOrDefault ( a => a . Name == "docdir" ) ;
363- if ( directoryAttribute != null )
364- {
365- document . Attributes . Remove ( directoryAttribute ) ;
366- }
323+ if ( directoryAttribute != null ) document . Attributes . Remove ( directoryAttribute ) ;
367324 }
368325
369326 private bool LastSectionTitleMatches ( Func < string , bool > predicate )
@@ -372,10 +329,7 @@ private bool LastSectionTitleMatches(Func<string, bool> predicate)
372329 if ( lastSectionTitle != null && lastSectionTitle . Level == _topSectionTitleLevel + 1 )
373330 {
374331 var builder = new StringBuilder ( ) ;
375- using ( var visitor = new AsciiDocVisitor ( new StringWriter ( builder ) ) )
376- {
377- visitor . VisitInlineContainer ( lastSectionTitle ) ;
378- }
332+ using ( var visitor = new AsciiDocVisitor ( new StringWriter ( builder ) ) ) visitor . VisitInlineContainer ( lastSectionTitle ) ;
379333
380334 return predicate ( builder . ToString ( ) ) ;
381335 }
0 commit comments