@@ -2,7 +2,6 @@ package checkstyle;
22
33import checkstyle .ChecksInfo ;
44import checkstyle .Config ;
5- import checkstyle .CheckMessage .SeverityLevel ;
65import checkstyle .checks .Check ;
76import checkstyle .reporter .IReporter ;
87import checkstyle .reporter .JSONReporter ;
@@ -13,11 +12,14 @@ import checkstyle.reporter.CodeClimateReporter;
1312import checkstyle .reporter .ExitCodeReporter ;
1413import checkstyle .reporter .ReporterManager ;
1514import checkstyle .errors .Error ;
15+ import checkstyle .detect .DetectCodingStyle ;
16+ import checkstyle .utils .ConfigUtils ;
1617import haxe .CallStack ;
1718import haxe .Json ;
1819import hxargs .Args ;
1920import sys .FileSystem ;
2021import sys .io .File ;
22+ import haxe .io .Path ;
2123
2224class Main {
2325
@@ -45,6 +47,7 @@ class Main {
4547 var numberOfCheckerThreads : Int ;
4648 var overrideCheckerThreads : Int ;
4749 var disableThreads : Bool ;
50+ var detectConfig : String ;
4851 var seenConfigPaths : Array <String >;
4952
5053 function new () {
@@ -59,6 +62,7 @@ class Main {
5962 excludePath = null ;
6063 numberOfCheckerThreads = 5 ;
6164 overrideCheckerThreads = 0 ;
65+ detectConfig = null ;
6266 }
6367
6468 function run (args : Array <String >) {
@@ -82,6 +86,7 @@ class Main {
8286 @doc (" Show checks missing from active config" ) [" -show-missing-checks" ] => function () SHOW_MISSING_CHECKS = true ,
8387 @doc (" Sets the number of checker threads" ) [" -checkerthreads" ] => function (num : Int ) overrideCheckerThreads = num ,
8488 @doc (" Do not use checker threads" ) [" -nothreads" ] => function () disableThreads = true ,
89+ @doc (" Try to detect your coding style (experimental)" ) [" -detect" ] => function (path ) detectCodingStyle (path ),
8590 @doc (" Show report [DEPRECATED]" ) [" -report" ] => function () Sys .println (" \n -report is no longer needed." ),
8691 _ => function (arg : String ) failWith (" Unknown command: " + arg )
8792 ]);
@@ -120,6 +125,7 @@ class Main {
120125 loadConfig (configPath );
121126
122127 if (excludePath != null ) loadExcludeConfig (excludePath );
128+
123129 start ();
124130 }
125131
@@ -133,7 +139,7 @@ class Main {
133139
134140 public function parseAndValidateConfig (config : Config ) {
135141
136- validateAllowedFields (config , Reflect .fields (getEmptyConfig ()), " Config" );
142+ validateAllowedFields (config , Reflect .fields (ConfigUtils . getEmptyConfig ()), " Config" );
137143
138144 if (! config .extendsConfigPath .isEmpty ()) {
139145 if (seenConfigPaths .contains (config .extendsConfigPath )) failWith (" extendsConfig: config file loop detected!" );
@@ -337,47 +343,22 @@ class Main {
337343 Sys .exit (0 );
338344 }
339345
340- function getEmptyConfig (): Config {
341- return {
342- defaultSeverity : SeverityLevel .INFO ,
343- extendsConfigPath : " " ,
344- numberOfCheckerThreads : 5 ,
345- baseDefines : [],
346- defineCombinations : [],
347- checks : [],
348- exclude : {}
349- };
350- }
351-
352346 function generateDefaultConfig (path ) {
353347 addAllChecks ();
354- var propsNotAllowed : Array <String > = [
355- " moduleName" , " severity" , " type" , " categories" ,
356- " points" , " desc" , " currentState" , " skipOverStringStart" ,
357- " commentStartRE" , " commentBlockEndRE" , " stringStartRE" ,
358- " stringInterpolatedEndRE" , " stringLiteralEndRE"
359- ];
360- var config = getEmptyConfig ();
361- for (check in checker .checks ) {
362- var checkConfig : CheckConfig = {
363- type : check .getModuleName (),
364- props : {}
365- };
366- for (prop in Reflect .fields (check )) {
367- if (propsNotAllowed .contains (prop )) continue ;
368- Reflect .setField (checkConfig .props , prop , Reflect .field (check , prop ));
369- }
370- config .checks .push (checkConfig );
371- }
372-
373- var file = File .write (path , false );
374- file .writeString (Json .stringify (config , null , " \t " ));
375- file .close ();
348+ ConfigUtils .saveConfig (checker , path );
376349 Sys .exit (0 );
377350 }
378351
379- function pathJoin (s : String , t : String ): String {
380- return s + " /" + t ;
352+ function detectCodingStyle (path : String ) {
353+ var checks : Array <Check > = [];
354+ for (checkInfo in info .checks ()) {
355+ if (checkInfo .isAlias ) continue ;
356+ var check : Check = info .build (checkInfo .name );
357+ checks .push (check );
358+ }
359+ var detectedChecks : Array <CheckConfig > = DetectCodingStyle .detectCodingStyle (checks , buildFileList ());
360+ if (detectedChecks .length > 0 ) ConfigUtils .saveCheckConfigList (detectedChecks , path );
361+ Sys .exit (0 );
381362 }
382363
383364 function start () {
@@ -386,15 +367,10 @@ class Main {
386367 Sys .exit (0 );
387368 }
388369
389- var files : Array <String > = [];
390- for (path in paths ) traverse (path , files );
391- files .sortStrings ();
392-
393- var i : Int = 0 ;
394- var toProcess : Array <CheckFile > = [for (file in files ) { name :file , content :null , index :i ++ }];
370+ var toProcess : Array <CheckFile > = buildFileList ();
395371
396- ReporterManager .INSTANCE .addReporter (createReporter (files .length ));
397- if (SHOW_PROGRESS ) ReporterManager .INSTANCE .addReporter (new ProgressReporter (files .length ));
372+ ReporterManager .INSTANCE .addReporter (createReporter (toProcess .length ));
373+ if (SHOW_PROGRESS ) ReporterManager .INSTANCE .addReporter (new ProgressReporter (toProcess .length ));
398374 if (EXIT_CODE ) ReporterManager .INSTANCE .addReporter (new ExitCodeReporter ());
399375
400376 #if (neko || cpp)
@@ -418,11 +394,20 @@ class Main {
418394 #end
419395 }
420396
397+ function buildFileList (): Array <CheckFile > {
398+ var files : Array <String > = [];
399+ for (path in paths ) traverse (path , files );
400+ files .sortStrings ();
401+
402+ var i : Int = 0 ;
403+ return [for (file in files ) { name :file , content :null , index :i ++ }];
404+ }
405+
421406 function traverse (path : String , files : Array <String >) {
422407 try {
423408 if (FileSystem .isDirectory (path ) && ! isExcludedFromAll (path )) {
424409 var nodes = FileSystem .readDirectory (path );
425- for (child in nodes ) traverse (pathJoin ( path , child ), files );
410+ for (child in nodes ) traverse (Path . join ([ path , child ] ), files );
426411 }
427412 else if (~/ (. hx)$ / i .match (path ) && ! isExcludedFromAll (path )) {
428413 files .push (path );
0 commit comments