2016-01-07 23:11:47 +00: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.
|
|
|
|
|
|
2015-12-17 23:04:18 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-02-10 18:07:22 +00:00
|
|
|
|
using System.IO;
|
2015-12-17 23:04:18 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel.Compilation;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel.Graph;
|
2016-02-10 18:07:22 +00:00
|
|
|
|
using Microsoft.DotNet.ProjectModel.Resolution;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel.Utilities;
|
2015-12-21 18:36:20 +00:00
|
|
|
|
using NuGet.Frameworks;
|
2015-12-17 23:04:18 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.Extensions.DependencyModel
|
|
|
|
|
{
|
|
|
|
|
public static class DependencyContextBuilder
|
|
|
|
|
{
|
2015-12-21 18:36:20 +00:00
|
|
|
|
public static DependencyContext Build(CommonCompilerOptions compilerOptions, LibraryExporter libraryExporter, string configuration, NuGetFramework target, string runtime)
|
2015-12-17 23:04:18 +00:00
|
|
|
|
{
|
2016-02-09 20:33:47 +00:00
|
|
|
|
var dependencies = libraryExporter.GetAllExports();
|
2015-12-17 23:04:18 +00:00
|
|
|
|
|
2015-12-21 18:36:20 +00:00
|
|
|
|
// Sometimes we have package and reference assembly with the same name (System.Runtime for example) thats why we
|
|
|
|
|
// deduplicating them prefering reference assembly
|
|
|
|
|
var dependencyLookup = dependencies
|
|
|
|
|
.OrderBy(export => export.Library.Identity.Type == LibraryType.ReferenceAssembly)
|
|
|
|
|
.GroupBy(export => export.Library.Identity.Name)
|
|
|
|
|
.Select(exports => exports.First())
|
|
|
|
|
.Select(export => new Dependency(export.Library.Identity.Name, export.Library.Identity.Version.ToString()))
|
|
|
|
|
.ToDictionary(dependency => dependency.Name);
|
|
|
|
|
|
2016-03-01 23:11:52 +00:00
|
|
|
|
return new DependencyContext(target.DotNetFrameworkName, runtime, false,
|
2015-12-21 18:36:20 +00:00
|
|
|
|
GetCompilationOptions(compilerOptions),
|
2016-01-07 23:11:47 +00:00
|
|
|
|
GetLibraries(dependencies, dependencyLookup, target, configuration, runtime: false).Cast<CompilationLibrary>().ToArray(),
|
2016-03-01 23:11:52 +00:00
|
|
|
|
GetLibraries(dependencies, dependencyLookup, target, configuration, runtime: true).Cast<RuntimeLibrary>().ToArray(),
|
|
|
|
|
new KeyValuePair<string, string[]>[0]);
|
2015-12-17 23:04:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-21 18:36:20 +00:00
|
|
|
|
private static CompilationOptions GetCompilationOptions(CommonCompilerOptions compilerOptions)
|
2015-12-17 23:04:18 +00:00
|
|
|
|
{
|
2015-12-21 18:36:20 +00:00
|
|
|
|
return new CompilationOptions(compilerOptions.Defines,
|
|
|
|
|
compilerOptions.LanguageVersion,
|
|
|
|
|
compilerOptions.Platform,
|
|
|
|
|
compilerOptions.AllowUnsafe,
|
|
|
|
|
compilerOptions.WarningsAsErrors,
|
|
|
|
|
compilerOptions.Optimize,
|
|
|
|
|
compilerOptions.KeyFile,
|
|
|
|
|
compilerOptions.DelaySign,
|
|
|
|
|
compilerOptions.PublicSign,
|
2016-03-02 23:53:59 +00:00
|
|
|
|
compilerOptions.DebugType,
|
2016-01-08 19:03:14 +00:00
|
|
|
|
compilerOptions.EmitEntryPoint,
|
|
|
|
|
compilerOptions.GenerateXmlDocumentation);
|
2015-12-17 23:04:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 23:11:47 +00:00
|
|
|
|
private static IEnumerable<Library> GetLibraries(IEnumerable<LibraryExport> dependencies,
|
2015-12-21 18:36:20 +00:00
|
|
|
|
IDictionary<string, Dependency> dependencyLookup,
|
|
|
|
|
NuGetFramework target,
|
|
|
|
|
string configuration,
|
2016-01-07 23:11:47 +00:00
|
|
|
|
bool runtime)
|
2015-12-17 23:04:18 +00:00
|
|
|
|
{
|
2016-01-07 23:11:47 +00:00
|
|
|
|
return dependencies.Select(export => GetLibrary(export, target, configuration, runtime, dependencyLookup));
|
2015-12-21 18:36:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Library GetLibrary(LibraryExport export,
|
|
|
|
|
NuGetFramework target,
|
|
|
|
|
string configuration,
|
2016-01-07 23:11:47 +00:00
|
|
|
|
bool runtime,
|
2015-12-21 18:36:20 +00:00
|
|
|
|
IDictionary<string, Dependency> dependencyLookup)
|
|
|
|
|
{
|
2016-02-10 18:07:22 +00:00
|
|
|
|
var type = export.Library.Identity.Type;
|
2015-12-21 18:36:20 +00:00
|
|
|
|
|
2015-12-17 23:04:18 +00:00
|
|
|
|
var serviceable = (export.Library as PackageDescription)?.Library.IsServiceable ?? false;
|
2015-12-21 18:36:20 +00:00
|
|
|
|
var libraryDependencies = new List<Dependency>();
|
2015-12-17 23:04:18 +00:00
|
|
|
|
|
2016-01-07 23:11:47 +00:00
|
|
|
|
var libraryAssets = runtime ? export.RuntimeAssemblies : export.CompilationAssemblies;
|
|
|
|
|
|
2016-02-05 22:27:09 +00:00
|
|
|
|
foreach (var libraryDependenciesGroup in export.Library.Dependencies.GroupBy(d => d.Name))
|
2015-12-21 18:36:20 +00:00
|
|
|
|
{
|
2016-02-05 22:27:09 +00:00
|
|
|
|
LibraryRange libraryDependency = libraryDependenciesGroup
|
|
|
|
|
.OrderByDescending(d => d.Target == LibraryType.ReferenceAssembly)
|
|
|
|
|
.First();
|
|
|
|
|
|
2015-12-21 18:36:20 +00:00
|
|
|
|
Dependency dependency;
|
|
|
|
|
if (dependencyLookup.TryGetValue(libraryDependency.Name, out dependency))
|
|
|
|
|
{
|
|
|
|
|
libraryDependencies.Add(dependency);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] assemblies;
|
2016-02-10 18:07:22 +00:00
|
|
|
|
if (type == LibraryType.Project)
|
2015-12-21 18:36:20 +00:00
|
|
|
|
{
|
2016-01-11 17:37:24 +00:00
|
|
|
|
var isExe = ((ProjectDescription)export.Library)
|
2015-12-21 18:36:20 +00:00
|
|
|
|
.Project
|
|
|
|
|
.GetCompilerOptions(target, configuration)
|
|
|
|
|
.EmitEntryPoint
|
|
|
|
|
.GetValueOrDefault(false);
|
|
|
|
|
|
2016-01-08 17:02:04 +00:00
|
|
|
|
isExe &= target.IsDesktop();
|
|
|
|
|
|
2016-01-11 17:37:24 +00:00
|
|
|
|
assemblies = new[] { export.Library.Identity.Name + (isExe ? ".exe" : ".dll") };
|
2015-12-21 18:36:20 +00:00
|
|
|
|
}
|
2016-02-10 18:07:22 +00:00
|
|
|
|
else if (type == LibraryType.ReferenceAssembly)
|
|
|
|
|
{
|
|
|
|
|
assemblies = ResolveReferenceAssembliesPath(libraryAssets);
|
|
|
|
|
}
|
2015-12-21 18:36:20 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
assemblies = libraryAssets.Select(libraryAsset => libraryAsset.RelativePath).ToArray();
|
|
|
|
|
}
|
2015-12-17 23:04:18 +00:00
|
|
|
|
|
2016-01-07 23:11:47 +00:00
|
|
|
|
if (runtime)
|
|
|
|
|
{
|
|
|
|
|
return new RuntimeLibrary(
|
2016-02-10 18:07:22 +00:00
|
|
|
|
type.ToString().ToLowerInvariant(),
|
2016-01-07 23:11:47 +00:00
|
|
|
|
export.Library.Identity.Name,
|
|
|
|
|
export.Library.Identity.Version.ToString(),
|
|
|
|
|
export.Library.Hash,
|
|
|
|
|
assemblies,
|
2016-03-01 23:11:52 +00:00
|
|
|
|
new RuntimeTarget[0],
|
2016-01-07 23:11:47 +00:00
|
|
|
|
libraryDependencies.ToArray(),
|
|
|
|
|
serviceable
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new CompilationLibrary(
|
2016-02-10 18:07:22 +00:00
|
|
|
|
type.ToString().ToLowerInvariant(),
|
2016-01-07 23:11:47 +00:00
|
|
|
|
export.Library.Identity.Name,
|
|
|
|
|
export.Library.Identity.Version.ToString(),
|
|
|
|
|
export.Library.Hash,
|
|
|
|
|
assemblies,
|
|
|
|
|
libraryDependencies.ToArray(),
|
|
|
|
|
serviceable
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-12-17 23:04:18 +00:00
|
|
|
|
}
|
2016-02-10 18:07:22 +00:00
|
|
|
|
|
|
|
|
|
private static string[] ResolveReferenceAssembliesPath(IEnumerable<LibraryAsset> libraryAssets)
|
|
|
|
|
{
|
|
|
|
|
var resolvedPaths = new List<string>();
|
|
|
|
|
var referenceAssembliesPath =
|
|
|
|
|
PathUtility.EnsureTrailingSlash(FrameworkReferenceResolver.Default.ReferenceAssembliesPath);
|
|
|
|
|
foreach (var libraryAsset in libraryAssets)
|
|
|
|
|
{
|
|
|
|
|
// If resolved path is under ReferenceAssembliesPath store it as a relative to it
|
|
|
|
|
// if not, save only assembly name and try to find it somehow later
|
|
|
|
|
if (libraryAsset.ResolvedPath.StartsWith(referenceAssembliesPath))
|
|
|
|
|
{
|
|
|
|
|
resolvedPaths.Add(libraryAsset.ResolvedPath.Substring(referenceAssembliesPath.Length));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
resolvedPaths.Add(Path.GetFileName(libraryAsset.ResolvedPath));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return resolvedPaths.ToArray();
|
|
|
|
|
}
|
2015-12-17 23:04:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|