Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit ed4512d

Browse files
authored
Merge pull request #62 from LokiMidgard/master
Added Assembly Resolve
2 parents 078476a + 9f35314 commit ed4512d

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed

src/CodeGeneration.Roslyn.Tests.Generators/CodeGeneration.Roslyn.Tests.Generators.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.5</TargetFramework>
4+
<TargetFramework>netstandard1.6</TargetFramework>
55
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
66
</PropertyGroup>
77

src/CodeGeneration.Roslyn/CodeGeneration.Roslyn.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.5</TargetFramework>
4+
<TargetFramework>netstandard1.6</TargetFramework>
55
<SignAssembly>True</SignAssembly>
66
<AssemblyOriginatorKeyFile>..\opensource.snk</AssemblyOriginatorKeyFile>
77
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
@@ -15,6 +15,7 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18+
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.4" />
1819
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
1920
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.2.0" />
2021
<PackageReference Include="Validation" Version="2.4.13" />

src/CodeGeneration.Roslyn/CompilationGenerator.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace CodeGeneration.Roslyn
77
using Microsoft.CodeAnalysis.CSharp;
88
using Microsoft.CodeAnalysis.CSharp.Syntax;
99
using Microsoft.CodeAnalysis.Text;
10+
using Microsoft.Extensions.DependencyModel;
11+
using Microsoft.Extensions.DependencyModel.Resolution;
1012
using System;
1113
using System.Collections.Generic;
1214
using System.Globalization;
@@ -29,6 +31,10 @@ public class CompilationGenerator
2931
private readonly List<string> generatedFiles = new List<string>();
3032
private readonly List<string> additionalWrittenFiles = new List<string>();
3133
private readonly List<string> loadedAssemblies = new List<string>();
34+
private readonly Dictionary<string, Assembly> assembliesByPath = new Dictionary<string, Assembly>();
35+
private readonly HashSet<string> directoriesWithResolver = new HashSet<string>();
36+
private CompositeCompilationAssemblyResolver assemblyResolver;
37+
private DependencyContext dependencyContext;
3238

3339
/// <summary>
3440
/// Gets or sets the list of paths of files to be compiled.
@@ -67,6 +73,19 @@ public class CompilationGenerator
6773

6874
public string ProjectDirectory { get; set; }
6975

76+
public CompilationGenerator()
77+
{
78+
this.assemblyResolver = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[]
79+
{
80+
new ReferenceAssemblyPathResolver(),
81+
new PackageCompilationAssemblyResolver()
82+
});
83+
this.dependencyContext = DependencyContext.Default;
84+
85+
var loadContext = AssemblyLoadContext.GetLoadContext(this.GetType().GetTypeInfo().Assembly);
86+
loadContext.Resolving += this.ResolveAssembly;
87+
}
88+
7089
public void Generate(IProgress<Diagnostic> progress = null, CancellationToken cancellationToken = default(CancellationToken))
7190
{
7291
Verify.Operation(this.Compile != null, $"{nameof(Compile)} must be set first.");
@@ -159,8 +178,45 @@ public class CompilationGenerator
159178

160179
protected virtual Assembly LoadAssembly(string path)
161180
{
181+
if (this.assembliesByPath.ContainsKey(path))
182+
return this.assembliesByPath[path];
183+
162184
var loadContext = AssemblyLoadContext.GetLoadContext(this.GetType().GetTypeInfo().Assembly);
163-
return loadContext.LoadFromAssemblyPath(path);
185+
var assembly = loadContext.LoadFromAssemblyPath(path);
186+
187+
this.dependencyContext = this.dependencyContext.Merge(DependencyContext.Load(assembly));
188+
var basePath = Path.GetDirectoryName(path);
189+
if (!this.directoriesWithResolver.Contains(basePath))
190+
{
191+
this.assemblyResolver = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[]
192+
{
193+
new AppBaseCompilationAssemblyResolver(basePath),
194+
this.assemblyResolver
195+
});
196+
}
197+
198+
this.assembliesByPath.Add(path, assembly);
199+
return assembly;
200+
}
201+
202+
private Assembly ResolveAssembly(AssemblyLoadContext context, AssemblyName name)
203+
{
204+
var library = this.dependencyContext.RuntimeLibraries.FirstOrDefault(runtime => string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase));
205+
if (library == null)
206+
return null;
207+
var wrapper = new CompilationLibrary(
208+
library.Type,
209+
library.Name,
210+
library.Version,
211+
library.Hash,
212+
library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
213+
library.Dependencies,
214+
library.Serviceable);
215+
216+
var assemblyPathes = new List<string>();
217+
this.assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblyPathes);
218+
219+
return assemblyPathes.Select(context.LoadFromAssemblyPath).FirstOrDefault();
164220
}
165221

166222
private static DateTime GetLastModifiedAssemblyTime(string assemblyListPath)

src/Directory.Build.props

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
<RepositoryUrl>https://github.com/aarnott/CodeGeneration.Roslyn</RepositoryUrl>
1212
<RepositoryType>git</RepositoryType>
1313
<IsPackable Condition=" $(MSBuildProjectName.Contains('Test')) ">false</IsPackable>
14-
15-
<NerdbankGitVersioningVersion>1.6.25</NerdbankGitVersioningVersion>
14+
<NerdbankGitVersioningVersion>2.1.23</NerdbankGitVersioningVersion>
1615
</PropertyGroup>
1716

1817
<ItemGroup>
@@ -26,13 +25,4 @@
2625
<PackageLicenseUrl>https://raw.githubusercontent.com/AArnott/CodeGeneration.Roslyn/$(GitCommitIdShort)/LICENSE.txt</PackageLicenseUrl>
2726
</PropertyGroup>
2827
</Target>
29-
30-
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' == 'true' ">
31-
<Import Project="$(UserProfile)\.nuget\packages\nerdbank.gitversioning\$(NerdbankGitVersioningVersion)\buildCrossTargeting\Nerdbank.GitVersioning.targets"
32-
Condition="Exists('$(UserProfile)\.nuget\packages\nerdbank.gitversioning\$(NerdbankGitVersioningVersion)\buildCrossTargeting\Nerdbank.GitVersioning.targets')" />
33-
</ImportGroup>
34-
<Target Name="FixUpVersion"
35-
BeforeTargets="_GenerateRestoreProjectSpec"
36-
DependsOnTargets="GetBuildVersion"
37-
Condition=" '$(NerdbankGitVersioningTasksPath)' != '' " />
3828
</Project>

0 commit comments

Comments
 (0)