< Summary

Information
Class: Dotnet.Installer.Core.Models.Component
Assembly: Dotnet.Installer.Core
File(s): /home/runner/work/dotnet-snap/dotnet-snap/src/Dotnet.Installer.Core/Models/Component.cs
Line coverage
86%
Covered lines: 73
Uncovered lines: 11
Coverable lines: 84
Total lines: 139
Line coverage: 86.9%
Branch coverage
75%
Covered branches: 27
Total branches: 36
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Key()100%11100%
get_Name()100%11100%
get_Description()100%11100%
get_BaseUrl()100%11100%
get_Version()100%11100%
get_Packages()100%11100%
get_Dependencies()100%11100%
get_Installation()100%11100%
CanInstall(...)75%4.01490.9%
Install()80%21.172085.71%
Uninstall()66.66%12.821282.14%

File(s)

/home/runner/work/dotnet-snap/dotnet-snap/src/Dotnet.Installer.Core/Models/Component.cs

#LineLine coverage
 1using System.Runtime.InteropServices;
 2using Dotnet.Installer.Core.Exceptions;
 3using Dotnet.Installer.Core.Models.Events;
 4using Dotnet.Installer.Core.Services.Contracts;
 5using Dotnet.Installer.Core.Types;
 6
 7namespace Dotnet.Installer.Core.Models;
 8
 9public class Component
 10{
 2811    public required string Key { get; init; }
 1312    public required string Name { get; init; }
 1313    public required string Description { get; init; }
 1914    public required Uri BaseUrl { get; init; }
 4615    public required DotnetVersion Version { get; init; }
 2116    public required IEnumerable<Package> Packages { get; init; }
 3717    public required IEnumerable<string> Dependencies { get; init; }
 1218    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)
 825    {
 826        if (Version.IsRuntime)
 127        {
 128            return Version <= limitsService.Runtime;
 29        }
 30
 731        if (Version.IsSdk)
 732        {
 733            var limitOnFeatureBand = limitsService.Sdk
 1434                .First(v => v.FeatureBand == Version.FeatureBand);
 35
 736            return Version <= limitOnFeatureBand;
 37        }
 38
 039        return false;
 840    }
 41
 42    public async Task Install(
 43        IFileService fileService,
 44        ILimitsService limitsService,
 45        IManifestService manifestService)
 846    {
 47        // TODO: Double-check architectures from Architecture enum
 848        var architecture = RuntimeInformation.OSArchitecture switch
 849        {
 850            Architecture.X64 => "amd64",
 051            Architecture.Arm64 => "arm64",
 052            _ => throw new UnsupportedArchitectureException(RuntimeInformation.OSArchitecture)
 853        };
 54
 855        if (!CanInstall(limitsService))
 256        {
 257            throw new VersionTooHighException(this,
 358                Version.IsRuntime ? limitsService.Runtime : limitsService.Sdk.First(v => v.FeatureBand == Version.Featur
 59        }
 60
 661        if (Installation is null)
 662        {
 663            InstallationStarted?.Invoke(this, new InstallationStartedEventArgs(Key));
 64
 65            // Install component packages
 3066            foreach (var package in Packages)
 667            {
 668                InstallingPackageChanged?.Invoke(this, new InstallingPackageChangedEventArgs(package, this));
 69
 670                var debUrl = new Uri(BaseUrl, $"{package.Name}_{package.Version}_{architecture}.deb");
 71
 672                var filePath = await fileService.DownloadFile(debUrl, manifestService.DotnetInstallLocation);
 73
 674                await fileService.ExtractDeb(filePath, manifestService.DotnetInstallLocation, manifestService.SnapConfig
 75
 676                fileService.DeleteFile(filePath);
 677            }
 78
 79            // Register the installation of this component in the local manifest file
 680            await manifestService.Add(this);
 681        }
 82        else
 083        {
 084            Console.WriteLine("{0} already installed!", Key);
 085        }
 86
 2287        foreach (var dependency in Dependencies)
 288        {
 789            var component = manifestService.Remote.First(c => c.Key == dependency);
 90
 291            component.InstallationStarted += InstallationStarted;
 292            component.InstallationFinished += InstallationFinished;
 293            component.InstallingPackageChanged += InstallingPackageChanged;
 94
 295            await component.Install(fileService, limitsService, manifestService);
 296        }
 97
 698        InstallationFinished?.Invoke(this, new InstallationFinishedEventArgs(Key));
 699    }
 100
 101    public async Task Uninstall(IFileService fileService, IManifestService manifestService)
 2102    {
 2103        if (Installation is not null)
 2104        {
 105            // TODO: Double-check architectures from Architecture enum
 2106            var architecture = RuntimeInformation.OSArchitecture switch
 2107            {
 2108                Architecture.X64 => "amd64",
 0109                Architecture.Arm64 => "arm64",
 0110                _ => throw new UnsupportedArchitectureException(RuntimeInformation.OSArchitecture)
 2111            };
 112
 9113            foreach (var package in Packages)
 2114            {
 2115                var registrationFileName = Path.Combine(manifestService.SnapConfigurationLocation,
 2116                    $"{package.Name}_{package.Version}_{architecture}.files");
 117
 2118                if (!fileService.FileExists(registrationFileName))
 1119                {
 1120                    throw new ApplicationException("Registration file does not exist!");
 121                }
 122
 1123                var files = await fileService.ReadAllLines(registrationFileName);
 3124                foreach (var file in files)
 0125                {
 0126                    fileService.DeleteFile(file);
 0127                }
 128
 1129                fileService.DeleteFile(registrationFileName);
 1130            }
 131
 132            // Check for any empty directories
 1133            fileService.RemoveEmptyDirectories(manifestService.DotnetInstallLocation);
 134
 1135            Installation = null;
 1136            await manifestService.Remove(this);
 1137        }
 1138    }
 139}