-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathConfigurationService.cs
More file actions
158 lines (132 loc) · 5.36 KB
/
ConfigurationService.cs
File metadata and controls
158 lines (132 loc) · 5.36 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using System.Text.Json;
namespace GitCredentialManager
{
/// <summary>
/// Targets for performing configuration changes using the <see cref="IConfigurationService"/>.
/// </summary>
public enum ConfigurationTarget
{
/// <summary>
/// Target configuration changes for the current user only.
/// </summary>
User,
/// <summary>
/// Target configuration changes for all users on the system.
/// </summary>
System,
}
/// <summary>
/// Component that requires Git or environment configuration to work.
/// </summary>
public interface IConfigurableComponent
{
/// <summary>
/// Name of the component that requires configuration.
/// </summary>
string Name { get; }
/// <summary>
/// Configure the environment and Git to work with this hosting provider.
/// </summary>
/// <param name="target">Configuration target.</param>
Task ConfigureAsync(ConfigurationTarget target);
/// <summary>
/// Remove changes to the environment and Git configuration previously made with <see cref="ConfigureAsync"/>.
/// </summary>
/// <param name="target">Configuration target.</param>
Task UnconfigureAsync(ConfigurationTarget target);
}
public interface IConfigurationService
{
/// <summary>
/// Add a <see cref="IConfigurableComponent"/> to the collection of components that will be configured.
/// </summary>
/// <param name="component"></param>
void AddComponent(IConfigurableComponent component);
/// <summary>
/// Configure all components.
/// </summary>
/// <param name="target">Target level to configure.</param>
Task ConfigureAsync(ConfigurationTarget target);
/// <summary>
/// Unconfigure all components.
/// </summary>
/// <param name="target">Target level to unconfigure.</param>
Task UnconfigureAsync(ConfigurationTarget target);
/// <summary>
/// Save the current configuration to a file.
/// </summary>
/// <param name="filePath">Path to the file where the configuration will be saved.</param>
Task SaveConfigurationAsync(string filePath);
/// <summary>
/// Load the configuration from a file.
/// </summary>
/// <param name="filePath">Path to the file from which the configuration will be loaded.</param>
Task LoadConfigurationAsync(string filePath);
}
public class ConfigurationService : IConfigurationService
{
private readonly ICommandContext _context;
private readonly IList<IConfigurableComponent> _components = new List<IConfigurableComponent>();
public ConfigurationService(ICommandContext context)
{
EnsureArgument.NotNull(context, nameof(context));
_context = context;
}
#region IConfigurationService
public void AddComponent(IConfigurableComponent component)
{
_components.Add(component);
}
public async Task ConfigureAsync(ConfigurationTarget target)
{
foreach (IConfigurableComponent component in _components)
{
_context.Trace.WriteLine($"Configuring component '{component.Name}'...");
_context.Streams.Error.WriteLine($"Configuring component '{component.Name}'...");
await component.ConfigureAsync(target);
}
}
public async Task UnconfigureAsync(ConfigurationTarget target)
{
foreach (IConfigurableComponent component in _components)
{
_context.Trace.WriteLine($"Unconfiguring component '{component.Name}'...");
_context.Streams.Error.WriteLine($"Unconfiguring component '{component.Name}'...");
await component.UnconfigureAsync(target);
}
}
public async Task SaveConfigurationAsync(string filePath)
{
var configData = new Dictionary<string, object>();
foreach (IConfigurableComponent component in _components)
{
var componentConfig = new Dictionary<string, object>
{
{ "Name", component.Name }
};
configData[component.Name] = componentConfig;
}
var options = new JsonSerializerOptions { WriteIndented = true };
var json = JsonSerializer.Serialize(configData, options);
await File.WriteAllTextAsync(filePath, json);
}
public async Task LoadConfigurationAsync(string filePath)
{
var json = await File.ReadAllTextAsync(filePath);
var configData = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, object>>>(json);
foreach (var componentConfig in configData)
{
var componentName = componentConfig.Key;
var component = _components.FirstOrDefault(c => c.Name == componentName);
if (component != null)
{
// Perform any necessary actions to apply the loaded configuration to the component
}
}
}
#endregion
}
}