Moving the ProjectToolsCommandResolver to dotnet out of Cli.Utils because of the dependency on Microsoft.Build. Also added a EndToEnd test for tools ref using the MSBuildTestApp.
This commit is contained in:
parent
12e8e8eca7
commit
1570e0fde4
21 changed files with 186 additions and 29 deletions
315
src/dotnet/CommandResolution/DepsJsonBuilder.cs
Normal file
315
src/dotnet/CommandResolution/DepsJsonBuilder.cs
Normal file
|
@ -0,0 +1,315 @@
|
|||
// 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.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using NuGet.Frameworks;
|
||||
using NuGet.Packaging;
|
||||
using NuGet.Packaging.Core;
|
||||
using NuGet.ProjectModel;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.CommandResolution
|
||||
{
|
||||
internal class DepsJsonBuilder
|
||||
{
|
||||
private readonly VersionFolderPathResolver _versionFolderPathResolver;
|
||||
|
||||
public DepsJsonBuilder()
|
||||
{
|
||||
// This resolver is only used for building file names, so that base path is not required.
|
||||
_versionFolderPathResolver = new VersionFolderPathResolver(path: null);
|
||||
}
|
||||
|
||||
public DependencyContext Build(
|
||||
SingleProjectInfo mainProjectInfo,
|
||||
CompilationOptions compilationOptions,
|
||||
LockFile lockFile,
|
||||
NuGetFramework framework,
|
||||
string runtime)
|
||||
{
|
||||
bool includeCompilationLibraries = compilationOptions != null;
|
||||
|
||||
LockFileTarget lockFileTarget = lockFile.GetTarget(framework, runtime);
|
||||
|
||||
IEnumerable<LockFileTargetLibrary> runtimeExports = lockFileTarget.GetRuntimeLibraries();
|
||||
IEnumerable<LockFileTargetLibrary> compilationExports =
|
||||
includeCompilationLibraries ?
|
||||
lockFileTarget.GetCompileLibraries() :
|
||||
Enumerable.Empty<LockFileTargetLibrary>();
|
||||
|
||||
var dependencyLookup = compilationExports
|
||||
.Concat(runtimeExports)
|
||||
.Distinct()
|
||||
.Select(library => new Dependency(library.Name, library.Version.ToString()))
|
||||
.ToDictionary(dependency => dependency.Name, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var libraryLookup = lockFile.Libraries.ToDictionary(l => l.Name, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var runtimeSignature = GenerateRuntimeSignature(runtimeExports);
|
||||
|
||||
IEnumerable<RuntimeLibrary> runtimeLibraries =
|
||||
GetLibraries(runtimeExports, libraryLookup, dependencyLookup, runtime: true).Cast<RuntimeLibrary>();
|
||||
|
||||
IEnumerable<CompilationLibrary> compilationLibraries;
|
||||
if (includeCompilationLibraries)
|
||||
{
|
||||
CompilationLibrary projectCompilationLibrary = GetProjectCompilationLibrary(
|
||||
mainProjectInfo,
|
||||
lockFile,
|
||||
lockFileTarget,
|
||||
dependencyLookup);
|
||||
compilationLibraries = new[] { projectCompilationLibrary }
|
||||
.Concat(
|
||||
GetLibraries(compilationExports, libraryLookup, dependencyLookup, runtime: false)
|
||||
.Cast<CompilationLibrary>());
|
||||
}
|
||||
else
|
||||
{
|
||||
compilationLibraries = Enumerable.Empty<CompilationLibrary>();
|
||||
}
|
||||
|
||||
return new DependencyContext(
|
||||
new TargetInfo(framework.DotNetFrameworkName, runtime, runtimeSignature, lockFileTarget.IsPortable()),
|
||||
compilationOptions ?? CompilationOptions.Default,
|
||||
compilationLibraries,
|
||||
runtimeLibraries,
|
||||
new RuntimeFallbacks[] { });
|
||||
}
|
||||
|
||||
private static string GenerateRuntimeSignature(IEnumerable<LockFileTargetLibrary> runtimeExports)
|
||||
{
|
||||
var sha1 = SHA1.Create();
|
||||
var builder = new StringBuilder();
|
||||
var packages = runtimeExports
|
||||
.Where(libraryExport => libraryExport.Type == "package");
|
||||
var separator = "|";
|
||||
foreach (var libraryExport in packages)
|
||||
{
|
||||
builder.Append(libraryExport.Name);
|
||||
builder.Append(separator);
|
||||
builder.Append(libraryExport.Version.ToString());
|
||||
builder.Append(separator);
|
||||
}
|
||||
var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString()));
|
||||
|
||||
builder.Clear();
|
||||
foreach (var hashByte in hash)
|
||||
{
|
||||
builder.AppendFormat("{0:x2}", hashByte);
|
||||
}
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
private List<Dependency> GetProjectDependencies(
|
||||
LockFile lockFile,
|
||||
LockFileTarget lockFileTarget,
|
||||
Dictionary<string, Dependency> dependencyLookup)
|
||||
{
|
||||
|
||||
List<Dependency> dependencies = new List<Dependency>();
|
||||
|
||||
IEnumerable<ProjectFileDependencyGroup> projectFileDependencies = lockFile
|
||||
.ProjectFileDependencyGroups
|
||||
.Where(dg => dg.FrameworkName == string.Empty ||
|
||||
dg.FrameworkName == lockFileTarget.TargetFramework.DotNetFrameworkName);
|
||||
|
||||
foreach (string projectFileDependency in projectFileDependencies.SelectMany(dg => dg.Dependencies))
|
||||
{
|
||||
int separatorIndex = projectFileDependency.IndexOf(' ');
|
||||
string dependencyName = separatorIndex > 0 ?
|
||||
projectFileDependency.Substring(0, separatorIndex) :
|
||||
projectFileDependency;
|
||||
|
||||
Dependency dependency;
|
||||
if (dependencyLookup.TryGetValue(dependencyName, out dependency))
|
||||
{
|
||||
dependencies.Add(dependency);
|
||||
}
|
||||
}
|
||||
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
private RuntimeLibrary GetProjectRuntimeLibrary(
|
||||
SingleProjectInfo projectInfo,
|
||||
LockFile lockFile,
|
||||
LockFileTarget lockFileTarget,
|
||||
Dictionary<string, Dependency> dependencyLookup)
|
||||
{
|
||||
|
||||
RuntimeAssetGroup[] runtimeAssemblyGroups = new[] { new RuntimeAssetGroup(string.Empty, projectInfo.GetOutputName()) };
|
||||
|
||||
List<Dependency> dependencies = GetProjectDependencies(lockFile, lockFileTarget, dependencyLookup);
|
||||
|
||||
ResourceAssembly[] resourceAssemblies = projectInfo
|
||||
.ResourceAssemblies
|
||||
.Select(r => new ResourceAssembly(r.RelativePath, r.Culture))
|
||||
.ToArray();
|
||||
|
||||
return new RuntimeLibrary(
|
||||
type: "project",
|
||||
name: projectInfo.Name,
|
||||
version: projectInfo.Version,
|
||||
hash: string.Empty,
|
||||
runtimeAssemblyGroups: runtimeAssemblyGroups,
|
||||
nativeLibraryGroups: new RuntimeAssetGroup[] { },
|
||||
resourceAssemblies: resourceAssemblies,
|
||||
dependencies: dependencies.ToArray(),
|
||||
serviceable: false);
|
||||
}
|
||||
|
||||
private CompilationLibrary GetProjectCompilationLibrary(
|
||||
SingleProjectInfo projectInfo,
|
||||
LockFile lockFile,
|
||||
LockFileTarget lockFileTarget,
|
||||
Dictionary<string, Dependency> dependencyLookup)
|
||||
{
|
||||
List<Dependency> dependencies = GetProjectDependencies(lockFile, lockFileTarget, dependencyLookup);
|
||||
|
||||
return new CompilationLibrary(
|
||||
type: "project",
|
||||
name: projectInfo.Name,
|
||||
version: projectInfo.Version,
|
||||
hash: string.Empty,
|
||||
assemblies: new[] { projectInfo.GetOutputName() },
|
||||
dependencies: dependencies.ToArray(),
|
||||
serviceable: false);
|
||||
}
|
||||
|
||||
private IEnumerable<Library> GetLibraries(
|
||||
IEnumerable<LockFileTargetLibrary> exports,
|
||||
IDictionary<string, LockFileLibrary> libraryLookup,
|
||||
IDictionary<string, Dependency> dependencyLookup,
|
||||
bool runtime)
|
||||
{
|
||||
return exports.Select(export => GetLibrary(export, libraryLookup, dependencyLookup, runtime));
|
||||
}
|
||||
|
||||
private Library GetLibrary(
|
||||
LockFileTargetLibrary export,
|
||||
IDictionary<string, LockFileLibrary> libraryLookup,
|
||||
IDictionary<string, Dependency> dependencyLookup,
|
||||
bool runtime)
|
||||
{
|
||||
var type = export.Type;
|
||||
|
||||
// TEMPORARY: All packages are serviceable in RC2
|
||||
// See https://github.com/dotnet/cli/issues/2569
|
||||
var serviceable = export.Type == "package";
|
||||
var libraryDependencies = new HashSet<Dependency>();
|
||||
|
||||
foreach (PackageDependency libraryDependency in export.Dependencies)
|
||||
{
|
||||
Dependency dependency;
|
||||
if (dependencyLookup.TryGetValue(libraryDependency.Id, out dependency))
|
||||
{
|
||||
libraryDependencies.Add(dependency);
|
||||
}
|
||||
}
|
||||
|
||||
string hash = string.Empty;
|
||||
string path = null;
|
||||
string hashPath = null;
|
||||
LockFileLibrary library;
|
||||
if (libraryLookup.TryGetValue(export.Name, out library))
|
||||
{
|
||||
if (!string.IsNullOrEmpty(library.Sha512))
|
||||
{
|
||||
hash = "sha512-" + library.Sha512;
|
||||
hashPath = _versionFolderPathResolver.GetHashFileName(export.Name, export.Version);
|
||||
}
|
||||
|
||||
path = library.Path;
|
||||
}
|
||||
|
||||
if (runtime)
|
||||
{
|
||||
return new RuntimeLibrary(
|
||||
type.ToLowerInvariant(),
|
||||
export.Name,
|
||||
export.Version.ToString(),
|
||||
hash,
|
||||
CreateRuntimeAssemblyGroups(export),
|
||||
CreateNativeLibraryGroups(export),
|
||||
export.ResourceAssemblies.FilterPlaceHolderFiles().Select(CreateResourceAssembly),
|
||||
libraryDependencies,
|
||||
serviceable,
|
||||
path,
|
||||
hashPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
IEnumerable<string> assemblies = export
|
||||
.CompileTimeAssemblies
|
||||
.FilterPlaceHolderFiles()
|
||||
.Select(libraryAsset => libraryAsset.Path);
|
||||
|
||||
return new CompilationLibrary(
|
||||
type.ToString().ToLowerInvariant(),
|
||||
export.Name,
|
||||
export.Version.ToString(),
|
||||
hash,
|
||||
assemblies,
|
||||
libraryDependencies,
|
||||
serviceable,
|
||||
path,
|
||||
hashPath);
|
||||
}
|
||||
}
|
||||
|
||||
private IReadOnlyList<RuntimeAssetGroup> CreateRuntimeAssemblyGroups(LockFileTargetLibrary export)
|
||||
{
|
||||
List<RuntimeAssetGroup> assemblyGroups = new List<RuntimeAssetGroup>();
|
||||
|
||||
assemblyGroups.Add(
|
||||
new RuntimeAssetGroup(
|
||||
string.Empty,
|
||||
export.RuntimeAssemblies.FilterPlaceHolderFiles().Select(a => a.Path)));
|
||||
|
||||
foreach (var runtimeTargetsGroup in export.GetRuntimeTargetsGroups("runtime"))
|
||||
{
|
||||
assemblyGroups.Add(
|
||||
new RuntimeAssetGroup(
|
||||
runtimeTargetsGroup.Key,
|
||||
runtimeTargetsGroup.Select(t => t.Path)));
|
||||
}
|
||||
|
||||
return assemblyGroups;
|
||||
}
|
||||
|
||||
private IReadOnlyList<RuntimeAssetGroup> CreateNativeLibraryGroups(LockFileTargetLibrary export)
|
||||
{
|
||||
List<RuntimeAssetGroup> nativeGroups = new List<RuntimeAssetGroup>();
|
||||
|
||||
nativeGroups.Add(
|
||||
new RuntimeAssetGroup(
|
||||
string.Empty,
|
||||
export.NativeLibraries.FilterPlaceHolderFiles().Select(a => a.Path)));
|
||||
|
||||
foreach (var runtimeTargetsGroup in export.GetRuntimeTargetsGroups("native"))
|
||||
{
|
||||
nativeGroups.Add(
|
||||
new RuntimeAssetGroup(
|
||||
runtimeTargetsGroup.Key,
|
||||
runtimeTargetsGroup.Select(t => t.Path)));
|
||||
}
|
||||
|
||||
return nativeGroups;
|
||||
}
|
||||
|
||||
private ResourceAssembly CreateResourceAssembly(LockFileItem resourceAssembly)
|
||||
{
|
||||
string locale;
|
||||
if (!resourceAssembly.Properties.TryGetValue("locale", out locale))
|
||||
{
|
||||
locale = null;
|
||||
}
|
||||
|
||||
return new ResourceAssembly(resourceAssembly.Path, locale);
|
||||
}
|
||||
}
|
||||
}
|
63
src/dotnet/CommandResolution/LockFilePathCalculator.cs
Normal file
63
src/dotnet/CommandResolution/LockFilePathCalculator.cs
Normal file
|
@ -0,0 +1,63 @@
|
|||
// 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 System.Linq;
|
||||
using Microsoft.Build.Evaluation;
|
||||
using NuGet.ProjectModel;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.CommandResolution
|
||||
{
|
||||
internal class LockFilePathCalculator
|
||||
{
|
||||
public string GetLockFilePath(string projectDirectory)
|
||||
{
|
||||
return ResolveLockFilePathUsingCSProj(projectDirectory) ??
|
||||
ReturnProjectLockJson(projectDirectory);
|
||||
}
|
||||
|
||||
private string ResolveLockFilePathUsingCSProj(string projectDirectory)
|
||||
{
|
||||
string csProjPath = GetCSProjPath(projectDirectory);
|
||||
if(csProjPath == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var globalProperties = new Dictionary<string, string>()
|
||||
{
|
||||
{ "MSBuildExtensionsPath", AppContext.BaseDirectory }
|
||||
};
|
||||
|
||||
Project project = new Project(csProjPath, globalProperties, null);
|
||||
// TODO: This is temporary. We should use ProjectLockFile property, but for some reason, it is coming up as project.lock.json
|
||||
// instead of the path to project.assets.json.
|
||||
var lockFilePath = project.AllEvaluatedProperties.FirstOrDefault(p => p.Name.Equals("BaseIntermediateOutputPath")).EvaluatedValue;
|
||||
return Path.Combine(lockFilePath, "project.assets.json");
|
||||
}
|
||||
|
||||
private string ReturnProjectLockJson(string projectDirectory)
|
||||
{
|
||||
return Path.Combine(projectDirectory, LockFileFormat.LockFileName);
|
||||
}
|
||||
|
||||
private string GetCSProjPath(string projectDirectory)
|
||||
{
|
||||
string[] projectFiles = Directory.GetFiles(projectDirectory, "*.*proj");
|
||||
|
||||
if (projectFiles.Length == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
else if (projectFiles.Length > 1)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Specify which project file to use because this '{projectDirectory}' contains more than one project file.");
|
||||
}
|
||||
|
||||
return projectFiles[0];
|
||||
}
|
||||
}
|
||||
}
|
105
src/dotnet/CommandResolution/LockFileTargetExtensions.cs
Normal file
105
src/dotnet/CommandResolution/LockFileTargetExtensions.cs
Normal file
|
@ -0,0 +1,105 @@
|
|||
// 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.Linq;
|
||||
using NuGet.Packaging.Core;
|
||||
using NuGet.ProjectModel;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.CommandResolution
|
||||
{
|
||||
internal static class LockFileTargetExtensions
|
||||
{
|
||||
public static bool IsPortable(this LockFileTarget lockFileTarget)
|
||||
{
|
||||
return string.IsNullOrEmpty(lockFileTarget.RuntimeIdentifier) &&
|
||||
lockFileTarget.GetPlatformLibrary() != null;
|
||||
}
|
||||
|
||||
public static LockFileTargetLibrary GetPlatformLibrary(this LockFileTarget lockFileTarget)
|
||||
{
|
||||
// TODO: https://github.com/dotnet/sdk/issues/17 get this from the lock file
|
||||
var platformPackageName = "Microsoft.NETCore.App";
|
||||
var platformLibrary = lockFileTarget
|
||||
.Libraries
|
||||
.FirstOrDefault(e => e.Name.Equals(platformPackageName, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
return platformLibrary;
|
||||
}
|
||||
|
||||
public static HashSet<string> GetPlatformExclusionList(
|
||||
this LockFileTarget lockFileTarget,
|
||||
IDictionary<string, LockFileTargetLibrary> libraryLookup)
|
||||
{
|
||||
var platformLibrary = lockFileTarget.GetPlatformLibrary();
|
||||
var exclusionList = new HashSet<string>();
|
||||
|
||||
exclusionList.Add(platformLibrary.Name);
|
||||
CollectDependencies(libraryLookup, platformLibrary.Dependencies, exclusionList);
|
||||
|
||||
return exclusionList;
|
||||
}
|
||||
|
||||
public static IEnumerable<LockFileTargetLibrary> GetRuntimeLibraries(this LockFileTarget lockFileTarget)
|
||||
{
|
||||
IEnumerable<LockFileTargetLibrary> runtimeLibraries = lockFileTarget.Libraries;
|
||||
Dictionary<string, LockFileTargetLibrary> libraryLookup =
|
||||
runtimeLibraries.ToDictionary(e => e.Name, StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
HashSet<string> allExclusionList = new HashSet<string>();
|
||||
|
||||
if (lockFileTarget.IsPortable())
|
||||
{
|
||||
allExclusionList.UnionWith(lockFileTarget.GetPlatformExclusionList(libraryLookup));
|
||||
}
|
||||
|
||||
// TODO: exclude "type: build" dependencies during publish - https://github.com/dotnet/sdk/issues/42
|
||||
|
||||
return runtimeLibraries.Filter(allExclusionList).ToArray();
|
||||
}
|
||||
|
||||
public static IEnumerable<LockFileTargetLibrary> GetCompileLibraries(this LockFileTarget lockFileTarget)
|
||||
{
|
||||
// TODO: exclude "type: build" dependencies during publish - https://github.com/dotnet/sdk/issues/42
|
||||
|
||||
return lockFileTarget.Libraries;
|
||||
}
|
||||
|
||||
public static IEnumerable<LockFileTargetLibrary> Filter(
|
||||
this IEnumerable<LockFileTargetLibrary> libraries,
|
||||
HashSet<string> exclusionList)
|
||||
{
|
||||
return libraries.Where(e => !exclusionList.Contains(e.Name));
|
||||
}
|
||||
|
||||
public static IEnumerable<IGrouping<string, LockFileRuntimeTarget>> GetRuntimeTargetsGroups(
|
||||
this LockFileTargetLibrary library,
|
||||
string assetType)
|
||||
{
|
||||
return library.RuntimeTargets
|
||||
.FilterPlaceHolderFiles()
|
||||
.Cast<LockFileRuntimeTarget>()
|
||||
.Where(t => string.Equals(t.AssetType, assetType, StringComparison.OrdinalIgnoreCase))
|
||||
.GroupBy(t => t.Runtime);
|
||||
}
|
||||
|
||||
private static void CollectDependencies(
|
||||
IDictionary<string, LockFileTargetLibrary> libraryLookup,
|
||||
IEnumerable<PackageDependency> dependencies,
|
||||
HashSet<string> exclusionList)
|
||||
{
|
||||
foreach (PackageDependency dependency in dependencies)
|
||||
{
|
||||
LockFileTargetLibrary library = libraryLookup[dependency.Id];
|
||||
if (library.Version.Equals(dependency.VersionRange.MinVersion))
|
||||
{
|
||||
if (exclusionList.Add(library.Name))
|
||||
{
|
||||
CollectDependencies(libraryLookup, library.Dependencies, exclusionList);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
25
src/dotnet/CommandResolution/NuGetUtils.cs
Normal file
25
src/dotnet/CommandResolution/NuGetUtils.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
// 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 System.Linq;
|
||||
using NuGet.Packaging.Core;
|
||||
using NuGet.ProjectModel;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.CommandResolution
|
||||
{
|
||||
internal static class NuGetUtils
|
||||
{
|
||||
public static bool IsPlaceholderFile(string path)
|
||||
{
|
||||
return string.Equals(Path.GetFileName(path), PackagingCoreConstants.EmptyFolder, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
public static IEnumerable<LockFileItem> FilterPlaceHolderFiles(this IEnumerable<LockFileItem> files)
|
||||
{
|
||||
return files.Where(f => !IsPlaceholderFile(f.Path));
|
||||
}
|
||||
}
|
||||
}
|
246
src/dotnet/CommandResolution/ProjectToolsCommandResolver.cs
Normal file
246
src/dotnet/CommandResolution/ProjectToolsCommandResolver.cs
Normal file
|
@ -0,0 +1,246 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.InternalAbstractions;
|
||||
using Microsoft.DotNet.ProjectModel;
|
||||
using Microsoft.DotNet.Tools.Common;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using NuGet.Frameworks;
|
||||
using NuGet.ProjectModel;
|
||||
using NuGet.Versioning;
|
||||
using FileFormatException = Microsoft.DotNet.ProjectModel.FileFormatException;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.CommandResolution
|
||||
{
|
||||
public class ProjectToolsCommandResolver : ICommandResolver
|
||||
{
|
||||
private static readonly NuGetFramework s_toolPackageFramework = FrameworkConstants.CommonFrameworks.NetCoreApp10;
|
||||
|
||||
private static readonly CommandResolutionStrategy s_commandResolutionStrategy =
|
||||
CommandResolutionStrategy.ProjectToolsPackage;
|
||||
|
||||
private List<string> _allowedCommandExtensions;
|
||||
private IPackagedCommandSpecFactory _packagedCommandSpecFactory;
|
||||
|
||||
public ProjectToolsCommandResolver(IPackagedCommandSpecFactory packagedCommandSpecFactory)
|
||||
{
|
||||
_packagedCommandSpecFactory = packagedCommandSpecFactory;
|
||||
|
||||
_allowedCommandExtensions = new List<string>()
|
||||
{
|
||||
FileNameSuffixes.DotNet.DynamicLib
|
||||
};
|
||||
}
|
||||
|
||||
public CommandSpec Resolve(CommandResolverArguments commandResolverArguments)
|
||||
{
|
||||
if (commandResolverArguments.CommandName == null
|
||||
|| commandResolverArguments.ProjectDirectory == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return ResolveFromProjectTools(
|
||||
commandResolverArguments.CommandName,
|
||||
commandResolverArguments.CommandArguments.OrEmptyIfNull(),
|
||||
commandResolverArguments.ProjectDirectory);
|
||||
}
|
||||
|
||||
private CommandSpec ResolveFromProjectTools(
|
||||
string commandName,
|
||||
IEnumerable<string> args,
|
||||
string projectDirectory)
|
||||
{
|
||||
var lockFilePathCalculator = new LockFilePathCalculator();
|
||||
var lockFile = new LockFileFormat().Read(lockFilePathCalculator.GetLockFilePath(projectDirectory));
|
||||
var tools = lockFile.Tools.Where(t => t.Name.Contains(".NETCoreApp")).SelectMany(t => t.Libraries);
|
||||
|
||||
return ResolveCommandSpecFromAllToolLibraries(
|
||||
tools,
|
||||
commandName,
|
||||
args,
|
||||
lockFile);
|
||||
}
|
||||
|
||||
private CommandSpec ResolveCommandSpecFromAllToolLibraries(
|
||||
IEnumerable<LockFileTargetLibrary> toolsLibraries,
|
||||
string commandName,
|
||||
IEnumerable<string> args,
|
||||
LockFile lockFile)
|
||||
{
|
||||
foreach (var toolLibrary in toolsLibraries)
|
||||
{
|
||||
var commandSpec = ResolveCommandSpecFromToolLibrary(toolLibrary, commandName, args, lockFile);
|
||||
|
||||
if (commandSpec != null)
|
||||
{
|
||||
return commandSpec;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private CommandSpec ResolveCommandSpecFromToolLibrary(
|
||||
LockFileTargetLibrary toolLibraryRange,
|
||||
string commandName,
|
||||
IEnumerable<string> args,
|
||||
LockFile lockFile)
|
||||
{
|
||||
var nugetPackagesRoot = lockFile.PackageFolders.First().Path;
|
||||
|
||||
var toolLockFile = GetToolLockFile(toolLibraryRange, nugetPackagesRoot);
|
||||
|
||||
var toolLibrary = toolLockFile.Targets
|
||||
.FirstOrDefault(
|
||||
t => t.TargetFramework.GetShortFolderName().Equals(s_toolPackageFramework.GetShortFolderName()))
|
||||
?.Libraries.FirstOrDefault(l => l.Name == toolLibraryRange.Name);
|
||||
|
||||
if (toolLibrary == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var depsFileRoot = Path.GetDirectoryName(toolLockFile.Path);
|
||||
var depsFilePath = GetToolDepsFilePath(toolLibraryRange, toolLockFile, depsFileRoot);
|
||||
|
||||
var normalizedNugetPackagesRoot = PathUtility.EnsureNoTrailingDirectorySeparator(nugetPackagesRoot);
|
||||
|
||||
return _packagedCommandSpecFactory.CreateCommandSpecFromLibrary(
|
||||
toolLibrary,
|
||||
commandName,
|
||||
args,
|
||||
_allowedCommandExtensions,
|
||||
normalizedNugetPackagesRoot,
|
||||
s_commandResolutionStrategy,
|
||||
depsFilePath,
|
||||
null);
|
||||
}
|
||||
|
||||
private LockFile GetToolLockFile(
|
||||
LockFileTargetLibrary toolLibrary,
|
||||
string nugetPackagesRoot)
|
||||
{
|
||||
var lockFilePath = GetToolLockFilePath(toolLibrary, nugetPackagesRoot);
|
||||
|
||||
if (!File.Exists(lockFilePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
LockFile lockFile = null;
|
||||
|
||||
try
|
||||
{
|
||||
lockFile = new LockFileFormat().Read(lockFilePath);
|
||||
}
|
||||
catch (FileFormatException ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
|
||||
return lockFile;
|
||||
}
|
||||
|
||||
private string GetToolLockFilePath(
|
||||
LockFileTargetLibrary toolLibrary,
|
||||
string nugetPackagesRoot)
|
||||
{
|
||||
var toolPathCalculator = new ToolPathCalculator(nugetPackagesRoot);
|
||||
|
||||
return toolPathCalculator.GetBestLockFilePath(
|
||||
toolLibrary.Name,
|
||||
new VersionRange(toolLibrary.Version),
|
||||
s_toolPackageFramework);
|
||||
}
|
||||
|
||||
private ProjectContext GetProjectContextFromDirectoryForFirstTarget(string projectRootPath)
|
||||
{
|
||||
if (projectRootPath == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!File.Exists(Path.Combine(projectRootPath, Project.FileName)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var projectContext = ProjectContext.CreateContextForEachTarget(projectRootPath).FirstOrDefault();
|
||||
|
||||
return projectContext;
|
||||
}
|
||||
|
||||
private string GetToolDepsFilePath(
|
||||
LockFileTargetLibrary toolLibrary,
|
||||
LockFile toolLockFile,
|
||||
string depsPathRoot)
|
||||
{
|
||||
var depsJsonPath = Path.Combine(
|
||||
depsPathRoot,
|
||||
toolLibrary.Name + FileNameSuffixes.DepsJson);
|
||||
|
||||
EnsureToolJsonDepsFileExists(toolLockFile, depsJsonPath, toolLibrary);
|
||||
|
||||
return depsJsonPath;
|
||||
}
|
||||
|
||||
private void EnsureToolJsonDepsFileExists(
|
||||
LockFile toolLockFile,
|
||||
string depsPath,
|
||||
LockFileTargetLibrary toolLibrary)
|
||||
{
|
||||
if (!File.Exists(depsPath))
|
||||
{
|
||||
GenerateDepsJsonFile(toolLockFile, depsPath, toolLibrary);
|
||||
}
|
||||
}
|
||||
|
||||
// Need to unit test this, so public
|
||||
public void GenerateDepsJsonFile(
|
||||
LockFile toolLockFile,
|
||||
string depsPath,
|
||||
LockFileTargetLibrary toolLibrary)
|
||||
{
|
||||
Reporter.Verbose.WriteLine($"Generating deps.json at: {depsPath}");
|
||||
|
||||
var singleProjectInfo = new SingleProjectInfo(
|
||||
toolLibrary.Name,
|
||||
toolLibrary.Version.ToFullString(),
|
||||
Enumerable.Empty<ResourceAssemblyInfo>());
|
||||
|
||||
var dependencyContext = new DepsJsonBuilder()
|
||||
.Build(singleProjectInfo, null, toolLockFile, s_toolPackageFramework, null);
|
||||
|
||||
var tempDepsFile = Path.GetTempFileName();
|
||||
using (var fileStream = File.Open(tempDepsFile, FileMode.Open, FileAccess.Write))
|
||||
{
|
||||
var dependencyContextWriter = new DependencyContextWriter();
|
||||
|
||||
dependencyContextWriter.Write(dependencyContext, fileStream);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
File.Copy(tempDepsFile, depsPath);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Reporter.Verbose.WriteLine($"unable to generate deps.json, it may have been already generated: {e.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(tempDepsFile);
|
||||
}
|
||||
catch (Exception e2)
|
||||
{
|
||||
Reporter.Verbose.WriteLine($"unable to delete temporary deps.json file: {e2.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
using Microsoft.DotNet.Cli.Utils;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.CommandResolution
|
||||
{
|
||||
public class ProjectToolsCommandResolverPolicy : ICommandResolverPolicy
|
||||
{
|
||||
public CompositeCommandResolver CreateCommandResolver()
|
||||
{
|
||||
var defaultCommandResolverPolicy = new DefaultCommandResolverPolicy();
|
||||
var compositeCommandResolver = defaultCommandResolverPolicy.CreateCommandResolver();
|
||||
var packagedCommandSpecFactory = new PackagedCommandSpecFactory();
|
||||
|
||||
compositeCommandResolver.AddCommandResolver(new ProjectToolsCommandResolver(packagedCommandSpecFactory));
|
||||
|
||||
return compositeCommandResolver;
|
||||
}
|
||||
}
|
||||
}
|
17
src/dotnet/CommandResolution/ResourceAssemblyInfo.cs
Normal file
17
src/dotnet/CommandResolution/ResourceAssemblyInfo.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
// 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.
|
||||
|
||||
namespace Microsoft.DotNet.Cli.CommandResolution
|
||||
{
|
||||
internal class ResourceAssemblyInfo
|
||||
{
|
||||
public string Culture { get; }
|
||||
public string RelativePath { get; }
|
||||
|
||||
public ResourceAssemblyInfo(string culture, string relativePath)
|
||||
{
|
||||
Culture = culture;
|
||||
RelativePath = relativePath;
|
||||
}
|
||||
}
|
||||
}
|
27
src/dotnet/CommandResolution/SingleProjectInfo.cs
Normal file
27
src/dotnet/CommandResolution/SingleProjectInfo.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
// 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.Collections.Generic;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.CommandResolution
|
||||
{
|
||||
internal class SingleProjectInfo
|
||||
{
|
||||
public string Name { get; }
|
||||
public string Version { get; }
|
||||
|
||||
public IEnumerable<ResourceAssemblyInfo> ResourceAssemblies { get; }
|
||||
|
||||
public SingleProjectInfo(string name, string version, IEnumerable<ResourceAssemblyInfo> resourceAssemblies)
|
||||
{
|
||||
Name = name;
|
||||
Version = version;
|
||||
ResourceAssemblies = resourceAssemblies;
|
||||
}
|
||||
|
||||
public string GetOutputName()
|
||||
{
|
||||
return $"{Name}.dll";
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue