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