-
Notifications
You must be signed in to change notification settings - Fork 1k
/
WorkloadSet.cs
102 lines (87 loc) · 3.86 KB
/
WorkloadSet.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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.DotNet.MSBuildSdkResolver;
using Strings = Microsoft.NET.Sdk.Localization.Strings;
#if USE_SYSTEM_TEXT_JSON
using System.Text.Json;
#else
using Newtonsoft.Json;
#endif
namespace Microsoft.NET.Sdk.WorkloadManifestReader
{
public class WorkloadSet
{
public Dictionary<ManifestId, (ManifestVersion Version, SdkFeatureBand FeatureBand)> ManifestVersions = new Dictionary<ManifestId, (ManifestVersion version, SdkFeatureBand featureBand)>();
// TODO: Generate version from hash of manifest versions if not otherwise set
public string? Version { get; set; }
public static WorkloadSet FromManifests(IEnumerable<WorkloadManifestInfo> manifests)
{
return new WorkloadSet()
{
ManifestVersions = manifests.ToDictionary(m => new ManifestId(m.Id), m => (new ManifestVersion(m.Version), new SdkFeatureBand(m.ManifestFeatureBand)))
};
}
public static WorkloadSet FromDictionaryForJson(IDictionary<string, string> dictionary, SdkFeatureBand defaultFeatureBand)
{
var manifestVersions = dictionary
.Select(manifest =>
{
ManifestVersion manifestVersion;
SdkFeatureBand manifestFeatureBand;
var parts = manifest.Value.Split('/');
string manifestVersionString = parts[0];
if (!FXVersion.TryParse(manifestVersionString, out FXVersion version))
{
throw new FormatException(String.Format(Strings.InvalidVersionForWorkload, manifest.Key, manifestVersionString));
}
manifestVersion = new ManifestVersion(parts[0]);
if (parts.Length == 1)
{
manifestFeatureBand = defaultFeatureBand;
}
else
{
manifestFeatureBand = new SdkFeatureBand(parts[1]);
}
return (id: new ManifestId(manifest.Key), manifestVersion, manifestFeatureBand);
}).ToDictionary(t => t.id, t => (t.manifestVersion, t.manifestFeatureBand));
return new WorkloadSet()
{
ManifestVersions = manifestVersions
};
}
public static WorkloadSet FromJson(string json, SdkFeatureBand defaultFeatureBand)
{
#if USE_SYSTEM_TEXT_JSON
var jsonSerializerOptions = new JsonSerializerOptions()
{
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip
};
return FromDictionaryForJson(JsonSerializer.Deserialize<IDictionary<string, string>>(json, jsonSerializerOptions)!, defaultFeatureBand);
#else
return FromDictionaryForJson(JsonConvert.DeserializeObject<IDictionary<string, string>>(json)!, defaultFeatureBand);
#endif
}
public Dictionary<string, string> ToDictionaryForJson()
{
var dictionary = ManifestVersions.ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.Version + "/" + kvp.Value.FeatureBand, StringComparer.OrdinalIgnoreCase);
return dictionary;
}
public string ToJson()
{
#if USE_SYSTEM_TEXT_JSON
var json = JsonSerializer.Serialize(ToDictionaryForJson(), new JsonSerializerOptions() { WriteIndented = true });
#else
var json = JsonConvert.SerializeObject(ToDictionaryForJson(), Formatting.Indented);
#endif
return json;
}
}
}