Skip to content

Commit

Permalink
fix MapsterMapper#319 cannot load library with multiple runtime assem…
Browse files Browse the repository at this point in the history
…blies
  • Loading branch information
chaowlert committed Apr 10, 2021
1 parent 3b40ea0 commit 1a6c142
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"mapster.tool": {
"version": "8.0.5",
"version": "8.0.13",
"commands": [
"dotnet-mapster"
]
Expand Down
30 changes: 26 additions & 4 deletions src/Mapster.Tool/AssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,27 @@ public void Dispose()
if (name.Name == "System.Text.Json")
return typeof(JsonIgnoreAttribute).Assembly;

var library = _dependencyContext.RuntimeLibraries
.FirstOrDefault(it => string.Equals(it.Name, name.Name, StringComparison.OrdinalIgnoreCase));
var (library, assetPath) = (from lib in _dependencyContext.RuntimeLibraries
from grp in lib.RuntimeAssemblyGroups
where grp.Runtime == string.Empty
from path in grp.AssetPaths
where string.Equals(GetAssemblyName(path), name.Name, StringComparison.OrdinalIgnoreCase)
select (lib, path)).FirstOrDefault();

if (library == null)
{
Console.WriteLine("Cannot find library: " + name.Name);
return null;
}

try
{
var wrapped = new CompilationLibrary(
library.Type,
library.Name,
library.Version,
library.Hash,
library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
new[] {assetPath},
library.Dependencies,
library.Serviceable);

Expand All @@ -88,5 +93,22 @@ public void Dispose()
return null;
}
}

private const string NativeImageSufix = ".ni";
private static string GetAssemblyName(string assetPath)
{
var name = Path.GetFileNameWithoutExtension(assetPath);
if (name == null)
{
throw new ArgumentException($"Provided path has empty file name '{assetPath}'", nameof(assetPath));
}

if (name.EndsWith(NativeImageSufix))
{
name = name.Substring(0, name.Length - NativeImageSufix.Length);
}

return name;
}
}
}
2 changes: 1 addition & 1 deletion src/Mapster.Tool/Mapster.Tool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Mapster.Tool.snk</AssemblyOriginatorKeyFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>8.0.5</Version>
<Version>8.0.13</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>Copyright (c) 2020 Chaowlert Chaisrichalermpol</Copyright>
<PackageIcon>icon.png</PackageIcon>
Expand Down

0 comments on commit 1a6c142

Please sign in to comment.