2015-12-17 15:04:18 -08:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Collections.Generic;
|
2016-03-04 10:57:38 -08:00
|
|
|
|
using System.Linq;
|
2015-12-17 15:04:18 -08:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.Extensions.DependencyModel
|
|
|
|
|
{
|
|
|
|
|
public class DependencyContext
|
|
|
|
|
{
|
2016-03-08 16:46:50 -08:00
|
|
|
|
private const string DepsJsonExtension = ".deps.json";
|
2016-02-19 16:48:43 -08:00
|
|
|
|
private const string DepsFileExtension = ".deps";
|
2015-12-17 15:04:18 -08:00
|
|
|
|
|
2016-02-07 23:05:35 -08:00
|
|
|
|
private static readonly Lazy<DependencyContext> _defaultContext = new Lazy<DependencyContext>(LoadDefault);
|
2016-01-05 15:35:32 -08:00
|
|
|
|
|
2016-03-04 14:12:16 -08:00
|
|
|
|
public DependencyContext(string targetFramework,
|
2016-02-25 11:00:48 -08:00
|
|
|
|
string runtime,
|
|
|
|
|
bool isPortable,
|
|
|
|
|
CompilationOptions compilationOptions,
|
2016-03-04 10:57:38 -08:00
|
|
|
|
IEnumerable<CompilationLibrary> compileLibraries,
|
|
|
|
|
IEnumerable<RuntimeLibrary> runtimeLibraries,
|
|
|
|
|
IEnumerable<KeyValuePair<string, string[]>> runtimeGraph)
|
2015-12-17 15:04:18 -08:00
|
|
|
|
{
|
2016-03-04 14:12:16 -08:00
|
|
|
|
if (targetFramework == null)
|
2016-03-04 09:13:04 -08:00
|
|
|
|
{
|
2016-03-04 14:12:16 -08:00
|
|
|
|
throw new ArgumentNullException(nameof(targetFramework));
|
2016-03-04 09:13:04 -08:00
|
|
|
|
}
|
|
|
|
|
if (runtime == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(runtime));
|
|
|
|
|
}
|
|
|
|
|
if (compileLibraries == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(compileLibraries));
|
|
|
|
|
}
|
|
|
|
|
if (runtimeLibraries == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(runtimeLibraries));
|
|
|
|
|
}
|
|
|
|
|
if (runtimeGraph == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(runtimeGraph));
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-04 14:12:16 -08:00
|
|
|
|
TargetFramework = targetFramework;
|
2015-12-17 15:04:18 -08:00
|
|
|
|
Runtime = runtime;
|
2016-02-25 11:00:48 -08:00
|
|
|
|
IsPortable = isPortable;
|
2015-12-21 10:36:20 -08:00
|
|
|
|
CompilationOptions = compilationOptions;
|
2016-03-04 10:57:38 -08:00
|
|
|
|
CompileLibraries = compileLibraries.ToArray();
|
|
|
|
|
RuntimeLibraries = runtimeLibraries.ToArray();
|
|
|
|
|
RuntimeGraph = runtimeGraph.ToArray();
|
2015-12-17 15:04:18 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-05 15:35:32 -08:00
|
|
|
|
public static DependencyContext Default => _defaultContext.Value;
|
|
|
|
|
|
2016-03-04 14:12:16 -08:00
|
|
|
|
public string TargetFramework { get; }
|
2015-12-17 15:04:18 -08:00
|
|
|
|
|
2015-12-21 10:36:20 -08:00
|
|
|
|
public string Runtime { get; }
|
|
|
|
|
|
2016-02-25 11:00:48 -08:00
|
|
|
|
public bool IsPortable { get; }
|
|
|
|
|
|
2015-12-21 10:36:20 -08:00
|
|
|
|
public CompilationOptions CompilationOptions { get; }
|
2015-12-17 15:04:18 -08:00
|
|
|
|
|
2016-01-07 15:11:47 -08:00
|
|
|
|
public IReadOnlyList<CompilationLibrary> CompileLibraries { get; }
|
2015-12-17 15:04:18 -08:00
|
|
|
|
|
2016-01-07 15:11:47 -08:00
|
|
|
|
public IReadOnlyList<RuntimeLibrary> RuntimeLibraries { get; }
|
2015-12-17 15:04:18 -08:00
|
|
|
|
|
2016-03-01 15:11:52 -08:00
|
|
|
|
public IReadOnlyList<KeyValuePair<string, string[]>> RuntimeGraph { get; }
|
2016-02-25 11:00:48 -08:00
|
|
|
|
|
2016-01-05 15:35:32 -08:00
|
|
|
|
private static DependencyContext LoadDefault()
|
2015-12-17 15:04:18 -08:00
|
|
|
|
{
|
2016-02-07 23:05:35 -08:00
|
|
|
|
var entryAssembly = Assembly.GetEntryAssembly();
|
2016-02-05 14:27:09 -08:00
|
|
|
|
return Load(entryAssembly);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DependencyContext Load(Assembly assembly)
|
|
|
|
|
{
|
2016-02-19 16:48:43 -08:00
|
|
|
|
if (assembly == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(assembly));
|
|
|
|
|
}
|
2015-12-17 15:04:18 -08:00
|
|
|
|
|
2016-03-08 16:46:50 -08:00
|
|
|
|
using (var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + DepsJsonExtension))
|
2015-12-17 15:04:18 -08:00
|
|
|
|
{
|
2016-02-19 16:48:43 -08:00
|
|
|
|
if (stream != null)
|
|
|
|
|
{
|
|
|
|
|
return new DependencyContextJsonReader().Read(stream);
|
|
|
|
|
}
|
2015-12-17 15:04:18 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 16:46:50 -08:00
|
|
|
|
var depsJsonFile = Path.ChangeExtension(assembly.Location, DepsJsonExtension);
|
|
|
|
|
if (File.Exists(depsJsonFile))
|
|
|
|
|
{
|
|
|
|
|
using (var stream = File.OpenRead(depsJsonFile))
|
|
|
|
|
{
|
|
|
|
|
return new DependencyContextJsonReader().Read(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 16:48:43 -08:00
|
|
|
|
var depsFile = Path.ChangeExtension(assembly.Location, DepsFileExtension);
|
|
|
|
|
if (File.Exists(depsFile))
|
2015-12-17 15:04:18 -08:00
|
|
|
|
{
|
2016-02-19 16:48:43 -08:00
|
|
|
|
using (var stream = File.OpenRead(depsFile))
|
|
|
|
|
{
|
|
|
|
|
return new DependencyContextCsvReader().Read(stream);
|
|
|
|
|
}
|
2015-12-17 15:04:18 -08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-19 16:48:43 -08:00
|
|
|
|
return null;
|
2015-12-17 15:04:18 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|