dotnet-installer/src/Microsoft.Extensions.DependencyModel/Resolution/AppBaseCompilationAssemblyResolver.cs

115 lines
4.2 KiB
C#
Raw Normal View History

// 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;
using Microsoft.DotNet.InternalAbstractions;
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";
private readonly IFileSystem _fileSystem;
private readonly string _basePath;
2016-04-29 08:11:59 -07:00
private readonly DependencyContextPaths _dependencyContextPaths;
public AppBaseCompilationAssemblyResolver()
: this(FileSystemWrapper.Default)
{
}
2016-04-29 08:11:59 -07:00
public AppBaseCompilationAssemblyResolver(string basePath)
: this(FileSystemWrapper.Default, basePath, DependencyContextPaths.Current)
{
}
internal AppBaseCompilationAssemblyResolver(IFileSystem fileSystem)
2016-04-29 08:11:59 -07:00
: this(fileSystem, ApplicationEnvironment.ApplicationBasePath, DependencyContextPaths.Current)
{
}
2016-04-29 08:11:59 -07:00
internal AppBaseCompilationAssemblyResolver(IFileSystem fileSystem, string basePath, DependencyContextPaths dependencyContextPaths)
{
_fileSystem = fileSystem;
_basePath = basePath;
2016-04-29 08:11:59 -07:00
_dependencyContextPaths = dependencyContextPaths;
}
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))
{
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;
}
var directories = new List<string>()
{
_basePath
};
2016-04-29 08:11:59 -07:00
if (isPublished)
{
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);
}
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)
{
throw new InvalidOperationException(
$"Can not find assembly file {assemblyFile} at '{string.Join(",", directories)}'");
}
2016-04-29 08:11:59 -07:00
return false;
}
}
return true;
}
}
}