@@ -4,37 +4,46 @@ namespace CodeLineCounter.Utils
44{
55 public static partial class FileUtils
66 {
7- public static List < string > GetAllCsFiles ( string rootPath )
7+ // Default file extensions
8+ private const string DEFAULT_CODE_EXTENSION = "*.cs" ;
9+ private const string DEFAULT_SOLUTION_EXTENSION = "*.sln" ;
10+ private const string DEFAULT_PROJECT_EXTENSION = ".csproj" ;
11+
12+ public static List < string > GetAllCodeFiles ( string rootPath , string fileExtension = DEFAULT_CODE_EXTENSION )
813 {
9- return Directory . GetFiles ( rootPath , "*.cs" , SearchOption . AllDirectories )
10- . Where ( f => ! f . Contains ( @"\obj\" ) )
11- . ToList ( ) ;
14+ var excludeFolders = new [ ] { @"\obj\" , @"\bin\" , @"\.*" } ;
15+ return Directory . GetFiles ( rootPath , fileExtension , SearchOption . AllDirectories )
16+ . Where ( f => ! excludeFolders . Any ( ef => f . Contains ( ef ) ) )
17+ . ToList ( ) ;
1218 }
1319
14- public static List < string > GetSolutionFiles ( string rootPath )
20+ // For backward compatibility
21+ public static List < string > GetAllCsFiles ( string rootPath ) => GetAllCodeFiles ( rootPath ) ;
22+
23+ public static List < string > GetSolutionFiles ( string rootPath , string fileExtension = DEFAULT_SOLUTION_EXTENSION )
1524 {
1625 if ( ! Directory . Exists ( rootPath ) )
1726 {
18- throw new UnauthorizedAccessException ( $ "Access to the path '{ rootPath } ' is denied .") ;
27+ throw new DirectoryNotFoundException ( $ "Directory '{ rootPath } ' not found .") ;
1928 }
2029
21- return Directory . GetFiles ( rootPath , "*.sln" , SearchOption . TopDirectoryOnly ) . ToList ( ) ;
30+ return Directory . GetFiles ( rootPath , fileExtension , SearchOption . TopDirectoryOnly ) . ToList ( ) ;
2231 }
2332
24- public static List < string > GetProjectFiles ( string solutionFilePath )
33+ public static List < string > GetProjectFiles ( string solutionFilePath , string projectExtension = DEFAULT_PROJECT_EXTENSION )
2534 {
2635 if ( ! File . Exists ( solutionFilePath ) )
2736 {
28- throw new UnauthorizedAccessException ( $ "Access to the path '{ solutionFilePath } ' is denied .") ;
37+ throw new FileNotFoundException ( $ "File '{ solutionFilePath } ' not found .") ;
2938 }
3039
3140 var projectFiles = new List < string > ( ) ;
3241 var lines = File . ReadAllLines ( solutionFilePath ) ;
3342
3443 foreach ( var line in lines )
3544 {
36- // Search for lines containing projects (Project("...") = "...", "...", "...")
37- var match = MyRegex ( ) . Match ( line ) ;
45+ // Search for lines containing projects with the specified extension
46+ var match = GenerateProjectRegex ( projectExtension ) . Match ( line ) ;
3847 if ( match . Success )
3948 {
4049 var relativePath = match . Groups [ 1 ] . Value ;
@@ -48,11 +57,15 @@ public static List<string> GetProjectFiles(string solutionFilePath)
4857
4958 public static string GetBasePath ( )
5059 {
51- // Arrange
5260 return Path . GetDirectoryName ( AppContext . BaseDirectory ) ?? string . Empty ;
5361 }
5462
63+ private static Regex GenerateProjectRegex ( string projectExtension )
64+ {
65+ return new Regex ( $@ "Project\(""{{.*}}""\) = "".*"", ""(.*{ Regex . Escape ( projectExtension ) } )""", RegexOptions . IgnoreCase , TimeSpan . FromMilliseconds ( 100 ) ) ;
66+ }
67+
5568 [ GeneratedRegex ( @"Project\(""{.*}""\) = "".*"", ""(.*\.csproj)""" ) ]
5669 private static partial Regex MyRegex ( ) ;
5770 }
58- }
71+ }
0 commit comments