dotnet-installer/src/Microsoft.Extensions.DependencyModel/DependencyContext.cs

116 lines
3.8 KiB
C#
Raw Normal View History

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";
private const string DepsFileExtension = ".deps";
2015-12-17 15:04:18 -08:00
private static readonly Lazy<DependencyContext> _defaultContext = new Lazy<DependencyContext>(LoadDefault);
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;
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
}
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
public string Runtime { get; }
2016-02-25 11:00:48 -08:00
public bool IsPortable { get; }
public CompilationOptions CompilationOptions { get; }
2015-12-17 15:04:18 -08:00
public IReadOnlyList<CompilationLibrary> CompileLibraries { get; }
2015-12-17 15:04:18 -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
private static DependencyContext LoadDefault()
2015-12-17 15:04:18 -08:00
{
var entryAssembly = Assembly.GetEntryAssembly();
return Load(entryAssembly);
}
public static DependencyContext Load(Assembly assembly)
{
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
{
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);
}
}
var depsFile = Path.ChangeExtension(assembly.Location, DepsFileExtension);
if (File.Exists(depsFile))
2015-12-17 15:04:18 -08:00
{
using (var stream = File.OpenRead(depsFile))
{
return new DependencyContextCsvReader().Read(stream);
}
2015-12-17 15:04:18 -08:00
}
return null;
2015-12-17 15:04:18 -08:00
}
}
}