|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using MonoDevelop.Core.Web; |
| 7 | + |
| 8 | +namespace Microsoft.WebTools.Scaffolding.Core.Config |
| 9 | +{ |
| 10 | + class ScaffoldingConfig |
| 11 | + { |
| 12 | + public static string ConfigPath { get; private set; } = Path.GetDirectoryName (typeof (ScaffoldingConfig).Assembly.Location); |
| 13 | + |
| 14 | + public string Version { get; set; } |
| 15 | + |
| 16 | + // LTS10, FTS11, NetStandard20, NetStandard21, and Net22 packages are set up as they are to maintain backwards compat. |
| 17 | + // They were (are) explicitly named sections before the config file format was generalized to support arbitrary support policy versions. |
| 18 | + public PackageDescription [] LTS10Packages { get; set; } |
| 19 | + |
| 20 | + public PackageDescription [] FTS11Packages { get; set; } |
| 21 | + |
| 22 | + public PackageDescription [] NetStandard20Packages { get; set; } |
| 23 | + |
| 24 | + public PackageDescription [] NetStandard21Packages { get; set; } |
| 25 | + |
| 26 | + public PackageDescription [] Net22Packages { get; set; } |
| 27 | + |
| 28 | + // This is public so the Json deserialization works (and for testing). |
| 29 | + // The data should be accessed via TryGetPackagesForSupportPolicyVersion |
| 30 | + [JsonProperty] |
| 31 | + public Dictionary<string, PackageDescription []> DynamicVersionedPackages { get; set; } |
| 32 | + |
| 33 | + public bool TryGetPackagesForSupportPolicyVersion (SupportPolicyVersion supportPolicyVersion, out PackageDescription [] packageDescriptions) |
| 34 | + { |
| 35 | + if (supportPolicyVersion == null || supportPolicyVersion.Version == null) { |
| 36 | + packageDescriptions = null; |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + if (supportPolicyVersion == SupportPolicyVersion.LTS10) { |
| 41 | + packageDescriptions = LTS10Packages; |
| 42 | + return true; |
| 43 | + } |
| 44 | + if (supportPolicyVersion == SupportPolicyVersion.FTS11) { |
| 45 | + packageDescriptions = FTS11Packages; |
| 46 | + return true; |
| 47 | + } |
| 48 | + if (supportPolicyVersion == SupportPolicyVersion.NetStandard20) { |
| 49 | + packageDescriptions = NetStandard20Packages; |
| 50 | + return true; |
| 51 | + } |
| 52 | + if (supportPolicyVersion == SupportPolicyVersion.NetStandard21) { |
| 53 | + packageDescriptions = NetStandard21Packages; |
| 54 | + return true; |
| 55 | + } |
| 56 | + if (supportPolicyVersion == SupportPolicyVersion.Net220) { |
| 57 | + packageDescriptions = Net22Packages; |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + if (DynamicVersionedPackages != null && DynamicVersionedPackages.TryGetValue (supportPolicyVersion.Version.ToString (), out packageDescriptions)) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + packageDescriptions = null; |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + static ScaffoldingConfig fetchedConfig; |
| 70 | + // This url will go live for 16.4 |
| 71 | + static string packageVersionsUrl = "https://webpifeed.blob.core.windows.net/webpifeed/partners/scaffoldingpackageversions_2108718.json"; |
| 72 | + |
| 73 | + public static async Task<ScaffoldingConfig> LoadFromJsonAsync () |
| 74 | + { |
| 75 | + if(fetchedConfig == null) { |
| 76 | + Stream stream; |
| 77 | + using var httpClient = HttpClientProvider.CreateHttpClient (packageVersionsUrl); |
| 78 | + httpClient.Timeout = TimeSpan.FromSeconds (2); |
| 79 | + |
| 80 | + try { |
| 81 | + stream = await httpClient.GetStreamAsync (packageVersionsUrl); |
| 82 | + } catch { |
| 83 | + // fallback to embedded resource |
| 84 | + stream = typeof (ScaffoldingConfig).Assembly.GetManifestResourceStream ("ScaffoldingPackageVersions.json"); |
| 85 | + } |
| 86 | + |
| 87 | + var serializer = new JsonSerializer (); |
| 88 | + |
| 89 | + using var sr = new StreamReader (stream); |
| 90 | + using var jsonTextReader = new JsonTextReader (sr); |
| 91 | + return serializer.Deserialize<ScaffoldingConfig> (jsonTextReader); |
| 92 | + } |
| 93 | + return fetchedConfig; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments