2
2
using NeoDoc . Langs ;
3
3
using NeoDoc . Params ;
4
4
using Newtonsoft . Json ;
5
+ using CommandLine ;
5
6
using System ;
6
7
using System . Collections . Generic ;
7
8
using System . IO ;
@@ -16,13 +17,13 @@ namespace NeoDoc
16
17
{
17
18
internal static class NeoDoc
18
19
{
19
- public const bool DEBUGGING = false ;
20
- public static int Progress = 0 ;
20
+ public static bool DEBUGGING ;
21
+ public static int Progress ;
21
22
public static string NEWDIR = "" ;
23
+ public static string ErrorLogFormat = "auto" ;
22
24
23
25
public enum ERROR_CODES : int
24
26
{
25
- INVALID_COMMAND_LINE = 1 ,
26
27
BAD_ARGUMENTS = 2 ,
27
28
NOT_EXISTS = 3 ,
28
29
MISSING_ESSENTIAL_PARAMS = 4 ,
@@ -43,38 +44,44 @@ public static void Empty(this DirectoryInfo directory)
43
44
subDirectory . Delete ( true ) ;
44
45
}
45
46
46
- public static int Main ( )
47
- {
48
- string [ ] args = Environment . GetCommandLineArgs ( ) ;
47
+ public class Options
48
+ {
49
+ [ Option ( 'f' , "format" , Default = "auto" , Required = false , HelpText = "Choose a log message format (auto, standard, github)." ) ]
50
+ public string LogFormat { get ; set ; }
49
51
50
- if ( args . Length == 1 )
51
- {
52
- Console . Error . WriteLine ( "Invalid command line (missing folder path)!" ) ;
52
+ [ Option ( 'v' , "verbose" , Default = false , Required = false , HelpText = "Should the output be more verbose and contain debugging logs?" ) ]
53
+ public bool Verbose { get ; set ; }
53
54
54
- return ( int ) ERROR_CODES . INVALID_COMMAND_LINE ;
55
- }
55
+ [ Value ( 0 , Required = true , HelpText = "The source folder to check and read files from." ) ]
56
+ public string Folder { get ; set ; }
56
57
57
- string folder = args [ 1 ] ;
58
-
59
- if ( string . IsNullOrEmpty ( folder ) )
60
- {
61
- Console . Error . WriteLine ( "Provided folder path is null or empty!" ) ;
58
+ [ Value ( 1 , HelpText = "Set this to a path and you will generate json output (usually used by external applications such as NeoVis)." ) ]
59
+ public string OutputFolder { get ; set ; }
60
+ }
62
61
63
- return ( int ) ERROR_CODES . BAD_ARGUMENTS ;
64
- }
62
+ public static int Main ( string [ ] args )
63
+ {
64
+ return Parser . Default . ParseArguments < Options > ( args )
65
+ . MapResult (
66
+ Run ,
67
+ error => ( int ) ERROR_CODES . BAD_ARGUMENTS
68
+ ) ;
69
+ }
65
70
66
- if ( ! Directory . Exists ( folder ) )
67
- {
68
- Console . Error . WriteLine ( "Provided folder '" + folder + "' does not exists!" ) ;
71
+ public static int Run ( Options options ) {
72
+ NEWDIR = options . OutputFolder ;
73
+ ErrorLogFormat = options . LogFormat ;
74
+ DEBUGGING = options . Verbose ;
69
75
70
- return ( int ) ERROR_CODES . NOT_EXISTS ;
71
- }
76
+ if ( ! Directory . Exists ( options . Folder ) )
77
+ {
78
+ Console . Error . WriteLine ( "Provided folder '" + options . Folder + "' does not exists!" ) ;
72
79
73
- if ( args . Length > 2 )
74
- NEWDIR = args [ 2 ] ;
80
+ return ( int ) ERROR_CODES . NOT_EXISTS ;
81
+ }
75
82
76
- // Build the file tree
77
- string [ ] files = Directory . GetFiles ( folder , "*.*" , SearchOption . AllDirectories ) ;
83
+ // Build the file tree
84
+ string [ ] files = Directory . GetFiles ( options . Folder , "*.*" , SearchOption . AllDirectories ) ;
78
85
79
86
// Prepare the files
80
87
List < FileParser > fileParsers = new List < FileParser > ( ) ;
@@ -87,7 +94,7 @@ public static int Main()
87
94
{
88
95
string file = files [ n ] ;
89
96
90
- string relPath = file . Remove ( 0 , folder . Length ) ;
97
+ string relPath = file . Remove ( 0 , options . Folder . Length ) ;
91
98
relPath = relPath . TrimStart ( '\\ ' ) ;
92
99
relPath = relPath . Replace ( '\\ ' , '/' ) ;
93
100
@@ -144,11 +151,8 @@ public static int Main()
144
151
return Environment . ExitCode ;
145
152
}
146
153
147
- #pragma warning disable IDE0060 // Nicht verwendete Parameter entfernen
148
154
internal static void WriteDebugInfo ( string debugInfo )
149
- #pragma warning restore IDE0060 // Nicht verwendete Parameter entfernen
150
155
{
151
- #pragma warning disable CS0162 // Unerreichbarer Code wurde entdeckt.
152
156
if ( ! DEBUGGING )
153
157
return ;
154
158
@@ -159,10 +163,9 @@ internal static void WriteDebugInfo(string debugInfo)
159
163
Console . Out . WriteLine ( debugInfo ) ;
160
164
161
165
Console . ForegroundColor = oldColor ;
162
- #pragma warning restore CS0162 // Unerreichbarer Code wurde entdeckt.
163
166
}
164
167
165
- internal static void WriteErrors ( string title , List < string > errors , string relPath , int ? foundLine , int ? exitCode )
168
+ internal static void WriteErrors ( string message , List < string > errors , string relPath , int ? foundLine , int ? exitCode )
166
169
{
167
170
if ( exitCode != null )
168
171
Environment . ExitCode = ( int ) exitCode ;
@@ -173,17 +176,56 @@ internal static void WriteErrors(string title, List<string> errors, string relPa
173
176
174
177
TextWriter textWriter = Console . Error ;
175
178
179
+ textWriter . WriteLine ( formatLogMessage ( message , errors , relPath , foundLine , exitCode ) ) ;
180
+
181
+ Console . ForegroundColor = oldColor ;
182
+ }
183
+
184
+ private static string formatLogMessage ( string message , List < string > errors , string relPath , int ? foundLine , int ? exitCode ) {
185
+ switch ( ErrorLogFormat ) {
186
+ case "auto" :
187
+ if ( System . Environment . GetEnvironmentVariable ( "GITHUB_ACTIONS" ) != null && System . Environment . GetEnvironmentVariable ( "GITHUB_WORKFLOW" ) != null ) {
188
+ return formatGithub ( message , errors , relPath , foundLine , exitCode ) ;
189
+ }
190
+ return formatStandard ( message , errors , relPath , foundLine , exitCode ) ;
191
+ case "standard" :
192
+ return formatStandard ( message , errors , relPath , foundLine , exitCode ) ;
193
+ case "github" :
194
+ return formatGithub ( message , errors , relPath , foundLine , exitCode ) ;
195
+ default :
196
+ Console . Error . WriteLine ( "Could not understand the given format param, falling back to mode: standard" ) ;
197
+ return formatStandard ( message , errors , relPath , foundLine , exitCode ) ;
198
+ }
199
+ }
200
+
201
+ private static string formatStandard ( string message , List < string > errors , string relPath , int ? foundLine , int ? exitCode ) {
176
202
StringBuilder errorBuilder = new StringBuilder ( ) ;
177
203
178
- errorBuilder . AppendLine ( "Error " + ( exitCode != null ? ( ( int ) exitCode ) . ToString ( ) : "???" ) + ": " + ( relPath ?? "?" ) + ": [Warning] line " + ( foundLine != null ? ( ( int ) foundLine ) . ToString ( ) : "?" ) + ": " + title ) ;
204
+ errorBuilder . AppendLine ( "Error " + ( exitCode != null ? ( ( int ) exitCode ) . ToString ( ) : "???" ) + ": " + ( relPath ?? "?" ) + ": [Warning] line " + ( foundLine != null ? ( ( int ) foundLine ) . ToString ( ) : "?" ) + ": " + message ) ;
179
205
180
206
if ( errors != null )
181
207
foreach ( string error in errors )
182
208
errorBuilder . AppendLine ( error ) ;
209
+
210
+ return errorBuilder . ToString ( ) ;
211
+ }
183
212
184
- textWriter . WriteLine ( errorBuilder . ToString ( ) ) ;
213
+ private static string formatGithub ( string message , List < string > errors , string relPath , int ? foundLine , int ? exitCode ) {
214
+ string output = "::error " ;
215
+
216
+ output += "file=" + ( relPath ?? "?" ) + "," ;
217
+ output += "line=" + ( foundLine != null ? ( ( int ) foundLine ) . ToString ( ) : "?" ) ;
218
+ output += "::" ;
219
+ output += "Code (" + ( exitCode != null ? ( ( int ) exitCode ) . ToString ( ) : "???" ) + ") " ;
220
+ output += message + "\\ n" ;
185
221
186
- Console . ForegroundColor = oldColor ;
222
+ if ( errors != null )
223
+ output += "Errors: \\ n" ;
224
+ foreach ( string error in errors )
225
+ output += error + "\\ n" ;
226
+
227
+ // Also output human readable format for the log files
228
+ return output + "\n " + formatStandard ( message , errors , relPath , foundLine , exitCode ) ;
187
229
}
188
230
189
231
private static WrapperParam [ ] ProcessFileParsers ( List < FileParser > fileParsers , out SortedDictionary < string , SortedDictionary < string , List < DataStructure > > > globalsDict )
0 commit comments