-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathNuGetDependency.cs
More file actions
89 lines (79 loc) · 2.88 KB
/
NuGetDependency.cs
File metadata and controls
89 lines (79 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using LabApi.Features.Console;
using System;
using System.Reflection;
namespace LabApi.Loader.Features.NuGet.Models;
/// <summary>
/// Represents a dependency entry within a NuGet package,
/// including its identifier and version, and provides
/// helper methods for installation and status checking.
/// </summary>
public class NuGetDependency
{
/// <summary>
/// Gets or sets the unique identifier (name) of the NuGet dependency.
/// </summary>
public required string Id { get; set; }
/// <summary>
/// Gets or sets the semantic version string of the dependency (e.g. "1.2.3").
/// </summary>
public required string Version { get; set; }
/// <summary>
/// Installs this NuGet dependency by downloading it from the configured source.
/// </summary>
/// <returns>
/// A <see cref="NuGetPackage"/> instance representing the downloaded package,
/// or <see langword="null"/> if the installation failed or the package could not be found.
/// </returns>
/// <remarks>
/// This method delegates to <see cref="NuGetPackagesManager.DownloadNugetPackage(string, string)"/>.
/// </remarks>
public NuGetPackage? Install() => NuGetPackagesManager.DownloadNugetPackage(Id, Version);
/// <summary>
/// Determines whether this dependency is already installed
/// or loaded in the current AppDomain.
/// </summary>
/// <returns>
/// <c>true</c> if the dependency is installed or the corresponding assembly is already loaded;
/// otherwise, <c>false</c>.
/// </returns>
public bool IsInstalled()
{
if (NuGetPackagesManager.Packages.TryGetValue($"{Id}.{Version}", out NuGetPackage package))
{
return string.Equals(package.Version, Version, StringComparison.OrdinalIgnoreCase);
}
if (IsAssemblyAlreadyLoaded(Id))
{
return true;
}
return false;
}
/// <summary>
/// Checks whether an assembly with the given identifier is already loaded
/// into the current application domain.
/// </summary>
/// <param name="id">The dependency or assembly identifier to check.</param>
/// <returns>
/// <c>true</c> if an assembly with the specified ID is already loaded;
/// otherwise, <c>false</c>.
/// </returns>
private bool IsAssemblyAlreadyLoaded(string id)
{
try
{
foreach (Assembly? asm in AppDomain.CurrentDomain.GetAssemblies())
{
string asmName = asm.GetName().Name ?? string.Empty;
if (asmName.Equals(id, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
catch (Exception ex)
{
Logger.Warn($"{NuGetPackagesManager.Prefix} Failed to check if assembly '{id}' is already loaded: {ex.Message}");
}
return false;
}
}