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

73 lines
2.4 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;
namespace Microsoft.Extensions.DependencyModel
{
public class DependencyContext
{
private const string DepsResourceSufix = ".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);
public DependencyContext(string target, string runtime, CompilationOptions compilationOptions, CompilationLibrary[] compileLibraries, RuntimeLibrary[] runtimeLibraries)
2015-12-17 15:04:18 -08:00
{
Target = target;
Runtime = runtime;
CompilationOptions = compilationOptions;
2015-12-17 15:04:18 -08:00
CompileLibraries = compileLibraries;
RuntimeLibraries = runtimeLibraries;
}
public static DependencyContext Default => _defaultContext.Value;
public string Target { get; }
2015-12-17 15:04:18 -08:00
public string Runtime { 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
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
using (var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + DepsResourceSufix))
2015-12-17 15:04:18 -08:00
{
if (stream != null)
{
return new DependencyContextJsonReader().Read(stream);
}
2015-12-17 15:04:18 -08:00
}
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
}
}
}