WIP
This commit is contained in:
parent
f67a247252
commit
fa52d02b9a
5 changed files with 109 additions and 7 deletions
|
@ -0,0 +1,38 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using Microsoft.Extensions.ProjectModel;
|
||||
|
||||
namespace Microsoft.DotNet.ProjectModel.Loader
|
||||
{
|
||||
public static class LoaderProjectContextExtensions
|
||||
{
|
||||
public static AssemblyLoadContext Create(this ProjectContext context, string configuration = "Debug")
|
||||
{
|
||||
var exporter = context.CreateExporter(configuration);
|
||||
var assemblies = new Dictionary<AssemblyName, string>();
|
||||
var dllImports = new Dictionary<string, string>();
|
||||
|
||||
foreach (var export in exporter.GetAllExports())
|
||||
{
|
||||
// TODO: Handle project references
|
||||
// TODO: Handle resource assemblies
|
||||
foreach (var asset in export.RuntimeAssemblies)
|
||||
{
|
||||
// REVIEW: Should we use the following?
|
||||
// AssemblyLoadContext.GetAssemblyName(asset.ResolvedPath);
|
||||
var assemblyName = new AssemblyName(asset.Name);
|
||||
assemblies[assemblyName] = asset.ResolvedPath;
|
||||
}
|
||||
|
||||
foreach (var asset in export.NativeLibraries)
|
||||
{
|
||||
dllImports[asset.Name] = asset.ResolvedPath;
|
||||
}
|
||||
}
|
||||
|
||||
return new ProjectContextLoadContext(assemblies, dllImports);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
|
||||
namespace Microsoft.DotNet.ProjectModel.Loader
|
||||
{
|
||||
public class ProjectContextLoadContext : AssemblyLoadContext
|
||||
{
|
||||
private readonly Dictionary<AssemblyName, string> _assemblies;
|
||||
private readonly Dictionary<string, string> _dllImports;
|
||||
public ProjectContextLoadContext(Dictionary<AssemblyName, string> assemblies,
|
||||
Dictionary<string, string> dllImports)
|
||||
{
|
||||
_assemblies = assemblies;
|
||||
_dllImports = dllImports;
|
||||
}
|
||||
|
||||
protected override Assembly Load(AssemblyName assemblyName)
|
||||
{
|
||||
string path;
|
||||
if (_assemblies.TryGetValue(assemblyName, out path))
|
||||
{
|
||||
return LoadFromAssemblyPath(path);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
|
||||
{
|
||||
string path;
|
||||
if (_dllImports.TryGetValue(unmanagedDllName, out path))
|
||||
{
|
||||
return LoadUnmanagedDllFromPath(path);
|
||||
}
|
||||
|
||||
return base.LoadUnmanagedDll(unmanagedDllName);
|
||||
}
|
||||
}
|
||||
}
|
9
src/Microsoft.DotNet.ProjectModel.Loader/project.json
Normal file
9
src/Microsoft.DotNet.ProjectModel.Loader/project.json
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"Microsoft.DotNet.ProjectModel": "1.0.0-*",
|
||||
"System.Runtime.Loader": "4.0.0-beta-23504"
|
||||
},
|
||||
"frameworks": {
|
||||
"dnxcore50": { }
|
||||
}
|
||||
}
|
|
@ -21,22 +21,23 @@ namespace Microsoft.DotNet.ProjectModel.Workspaces
|
|||
{
|
||||
private Dictionary<string, AssemblyMetadata> _cache = new Dictionary<string, AssemblyMetadata>();
|
||||
|
||||
private readonly string[] _projectPaths;
|
||||
public ProjectJsonWorkspace(ProjectContext context) : base(MefHostServices.DefaultHost, "Custom")
|
||||
{
|
||||
AddProject(context);
|
||||
}
|
||||
|
||||
public ProjectJsonWorkspace(string projectPath) : this(new[] { projectPath })
|
||||
{
|
||||
}
|
||||
|
||||
public ProjectJsonWorkspace(string[] projectPaths) : base(MefHostServices.DefaultHost, "Custom")
|
||||
public ProjectJsonWorkspace(IEnumerable<string> projectPaths) : base(MefHostServices.DefaultHost, "Custom")
|
||||
{
|
||||
_projectPaths = projectPaths;
|
||||
|
||||
Initialize();
|
||||
Initialize(projectPaths);
|
||||
}
|
||||
|
||||
private void Initialize()
|
||||
private void Initialize(IEnumerable<string> projectPaths)
|
||||
{
|
||||
foreach (var projectPath in _projectPaths)
|
||||
foreach (var projectPath in projectPaths)
|
||||
{
|
||||
AddProject(projectPath);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.Extensions.ProjectModel;
|
||||
|
||||
namespace Microsoft.DotNet.ProjectModel.Workspaces
|
||||
{
|
||||
public static class WorkspaceProjectContextExtensions
|
||||
{
|
||||
public static Workspace CreateWorkspace(this ProjectContext context)
|
||||
{
|
||||
return new ProjectJsonWorkspace(context);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue