11using GitVersion . Extensions ;
22using GitVersion . Helpers ;
3+ using GitVersion . Logging ;
34using Microsoft . Extensions . Options ;
45
56namespace GitVersion . Configuration ;
67
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 )
812 : IConfigurationFileLocator
913{
1014 public const string DefaultFileName = "GitVersion.yml" ;
1115 public const string DefaultAlternativeFileName = "GitVersion.yaml" ;
1216
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 ( ) ;
20+
21+ private string ? ConfigurationFile => options . Value . ConfigurationInfo . ConfigurationFile ;
1422
1523 public bool TryGetConfigurationFile ( string ? workingDirectory , string ? projectRootDirectory , out string ? configFilePath )
16- =>
17- HasConfigurationFile ( workingDirectory , out configFilePath )
18- || HasConfigurationFile ( projectRootDirectory , out configFilePath ) ;
24+ {
25+ configFilePath = GetConfigurationFile ( workingDirectory ) ?? GetConfigurationFile ( projectRootDirectory ) ;
26+ return configFilePath != null ;
27+ }
1928
2029 public void Verify ( string ? workingDirectory , string ? projectRootDirectory )
2130 {
22- if ( Path . IsPathRooted ( this . configurationFile ) ) return ;
31+ if ( Path . IsPathRooted ( this . ConfigurationFile ) ) return ;
2332 if ( PathHelper . Equal ( workingDirectory , projectRootDirectory ) ) return ;
2433 WarnAboutAmbiguousConfigFileSelection ( workingDirectory , projectRootDirectory ) ;
2534 }
2635
27- public IGitVersionConfiguration ReadConfiguration ( string ? configFilePath )
28- {
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 )
36+ public string ? GetConfigurationFile ( string ? directory )
3837 {
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 )
38+ if ( directory is null ) return null ;
39+ string ? [ ] candidates = [ this . ConfigurationFile , DefaultFileName , DefaultAlternativeFileName ] ;
40+ var candidatePaths =
41+ from candidate in candidates
42+ where ! candidate . IsNullOrWhiteSpace ( )
43+ select PathHelper . Combine ( directory , candidate ) ;
44+
45+ foreach ( var candidatePath in candidatePaths )
4946 {
50- configFile = null ;
51- if ( ! fileSystem . Exists ( PathHelper . Combine ( workingDirectory , fileName ) ) ) return false ;
52-
53- configFile = PathHelper . Combine ( workingDirectory , fileName ) ;
54- return true ;
47+ this . log . Debug ( $ "Trying to find configuration file at '{ candidatePath } '") ;
48+ if ( fileSystem . Exists ( candidatePath ) )
49+ {
50+ this . log . Info ( $ "Found configuration file at '{ candidatePath } '") ;
51+ return candidatePath ;
52+ }
53+ this . log . Debug ( $ "Configuration file not found at '{ candidatePath } '") ;
5554 }
5655
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 ) ;
56+ return null ;
6357 }
6458
6559 private void WarnAboutAmbiguousConfigFileSelection ( string ? workingDirectory , string ? projectRootDirectory )
6660 {
67- TryGetConfigurationFile ( workingDirectory , null , out var workingConfigFile ) ;
68- TryGetConfigurationFile ( null , projectRootDirectory , out var projectRootConfigFile ) ;
61+ var workingConfigFile = GetConfigurationFile ( workingDirectory ) ;
62+ var projectRootConfigFile = GetConfigurationFile ( projectRootDirectory ) ;
6963
70- var hasConfigInWorkingDirectory = workingConfigFile != null && fileSystem . Exists ( workingConfigFile ) ;
71- var hasConfigInProjectRootDirectory = projectRootConfigFile != null && fileSystem . Exists ( projectRootConfigFile ) ;
64+ var hasConfigInWorkingDirectory = workingConfigFile != null ;
65+ var hasConfigInProjectRootDirectory = projectRootConfigFile != null ;
7266
7367 if ( hasConfigInProjectRootDirectory && hasConfigInWorkingDirectory )
7468 {
@@ -77,10 +71,10 @@ private void WarnAboutAmbiguousConfigFileSelection(string? workingDirectory, str
7771
7872 if ( ! hasConfigInProjectRootDirectory && ! hasConfigInWorkingDirectory )
7973 {
80- if ( this . configurationFile != DefaultFileName && this . configurationFile != DefaultAlternativeFileName )
74+ if ( this . ConfigurationFile is not ( DefaultFileName or DefaultAlternativeFileName ) )
8175 {
82- workingConfigFile = PathHelper . Combine ( workingDirectory , this . configurationFile ) ;
83- projectRootConfigFile = PathHelper . Combine ( projectRootDirectory , this . configurationFile ) ;
76+ workingConfigFile = PathHelper . Combine ( workingDirectory , this . ConfigurationFile ) ;
77+ projectRootConfigFile = PathHelper . Combine ( projectRootDirectory , this . ConfigurationFile ) ;
8478 throw new WarningException ( $ "The configuration file was not found at '{ workingConfigFile } ' or '{ projectRootConfigFile } '") ;
8579 }
8680 }
0 commit comments