| | 1 | | using System.Runtime.InteropServices; |
| | 2 | | using Dotnet.Installer.Core.Exceptions; |
| | 3 | | using Dotnet.Installer.Core.Models.Events; |
| | 4 | | using Dotnet.Installer.Core.Services.Contracts; |
| | 5 | | using Dotnet.Installer.Core.Types; |
| | 6 | |
|
| | 7 | | namespace Dotnet.Installer.Core.Models; |
| | 8 | |
|
| | 9 | | public class Component |
| | 10 | | { |
| 28 | 11 | | public required string Key { get; init; } |
| 13 | 12 | | public required string Name { get; init; } |
| 13 | 13 | | public required string Description { get; init; } |
| 19 | 14 | | public required Uri BaseUrl { get; init; } |
| 46 | 15 | | public required DotnetVersion Version { get; init; } |
| 21 | 16 | | public required IEnumerable<Package> Packages { get; init; } |
| 37 | 17 | | public required IEnumerable<string> Dependencies { get; init; } |
| 12 | 18 | | public Installation? Installation { get; set; } |
| | 19 | |
|
| | 20 | | public event EventHandler<InstallationStartedEventArgs>? InstallationStarted; |
| | 21 | | public event EventHandler<InstallationFinishedEventArgs>? InstallationFinished; |
| | 22 | | public event EventHandler<InstallingPackageChangedEventArgs>? InstallingPackageChanged; |
| | 23 | |
|
| | 24 | | public bool CanInstall(ILimitsService limitsService) |
| 8 | 25 | | { |
| 8 | 26 | | if (Version.IsRuntime) |
| 1 | 27 | | { |
| 1 | 28 | | return Version <= limitsService.Runtime; |
| | 29 | | } |
| | 30 | |
|
| 7 | 31 | | if (Version.IsSdk) |
| 7 | 32 | | { |
| 7 | 33 | | var limitOnFeatureBand = limitsService.Sdk |
| 14 | 34 | | .First(v => v.FeatureBand == Version.FeatureBand); |
| | 35 | |
|
| 7 | 36 | | return Version <= limitOnFeatureBand; |
| | 37 | | } |
| | 38 | |
|
| 0 | 39 | | return false; |
| 8 | 40 | | } |
| | 41 | |
|
| | 42 | | public async Task Install( |
| | 43 | | IFileService fileService, |
| | 44 | | ILimitsService limitsService, |
| | 45 | | IManifestService manifestService) |
| 8 | 46 | | { |
| | 47 | | // TODO: Double-check architectures from Architecture enum |
| 8 | 48 | | var architecture = RuntimeInformation.OSArchitecture switch |
| 8 | 49 | | { |
| 8 | 50 | | Architecture.X64 => "amd64", |
| 0 | 51 | | Architecture.Arm64 => "arm64", |
| 0 | 52 | | _ => throw new UnsupportedArchitectureException(RuntimeInformation.OSArchitecture) |
| 8 | 53 | | }; |
| | 54 | |
|
| 8 | 55 | | if (!CanInstall(limitsService)) |
| 2 | 56 | | { |
| 2 | 57 | | throw new VersionTooHighException(this, |
| 3 | 58 | | Version.IsRuntime ? limitsService.Runtime : limitsService.Sdk.First(v => v.FeatureBand == Version.Featur |
| | 59 | | } |
| | 60 | |
|
| 6 | 61 | | if (Installation is null) |
| 6 | 62 | | { |
| 6 | 63 | | InstallationStarted?.Invoke(this, new InstallationStartedEventArgs(Key)); |
| | 64 | |
|
| | 65 | | // Install component packages |
| 30 | 66 | | foreach (var package in Packages) |
| 6 | 67 | | { |
| 6 | 68 | | InstallingPackageChanged?.Invoke(this, new InstallingPackageChangedEventArgs(package, this)); |
| | 69 | |
|
| 6 | 70 | | var debUrl = new Uri(BaseUrl, $"{package.Name}_{package.Version}_{architecture}.deb"); |
| | 71 | |
|
| 6 | 72 | | var filePath = await fileService.DownloadFile(debUrl, manifestService.DotnetInstallLocation); |
| | 73 | |
|
| 6 | 74 | | await fileService.ExtractDeb(filePath, manifestService.DotnetInstallLocation, manifestService.SnapConfig |
| | 75 | |
|
| 6 | 76 | | fileService.DeleteFile(filePath); |
| 6 | 77 | | } |
| | 78 | |
|
| | 79 | | // Register the installation of this component in the local manifest file |
| 6 | 80 | | await manifestService.Add(this); |
| 6 | 81 | | } |
| | 82 | | else |
| 0 | 83 | | { |
| 0 | 84 | | Console.WriteLine("{0} already installed!", Key); |
| 0 | 85 | | } |
| | 86 | |
|
| 22 | 87 | | foreach (var dependency in Dependencies) |
| 2 | 88 | | { |
| 7 | 89 | | var component = manifestService.Remote.First(c => c.Key == dependency); |
| | 90 | |
|
| 2 | 91 | | component.InstallationStarted += InstallationStarted; |
| 2 | 92 | | component.InstallationFinished += InstallationFinished; |
| 2 | 93 | | component.InstallingPackageChanged += InstallingPackageChanged; |
| | 94 | |
|
| 2 | 95 | | await component.Install(fileService, limitsService, manifestService); |
| 2 | 96 | | } |
| | 97 | |
|
| 6 | 98 | | InstallationFinished?.Invoke(this, new InstallationFinishedEventArgs(Key)); |
| 6 | 99 | | } |
| | 100 | |
|
| | 101 | | public async Task Uninstall(IFileService fileService, IManifestService manifestService) |
| 2 | 102 | | { |
| 2 | 103 | | if (Installation is not null) |
| 2 | 104 | | { |
| | 105 | | // TODO: Double-check architectures from Architecture enum |
| 2 | 106 | | var architecture = RuntimeInformation.OSArchitecture switch |
| 2 | 107 | | { |
| 2 | 108 | | Architecture.X64 => "amd64", |
| 0 | 109 | | Architecture.Arm64 => "arm64", |
| 0 | 110 | | _ => throw new UnsupportedArchitectureException(RuntimeInformation.OSArchitecture) |
| 2 | 111 | | }; |
| | 112 | |
|
| 9 | 113 | | foreach (var package in Packages) |
| 2 | 114 | | { |
| 2 | 115 | | var registrationFileName = Path.Combine(manifestService.SnapConfigurationLocation, |
| 2 | 116 | | $"{package.Name}_{package.Version}_{architecture}.files"); |
| | 117 | |
|
| 2 | 118 | | if (!fileService.FileExists(registrationFileName)) |
| 1 | 119 | | { |
| 1 | 120 | | throw new ApplicationException("Registration file does not exist!"); |
| | 121 | | } |
| | 122 | |
|
| 1 | 123 | | var files = await fileService.ReadAllLines(registrationFileName); |
| 3 | 124 | | foreach (var file in files) |
| 0 | 125 | | { |
| 0 | 126 | | fileService.DeleteFile(file); |
| 0 | 127 | | } |
| | 128 | |
|
| 1 | 129 | | fileService.DeleteFile(registrationFileName); |
| 1 | 130 | | } |
| | 131 | |
|
| | 132 | | // Check for any empty directories |
| 1 | 133 | | fileService.RemoveEmptyDirectories(manifestService.DotnetInstallLocation); |
| | 134 | |
|
| 1 | 135 | | Installation = null; |
| 1 | 136 | | await manifestService.Remove(this); |
| 1 | 137 | | } |
| 1 | 138 | | } |
| | 139 | | } |