Skip to content

Commit 5b21e56

Browse files
committed
NugetAssemblyLocator better logging
1 parent 7aec5a2 commit 5b21e56

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

Core.Common.Standard/KY.Core.Common.Standard.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<RootNamespace>KY.Core</RootNamespace>
66
<AssemblyName>KY.Core.Common</AssemblyName>
7-
<Version>4.18.0</Version>
7+
<Version>4.19.0</Version>
88
<Authors>KY-Programming</Authors>
99
<Company>KY-Programmingp</Company>
1010
<Product>KY.Core</Product>
11-
<Copyright>2020 - KY-Programming</Copyright>
11+
<Copyright>2021 - KY-Programming</Copyright>
1212
<Description>KY.Core.Common for .net Standard
1313
Contains Dependency Injection, Module Loader and some helper classes</Description>
1414
<PackageLicenseExpression>GPL-3.0-or-later</PackageLicenseExpression>

Core.Common.Standard/Nuget/NugetAssemblyLocator.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public class NugetAssemblyLocator
1313
{
1414
private const string PackageDirectoryName = "\\packages\\";
1515

16+
private readonly List<string> resolvedAssemblies = new List<string>();
17+
1618
public static List<string> FrameworkFolders { get; } = new List<string>(new[] { "netstandard*", "netcore*", "net5*", "net4*" });
1719
public static List<string> NugetFolders { get; } = new List<string>(new[] { "lib", "ref" });
1820

19-
private readonly List<string> resolvedAssemblies = new List<string>();
20-
2121
public List<SearchLocation> Locations { get; }
2222
public bool SkipResourceAssemblies { get; set; }
2323
public bool SkipSystemAssemblies { get; set; }
@@ -53,8 +53,13 @@ public Assembly Locate(string search, bool loadDependencies)
5353
public Assembly Locate(string search, Version defaultVersion = null, bool loadDependencies = false, bool forceSearchOnDisk = false)
5454
{
5555
AssemblyInfo info = (this.GetAssemblyInfoFromLongName(search) ?? this.GetAssemblyInfoFromPath(search, defaultVersion)) ?? new AssemblyInfo(search, defaultVersion);
56-
if (this.SkipResourceAssemblies && info.IsResource || this.SkipSystemAssemblies && info.Name.StartsWith("System.") || info.Name == "netstandard" || info.Name == "mscorlib")
56+
if (this.SkipResourceAssemblies && info.IsResource || info.Name == "netstandard" || info.Name == "mscorlib")
57+
{
58+
return null;
59+
}
60+
if(this.SkipSystemAssemblies && info.Name.StartsWith("System."))
5761
{
62+
Logger.Trace($"Search for {info.Name} skipped. Set {nameof(this.SkipSystemAssemblies)} to false to disable this behaviour.");
5863
return null;
5964
}
6065
Assembly assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name == info.Name);
@@ -73,15 +78,15 @@ public Assembly Locate(string search, Version defaultVersion = null, bool loadDe
7378
}
7479
if (!forceSearchOnDisk && info.Name.StartsWith("System."))
7580
{
76-
assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(search));
77-
if (assembly != null)
81+
assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(search));
82+
if (assembly != null)
83+
{
84+
if (loadDependencies)
7885
{
79-
if (loadDependencies)
80-
{
81-
this.ResolveDependencies(assembly);
82-
}
83-
return assembly;
86+
this.ResolveDependencies(assembly);
8487
}
88+
return assembly;
89+
}
8590
}
8691
Logger.Trace($"Try to find assembly {info}...");
8792
List<SearchLocation> locations = this.CleanLocations(this.Locations);
@@ -109,21 +114,29 @@ public Assembly Locate(string search, Version defaultVersion = null, bool loadDe
109114
List<PossibleLocation> possibleLocations = new List<PossibleLocation>();
110115
foreach (SearchLocation location in locations.Where(x => x.SearchByVersion))
111116
{
117+
List<PossibleLocation> foundLocations = new List<PossibleLocation>();
112118
DirectoryInfo packageDirectory = FileSystem.GetDirectoryInfo(location.Path, info.Name);
113119
if (packageDirectory.Exists)
114120
{
115121
packageDirectory.GetDirectories()
116122
.Select(x => new PossibleLocation(x.FullName, this.Parse(x.Name)))
117123
.Where(x => x.Version != null)
118-
.ForEach(possibleLocations.Add);
124+
.ForEach(foundLocations.Add);
119125
}
120126
FileSystem.GetDirectoryInfos(location.Path, info.Name + "*")
121127
.Select(x => new PossibleLocation(x.FullName, this.Parse(Regex.Replace(x.Name, Regex.Escape(info.Name), string.Empty, RegexOptions.IgnoreCase).Trim("."))))
122128
.Where(x => x.Version != null)
123-
.ForEach(possibleLocations.Add);
129+
.ForEach(foundLocations.Add);
130+
if (foundLocations.Count > 0)
131+
{
132+
foundLocations.ForEach(x => Logger.Trace($"Assembly found in: {x.Path}"));
133+
}
134+
else
135+
{
136+
Logger.Trace($"Assembly not found in: {location.Path}");
137+
}
124138
}
125139
possibleLocations = possibleLocations.OrderByDescending(x => x.Version).ToList();
126-
possibleLocations.ForEach(x => Logger.Trace($"Assembly found in: {x.Path}"));
127140
PossibleLocation possibleLocation = possibleLocations.FirstOrDefault(x => info.Version == null || x.Version.ToString() == info.Version.ToString())
128141
?? possibleLocations.FirstOrDefault(x => x.Version.ToString(3) == info.Version.ToString(3))
129142
?? possibleLocations.FirstOrDefault(x => x.Version.ToString(2) == info.Version.ToString(2))
@@ -201,7 +214,7 @@ private Assembly TryFind(AssemblyInfo info, params string[] chunks)
201214
}
202215
catch (TargetInvocationException)
203216
{
204-
if (DoNotTryToLoadDependenciesOnError)
217+
if (this.DoNotTryToLoadDependenciesOnError)
205218
{
206219
throw;
207220
}

Core.Common.Standard/Nuget/NugetPackageDependencyLoader.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Linq;
54
using System.Reflection;
6-
using System.Runtime.CompilerServices;
75
using System.Runtime.InteropServices;
86
using KY.Core.DataAccess;
97
using KY.Core.Nuget;
@@ -27,13 +25,13 @@ static NugetPackageDependencyLoader()
2725
};
2826
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2927
{
30-
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%USERPROFILE%"), ".nuget\\packages")).SearchOnlyByVersion());
31-
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%PROGRAMFILES%"), "dotnet\\sdk\\NuGetFallbackFolder")).SearchOnlyByVersion());
32-
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%PROGRAMFILES%"), "dotnet\\sdk\\NuGetFallbackFolder")).SearchOnlyLocal());
28+
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%USERPROFILE%"), ".nuget", "packages")).SearchOnlyByVersion());
29+
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%PROGRAMFILES%"), "dotnet", "sdk", "NuGetFallbackFolder")).SearchOnlyByVersion());
30+
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.ExpandEnvironmentVariables("%PROGRAMFILES%"), "dotnet", "sdk", "NuGetFallbackFolder")).SearchOnlyLocal());
3331
}
3432
else
3533
{
36-
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.GetEnvironmentVariable("HOME"), ".nuget\\packages")).SearchOnlyByVersion());
34+
Locations.Add(new SearchLocation(FileSystem.Combine(Environment.GetEnvironmentVariable("HOME"), ".nuget", "packages")).SearchOnlyByVersion());
3735
}
3836
}
3937

0 commit comments

Comments
 (0)