@@ -16,7 +16,14 @@ internal class ConfigurationFileLocator(
16
16
public const string DefaultAlternativeFileName = "GitVersion.yaml" ;
17
17
public const string DefaultFileNameDotted = $ ".{ DefaultFileName } ";
18
18
public const string DefaultAlternativeFileNameDotted = $ ".{ DefaultAlternativeFileName } ";
19
- public List < string > SupportedConfigFileNames = [ DefaultFileName , DefaultAlternativeFileName , DefaultFileNameDotted , DefaultAlternativeFileNameDotted ] ;
19
+
20
+ private readonly string [ ] SupportedConfigFileNames =
21
+ [
22
+ DefaultFileName ,
23
+ DefaultAlternativeFileName ,
24
+ DefaultFileNameDotted ,
25
+ DefaultAlternativeFileNameDotted
26
+ ] ;
20
27
21
28
private readonly IFileSystem fileSystem = fileSystem . NotNull ( ) ;
22
29
private readonly ILog log = log . NotNull ( ) ;
@@ -35,21 +42,28 @@ public void Verify(string? workingDirectory, string? projectRootDirectory)
35
42
{
36
43
if ( directory is null ) return null ;
37
44
38
- string ? [ ] candidates = [ this . ConfigurationFile , .. SupportedConfigFileNames ] ;
39
- var candidatePaths =
40
- from candidate in candidates
41
- where ! candidate . IsNullOrWhiteSpace ( )
42
- select PathHelper . Combine ( directory , candidate ) ;
45
+ string [ ] candidates = ! string . IsNullOrWhiteSpace ( this . ConfigurationFile )
46
+ ? [ this . ConfigurationFile , .. this . SupportedConfigFileNames ]
47
+ : this . SupportedConfigFileNames ;
43
48
44
- foreach ( var candidatePath in candidatePaths )
49
+ foreach ( var fileName in candidates )
45
50
{
46
- this . log . Debug ( $ "Trying to find configuration file at '{ candidatePath } '") ;
47
- if ( fileSystem . File . Exists ( candidatePath ) )
51
+ this . log . Debug ( $ "Trying to find configuration file { fileName } at '{ directory } '") ;
52
+ if ( directory != null && fileSystem . Directory . Exists ( directory ) )
48
53
{
49
- this . log . Info ( $ "Found configuration file at '{ candidatePath } '") ;
50
- return candidatePath ;
54
+ var files = fileSystem . Directory . GetFiles ( directory ) ;
55
+
56
+ var matchingFile = files . FirstOrDefault ( file =>
57
+ string . Equals ( fileSystem . Path . GetFileName ( file ) , fileName , StringComparison . OrdinalIgnoreCase ) ) ;
58
+
59
+ if ( matchingFile != null )
60
+ {
61
+ this . log . Info ( $ "Found configuration file at '{ matchingFile } '") ;
62
+ return matchingFile ;
63
+ }
51
64
}
52
- this . log . Debug ( $ "Configuration file not found at '{ candidatePath } '") ;
65
+
66
+ this . log . Debug ( $ "Configuration file { fileName } not found at '{ directory } '") ;
53
67
}
54
68
55
69
return null ;
@@ -60,22 +74,18 @@ private void WarnAboutAmbiguousConfigFileSelection(string? workingDirectory, str
60
74
var workingConfigFile = GetConfigurationFile ( workingDirectory ) ;
61
75
var projectRootConfigFile = GetConfigurationFile ( projectRootDirectory ) ;
62
76
63
- var hasConfigInWorkingDirectory = workingConfigFile != null ;
64
- var hasConfigInProjectRootDirectory = projectRootConfigFile != null ;
77
+ var hasConfigInWorkingDirectory = workingConfigFile is not null ;
78
+ var hasConfigInProjectRootDirectory = projectRootConfigFile is not null ;
65
79
66
80
if ( hasConfigInProjectRootDirectory && hasConfigInWorkingDirectory )
67
81
{
68
82
throw new WarningException ( $ "Ambiguous configuration file selection from '{ workingConfigFile } ' and '{ projectRootConfigFile } '") ;
69
83
}
70
84
71
- if ( ! hasConfigInProjectRootDirectory && ! hasConfigInWorkingDirectory )
72
- {
73
- if ( ! SupportedConfigFileNames . Any ( entry => entry . Equals ( this . ConfigurationFile , StringComparison . OrdinalIgnoreCase ) ) )
74
- {
75
- workingConfigFile = PathHelper . Combine ( workingDirectory , this . ConfigurationFile ) ;
76
- projectRootConfigFile = PathHelper . Combine ( projectRootDirectory , this . ConfigurationFile ) ;
77
- throw new WarningException ( $ "The configuration file was not found at '{ workingConfigFile } ' or '{ projectRootConfigFile } '") ;
78
- }
79
- }
85
+ if ( hasConfigInProjectRootDirectory || hasConfigInWorkingDirectory || this . SupportedConfigFileNames . Any ( entry => entry . Equals ( this . ConfigurationFile , StringComparison . OrdinalIgnoreCase ) ) ) return ;
86
+
87
+ workingConfigFile = PathHelper . Combine ( workingDirectory , this . ConfigurationFile ) ;
88
+ projectRootConfigFile = PathHelper . Combine ( projectRootDirectory , this . ConfigurationFile ) ;
89
+ throw new WarningException ( $ "The configuration file was not found at '{ workingConfigFile } ' or '{ projectRootConfigFile } '") ;
80
90
}
81
91
}
0 commit comments