2016-02-10 20:13:56 -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.Collections.Generic;
|
|
|
|
using System.IO;
|
2016-04-28 16:30:32 -07:00
|
|
|
using Microsoft.DotNet.InternalAbstractions;
|
2016-02-10 20:13:56 -08:00
|
|
|
using Microsoft.Extensions.EnvironmentAbstractions;
|
|
|
|
|
|
|
|
namespace Microsoft.Extensions.DependencyModel.Resolution
|
|
|
|
{
|
|
|
|
public class AppBaseCompilationAssemblyResolver : ICompilationAssemblyResolver
|
|
|
|
{
|
2016-04-29 08:11:59 -07:00
|
|
|
private static string RefsDirectoryName = "refs";
|
2016-02-10 20:13:56 -08:00
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly string _basePath;
|
2016-04-29 08:11:59 -07:00
|
|
|
private readonly DependencyContextPaths _dependencyContextPaths;
|
2016-02-10 20:13:56 -08:00
|
|
|
|
|
|
|
public AppBaseCompilationAssemblyResolver()
|
|
|
|
: this(FileSystemWrapper.Default)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-29 08:11:59 -07:00
|
|
|
public AppBaseCompilationAssemblyResolver(string basePath)
|
|
|
|
: this(FileSystemWrapper.Default, basePath, DependencyContextPaths.Current)
|
2016-02-10 20:13:56 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
internal AppBaseCompilationAssemblyResolver(IFileSystem fileSystem)
|
2016-04-29 08:11:59 -07:00
|
|
|
: this(fileSystem, ApplicationEnvironment.ApplicationBasePath, DependencyContextPaths.Current)
|
2016-02-10 20:13:56 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-29 08:11:59 -07:00
|
|
|
internal AppBaseCompilationAssemblyResolver(IFileSystem fileSystem, string basePath, DependencyContextPaths dependencyContextPaths)
|
2016-02-10 20:13:56 -08:00
|
|
|
{
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_basePath = basePath;
|
2016-04-29 08:11:59 -07:00
|
|
|
_dependencyContextPaths = dependencyContextPaths;
|
2016-02-10 20:13:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool TryResolveAssemblyPaths(CompilationLibrary library, List<string> assemblies)
|
|
|
|
{
|
2016-03-04 14:12:16 -08:00
|
|
|
var isProject = string.Equals(library.Type, "project", StringComparison.OrdinalIgnoreCase);
|
2016-04-29 08:11:59 -07:00
|
|
|
var isPackage = string.Equals(library.Type, "package", StringComparison.OrdinalIgnoreCase);
|
2016-02-17 16:39:48 -08:00
|
|
|
if (!isProject &&
|
2016-04-29 08:11:59 -07:00
|
|
|
!isPackage &&
|
2016-03-04 14:12:16 -08:00
|
|
|
!string.Equals(library.Type, "referenceassembly", StringComparison.OrdinalIgnoreCase))
|
2016-02-10 20:13:56 -08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-29 08:11:59 -07:00
|
|
|
var refsPath = Path.Combine(_basePath, RefsDirectoryName);
|
|
|
|
var isPublished = _fileSystem.Directory.Exists(refsPath);
|
2016-02-17 16:39:48 -08:00
|
|
|
|
2016-04-29 08:11:59 -07:00
|
|
|
// Resolving reference assebmlies requires refs folder to exist
|
|
|
|
if (!isProject && !isPackage && !isPublished)
|
2016-02-17 16:39:48 -08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-10 20:13:56 -08:00
|
|
|
var directories = new List<string>()
|
|
|
|
{
|
|
|
|
_basePath
|
|
|
|
};
|
|
|
|
|
2016-04-29 08:11:59 -07:00
|
|
|
if (isPublished)
|
2016-02-10 20:13:56 -08:00
|
|
|
{
|
|
|
|
directories.Insert(0, refsPath);
|
|
|
|
}
|
|
|
|
|
2016-04-29 08:11:59 -07:00
|
|
|
// Only packages can come from shared runtime
|
|
|
|
var sharedPath = _dependencyContextPaths.SharedRuntime;
|
|
|
|
if (isPublished && isPackage && !string.IsNullOrEmpty(sharedPath))
|
|
|
|
{
|
|
|
|
var sharedDirectory = Path.GetDirectoryName(sharedPath);
|
|
|
|
var sharedRefs = Path.Combine(sharedDirectory, RefsDirectoryName);
|
|
|
|
if (_fileSystem.Directory.Exists(sharedRefs))
|
|
|
|
{
|
|
|
|
directories.Add(sharedRefs);
|
|
|
|
}
|
|
|
|
directories.Add(sharedDirectory);
|
|
|
|
}
|
|
|
|
|
2016-02-10 20:13:56 -08:00
|
|
|
foreach (var assembly in library.Assemblies)
|
|
|
|
{
|
|
|
|
bool resolved = false;
|
|
|
|
var assemblyFile = Path.GetFileName(assembly);
|
|
|
|
foreach (var directory in directories)
|
|
|
|
{
|
|
|
|
string fullName;
|
|
|
|
if (ResolverUtils.TryResolveAssemblyFile(_fileSystem, directory, assemblyFile, out fullName))
|
|
|
|
{
|
|
|
|
assemblies.Add(fullName);
|
|
|
|
resolved = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!resolved)
|
|
|
|
{
|
2016-04-29 08:11:59 -07:00
|
|
|
// throw in case when we are published app and nothing found
|
|
|
|
// because we cannot rely on nuget package cache in this case
|
|
|
|
if (isPublished)
|
|
|
|
{
|
2016-02-10 20:13:56 -08:00
|
|
|
throw new InvalidOperationException(
|
|
|
|
$"Can not find assembly file {assemblyFile} at '{string.Join(",", directories)}'");
|
|
|
|
}
|
2016-04-29 08:11:59 -07:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-10 20:13:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|