1
1
using GitVersion . Extensions ;
2
2
using GitVersion . Helpers ;
3
+ using GitVersion . Logging ;
3
4
using Microsoft . Extensions . Options ;
4
5
5
6
namespace GitVersion . Configuration ;
6
7
7
- internal class ConfigurationFileLocator ( IFileSystem fileSystem , IConfigurationSerializer configurationSerializer , IOptions < GitVersionOptions > options )
8
+ internal class ConfigurationFileLocator (
9
+ IFileSystem fileSystem ,
10
+ ILog log ,
11
+ IOptions < GitVersionOptions > options )
8
12
: IConfigurationFileLocator
9
13
{
10
14
public const string DefaultFileName = "GitVersion.yml" ;
11
15
public const string DefaultAlternativeFileName = "GitVersion.yaml" ;
12
16
13
- private readonly string ? configurationFile = options . Value . ConfigurationInfo . ConfigurationFile ;
17
+ private readonly IFileSystem fileSystem = fileSystem . NotNull ( ) ;
18
+ private readonly ILog log = log . NotNull ( ) ;
19
+ private readonly IOptions < GitVersionOptions > options = options . NotNull ( ) ;
14
20
15
- public bool TryGetConfigurationFile ( string ? workingDirectory , string ? projectRootDirectory , out string ? configFilePath )
16
- =>
17
- HasConfigurationFile ( workingDirectory , out configFilePath )
18
- || HasConfigurationFile ( projectRootDirectory , out configFilePath ) ;
21
+ private string ? ConfigurationFile => options . Value . ConfigurationInfo . ConfigurationFile ;
19
22
20
23
public void Verify ( string ? workingDirectory , string ? projectRootDirectory )
21
24
{
22
- if ( Path . IsPathRooted ( this . configurationFile ) ) return ;
25
+ if ( Path . IsPathRooted ( this . ConfigurationFile ) ) return ;
23
26
if ( PathHelper . Equal ( workingDirectory , projectRootDirectory ) ) return ;
24
27
WarnAboutAmbiguousConfigFileSelection ( workingDirectory , projectRootDirectory ) ;
25
28
}
26
29
27
- public IGitVersionConfiguration ReadConfiguration ( string ? configFilePath )
30
+ public string ? GetConfigurationFile ( string ? directory )
28
31
{
29
- if ( configFilePath == null || ! fileSystem . Exists ( configFilePath ) ) return GitHubFlowConfigurationBuilder . New . Build ( ) ;
30
-
31
- var readAllText = fileSystem . ReadAllText ( configFilePath ) ;
32
- var configuration = configurationSerializer . ReadConfiguration ( readAllText )
33
- ?? GitHubFlowConfigurationBuilder . New . Build ( ) ;
34
- return configuration ;
35
- }
36
-
37
- public IReadOnlyDictionary < object , object ? > ? ReadOverrideConfiguration ( string ? configFilePath )
38
- {
39
- if ( configFilePath == null || ! fileSystem . Exists ( configFilePath ) ) return null ;
40
-
41
- var readAllText = fileSystem . ReadAllText ( configFilePath ) ;
42
-
43
- return configurationSerializer . Deserialize < Dictionary < object , object ? > > ( readAllText ) ;
44
- }
45
-
46
- private bool HasConfigurationFile ( string ? workingDirectory , out string ? path )
47
- {
48
- bool HasConfigurationFileAt ( string fileName , out string ? configFile )
32
+ if ( directory is null ) return null ;
33
+ string ? [ ] candidates = [ this . ConfigurationFile , DefaultFileName , DefaultAlternativeFileName ] ;
34
+ var candidatePaths =
35
+ from candidate in candidates
36
+ where ! candidate . IsNullOrWhiteSpace ( )
37
+ select PathHelper . Combine ( directory , candidate ) ;
38
+
39
+ foreach ( var candidatePath in candidatePaths )
49
40
{
50
- configFile = null ;
51
- if ( ! fileSystem . Exists ( PathHelper . Combine ( workingDirectory , fileName ) ) ) return false ;
52
-
53
- configFile = PathHelper . Combine ( workingDirectory , fileName ) ;
54
- return true ;
41
+ this . log . Debug ( $ "Trying to find configuration file at '{ candidatePath } '") ;
42
+ if ( fileSystem . Exists ( candidatePath ) )
43
+ {
44
+ this . log . Info ( $ "Found configuration file at '{ candidatePath } '") ;
45
+ return candidatePath ;
46
+ }
47
+ this . log . Debug ( $ "Configuration file not found at '{ candidatePath } '") ;
55
48
}
56
49
57
- path = null ;
58
- if ( workingDirectory is null ) return false ;
59
- return ! this . configurationFile . IsNullOrWhiteSpace ( )
60
- ? HasConfigurationFileAt ( this . configurationFile , out path )
61
- : HasConfigurationFileAt ( DefaultFileName , out path )
62
- || HasConfigurationFileAt ( DefaultAlternativeFileName , out path ) ;
50
+ return null ;
63
51
}
64
52
65
53
private void WarnAboutAmbiguousConfigFileSelection ( string ? workingDirectory , string ? projectRootDirectory )
66
54
{
67
- TryGetConfigurationFile ( workingDirectory , null , out var workingConfigFile ) ;
68
- TryGetConfigurationFile ( null , projectRootDirectory , out var projectRootConfigFile ) ;
55
+ var workingConfigFile = GetConfigurationFile ( workingDirectory ) ;
56
+ var projectRootConfigFile = GetConfigurationFile ( projectRootDirectory ) ;
69
57
70
- var hasConfigInWorkingDirectory = workingConfigFile != null && fileSystem . Exists ( workingConfigFile ) ;
71
- var hasConfigInProjectRootDirectory = projectRootConfigFile != null && fileSystem . Exists ( projectRootConfigFile ) ;
58
+ var hasConfigInWorkingDirectory = workingConfigFile != null ;
59
+ var hasConfigInProjectRootDirectory = projectRootConfigFile != null ;
72
60
73
61
if ( hasConfigInProjectRootDirectory && hasConfigInWorkingDirectory )
74
62
{
@@ -77,10 +65,10 @@ private void WarnAboutAmbiguousConfigFileSelection(string? workingDirectory, str
77
65
78
66
if ( ! hasConfigInProjectRootDirectory && ! hasConfigInWorkingDirectory )
79
67
{
80
- if ( this . configurationFile != DefaultFileName && this . configurationFile != DefaultAlternativeFileName )
68
+ if ( this . ConfigurationFile is not ( DefaultFileName or DefaultAlternativeFileName ) )
81
69
{
82
- workingConfigFile = PathHelper . Combine ( workingDirectory , this . configurationFile ) ;
83
- projectRootConfigFile = PathHelper . Combine ( projectRootDirectory , this . configurationFile ) ;
70
+ workingConfigFile = PathHelper . Combine ( workingDirectory , this . ConfigurationFile ) ;
71
+ projectRootConfigFile = PathHelper . Combine ( projectRootDirectory , this . ConfigurationFile ) ;
84
72
throw new WarningException ( $ "The configuration file was not found at '{ workingConfigFile } ' or '{ projectRootConfigFile } '") ;
85
73
}
86
74
}
0 commit comments