-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathUtilities.cs
More file actions
248 lines (212 loc) · 8.78 KB
/
Copy pathUtilities.cs
File metadata and controls
248 lines (212 loc) · 8.78 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
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
243
244
245
246
247
248
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.VisualStudio.Composition
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.Composition.Reflection;
internal static class Utilities
{
private const string CompositionErrorHeaderFormat = "----- CompositionError level {0} ------";
internal static void WriteErrors(TextWriter textWriter, IImmutableStack<IReadOnlyCollection<ComposedPartDiagnostic>> errorStack)
{
int level = 1;
foreach (IReadOnlyCollection<ComposedPartDiagnostic> errors in errorStack)
{
textWriter.WriteLine(string.Format(CultureInfo.CurrentCulture, CompositionErrorHeaderFormat, level++));
foreach (ComposedPartDiagnostic error in errors)
{
textWriter.WriteLine(error.Message);
foreach (ComposedPart part in error.Parts)
{
textWriter.WriteLine(string.Format(CultureInfo.CurrentCulture, " part definition {0}", part.Definition.Type));
}
textWriter.WriteLine();
}
}
}
internal static ComposablePartDefinition GetMetadataViewProviderPartDefinition(Type providerType, int orderPrecedence, Resolver resolver)
{
Requires.NotNull(providerType, nameof(providerType));
Requires.NotNull(resolver, nameof(resolver));
var exportDefinition = new ExportDefinition(
ContractNameServices.GetTypeIdentity(typeof(IMetadataViewProvider)),
PartCreationPolicyConstraint.GetExportMetadata(CreationPolicy.Shared)
.AddRange(ExportTypeIdentityConstraint.GetExportMetadata(typeof(IMetadataViewProvider)))
.SetItem("OrderPrecedence", orderPrecedence));
var partDefinition = new ComposablePartDefinition(
TypeRef.Get(providerType, resolver),
ImmutableDictionary<string, object?>.Empty.Add(CompositionConstants.DgmlCategoryPartMetadataName, new[] { "VsMEFBuiltIn" }),
new[] { exportDefinition },
ImmutableDictionary<MemberRef, IReadOnlyCollection<ExportDefinition>>.Empty,
ImmutableList<ImportDefinitionBinding>.Empty,
string.Empty,
ImmutableList<MethodRef>.Empty,
MethodRef.Get(providerType.GetTypeInfo().GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single(c => c.GetParameters().Length == 0), resolver),
ImmutableList<ImportDefinitionBinding>.Empty,
CreationPolicy.Shared,
false);
return partDefinition;
}
internal static TValue? GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue?> dictionary, TKey key)
where TKey : notnull
where TValue : class
{
dictionary.TryGetValue(key, out TValue? value);
return value;
}
[return: NotNullIfNotNull("defaultValue")]
internal static TValue? GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue? defaultValue)
where TKey : notnull
where TValue : class
{
if (!dictionary.TryGetValue(key, out TValue? value))
{
value = defaultValue;
}
return value;
}
internal static bool EqualsByValue<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> expected, IReadOnlyDictionary<TKey, TValue> actual, IEqualityComparer<TValue>? valueComparer = null)
where TKey : notnull
{
Requires.NotNull(expected, nameof(expected));
Requires.NotNull(actual, nameof(actual));
valueComparer = valueComparer ?? EqualityComparer<TValue>.Default;
if (expected.Count != actual.Count)
{
return false;
}
foreach (var entry in expected)
{
TValue? actualValue;
if (!actual.TryGetValue(entry.Key, out actualValue))
{
// missing key
return false;
}
if (!valueComparer.Equals(entry.Value, actualValue))
{
return false;
}
}
return true;
}
internal static bool TryGetValue<TValue>(this IReadOnlyDictionary<string, object?> metadata, string key, [MaybeNull, NotNullWhen(true)] out TValue value)
{
object? valueObject;
if (metadata.TryGetValue(key, out valueObject) && valueObject is TValue)
{
value = (TValue)valueObject;
return true;
}
value = default(TValue);
return false;
}
internal static string MakeIdentifierNameSafe(string value)
{
return value
.Replace('`', '_')
.Replace('.', '_')
.Replace('+', '_')
.Replace('{', '_')
.Replace('}', '_')
.Replace('(', '_')
.Replace(')', '_')
.Replace(',', '_')
.Replace('-', '_');
}
internal static bool Contains<T>(this ImmutableStack<T> stack, T value)
{
Requires.NotNull(stack, nameof(stack));
while (!stack.IsEmpty)
{
if (EqualityComparer<T>.Default.Equals(value, stack.Peek()))
{
return true;
}
stack = stack.Pop();
}
return false;
}
internal static bool EqualsByValue<T>(this ImmutableArray<T> array, ImmutableArray<T> other)
where T : IEquatable<T>
{
if (array.Length != other.Length)
{
return false;
}
for (int i = 0; i < array.Length; i++)
{
if (!array[i].Equals(other[i]))
{
return false;
}
}
return true;
}
internal static void ToString(this IReadOnlyDictionary<string, object?> metadata, IndentingTextWriter writer)
{
Requires.NotNull(metadata, nameof(metadata));
Requires.NotNull(writer, nameof(writer));
foreach (var item in metadata)
{
writer.WriteLine("{0} = {1}", item.Key, item.Value);
}
}
internal static void ToString(this object value, TextWriter writer)
{
Requires.NotNull(value, nameof(value));
Requires.NotNull(writer, nameof(writer));
var descriptiveValue = value as IDescriptiveToString;
if (descriptiveValue != null)
{
descriptiveValue.ToString(writer);
}
else
{
writer.WriteLine(value);
}
}
internal static object SpecifyIfNull(this object? value)
{
return value == null ? "<null>" : value;
}
internal static void ReportNullSafe<T>(this IProgress<T>? progress, T value)
{
if (progress != null)
{
progress.Report(value);
}
}
internal static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<TValue> source, Func<TValue, TKey> keySelector, int capacity)
where TKey : notnull
{
var dictionary = new Dictionary<TKey, TValue>(capacity);
foreach (var item in source)
{
dictionary.Add(keySelector(item), item);
}
return dictionary;
}
/// <summary>
/// Creates a <see cref="Dictionary{TKey, TValue}"/> from an <see cref="IReadOnlyDictionary{TKey, TValue}"/>.
/// The BCL Dictionary constructor does not accept IReadOnlyDictionary on netstandard2.0.
/// </summary>
internal static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source)
where TKey : notnull
{
var dictionary = new Dictionary<TKey, TValue>(source.Count);
foreach (var kvp in source)
{
dictionary.Add(kvp.Key, kvp.Value);
}
return dictionary;
}
}
}