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 ;
6
6
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 ( ) ;
9
21
10
22
#if DEBUG
11
- repoRoot = Path . GetFullPath ( Path . Combine ( repoRoot , ".." , ".." , ".." , ".." , ".." ) ) ;
23
+ repoRoot = Path . GetFullPath ( Path . Combine ( repoRoot , ".." , ".." , ".." , ".." , ".." ) ) ;
12
24
#endif
13
25
14
- var folderPath = Path . Combine ( repoRoot , "src" , "Box2D.NET" ) ;
26
+ var folderPath = Path . Combine ( repoRoot , "src" , "Box2D.NET" ) ;
15
27
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 ( ) ;
28
33
29
- Console . WriteLine ( "*** Processing completed *** ") ;
34
+ Console . WriteLine ( $ "Found { files . Count } C# files in the folder: { folderPath } ") ;
30
35
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
+ }
34
40
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 ***" ) ;
38
42
39
- RemovePreNamespaceComments ( lines , commentLineIndexes ) ;
43
+ return 0 ;
44
+ }
40
45
41
- if ( commentLineIndexes . Count == 0 )
46
+ public static async Task ProcessFileAsync ( string filePath )
42
47
{
43
- Console . WriteLine ( "No comment lines found in the file." ) ;
44
- return ;
45
- }
48
+ Console . WriteLine ( $ "\n Processing file: { filePath } ") ;
46
49
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 ) ;
48
53
49
- if ( commentBlocks . Count == 0 )
50
- {
51
- Console . WriteLine ( "No comment blocks found." ) ;
52
- return ;
53
- }
54
+ RemovePreNamespaceComments ( lines , commentLineIndexes ) ;
54
55
55
- ConvertCommentsToTripleSlash ( lines , commentBlocks ) ;
56
+ if ( commentLineIndexes . Count == 0 )
57
+ {
58
+ Console . WriteLine ( "No comment lines found in the file." ) ;
59
+ return ;
60
+ }
56
61
57
- WrapCommentsWithSummaryTags ( lines , commentBlocks ) ;
62
+ var commentBlocks = ExtractCommentBlocks ( lines , commentLineIndexes ) ;
58
63
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
+ }
61
69
62
- Console . WriteLine ( $ "Output written to: { filePath } ") ;
63
- }
70
+ ConvertCommentsToTripleSlash ( lines , commentBlocks ) ;
64
71
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 ) ;
68
73
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 ) ) ;
71
76
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
+ }
77
79
78
- if ( commentLineIndexes . Count == 1 )
80
+ public static void RemovePreNamespaceComments ( List < string > lines , List < int > commentLineIndexes )
79
81
{
80
- AddBlockIfFollowedByPublic ( startIndex , endIndex ) ;
82
+ var namespaceLineIndex = lines . FindIndex ( line => line . TrimStart ( ) . StartsWith ( "namespace " ) ) ;
81
83
82
- return commentBlocks ;
84
+ commentLineIndexes . RemoveAll ( index => index < namespaceLineIndex ) ;
83
85
}
84
86
85
- for ( int i = 1 ; i < commentLineIndexes . Count ; i ++ )
87
+ public static List < CommentBlock > ExtractCommentBlocks ( List < string > lines , List < int > commentLineIndexes )
86
88
{
87
- var nextIndex = commentLineIndexes [ i ] ;
89
+ var commentBlocks = new List < CommentBlock > ( ) ;
90
+ var startIndex = commentLineIndexes [ 0 ] ;
91
+ var endIndex = commentLineIndexes [ 0 ] ;
88
92
89
- if ( nextIndex - endIndex ! = 1 )
93
+ if ( commentLineIndexes . Count = = 1 )
90
94
{
91
95
AddBlockIfFollowedByPublic ( startIndex , endIndex ) ;
92
- startIndex = nextIndex ;
93
- }
94
96
95
- endIndex = nextIndex ;
97
+ return commentBlocks ;
98
+ }
96
99
97
- if ( i == commentLineIndexes . Count - 1 )
100
+ for ( int i = 1 ; i < commentLineIndexes . Count ; i ++ )
98
101
{
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
+ }
100
116
}
101
- }
102
117
103
- return commentBlocks ;
118
+ return commentBlocks ;
104
119
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 )
108
121
{
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
+ }
110
126
}
111
127
}
112
- }
113
128
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 )
127
130
{
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
+ }
129
139
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 )
131
143
{
132
- int lineIndex = block . StartIndex + i ;
144
+ Console . WriteLine ( $ "Comment block from { block . StartIndex + 1 } to { block . EndIndex + 1 } (length: { block . Length } )" ) ;
133
145
134
- if ( ! lines [ lineIndex ] . TrimStart ( ) . StartsWith ( TripleSlashCommentPrefix ) )
146
+ for ( int i = 0 ; i < block . Length ; i ++ )
135
147
{
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
+ }
137
154
}
138
155
}
139
156
}
140
- }
141
157
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 )
145
159
{
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
+ }
148
168
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 ) ;
151
170
}
152
171
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
+ }
155
176
156
- record class CommentBlock ( int StartIndex , int EndIndex )
157
- {
158
- public int Length => EndIndex - StartIndex + 1 ;
159
- }
177
+ }
0 commit comments