-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDotnetUtilities.cs
158 lines (133 loc) · 5.18 KB
/
DotnetUtilities.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
using System.Diagnostics;
using System.Collections.Immutable;
using System.Text.Json.Serialization;
using System.Text.Json;
using static Msbuild.Clean.Processor;
namespace Msbuild.Clean;
internal enum MsbuildExecType {
ExecTypeExe = 0x1,
ExecTypeDllDotnet = 0x2,
}
internal enum MsbuildLocationType {
VisualStudio,
DotNetSdk,
NetFx,
Manual,
}
internal enum MsbuildCleanExecutionType {
PreferVs,
PeekProjFile,
}
#if DEBUG
[JsonSerializable(typeof(Dir))]
[JsonSerializable(typeof(ProcessorDirOptions))]
[JsonSerializable(typeof(ProcessorOptions))]
[JsonSerializable(typeof(Dictionary<string, string?>))]
[JsonSerializable(typeof(HashSet<string>))]
#endif
[JsonSerializable(typeof(Output))]
[JsonSerializable(typeof(ImmutableDictionary<string, string>))]
internal partial class OutputJsonSerializerContext : JsonSerializerContext;
internal static class JsonExt {
internal static JsonSerializerOptions _sDefaults { get; } = Init();
private static JsonSerializerOptions Init() {
var options = new JsonSerializerOptions { WriteIndented = true, Converters = { new JsonStringEnumConverter() }, };
options.TypeInfoResolverChain.Insert(0, new OutputJsonSerializerContext());
return options;
}
internal static string Ser(this object obj) => JsonSerializer.Serialize(obj, _sDefaults);
}
internal record class Output(ImmutableDictionary<string, string> Properties) {
internal static Output Empty { get; } = new Output(ImmutableDictionary<string, string>.Empty);
internal string? OutDir => Properties.TryGetValue("OutDir", out var val) ? val : default;
internal string? BaseIntermediateOutputPath => Properties.TryGetValue("BaseIntermediateOutputPath", out var val) ? val : default;
internal string? BaseOutputPath => Properties.TryGetValue("BaseOutputPath", out var val) ? val : default;
internal string? PackageOutputPath => Properties.TryGetValue("PackageOutputPath", out var val) ? val : default;
internal string? AssemblyName => Properties.TryGetValue("AssemblyName", out var val) ? val : default;
internal string? PackageId => Properties.TryGetValue("PackageId", out var val) ? val : default;
internal string? ProjectName => Properties.TryGetValue("ProjectName", out var val) ? val : default;
internal string? TargetFramework => Properties.TryGetValue("TargetFramework", out var val) ? val : default;
internal IEnumerable<string> TargetFrameworks => Properties.TryGetValue("TargetFrameworks", out var val) ? Split(val) : Enumerable.Empty<string>();
internal HashSet<string> TfmsCombined {
get {
var hs = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
if (TargetFramework is string tfm && tfm is not null) {
hs.Add(tfm);
}
if (TargetFrameworks is { } tfms) {
hs.UnionWith(tfms);
}
return hs;
}
}
private IEnumerable<string> Split(string? val) {
if (string.IsNullOrWhiteSpace(val)) return Enumerable.Empty<string>();
return val
.Split(";", StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim());
}
internal string? this[string key] => Properties.TryGetValue(key, out var val) ? val : default;
internal bool IsDotnet8OrHigher() => TargetFramework?.StartsWith("Net8", StringComparison.OrdinalIgnoreCase) ?? false || TfmsCombined.Any(tfm => NetUtil.IsNet8OrHigher(tfm));
private readonly string[] _dockerPropertyNames = ["ContainerBaseImage",
"ContainerFamily",
"ContainerRuntimeIdentifier",
"ContainerRegistry",
"ContainerRepository",
"ContainerImageTag",
"ContainerImageTags"];
internal bool HasDockerProperties() => _dockerPropertyNames.Any(propName => Properties.TryGetValue(propName, out var val) && !string.IsNullOrWhiteSpace(val));
}
internal class NetUtil {
internal static NetUtil Instance { get; } = new();
internal static bool IsNet8OrHigher(string? tfm) {
if (tfm == null) return false;
if (tfm.StartsWith("net8", StringComparison.OrdinalIgnoreCase)) return true;
if (tfm.StartsWith("net9", StringComparison.OrdinalIgnoreCase)) return true;
return false;
}
internal bool IsTfmName(string name, StringComparison defaultComparison) {
if (defaultComparison == StringComparison.OrdinalIgnoreCase) return _validTfms.Contains(name);
return ValidTfms.Any(x => 0 == string.Compare(name, x, defaultComparison));
}
private NetUtil() {
_validTfms = new HashSet<string>(ValidTfms, StringComparer.OrdinalIgnoreCase);
}
private HashSet<string> _validTfms { get; init; }
private readonly string[] ValidTfms = new string[] {
"netcoreapp1.0"
, "netcoreapp1.1"
, "netcoreapp2.0"
, "netcoreapp2.1"
, "netcoreapp2.2"
, "netcoreapp3.0"
, "netcoreapp3.1"
, "net5.0"
, "net6.0"
, "net7.0"
, "net8.0"
, "net9.0"
, "netstandard1.0"
, "netstandard1.1"
, "netstandard1.2"
, "netstandard1.3"
, "netstandard1.4"
, "netstandard1.5"
, "netstandard1.6"
, "netstandard2.0"
, "netstandard2.1"
, "net11"
, "net20"
, "net35"
, "net40"
, "net403"
, "net45"
, "net451"
, "net452"
, "net46"
, "net461"
, "net462"
, "net47"
, "net471"
, "net472"
, "net48" };
}