1- const string DoubleSlashCommentPrefix = "// " ;
2- const string TripleSlashCommentPrefix = "///" ;
3- const string SummaryStart = "/// <summary>" ;
4- const string SummaryEnd = "/// </summary>" ;
5- const string FileFilter = "*.cs" ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Linq ;
5+ using System . Threading . Tasks ;
66
7- var commentStarts = new HashSet < string > { DoubleSlashCommentPrefix , TripleSlashCommentPrefix } ;
8- var repoRoot = Directory . GetCurrentDirectory ( ) ;
7+ namespace Box2D . NET . CommentConverter ;
8+
9+ public static class Program
10+ {
11+ public const string DoubleSlashCommentPrefix = "// " ;
12+ public const string TripleSlashCommentPrefix = "///" ;
13+ public const string SummaryStart = "/// <summary>" ;
14+ public const string SummaryEnd = "/// </summary>" ;
15+ public const string FileFilter = "*.cs" ;
16+ public static readonly HashSet < string > commentStarts = new HashSet < string > { DoubleSlashCommentPrefix , TripleSlashCommentPrefix } ;
17+
18+ public static async Task < int > Main ( string [ ] args )
19+ {
20+ var repoRoot = Directory . GetCurrentDirectory ( ) ;
921
1022#if DEBUG
11- repoRoot = Path . GetFullPath ( Path . Combine ( repoRoot , ".." , ".." , ".." , ".." , ".." ) ) ;
23+ repoRoot = Path . GetFullPath ( Path . Combine ( repoRoot , ".." , ".." , ".." , ".." , ".." ) ) ;
1224#endif
1325
14- var folderPath = Path . Combine ( repoRoot , "src" , "Box2D.NET" ) ;
26+ var folderPath = Path . Combine ( repoRoot , "src" , "Box2D.NET" ) ;
1527
16- // Tests: B2BodySim, B2World, B2WorldId
17- var files = Directory . GetFiles ( folderPath , FileFilter , SearchOption . AllDirectories )
18- //.Where(w => w.Contains("B2PrismaticJointDef"))
19- //.Take(50)
20- . ToList ( ) ;
21-
22- Console . WriteLine ( $ "Found { files . Count } C# files in the folder: { folderPath } ") ;
23-
24- foreach ( var filePath in files )
25- {
26- await ProcessFileAsync ( filePath ) ;
27- }
28+ // Tests: B2BodySim, B2World, B2WorldId
29+ var files = Directory . GetFiles ( folderPath , FileFilter , SearchOption . AllDirectories )
30+ //.Where(w => w.Contains("B2PrismaticJointDef"))
31+ //.Take(50)
32+ . ToList ( ) ;
2833
29- Console . WriteLine ( "*** Processing completed *** ") ;
34+ Console . WriteLine ( $ "Found { files . Count } C# files in the folder: { folderPath } ") ;
3035
31- async Task ProcessFileAsync ( string filePath )
32- {
33- Console . WriteLine ( $ "\n Processing file: { filePath } ") ;
36+ foreach ( var filePath in files )
37+ {
38+ await ProcessFileAsync ( filePath ) ;
39+ }
3440
35- var content = await File . ReadAllTextAsync ( filePath ) ;
36- var lines = content . Split ( [ "\r \n " , "\n " ] , StringSplitOptions . None ) . ToList ( ) ;
37- var commentLineIndexes = ExtractCommentLineIndexes ( lines ) ;
41+ Console . WriteLine ( "*** Processing completed ***" ) ;
3842
39- RemovePreNamespaceComments ( lines , commentLineIndexes ) ;
43+ return 0 ;
44+ }
4045
41- if ( commentLineIndexes . Count == 0 )
46+ public static async Task ProcessFileAsync ( string filePath )
4247 {
43- Console . WriteLine ( "No comment lines found in the file." ) ;
44- return ;
45- }
48+ Console . WriteLine ( $ "\n Processing file: { filePath } ") ;
4649
47- var commentBlocks = ExtractCommentBlocks ( lines , commentLineIndexes ) ;
50+ var content = await File . ReadAllTextAsync ( filePath ) ;
51+ var lines = content . Split ( [ "\r \n " , "\n " ] , StringSplitOptions . None ) . ToList ( ) ;
52+ var commentLineIndexes = ExtractCommentLineIndexes ( lines ) ;
4853
49- if ( commentBlocks . Count == 0 )
50- {
51- Console . WriteLine ( "No comment blocks found." ) ;
52- return ;
53- }
54+ RemovePreNamespaceComments ( lines , commentLineIndexes ) ;
5455
55- ConvertCommentsToTripleSlash ( lines , commentBlocks ) ;
56+ if ( commentLineIndexes . Count == 0 )
57+ {
58+ Console . WriteLine ( "No comment lines found in the file." ) ;
59+ return ;
60+ }
5661
57- WrapCommentsWithSummaryTags ( lines , commentBlocks ) ;
62+ var commentBlocks = ExtractCommentBlocks ( lines , commentLineIndexes ) ;
5863
59- //File.WriteAllText(filePath.Replace(".cs", ".xmlcomments.cs"), string.Join(Environment.NewLine, lines));
60- File . WriteAllText ( filePath , string . Join ( Environment . NewLine , lines ) ) ;
64+ if ( commentBlocks . Count == 0 )
65+ {
66+ Console . WriteLine ( "No comment blocks found." ) ;
67+ return ;
68+ }
6169
62- Console . WriteLine ( $ "Output written to: { filePath } ") ;
63- }
70+ ConvertCommentsToTripleSlash ( lines , commentBlocks ) ;
6471
65- static void RemovePreNamespaceComments ( List < string > lines , List < int > commentLineIndexes )
66- {
67- var namespaceLineIndex = lines . FindIndex ( line => line . TrimStart ( ) . StartsWith ( "namespace " ) ) ;
72+ WrapCommentsWithSummaryTags ( lines , commentBlocks ) ;
6873
69- commentLineIndexes . RemoveAll ( index => index < namespaceLineIndex ) ;
70- }
74+ //File.WriteAllText(filePath.Replace(".cs", ".xmlcomments.cs"), string.Join(Environment.NewLine, lines) );
75+ File . WriteAllText ( filePath , string . Join ( Environment . NewLine , lines ) ) ;
7176
72- static List < CommentBlock > ExtractCommentBlocks ( List < string > lines , List < int > commentLineIndexes )
73- {
74- var commentBlocks = new List < CommentBlock > ( ) ;
75- var startIndex = commentLineIndexes [ 0 ] ;
76- var endIndex = commentLineIndexes [ 0 ] ;
77+ Console . WriteLine ( $ "Output written to: { filePath } ") ;
78+ }
7779
78- if ( commentLineIndexes . Count == 1 )
80+ public static void RemovePreNamespaceComments ( List < string > lines , List < int > commentLineIndexes )
7981 {
80- AddBlockIfFollowedByPublic ( startIndex , endIndex ) ;
82+ var namespaceLineIndex = lines . FindIndex ( line => line . TrimStart ( ) . StartsWith ( "namespace " ) ) ;
8183
82- return commentBlocks ;
84+ commentLineIndexes . RemoveAll ( index => index < namespaceLineIndex ) ;
8385 }
8486
85- for ( int i = 1 ; i < commentLineIndexes . Count ; i ++ )
87+ public static List < CommentBlock > ExtractCommentBlocks ( List < string > lines , List < int > commentLineIndexes )
8688 {
87- var nextIndex = commentLineIndexes [ i ] ;
89+ var commentBlocks = new List < CommentBlock > ( ) ;
90+ var startIndex = commentLineIndexes [ 0 ] ;
91+ var endIndex = commentLineIndexes [ 0 ] ;
8892
89- if ( nextIndex - endIndex ! = 1 )
93+ if ( commentLineIndexes . Count = = 1 )
9094 {
9195 AddBlockIfFollowedByPublic ( startIndex , endIndex ) ;
92- startIndex = nextIndex ;
93- }
9496
95- endIndex = nextIndex ;
97+ return commentBlocks ;
98+ }
9699
97- if ( i == commentLineIndexes . Count - 1 )
100+ for ( int i = 1 ; i < commentLineIndexes . Count ; i ++ )
98101 {
99- AddBlockIfFollowedByPublic ( startIndex , endIndex ) ;
102+ var nextIndex = commentLineIndexes [ i ] ;
103+
104+ if ( nextIndex - endIndex != 1 )
105+ {
106+ AddBlockIfFollowedByPublic ( startIndex , endIndex ) ;
107+ startIndex = nextIndex ;
108+ }
109+
110+ endIndex = nextIndex ;
111+
112+ if ( i == commentLineIndexes . Count - 1 )
113+ {
114+ AddBlockIfFollowedByPublic ( startIndex , endIndex ) ;
115+ }
100116 }
101- }
102117
103- return commentBlocks ;
118+ return commentBlocks ;
104119
105- void AddBlockIfFollowedByPublic ( int startIndex , int endIndex )
106- {
107- if ( endIndex + 1 < lines . Count && lines [ endIndex + 1 ] . Contains ( "public" ) )
120+ void AddBlockIfFollowedByPublic ( int startIndex , int endIndex )
108121 {
109- commentBlocks . Add ( new CommentBlock ( startIndex , endIndex ) ) ;
122+ if ( endIndex + 1 < lines . Count && lines [ endIndex + 1 ] . Contains ( "public" ) )
123+ {
124+ commentBlocks . Add ( new CommentBlock ( startIndex , endIndex ) ) ;
125+ }
110126 }
111127 }
112- }
113128
114- List < int > ExtractCommentLineIndexes ( List < string > lines )
115- {
116- return lines
117- . Select ( ( line , index ) => ( line , index ) )
118- . Where ( item => commentStarts . Any ( commentStart =>
119- item . line . TrimStart ( ) . StartsWith ( commentStart ) ) )
120- . Select ( item => item . index )
121- . ToList ( ) ;
122- }
123-
124- static void ConvertCommentsToTripleSlash ( List < string > lines , List < CommentBlock > commentBlocks )
125- {
126- foreach ( var block in commentBlocks )
129+ public static List < int > ExtractCommentLineIndexes ( List < string > lines )
127130 {
128- Console . WriteLine ( $ "Comment block from { block . StartIndex + 1 } to { block . EndIndex + 1 } (length: { block . Length } )") ;
131+
132+ return lines
133+ . Select ( ( line , index ) => ( line , index ) )
134+ . Where ( item => commentStarts . Any ( commentStart =>
135+ item . line . TrimStart ( ) . StartsWith ( commentStart ) ) )
136+ . Select ( item => item . index )
137+ . ToList ( ) ;
138+ }
129139
130- for ( int i = 0 ; i < block . Length ; i ++ )
140+ public static void ConvertCommentsToTripleSlash ( List < string > lines , List < CommentBlock > commentBlocks )
141+ {
142+ foreach ( var block in commentBlocks )
131143 {
132- int lineIndex = block . StartIndex + i ;
144+ Console . WriteLine ( $ "Comment block from { block . StartIndex + 1 } to { block . EndIndex + 1 } (length: { block . Length } )" ) ;
133145
134- if ( ! lines [ lineIndex ] . TrimStart ( ) . StartsWith ( TripleSlashCommentPrefix ) )
146+ for ( int i = 0 ; i < block . Length ; i ++ )
135147 {
136- lines [ lineIndex ] = lines [ lineIndex ] . Replace ( DoubleSlashCommentPrefix , "/// " ) ;
148+ int lineIndex = block . StartIndex + i ;
149+
150+ if ( ! lines [ lineIndex ] . TrimStart ( ) . StartsWith ( TripleSlashCommentPrefix ) )
151+ {
152+ lines [ lineIndex ] = lines [ lineIndex ] . Replace ( DoubleSlashCommentPrefix , "/// " ) ;
153+ }
137154 }
138155 }
139156 }
140- }
141157
142- void WrapCommentsWithSummaryTags ( List < string > lines , List < CommentBlock > commentBlocks )
143- {
144- for ( int i = commentBlocks . Count - 1 ; i >= 0 ; i -- )
158+ public static void WrapCommentsWithSummaryTags ( List < string > lines , List < CommentBlock > commentBlocks )
145159 {
146- var block = commentBlocks [ i ] ;
147- var indentation = GetIndentation ( lines [ block . StartIndex ] ) ;
160+ for ( int i = commentBlocks . Count - 1 ; i >= 0 ; i -- )
161+ {
162+ var block = commentBlocks [ i ] ;
163+ var indentation = GetIndentation ( lines [ block . StartIndex ] ) ;
164+
165+ lines . Insert ( block . EndIndex + 1 , $ "{ indentation } { SummaryEnd } ") ;
166+ lines . Insert ( block . StartIndex , $ "{ indentation } { SummaryStart } ") ;
167+ }
148168
149- lines . Insert ( block . EndIndex + 1 , $ "{ indentation } { SummaryEnd } ") ;
150- lines . Insert ( block . StartIndex , $ "{ indentation } { SummaryStart } ") ;
169+ string GetIndentation ( string line ) => new ( ' ' , line . Length - line . TrimStart ( ) . Length ) ;
151170 }
152171
153- string GetIndentation ( string line ) => new ( ' ' , line . Length - line . TrimStart ( ) . Length ) ;
154- }
172+ public record class CommentBlock ( int StartIndex , int EndIndex )
173+ {
174+ public int Length => EndIndex - StartIndex + 1 ;
175+ }
155176
156- record class CommentBlock ( int StartIndex , int EndIndex )
157- {
158- public int Length => EndIndex - StartIndex + 1 ;
159- }
177+ }
0 commit comments