-
Notifications
You must be signed in to change notification settings - Fork 420
/
PropertyConverter.cs
138 lines (116 loc) · 4.24 KB
/
PropertyConverter.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
using System;
using System.Collections.Immutable;
using System.Globalization;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using NuGet.Versioning;
namespace OmniSharp.MSBuild.ProjectFile
{
internal static class PropertyConverter
{
public static bool ToBoolean(string propertyValue, bool defaultValue)
{
if (string.IsNullOrWhiteSpace(propertyValue))
{
return defaultValue;
}
try
{
return Convert.ToBoolean(propertyValue);
}
catch (FormatException)
{
return defaultValue;
}
}
public static LanguageVersion ToLanguageVersion(string propertyValue)
{
if (LanguageVersionFacts.TryParse(propertyValue, out var result))
return result;
return LanguageVersion.Default;
}
public static ImmutableArray<string> SplitList(string propertyValue, char separator)
{
if (string.IsNullOrWhiteSpace(propertyValue))
{
return ImmutableArray<string>.Empty;
}
var builder = ImmutableArray.CreateBuilder<string>();
var values = propertyValue.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
foreach (var value in values)
{
builder.Add(value.Trim());
}
return builder.ToImmutable();
}
public static ImmutableArray<string> ToPreprocessorSymbolNames(string propertyValue)
{
if (string.IsNullOrWhiteSpace(propertyValue))
{
return ImmutableArray<string>.Empty;
}
var values = propertyValue.Split(new[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);
return ImmutableArray.CreateRange(values);
}
public static ImmutableArray<string> ToSuppressedDiagnosticIds(string propertyValue)
{
if (string.IsNullOrWhiteSpace(propertyValue))
{
return ImmutableArray<string>.Empty;
}
var builder = ImmutableArray.CreateBuilder<string>();
// Remove quotes
propertyValue = propertyValue.Trim('"');
var values = propertyValue.Split(new[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var id in values)
{
if (ushort.TryParse(id, NumberStyles.Integer, CultureInfo.InvariantCulture, out var number))
{
builder.Add("CS" + number.ToString("0000"));
}
else
{
builder.Add(id);
}
}
return builder.ToImmutable();
}
public static Guid ToGuid(string propertyValue)
{
if (!Guid.TryParse(propertyValue, out var result))
{
return Guid.Empty;
}
return result;
}
public static OutputKind ToOutputKind(string propertyValue)
{
switch (propertyValue)
{
case "Library": return OutputKind.DynamicallyLinkedLibrary;
case "WinExe": return OutputKind.WindowsApplication;
case "Exe": return OutputKind.ConsoleApplication;
default: return OutputKind.ConsoleApplication;
}
}
public static NullableContextOptions ToNullableContextOptions(string propertyValue)
{
switch (propertyValue?.ToLowerInvariant())
{
case "disable": return NullableContextOptions.Disable;
case "enable": return NullableContextOptions.Enable;
case "warnings": return NullableContextOptions.Warnings;
case "annotations": return NullableContextOptions.Annotations;
default: return NullableContextOptions.Disable;
}
}
public static VersionRange ToVersionRange(string propertyValue)
{
if (VersionRange.TryParse(propertyValue.Trim(), out var version))
{
return version;
}
return null;
}
}
}