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

Commit

Permalink
Merge pull request #62 from LokiMidgard/master
Browse files Browse the repository at this point in the history
Added Assembly Resolve
  • Loading branch information
AArnott authored May 10, 2018
2 parents 078476a + 9f35314 commit ed4512d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

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

Expand Down
3 changes: 2 additions & 1 deletion src/CodeGeneration.Roslyn/CodeGeneration.Roslyn.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.5</TargetFramework>
<TargetFramework>netstandard1.6</TargetFramework>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\opensource.snk</AssemblyOriginatorKeyFile>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
Expand All @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.0.4" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.2.0" />
<PackageReference Include="Validation" Version="2.4.13" />
Expand Down
58 changes: 57 additions & 1 deletion src/CodeGeneration.Roslyn/CompilationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace CodeGeneration.Roslyn
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -29,6 +31,10 @@ public class CompilationGenerator
private readonly List<string> generatedFiles = new List<string>();
private readonly List<string> additionalWrittenFiles = new List<string>();
private readonly List<string> loadedAssemblies = new List<string>();
private readonly Dictionary<string, Assembly> assembliesByPath = new Dictionary<string, Assembly>();
private readonly HashSet<string> directoriesWithResolver = new HashSet<string>();
private CompositeCompilationAssemblyResolver assemblyResolver;
private DependencyContext dependencyContext;

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

public string ProjectDirectory { get; set; }

public CompilationGenerator()
{
this.assemblyResolver = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[]
{
new ReferenceAssemblyPathResolver(),
new PackageCompilationAssemblyResolver()
});
this.dependencyContext = DependencyContext.Default;

var loadContext = AssemblyLoadContext.GetLoadContext(this.GetType().GetTypeInfo().Assembly);
loadContext.Resolving += this.ResolveAssembly;
}

public void Generate(IProgress<Diagnostic> progress = null, CancellationToken cancellationToken = default(CancellationToken))
{
Verify.Operation(this.Compile != null, $"{nameof(Compile)} must be set first.");
Expand Down Expand Up @@ -159,8 +178,45 @@ public class CompilationGenerator

protected virtual Assembly LoadAssembly(string path)
{
if (this.assembliesByPath.ContainsKey(path))
return this.assembliesByPath[path];

var loadContext = AssemblyLoadContext.GetLoadContext(this.GetType().GetTypeInfo().Assembly);
return loadContext.LoadFromAssemblyPath(path);
var assembly = loadContext.LoadFromAssemblyPath(path);

this.dependencyContext = this.dependencyContext.Merge(DependencyContext.Load(assembly));
var basePath = Path.GetDirectoryName(path);
if (!this.directoriesWithResolver.Contains(basePath))
{
this.assemblyResolver = new CompositeCompilationAssemblyResolver(new ICompilationAssemblyResolver[]
{
new AppBaseCompilationAssemblyResolver(basePath),
this.assemblyResolver
});
}

this.assembliesByPath.Add(path, assembly);
return assembly;
}

private Assembly ResolveAssembly(AssemblyLoadContext context, AssemblyName name)
{
var library = this.dependencyContext.RuntimeLibraries.FirstOrDefault(runtime => string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase));
if (library == null)
return null;
var wrapper = new CompilationLibrary(
library.Type,
library.Name,
library.Version,
library.Hash,
library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
library.Dependencies,
library.Serviceable);

var assemblyPathes = new List<string>();
this.assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblyPathes);

return assemblyPathes.Select(context.LoadFromAssemblyPath).FirstOrDefault();
}

private static DateTime GetLastModifiedAssemblyTime(string assemblyListPath)
Expand Down
12 changes: 1 addition & 11 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
<RepositoryUrl>https://github.com/aarnott/CodeGeneration.Roslyn</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<IsPackable Condition=" $(MSBuildProjectName.Contains('Test')) ">false</IsPackable>

<NerdbankGitVersioningVersion>1.6.25</NerdbankGitVersioningVersion>
<NerdbankGitVersioningVersion>2.1.23</NerdbankGitVersioningVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -26,13 +25,4 @@
<PackageLicenseUrl>https://raw.githubusercontent.com/AArnott/CodeGeneration.Roslyn/$(GitCommitIdShort)/LICENSE.txt</PackageLicenseUrl>
</PropertyGroup>
</Target>

<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' == 'true' ">
<Import Project="$(UserProfile)\.nuget\packages\nerdbank.gitversioning\$(NerdbankGitVersioningVersion)\buildCrossTargeting\Nerdbank.GitVersioning.targets"
Condition="Exists('$(UserProfile)\.nuget\packages\nerdbank.gitversioning\$(NerdbankGitVersioningVersion)\buildCrossTargeting\Nerdbank.GitVersioning.targets')" />
</ImportGroup>
<Target Name="FixUpVersion"
BeforeTargets="_GenerateRestoreProjectSpec"
DependsOnTargets="GetBuildVersion"
Condition=" '$(NerdbankGitVersioningTasksPath)' != '' " />
</Project>

0 comments on commit ed4512d

Please sign in to comment.