-
Notifications
You must be signed in to change notification settings - Fork 727
/
FileConfigurationSource.cs
89 lines (78 loc) · 3.43 KB
/
FileConfigurationSource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.Extensions.Configuration
{
/// <summary>
/// Represents a base class for file based <see cref="IConfigurationSource"/>.
/// </summary>
public abstract class FileConfigurationSource : IConfigurationSource
{
/// <summary>
/// Used to access the contents of the file.
/// </summary>
public IFileProvider FileProvider { get; set; }
/// <summary>
/// The path to the file.
/// </summary>
public string Path { get; set; }
/// <summary>
/// Determines if loading the file is optional.
/// </summary>
public bool Optional { get; set; }
/// <summary>
/// Determines whether the source will be loaded if the underlying file changes.
/// </summary>
public bool ReloadOnChange { get; set; }
/// <summary>
/// Number of milliseconds that reload will wait before calling Load. This helps
/// avoid triggering reload before a file is completely written. Default is 250.
/// </summary>
public int ReloadDelay { get; set; } = 250;
/// <summary>
/// Will be called if an uncaught exception occurs in FileConfigurationProvider.Load.
/// </summary>
public Action<FileLoadExceptionContext> OnLoadException { get; set; }
/// <summary>
/// Builds the <see cref="IConfigurationProvider"/> for this source.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>A <see cref="IConfigurationProvider"/></returns>
public abstract IConfigurationProvider Build(IConfigurationBuilder builder);
/// <summary>
/// Called to use any default settings on the builder like the FileProvider or FileLoadExceptionHandler.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
public void EnsureDefaults(IConfigurationBuilder builder)
{
FileProvider = FileProvider ?? builder.GetFileProvider();
OnLoadException = OnLoadException ?? builder.GetFileLoadExceptionHandler();
}
/// <summary>
/// If no file provider has been set, for absolute Path, this will creates a physical file provider
/// for the nearest existing directory.
/// </summary>
public void ResolveFileProvider()
{
if (FileProvider == null &&
!string.IsNullOrEmpty(Path) &&
System.IO.Path.IsPathRooted(Path))
{
var directory = System.IO.Path.GetDirectoryName(Path);
var pathToFile = System.IO.Path.GetFileName(Path);
while (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
pathToFile = System.IO.Path.Combine(System.IO.Path.GetFileName(directory), pathToFile);
directory = System.IO.Path.GetDirectoryName(directory);
}
if (Directory.Exists(directory))
{
FileProvider = new PhysicalFileProvider(directory);
Path = pathToFile;
}
}
}
}
}