-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ConfigurationUnitInternal.cs
242 lines (213 loc) · 9.33 KB
/
ConfigurationUnitInternal.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// -----------------------------------------------------------------------------
// <copyright file="ConfigurationUnitInternal.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. Licensed under the MIT License.
// </copyright>
// -----------------------------------------------------------------------------
namespace Microsoft.Management.Configuration.Processor.Helpers
{
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Management.Configuration.Processor.Constants;
using Microsoft.Management.Configuration.Processor.Exceptions;
using Microsoft.PowerShell.Commands;
using Windows.Foundation.Collections;
/// <summary>
/// Wrapper around Configuration units and its directives. Creates a normalized directives map
/// for consumption.
/// </summary>
internal class ConfigurationUnitInternal
{
private const string ConfigRootVar = "${WinGetConfigRoot}";
private readonly string? configurationFileRootPath = null;
private readonly Dictionary<string, object> normalizedDirectives = new ();
/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationUnitInternal"/> class.
/// </summary>
/// <param name="unit">Configuration unit.</param>
/// <param name="configurationFilePath">The configuration file path.</param>
/// <param name="directivesOverlay">Directives overlay.</param>
public ConfigurationUnitInternal(
ConfigurationUnit unit,
string configurationFilePath,
IReadOnlyDictionary<string, object>? directivesOverlay = null)
{
this.Unit = unit;
this.DirectivesOverlay = directivesOverlay;
this.InitializeDirectives();
string? moduleName = this.GetDirective<string>(DirectiveConstants.Module);
if (string.IsNullOrEmpty(moduleName))
{
this.Module = null;
}
else
{
this.Module = PowerShellHelpers.CreateModuleSpecification(
moduleName,
this.GetDirective<string>(DirectiveConstants.Version),
this.GetDirective<string>(DirectiveConstants.MinVersion),
this.GetDirective<string>(DirectiveConstants.MaxVersion),
this.GetDirective<string>(DirectiveConstants.ModuleGuid));
}
if (!string.IsNullOrEmpty(configurationFilePath))
{
if (!File.Exists(configurationFilePath))
{
throw new FileNotFoundException(configurationFilePath);
}
this.configurationFileRootPath = Path.GetDirectoryName(configurationFilePath);
}
}
/// <summary>
/// Gets the configuration unit.
/// </summary>
public ConfigurationUnit Unit { get; }
/// <summary>
/// Gets the directives overlay.
/// </summary>
public IReadOnlyDictionary<string, object>? DirectivesOverlay { get; }
/// <summary>
/// Gets the module specification.
/// </summary>
public ModuleSpecification? Module { get; }
/// <summary>
/// Creates a string that identifies this unit for diagnostics.
/// </summary>
/// <returns>The string that identifies this unit for diagnostics.</returns>
public string ToIdentifyingString()
{
return $"{this.Unit.UnitName} [{this.Module?.ToString() ?? "<no module>"}]";
}
/// <summary>
/// Gets the directive value from the unit taking into account the directives overlay.
/// </summary>
/// <param name="directiveName">Directive name.</param>
/// <returns>Value of directive, null if not found.</returns>
/// <typeparam name="TType">Directive type value.</typeparam>
public TType? GetDirective<TType>(string directiveName)
where TType : class
{
var normalizedDirectiveName = StringHelpers.Normalize(directiveName);
if (this.normalizedDirectives.TryGetValue(normalizedDirectiveName, out object? value))
{
return value as TType;
}
return null;
}
/// <summary>
/// Gets the bool value of a directive from the unit taking into account the directives overlay.
/// </summary>
/// <param name="directiveName">Directive name.</param>
/// <returns>Value of directive, false if not found.</returns>
public bool? GetDirective(string directiveName)
{
var normalizedDirectiveName = StringHelpers.Normalize(directiveName);
if (this.normalizedDirectives.TryGetValue(normalizedDirectiveName, out object? value))
{
return value as bool?;
}
return null;
}
/// <summary>
/// Gets the semantic version, if any.
/// </summary>
/// <returns>SemanticVersion, null if not specified.</returns>
public SemanticVersion? GetSemanticVersion()
{
string? semanticVersion = this.GetDirective<string>(DirectiveConstants.Version);
if (!string.IsNullOrWhiteSpace(semanticVersion))
{
return new SemanticVersion(semanticVersion);
}
return null;
}
/// <summary>
/// Gets the semantic min version, if any.
/// </summary>
/// <returns>SemanticVersion, null if not specified.</returns>
public SemanticVersion? GetSemanticMinVersion()
{
string? semanticVersion = this.GetDirective<string>(DirectiveConstants.MinVersion);
if (!string.IsNullOrWhiteSpace(semanticVersion))
{
return new SemanticVersion(semanticVersion);
}
return null;
}
/// <summary>
/// Gets the semantic max version, if any.
/// </summary>
/// <returns>SemanticVersion, null if not specified.</returns>
public SemanticVersion? GetSemanticMaxVersion()
{
string? semanticVersion = this.GetDirective<string>(DirectiveConstants.MaxVersion);
if (!string.IsNullOrWhiteSpace(semanticVersion))
{
return new SemanticVersion(semanticVersion);
}
return null;
}
/// <summary>
/// TODO: Implement for more variables.
/// I am so sad because rs.SessionStateProxy.InvokeCommand.ExpandString doesn't work as I wanted.
/// PowerShell assumes all code passed to ExpandString is trusted and we cannot assume that.
/// </summary>
/// <returns>ValueSet with settings.</returns>
public ValueSet GetExpandedSettings()
{
var valueSet = new ValueSet();
foreach (var value in this.Unit.Settings)
{
if (value.Value is string)
{
// For now, we just expand config root.
valueSet.Add(value.Key, this.ExpandConfigRoot(value.Value as string, value.Key));
}
else
{
valueSet.Add(value);
}
}
return valueSet;
}
private string? ExpandConfigRoot(string? value, string settingName)
{
if (!string.IsNullOrEmpty(value))
{
// TODO: since we only support one variable, this only finds and replace
// ${WingetConfigRoot} if found in the string when the work of expanding
// string is done it should take into account other operators like the subexpression operator $()
if (value.Contains(ConfigRootVar, StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrEmpty(this.configurationFileRootPath))
{
throw new UnitSettingConfigRootException(this.Unit.UnitName, settingName);
}
if (this.configurationFileRootPath == null)
{
throw new ArgumentException();
}
return value.Replace(ConfigRootVar, this.configurationFileRootPath, StringComparison.OrdinalIgnoreCase);
}
}
return value;
}
private void InitializeDirectives()
{
// Overlay directives have precedence.
if (this.DirectivesOverlay is not null)
{
foreach (var directive in this.DirectivesOverlay)
{
var normalizedKey = StringHelpers.Normalize(directive.Key);
this.normalizedDirectives.Add(normalizedKey, directive.Value);
}
}
foreach (var directive in this.Unit.Directives)
{
var normalizedKey = StringHelpers.Normalize(directive.Key);
_ = this.normalizedDirectives.TryAdd(normalizedKey, directive.Value);
}
}
}
}