| | 1 | | using System.Net.Http.Json; |
| | 2 | | using System.Text; |
| | 3 | | using System.Text.Json; |
| | 4 | | using CliWrap; |
| | 5 | | using Dotnet.Installer.Core.Models; |
| | 6 | |
|
| | 7 | | namespace Dotnet.Installer.Core.Services.Implementations; |
| | 8 | |
|
| | 9 | | public partial class ManifestService |
| | 10 | | { |
| 0 | 11 | | private static readonly string SnapConfigPath = Path.Join( |
| 0 | 12 | | Environment.GetEnvironmentVariable("DOTNET_INSTALL_DIR"), "..", "snap"); |
| | 13 | |
|
| 0 | 14 | | private static readonly string LocalManifestPath = Path.Join(SnapConfigPath, "manifest.json"); |
| | 15 | |
|
| 0 | 16 | | private static readonly string[] SupportedVersions = ["6.0", "7.0", "8.0"]; |
| | 17 | |
|
| 0 | 18 | | private static readonly HttpClient HttpClient = new() |
| 0 | 19 | | { |
| 0 | 20 | | #if !DEBUG |
| 0 | 21 | | BaseAddress = new Uri(Environment.GetEnvironmentVariable("SERVER_URL") |
| 0 | 22 | | ?? throw new ApplicationException( |
| 0 | 23 | | "SERVER_URL environment variable is not defined.")) |
| 0 | 24 | | #endif |
| 0 | 25 | | }; |
| | 26 | |
|
| | 27 | | private static async Task<List<Component>> LoadLocal(CancellationToken cancellationToken = default) |
| 0 | 28 | | { |
| 0 | 29 | | if (!File.Exists(LocalManifestPath)) return []; |
| | 30 | |
|
| 0 | 31 | | await using var fs = File.OpenRead(LocalManifestPath); |
| 0 | 32 | | var result = await JsonSerializer.DeserializeAsync<List<Component>>( |
| 0 | 33 | | fs, JsonSerializerOptions, cancellationToken |
| 0 | 34 | | ); |
| | 35 | |
|
| 0 | 36 | | return result ?? []; |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | private static async Task<List<Component>> LoadRemote(bool includeArchive = false, CancellationToken cancellationTok |
| 0 | 40 | | { |
| 0 | 41 | | var content = new List<Component>(); |
| | 42 | |
|
| | 43 | | #if DEBUG |
| 0 | 44 | | await Cli.Wrap("git") |
| 0 | 45 | | .WithArguments(["rev-parse", "--show-toplevel"]) |
| 0 | 46 | | .WithStandardOutputPipe(PipeTarget.ToDelegate(path => |
| 0 | 47 | | { |
| 0 | 48 | | var manifestLocation = Path.Join(path, "manifest", "latest.json"); |
| 0 | 49 | | if (!File.Exists(manifestLocation)) return; |
| 0 | 50 | | var latest = JsonSerializer.Deserialize<List<Component>>( |
| 0 | 51 | | File.ReadAllText(manifestLocation), JsonSerializerOptions); |
| 0 | 52 | | content.AddRange(latest ?? []); |
| 0 | 53 | |
|
| 0 | 54 | | if (includeArchive) |
| 0 | 55 | | { |
| 0 | 56 | | foreach (var version in SupportedVersions) |
| 0 | 57 | | { |
| 0 | 58 | | var versionArchiveLocation = Path.Join(path, "manifest", version, "archive.json"); |
| 0 | 59 | | if (!File.Exists(versionArchiveLocation)) return; |
| 0 | 60 | | var versionArchive = JsonSerializer.Deserialize<List<Component>>( |
| 0 | 61 | | File.ReadAllText(versionArchiveLocation), JsonSerializerOptions); |
| 0 | 62 | | content.AddRange(versionArchive ?? []); |
| 0 | 63 | | } |
| 0 | 64 | | } |
| 0 | 65 | | })) |
| 0 | 66 | | .ExecuteAsync(cancellationToken); |
| | 67 | | #else |
| | 68 | | var response = await HttpClient.GetAsync("latest.json", cancellationToken); |
| | 69 | |
|
| | 70 | | if (response.IsSuccessStatusCode) |
| | 71 | | { |
| | 72 | | var latest = await response.Content.ReadFromJsonAsync<List<Component>>( |
| | 73 | | cancellationToken: cancellationToken); |
| | 74 | | if (latest is not null) content.AddRange(latest); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | if (includeArchive) |
| | 78 | | { |
| | 79 | | foreach (var version in SupportedVersions) |
| | 80 | | { |
| | 81 | | response = await HttpClient.GetAsync($"{version}/archive.json", cancellationToken); |
| | 82 | |
|
| | 83 | | if (!response.IsSuccessStatusCode) continue; |
| | 84 | |
|
| | 85 | | var archive = |
| | 86 | | await response.Content.ReadFromJsonAsync<List<Component>>(cancellationToken: cancellationToken); |
| | 87 | |
|
| | 88 | | if (archive is not null) content.AddRange(archive); |
| | 89 | | } |
| | 90 | | } |
| | 91 | | #endif |
| | 92 | |
|
| 0 | 93 | | return content; |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | private static List<Component> Merge(List<Component> remoteComponents, List<Component> localComponents) |
| 0 | 97 | | { |
| 0 | 98 | | var result = new List<Component>(); |
| 0 | 99 | | result.AddRange(remoteComponents); |
| | 100 | |
|
| 0 | 101 | | foreach (var localComponent in localComponents) |
| 0 | 102 | | { |
| 0 | 103 | | if (result.All(c => c.Key != localComponent.Key)) |
| 0 | 104 | | { |
| 0 | 105 | | result.Add(localComponent); |
| 0 | 106 | | } |
| | 107 | | else |
| 0 | 108 | | { |
| 0 | 109 | | var remote = result.First(c => c.Key == localComponent.Key); |
| 0 | 110 | | remote.Installation = localComponent.Installation; |
| 0 | 111 | | } |
| 0 | 112 | | } |
| | 113 | |
|
| 0 | 114 | | return result; |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | private async Task Save(CancellationToken cancellationToken = default) |
| 0 | 118 | | { |
| 0 | 119 | | await using var sw = new StreamWriter(LocalManifestPath, append: false, Encoding.UTF8); |
| 0 | 120 | | var content = JsonSerializer.Serialize(_local, JsonSerializerOptions); |
| 0 | 121 | | var stringBuilder = new StringBuilder(content); |
| 0 | 122 | | await sw.WriteLineAsync(stringBuilder, cancellationToken); |
| 0 | 123 | | } |
| | 124 | | } |