Merge branch 'rel/1.0.0' of https://github.com/dotnet/cli into build-refactor1

Conflicts:
	scripts/Microsoft.DotNet.Cli.Build.Framework/CurrentPlatform.cs
	scripts/Microsoft.DotNet.Cli.Build.Framework/Enumerations/BuildPlatform.cs
This commit is contained in:
Sridhar Periyasamy 2016-03-08 23:42:15 +00:00
commit 3edcab8eab
112 changed files with 3673 additions and 723 deletions

View file

@ -15,6 +15,6 @@
},
"tools": {
"dotnet-hello": { "version": "1.0.0", "target": "package" }
"dotnet-hello": { "version": "2.0.0", "target": "package" }
}
}

View file

@ -0,0 +1,3 @@
public static class Thingy
{
}

View file

@ -0,0 +1,15 @@
{
"dependencies": {
},
"frameworks": {
"netstandardapp1.5": {
"imports": [
"dnxcore50",
"portable-net45+win8"
],
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23901"
}
}
}
}

View file

@ -0,0 +1,3 @@
public static class Thingy
{
}

View file

@ -0,0 +1,20 @@
{
"dependencies": { },
"frameworks": {
"netstandardapp1.5": {
"imports": [
"dnxcore50",
"portable-net45+win8"
],
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23901"
}
}
},
"runtimes": {
"win7-x64": {},
"osx.10.10-x64": {},
"ubuntu.14.04-x64": {},
"centos.7-x64": {}
}
}

View file

@ -48,6 +48,15 @@ namespace Microsoft.DotNet.Cli.Build.Framework
}
}
public static bool IsRHEL
{
get
{
var osname = PlatformServices.Default.Runtime.OperatingSystem;
return string.Equals(osname, "rhel", StringComparison.OrdinalIgnoreCase);
}
}
public static bool IsUnix
{
get
@ -60,7 +69,7 @@ namespace Microsoft.DotNet.Cli.Build.Framework
{
get
{
return IsUbuntu || IsCentOS;
return IsUbuntu || IsCentOS || IsRHEL;
}
}
@ -76,6 +85,8 @@ namespace Microsoft.DotNet.Cli.Build.Framework
return IsOSX;
case BuildPlatform.CentOS:
return IsCentOS;
case BuildPlatform.RHEL:
return IsRHEL;
case BuildPlatform.Unix:
return IsUnix;
case BuildPlatform.Linux:
@ -103,6 +114,10 @@ namespace Microsoft.DotNet.Cli.Build.Framework
{
return BuildPlatform.CentOS;
}
else if (IsRHEL)
{
return BuildPlatform.RHEL;
}
else
{
return default(BuildPlatform);

View file

@ -7,6 +7,7 @@ namespace Microsoft.DotNet.Cli.Build.Framework
Linux = 3,
OSX = 4,
Ubuntu = 5,
CentOS = 6
CentOS = 6,
RHEL = 7
}
}

View file

@ -6,11 +6,16 @@
$oldPath = $env:PATH
try {
# Put the stage2 output on the front of the path
$stage2 = "$PSScriptRoot\..\artifacts\win7-x64\stage2\bin"
if(!(Get-Command dotnet -ErrorAction SilentlyContinue)) {
throw "You need to have a version of 'dotnet' on your path so we can determine the RID"
}
$rid = dotnet --version | where { $_ -match "^ Runtime Id:\s*(.*)$" } | foreach { $matches[1] }
$stage2 = "$PSScriptRoot\..\artifacts\$rid\stage2\bin"
if (Test-Path $stage2) {
$env:PATH="$stage2;$env:PATH"
} else {
Write-Host "You don't have a dev build in the 'artifacts\win7-x64\stage2' folder!"
Write-Host "You don't have a dev build in the 'artifacts\$rid\stage2' folder!"
}
dotnet @args

View file

@ -45,6 +45,13 @@ namespace Microsoft.DotNet.Cli.Build
"Microsoft.Extensions.Testing.Abstractions"
};
// Updates the stage 2 with recent changes.
[Target(nameof(PrepareTargets.Init), nameof(CompileStage2))]
public static BuildTargetResult UpdateBuild(BuildTargetContext c)
{
return c.Success();
}
[Target(nameof(PrepareTargets.Init), nameof(CompileCoreHost), nameof(CompileStage1), nameof(CompileStage2))]
public static BuildTargetResult Compile(BuildTargetContext c)
{
@ -238,9 +245,9 @@ namespace Microsoft.DotNet.Cli.Build
// Find toolchain package
string packageId;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (CurrentPlatform.IsWindows)
{
if (IsWinx86)
if (CurrentArchitecture.Isx86)
{
// https://github.com/dotnet/cli/issues/1550
c.Warn("Native compilation is not yet working on Windows x86");
@ -249,24 +256,16 @@ namespace Microsoft.DotNet.Cli.Build
packageId = "toolchain.win7-x64.Microsoft.DotNet.AppDep";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var osname = PlatformServices.Default.Runtime.OperatingSystem;
if (string.Equals(osname, "ubuntu", StringComparison.OrdinalIgnoreCase))
else if (CurrentPlatform.IsUbuntu)
{
packageId = "toolchain.ubuntu.14.04-x64.Microsoft.DotNet.AppDep";
}
else if (string.Equals(osname, "centos", StringComparison.OrdinalIgnoreCase))
else if (CurrentPlatform.IsCentOS || CurrentPlatform.IsRHEL)
{
c.Warn("Native compilation is not yet working on CentOS");
c.Warn($"Native compilation is not yet working on {CurrentPlatform.Current}");
return c.Success();
}
else
{
return c.Failed($"Unknown Linux Distro: {osname}");
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
else if (CurrentPlatform.IsOSX)
{
packageId = "toolchain.osx.10.10-x64.Microsoft.DotNet.AppDep";
}
@ -296,28 +295,20 @@ namespace Microsoft.DotNet.Cli.Build
// Find crossgen
string arch = PlatformServices.Default.Runtime.RuntimeArchitecture;
string packageId;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (CurrentPlatform.IsWindows)
{
packageId = $"runtime.win7-{arch}.Microsoft.NETCore.Runtime.CoreCLR";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var osname = PlatformServices.Default.Runtime.OperatingSystem;
if (string.Equals(osname, "ubuntu", StringComparison.OrdinalIgnoreCase))
else if (CurrentPlatform.IsUbuntu)
{
packageId = "runtime.ubuntu.14.04-x64.Microsoft.NETCore.Runtime.CoreCLR";
}
else if (string.Equals(osname, "centos", StringComparison.OrdinalIgnoreCase))
else if (CurrentPlatform.IsCentOS || CurrentPlatform.IsRHEL)
{
// CentOS runtime is in the runtime.rhel.7-x64... package.
packageId = "runtime.rhel.7-x64.Microsoft.NETCore.Runtime.CoreCLR";
}
else
{
return c.Failed($"Unknown Linux Distro: {osname}");
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
else if (CurrentPlatform.IsOSX)
{
packageId = "runtime.osx.10.10-x64.Microsoft.NETCore.Runtime.CoreCLR";
}

View file

@ -83,6 +83,8 @@ current_os()
echo "ubuntu"
elif [ "$(cat /etc/*-release | grep -cim1 centos)" -eq 1 ]; then
echo "centos"
elif [ "$(cat /etc/*-release | grep -cim1 rhel)" -eq 1 ]; then
echo "rhel.7"
fi
fi
}

View file

@ -14,6 +14,8 @@ while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symli
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
source "$DIR/common/_prettyprint.sh"
while [[ $# > 0 ]]; do
lowerI="$(echo $1 | awk '{print tolower($0)}')"
case $lowerI in

View file

@ -86,6 +86,11 @@ namespace Microsoft.DotNet.Cli.Utils
return command;
}
public static Command Create(CommandSpec commandSpec)
{
return new Command(commandSpec);
}
public static Command CreateForScript(
string commandName,
IEnumerable<string> args,

View file

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public abstract class AbstractPathBasedCommandResolver : ICommandResolver
{
protected IEnvironmentProvider _environment;
protected IPlatformCommandSpecFactory _commandSpecFactory;
public AbstractPathBasedCommandResolver(IEnvironmentProvider environment,
IPlatformCommandSpecFactory commandSpecFactory)
{
if (environment == null)
{
throw new ArgumentNullException("environment");
}
if (commandSpecFactory == null)
{
throw new ArgumentNullException("commandSpecFactory");
}
_environment = environment;
_commandSpecFactory = commandSpecFactory;
}
public CommandSpec Resolve(CommandResolverArguments commandResolverArguments)
{
if (commandResolverArguments.CommandName == null)
{
return null;
}
var commandPath = ResolveCommandPath(commandResolverArguments);
if (commandPath == null)
{
return null;
}
return _commandSpecFactory.CreateCommandSpec(
commandResolverArguments.CommandName,
commandResolverArguments.CommandArguments.OrEmptyIfNull(),
commandPath,
GetCommandResolutionStrategy(),
_environment);
}
internal abstract string ResolveCommandPath(CommandResolverArguments commandResolverArguments);
internal abstract CommandResolutionStrategy GetCommandResolutionStrategy();
}
}

View file

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class AppBaseCommandResolver : AbstractPathBasedCommandResolver
{
public AppBaseCommandResolver(IEnvironmentProvider environment,
IPlatformCommandSpecFactory commandSpecFactory) : base(environment, commandSpecFactory) { }
internal override string ResolveCommandPath(CommandResolverArguments commandResolverArguments)
{
return _environment.GetCommandPathFromRootPath(
PlatformServices.Default.Application.ApplicationBasePath,
commandResolverArguments.CommandName);
}
internal override CommandResolutionStrategy GetCommandResolutionStrategy()
{
return CommandResolutionStrategy.BaseDirectory;
}
}
}

View file

@ -0,0 +1,26 @@
namespace Microsoft.DotNet.Cli.Utils
{
public enum CommandResolutionStrategy
{
// command loaded from project dependencies nuget package
ProjectDependenciesPackage,
// command loaded from project tools nuget package
ProjectToolsPackage,
// command loaded from the same directory as the executing assembly
BaseDirectory,
// command loaded from the same directory as a project.json file
ProjectLocal,
// command loaded from PATH environment variable
Path,
// command loaded from rooted path
RootedPath,
// command not found
None
}
}

View file

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils
{
public class CommandResolverArguments
{
public string CommandName { get; set; }
public IEnumerable<string> CommandArguments { get; set; }
public NuGetFramework Framework { get; set; }
public string OutputPath { get; set; }
public string ProjectDirectory { get; set; }
public string Configuration { get; set; }
public IEnumerable<string> InferredExtensions { get; set; }
}
}

View file

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class CompositeCommandResolver : ICommandResolver
{
private IList<ICommandResolver> _orderedCommandResolvers;
public IEnumerable<ICommandResolver> OrderedCommandResolvers
{
get
{
return _orderedCommandResolvers;
}
}
public CompositeCommandResolver()
{
_orderedCommandResolvers = new List<ICommandResolver>();
}
public void AddCommandResolver(ICommandResolver commandResolver)
{
_orderedCommandResolvers.Add(commandResolver);
}
public CommandSpec Resolve(CommandResolverArguments commandResolverArguments)
{
foreach (var commandResolver in _orderedCommandResolvers)
{
var commandSpec = commandResolver.Resolve(commandResolverArguments);
if (commandSpec != null)
{
return commandSpec;
}
}
return null;
}
}
}

View file

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class DefaultCommandResolverPolicy
{
public static CompositeCommandResolver Create()
{
var environment = new EnvironmentProvider();
var packagedCommandSpecFactory = new PackagedCommandSpecFactory();
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory);
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
}
else
{
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
}
return CreateDefaultCommandResolver(environment, packagedCommandSpecFactory, platformCommandSpecFactory);
}
public static CompositeCommandResolver CreateDefaultCommandResolver(
IEnvironmentProvider environment,
IPackagedCommandSpecFactory packagedCommandSpecFactory,
IPlatformCommandSpecFactory platformCommandSpecFactory)
{
var compositeCommandResolver = new CompositeCommandResolver();
compositeCommandResolver.AddCommandResolver(new RootedCommandResolver());
compositeCommandResolver.AddCommandResolver(new ProjectToolsCommandResolver(packagedCommandSpecFactory));
compositeCommandResolver.AddCommandResolver(new AppBaseCommandResolver(environment, platformCommandSpecFactory));
compositeCommandResolver.AddCommandResolver(new PathCommandResolver(environment, platformCommandSpecFactory));
return compositeCommandResolver;
}
}
}

View file

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class GenericPlatformCommandSpecFactory : IPlatformCommandSpecFactory
{
public CommandSpec CreateCommandSpec(
string commandName,
IEnumerable<string> args,
string commandPath,
CommandResolutionStrategy resolutionStrategy,
IEnvironmentProvider environment)
{
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
return new CommandSpec(commandPath, escapedArgs, resolutionStrategy);
}
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.DotNet.Cli.Utils
{
public interface ICommandResolver
{
CommandSpec Resolve(CommandResolverArguments arguments);
}
}

View file

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.ProjectModel.Graph;
namespace Microsoft.DotNet.Cli.Utils
{
public interface IPackagedCommandSpecFactory
{
CommandSpec CreateCommandSpecFromLibrary(
LockFilePackageLibrary library,
string commandName,
IEnumerable<string> commandArguments,
IEnumerable<string> allowedExtensions,
string nugetPackagesRoot,
CommandResolutionStrategy commandResolutionStrategy,
string depsFilePath);
}
}

View file

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public interface IPlatformCommandSpecFactory
{
CommandSpec CreateCommandSpec(
string commandName,
IEnumerable<string> args,
string commandPath,
CommandResolutionStrategy resolutionStrategy,
IEnvironmentProvider environment);
}
}

View file

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class PackagedCommandSpecFactory : IPackagedCommandSpecFactory
{
public CommandSpec CreateCommandSpecFromLibrary(
LockFilePackageLibrary library,
string commandName,
IEnumerable<string> commandArguments,
IEnumerable<string> allowedExtensions,
string nugetPackagesRoot,
CommandResolutionStrategy commandResolutionStrategy,
string depsFilePath)
{
var packageDirectory = GetPackageDirectoryFullPath(library, nugetPackagesRoot);
if (!Directory.Exists(packageDirectory))
{
return null;
}
var commandFile = GetCommandFileRelativePath(library, commandName, allowedExtensions);
if (commandFile == null)
{
return null;
}
var commandPath = Path.Combine(packageDirectory, commandFile);
return CreateCommandSpecWrappingWithCorehostfDll(
commandPath,
commandArguments,
depsFilePath,
commandResolutionStrategy);
}
private string GetPackageDirectoryFullPath(LockFilePackageLibrary library, string nugetPackagesRoot)
{
var packageDirectory = new VersionFolderPathResolver(nugetPackagesRoot)
.GetInstallPath(library.Name, library.Version);
return packageDirectory;
}
private string GetCommandFileRelativePath(
LockFilePackageLibrary library,
string commandName,
IEnumerable<string> allowedExtensions)
{
// TODO: Should command names be case sensitive?
return library.Files
.Where(f => Path.GetFileNameWithoutExtension(f) == commandName)
.Where(e => allowedExtensions.Contains(Path.GetExtension(e)))
.FirstOrDefault();
}
private CommandSpec CreateCommandSpecWrappingWithCorehostfDll(
string commandPath,
IEnumerable<string> commandArguments,
string depsFilePath,
CommandResolutionStrategy commandResolutionStrategy)
{
var commandExtension = Path.GetExtension(commandPath);
if (commandExtension == FileNameSuffixes.DotNet.DynamicLib)
{
return CreatePackageCommandSpecUsingCorehost(
commandPath,
commandArguments,
depsFilePath,
commandResolutionStrategy);
}
return CreateCommandSpec(commandPath, commandArguments, commandResolutionStrategy);
}
private CommandSpec CreatePackageCommandSpecUsingCorehost(
string commandPath,
IEnumerable<string> commandArguments,
string depsFilePath,
CommandResolutionStrategy commandResolutionStrategy)
{
var corehost = CoreHost.HostExePath;
var arguments = new List<string>();
arguments.Add(commandPath);
if (depsFilePath != null)
{
arguments.Add($"--depsfile:{depsFilePath}");
}
arguments.AddRange(commandArguments);
return CreateCommandSpec(corehost, arguments, commandResolutionStrategy);
}
private CommandSpec CreateCommandSpec(
string commandPath,
IEnumerable<string> commandArguments,
CommandResolutionStrategy commandResolutionStrategy)
{
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(commandArguments);
return new CommandSpec(commandPath, escapedArgs, commandResolutionStrategy);
}
}
}

View file

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class PathCommandResolver : AbstractPathBasedCommandResolver
{
public PathCommandResolver(IEnvironmentProvider environment,
IPlatformCommandSpecFactory commandSpecFactory) : base(environment, commandSpecFactory) { }
internal override string ResolveCommandPath(CommandResolverArguments commandResolverArguments)
{
return _environment.GetCommandPath(commandResolverArguments.CommandName);
}
internal override CommandResolutionStrategy GetCommandResolutionStrategy()
{
return CommandResolutionStrategy.Path;
}
}
}

View file

@ -0,0 +1,181 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class ProjectDependenciesCommandResolver : ICommandResolver
{
private static readonly CommandResolutionStrategy s_commandResolutionStrategy =
CommandResolutionStrategy.ProjectDependenciesPackage;
private IEnvironmentProvider _environment;
private IPackagedCommandSpecFactory _packagedCommandSpecFactory;
public ProjectDependenciesCommandResolver(
IEnvironmentProvider environment,
IPackagedCommandSpecFactory packagedCommandSpecFactory)
{
if (environment == null)
{
throw new ArgumentNullException("environment");
}
if (packagedCommandSpecFactory == null)
{
throw new ArgumentNullException("packagedCommandSpecFactory");
}
_environment = environment;
_packagedCommandSpecFactory = packagedCommandSpecFactory;
}
public CommandSpec Resolve(CommandResolverArguments commandResolverArguments)
{
if (commandResolverArguments.Framework == null
|| commandResolverArguments.ProjectDirectory == null
|| commandResolverArguments.Configuration == null
|| commandResolverArguments.CommandName == null)
{
return null;
}
return ResolveFromProjectDependencies(
commandResolverArguments.ProjectDirectory,
commandResolverArguments.Framework,
commandResolverArguments.Configuration,
commandResolverArguments.CommandName,
commandResolverArguments.CommandArguments.OrEmptyIfNull(),
commandResolverArguments.OutputPath);
}
private CommandSpec ResolveFromProjectDependencies(
string projectDirectory,
NuGetFramework framework,
string configuration,
string commandName,
IEnumerable<string> commandArguments,
string outputPath)
{
var allowedExtensions = GetAllowedCommandExtensionsFromEnvironment(_environment);
var projectContext = GetProjectContextFromDirectory(
projectDirectory,
framework);
if (projectContext == null)
{
return null;
}
var depsFilePath = projectContext.GetOutputPaths(configuration, outputPath: outputPath).RuntimeFiles.Deps;
var dependencyLibraries = GetAllDependencyLibraries(projectContext);
return ResolveFromDependencyLibraries(
dependencyLibraries,
depsFilePath,
commandName,
allowedExtensions,
commandArguments,
projectContext);
}
private CommandSpec ResolveFromDependencyLibraries(
IEnumerable<LockFilePackageLibrary> dependencyLibraries,
string depsFilePath,
string commandName,
IEnumerable<string> allowedExtensions,
IEnumerable<string> commandArguments,
ProjectContext projectContext)
{
foreach (var dependencyLibrary in dependencyLibraries)
{
var commandSpec = ResolveFromDependencyLibrary(
dependencyLibrary,
depsFilePath,
commandName,
allowedExtensions,
commandArguments,
projectContext);
if (commandSpec != null)
{
return commandSpec;
}
}
return null;
}
private CommandSpec ResolveFromDependencyLibrary(
LockFilePackageLibrary dependencyLibrary,
string depsFilePath,
string commandName,
IEnumerable<string> allowedExtensions,
IEnumerable<string> commandArguments,
ProjectContext projectContext)
{
return _packagedCommandSpecFactory.CreateCommandSpecFromLibrary(
dependencyLibrary,
commandName,
commandArguments,
allowedExtensions,
projectContext.PackagesDirectory,
s_commandResolutionStrategy,
depsFilePath);
}
private IEnumerable<LockFilePackageLibrary> GetAllDependencyLibraries(
ProjectContext projectContext)
{
return projectContext.LibraryManager.GetLibraries()
.Where(l => l.GetType() == typeof(PackageDescription))
.Select(l => l as PackageDescription)
.Select(p => p.Library);
}
private ProjectContext GetProjectContextFromDirectory(string directory, NuGetFramework framework)
{
if (directory == null || framework == null)
{
return null;
}
var projectRootPath = directory;
if (!File.Exists(Path.Combine(projectRootPath, Project.FileName)))
{
return null;
}
var projectContext = ProjectContext.Create(
projectRootPath,
framework,
PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers());
if (projectContext.RuntimeIdentifier == null)
{
return null;
}
return projectContext;
}
private IEnumerable<string> GetAllowedCommandExtensionsFromEnvironment(IEnvironmentProvider environment)
{
var allowedCommandExtensions = new List<string>();
allowedCommandExtensions.AddRange(environment.ExecutableExtensions);
allowedCommandExtensions.Add(FileNameSuffixes.DotNet.DynamicLib);
return allowedCommandExtensions;
}
}
}

View file

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class ProjectPathCommandResolver : AbstractPathBasedCommandResolver
{
public ProjectPathCommandResolver(IEnvironmentProvider environment,
IPlatformCommandSpecFactory commandSpecFactory) : base(environment, commandSpecFactory) { }
internal override string ResolveCommandPath(CommandResolverArguments commandResolverArguments)
{
if (commandResolverArguments.ProjectDirectory == null)
{
return null;
}
return _environment.GetCommandPathFromRootPath(
commandResolverArguments.ProjectDirectory,
commandResolverArguments.CommandName,
commandResolverArguments.InferredExtensions.OrEmptyIfNull());
}
internal override CommandResolutionStrategy GetCommandResolutionStrategy()
{
return CommandResolutionStrategy.ProjectLocal;
}
}
}

View file

@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class ProjectToolsCommandResolver : ICommandResolver
{
private static readonly NuGetFramework s_toolPackageFramework = FrameworkConstants.CommonFrameworks.NetStandardApp15;
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 projectContext = GetProjectContextFromDirectory(projectDirectory, s_toolPackageFramework);
if (projectContext == null)
{
return null;
}
var toolsLibraries = projectContext.ProjectFile.Tools.OrEmptyIfNull();
return ResolveCommandSpecFromAllToolLibraries(
toolsLibraries,
commandName,
args,
projectContext);
}
private CommandSpec ResolveCommandSpecFromAllToolLibraries(
IEnumerable<LibraryRange> toolsLibraries,
string commandName,
IEnumerable<string> args,
ProjectContext projectContext)
{
foreach (var toolLibrary in toolsLibraries)
{
var commandSpec = ResolveCommandSpecFromToolLibrary(toolLibrary, commandName, args, projectContext);
if (commandSpec != null)
{
return commandSpec;
}
}
return null;
}
private CommandSpec ResolveCommandSpecFromToolLibrary(
LibraryRange toolLibrary,
string commandName,
IEnumerable<string> args,
ProjectContext projectContext)
{
//todo: change this for new resolution strategy
var lockFilePath = Path.Combine(
projectContext.ProjectDirectory,
"artifacts", "Tools", toolLibrary.Name,
"project.lock.json");
if (!File.Exists(lockFilePath))
{
return null;
}
var lockFile = LockFileReader.Read(lockFilePath);
var lockFilePackageLibrary = lockFile.PackageLibraries.FirstOrDefault(l => l.Name == toolLibrary.Name);
var nugetPackagesRoot = projectContext.PackagesDirectory;
return _packagedCommandSpecFactory.CreateCommandSpecFromLibrary(
lockFilePackageLibrary,
commandName,
args,
_allowedCommandExtensions,
projectContext.PackagesDirectory,
s_commandResolutionStrategy,
null);
}
private ProjectContext GetProjectContextFromDirectory(string directory, NuGetFramework framework)
{
if (directory == null || framework == null)
{
return null;
}
var projectRootPath = directory;
if (!File.Exists(Path.Combine(projectRootPath, Project.FileName)))
{
return null;
}
var projectContext = ProjectContext.Create(
projectRootPath,
framework,
PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers());
if (projectContext.RuntimeIdentifier == null)
{
return null;
}
return projectContext;
}
}
}

View file

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class RootedCommandResolver : ICommandResolver
{
public CommandSpec Resolve(CommandResolverArguments commandResolverArguments)
{
if (commandResolverArguments.CommandName == null)
{
return null;
}
if (Path.IsPathRooted(commandResolverArguments.CommandName))
{
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(
commandResolverArguments.CommandArguments.OrEmptyIfNull());
return new CommandSpec(commandResolverArguments.CommandName, escapedArgs, CommandResolutionStrategy.RootedPath);
}
return null;
}
}
}

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class ScriptCommandResolverPolicy
{
public static CompositeCommandResolver Create()
{
var environment = new EnvironmentProvider();
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory);
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
}
else
{
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
}
return CreateScriptCommandResolver(environment, platformCommandSpecFactory);
}
public static CompositeCommandResolver CreateScriptCommandResolver(
IEnvironmentProvider environment,
IPlatformCommandSpecFactory platformCommandSpecFactory)
{
var compositeCommandResolver = new CompositeCommandResolver();
compositeCommandResolver.AddCommandResolver(new RootedCommandResolver());
compositeCommandResolver.AddCommandResolver(new ProjectPathCommandResolver(environment, platformCommandSpecFactory));
compositeCommandResolver.AddCommandResolver(new AppBaseCommandResolver(environment, platformCommandSpecFactory));
compositeCommandResolver.AddCommandResolver(new PathCommandResolver(environment, platformCommandSpecFactory));
return compositeCommandResolver;
}
}
}

View file

@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
using NuGet.Packaging;
namespace Microsoft.DotNet.Cli.Utils
{
public class WindowsExePreferredCommandSpecFactory : IPlatformCommandSpecFactory
{
public CommandSpec CreateCommandSpec(
string commandName,
IEnumerable<string> args,
string commandPath,
CommandResolutionStrategy resolutionStrategy,
IEnvironmentProvider environment)
{
var useCmdWrapper = false;
if (Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
{
var preferredCommandPath = environment.GetCommandPath(commandName, ".exe");
if (preferredCommandPath == null)
{
useCmdWrapper = true;
}
else
{
commandPath = preferredCommandPath;
}
}
return useCmdWrapper
? CreateCommandSpecWrappedWithCmd(commandPath, args, resolutionStrategy)
: CreateCommandSpecFromExecutable(commandPath, args, resolutionStrategy);
}
private CommandSpec CreateCommandSpecFromExecutable(
string command,
IEnumerable<string> args,
CommandResolutionStrategy resolutionStrategy)
{
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
return new CommandSpec(command, escapedArgs, resolutionStrategy);
}
private CommandSpec CreateCommandSpecWrappedWithCmd(
string command,
IEnumerable<string> args,
CommandResolutionStrategy resolutionStrategy)
{
var comSpec = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe";
// Handle the case where ComSpec is already the command
if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
{
command = args.FirstOrDefault();
args = args.Skip(1);
}
var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args);
if (ArgumentEscaper.ShouldSurroundWithQuotes(command))
{
command = $"\"{command}\"";
}
var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\"";
return new CommandSpec(comSpec, escapedArgString, resolutionStrategy);
}
}
}

View file

@ -1,20 +0,0 @@
namespace Microsoft.DotNet.Cli.Utils
{
public enum CommandResolutionStrategy
{
//command loaded from a nuget package
NugetPackage,
//command loaded from the same directory as the executing assembly
BaseDirectory,
//command loaded from the same directory as a project.json file
ProjectLocal,
//command loaded from path
Path,
//command not found
None
}
}

View file

@ -17,261 +17,41 @@ namespace Microsoft.DotNet.Cli.Utils
string commandName,
IEnumerable<string> args,
NuGetFramework framework = null,
string configuration = Constants.DefaultConfiguration,
string outputPath = null)
string configuration=Constants.DefaultConfiguration,
string outputPath=null)
{
return ResolveFromRootedCommand(commandName, args) ??
ResolveFromProjectDependencies(commandName, args, framework, configuration, outputPath) ??
ResolveFromProjectTools(commandName, args) ??
ResolveFromAppBase(commandName, args) ??
ResolveFromPath(commandName, args);
var commandResolverArgs = new CommandResolverArguments
{
CommandName = commandName,
CommandArguments = args,
Framework = framework,
ProjectDirectory = Directory.GetCurrentDirectory(),
Configuration = configuration,
OutputPath = outputPath
};
var defaultCommandResolver = DefaultCommandResolverPolicy.Create();
return defaultCommandResolver.Resolve(commandResolverArgs);
}
public static CommandSpec TryResolveScriptCommandSpec(string commandName, IEnumerable<string> args, Project project, string[] inferredExtensionList)
{
return ResolveFromRootedCommand(commandName, args) ??
ResolveFromProjectPath(commandName, args, project, inferredExtensionList) ??
ResolveFromAppBase(commandName, args) ??
ResolveFromPath(commandName, args);
}
private static CommandSpec ResolveFromPath(string commandName, IEnumerable<string> args)
{
var commandPath = Env.GetCommandPath(commandName);
return commandPath == null
? null
: CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.Path);
}
private static CommandSpec ResolveFromAppBase(string commandName, IEnumerable<string> args)
{
var commandPath = Env.GetCommandPathFromRootPath(PlatformServices.Default.Application.ApplicationBasePath, commandName);
return commandPath == null
? null
: CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.BaseDirectory);
}
private static CommandSpec ResolveFromProjectPath(string commandName, IEnumerable<string> args, Project project, string[] inferredExtensionList)
{
var commandPath = Env.GetCommandPathFromRootPath(project.ProjectDirectory, commandName, inferredExtensionList);
return commandPath == null
? null
: CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.ProjectLocal);
}
private static CommandSpec ResolveFromRootedCommand(string commandName, IEnumerable<string> args)
{
if (Path.IsPathRooted(commandName))
{
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
return new CommandSpec(commandName, escapedArgs, CommandResolutionStrategy.Path);
}
return null;
}
public static CommandSpec ResolveFromProjectDependencies(
public static CommandSpec TryResolveScriptCommandSpec(
string commandName,
IEnumerable<string> args,
NuGetFramework framework,
string configuration,
string outputPath)
Project project,
string[] inferredExtensionList)
{
if (framework == null) return null;
var projectContext = GetProjectContext(framework);
if (projectContext == null) return null;
var commandPackage = GetCommandPackage(projectContext, commandName);
if (commandPackage == null) return null;
var depsPath = projectContext.GetOutputPaths(configuration, outputPath: outputPath).RuntimeFiles.Deps;
return ConfigureCommandFromPackage(commandName, args, commandPackage, projectContext, depsPath);
}
private static ProjectContext GetProjectContext(NuGetFramework framework)
var commandResolverArgs = new CommandResolverArguments
{
var projectRootPath = Directory.GetCurrentDirectory();
CommandName = commandName,
CommandArguments = args,
ProjectDirectory = project.ProjectDirectory,
InferredExtensions = inferredExtensionList
};
if (!File.Exists(Path.Combine(projectRootPath, Project.FileName)))
{
return null;
}
var scriptCommandResolver = ScriptCommandResolverPolicy.Create();
var projectContext = ProjectContext.Create(projectRootPath, framework, PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers());
return projectContext;
}
private static PackageDescription GetCommandPackage(ProjectContext projectContext, string commandName)
{
return projectContext.LibraryManager.GetLibraries()
.Where(l => l.GetType() == typeof(PackageDescription))
.Select(l => l as PackageDescription)
.FirstOrDefault(p => p.Library.Files
.Select(Path.GetFileName)
.Where(f => Path.GetFileNameWithoutExtension(f) == commandName)
.Select(Path.GetExtension)
.Any(e => Env.ExecutableExtensions.Contains(e) ||
e == FileNameSuffixes.DotNet.DynamicLib));
}
public static CommandSpec ResolveFromProjectTools(string commandName, IEnumerable<string> args)
{
var context = GetProjectContext(FrameworkConstants.CommonFrameworks.NetStandardApp15);
if (context == null)
{
return null;
}
var commandLibrary = context.ProjectFile.Tools
.FirstOrDefault(l => l.Name == commandName);
if (commandLibrary == default(LibraryRange))
{
return null;
}
var lockPath = Path.Combine(context.ProjectDirectory, "artifacts", "Tools", commandName,
"project.lock.json");
if (!File.Exists(lockPath))
{
return null;
}
var lockFile = LockFileReader.Read(lockPath);
var lib = lockFile.PackageLibraries.FirstOrDefault(l => l.Name == commandName);
var packageDir = new VersionFolderPathResolver(context.PackagesDirectory)
.GetInstallPath(lib.Name, lib.Version);
return Directory.Exists(packageDir)
? ConfigureCommandFromPackage(commandName, args, lib.Files, packageDir)
: null;
}
private static CommandSpec ConfigureCommandFromPackage(string commandName, IEnumerable<string> args, string packageDir)
{
var commandPackage = new PackageFolderReader(packageDir);
var files = commandPackage.GetFiles();
return ConfigureCommandFromPackage(commandName, args, files, packageDir);
}
private static CommandSpec ConfigureCommandFromPackage(string commandName, IEnumerable<string> args,
PackageDescription commandPackage, ProjectContext projectContext, string depsPath = null)
{
var files = commandPackage.Library.Files;
var packageRoot = projectContext.PackagesDirectory;
var packagePath = commandPackage.Path;
var packageDir = Path.Combine(packageRoot, packagePath);
return ConfigureCommandFromPackage(commandName, args, files, packageDir, depsPath);
}
private static CommandSpec ConfigureCommandFromPackage(string commandName, IEnumerable<string> args,
IEnumerable<string> files, string packageDir, string depsPath = null)
{
var fileName = string.Empty;
var commandPath = files
.FirstOrDefault(f => Env.ExecutableExtensions.Contains(Path.GetExtension(f)));
if (commandPath == null)
{
var dllPath = files
.Where(f => Path.GetFileName(f) == commandName + FileNameSuffixes.DotNet.DynamicLib)
.Select(f => Path.Combine(packageDir, f))
.FirstOrDefault();
fileName = CoreHost.HostExePath;
var additionalArgs = new List<string>();
additionalArgs.Add(dllPath);
if (depsPath != null)
{
additionalArgs.Add($"--depsfile:{depsPath}");
}
args = additionalArgs.Concat(args);
}
else
{
fileName = Path.Combine(packageDir, commandPath);
}
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
return new CommandSpec(fileName, escapedArgs, CommandResolutionStrategy.NugetPackage);
}
private static CommandSpec CreateCommandSpecPreferringExe(
string commandName,
IEnumerable<string> args,
string commandPath,
CommandResolutionStrategy resolutionStrategy)
{
var useComSpec = false;
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows &&
Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
{
var preferredCommandPath = Env.GetCommandPath(commandName, ".exe");
// Use cmd if we can't find an exe
if (preferredCommandPath == null)
{
useComSpec = true;
}
else
{
commandPath = preferredCommandPath;
}
}
if (useComSpec)
{
return CreateCmdCommandSpec(commandPath, args, resolutionStrategy);
}
else
{
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
return new CommandSpec(commandPath, escapedArgs, resolutionStrategy);
}
}
private static CommandSpec CreateCmdCommandSpec(
string command,
IEnumerable<string> args,
CommandResolutionStrategy resolutionStrategy)
{
var comSpec = Environment.GetEnvironmentVariable("ComSpec");
// Handle the case where ComSpec is already the command
if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
{
command = args.FirstOrDefault();
args = args.Skip(1);
}
var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args);
if (ArgumentEscaper.ShouldSurroundWithQuotes(command))
{
command = $"\"{command}\"";
}
var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\"";
return new CommandSpec(comSpec, escapedArgString, resolutionStrategy);
return scriptCommandResolver.Resolve(commandResolverArgs);
}
}
}

View file

@ -1,6 +1,6 @@
namespace Microsoft.DotNet.Cli.Utils
{
internal class CommandSpec
public class CommandSpec
{
public CommandSpec(string path, string args, CommandResolutionStrategy resolutionStrategy)
{

View file

@ -1,3 +1,4 @@
using System;
using System.IO;
using Microsoft.Extensions.PlatformAbstractions;

View file

@ -9,74 +9,29 @@ namespace Microsoft.DotNet.Cli.Utils
{
public static class Env
{
private static IEnumerable<string> _searchPaths;
private static IEnumerable<string> _executableExtensions;
private static IEnvironmentProvider _environment = new EnvironmentProvider();
public static IEnumerable<string> ExecutableExtensions
{
get
{
if (_executableExtensions == null)
{
_executableExtensions = PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows
? Environment.GetEnvironmentVariable("PATHEXT")
.Split(';')
.Select(e => e.ToLower().Trim('"'))
: new [] { string.Empty };
}
return _executableExtensions;
}
}
private static IEnumerable<string> SearchPaths
{
get
{
if (_searchPaths == null)
{
var searchPaths = new List<string> { PlatformServices.Default.Application.ApplicationBasePath };
searchPaths.AddRange(Environment
.GetEnvironmentVariable("PATH")
.Split(Path.PathSeparator)
.Select(p => p.Trim('"')));
_searchPaths = searchPaths;
}
return _searchPaths;
return _environment.ExecutableExtensions;
}
}
public static string GetCommandPath(string commandName, params string[] extensions)
{
if (!extensions.Any())
{
extensions = Env.ExecutableExtensions.ToArray();
}
var commandPath = Env.SearchPaths.Join(
extensions,
p => true, s => true,
(p, s) => Path.Combine(p, commandName + s))
.FirstOrDefault(File.Exists);
return commandPath;
return _environment.GetCommandPath(commandName, extensions);
}
public static string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions)
{
if (!extensions.Any())
{
extensions = Env.ExecutableExtensions.ToArray();
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
}
var commandPath = extensions.Select(e => Path.Combine(rootPath, commandName + e))
.FirstOrDefault(File.Exists);
return commandPath;
public static string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions)
{
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
}
}
}

View file

@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.DotNet.Cli.Utils
{
public class EnvironmentProvider : IEnvironmentProvider
{
private IEnumerable<string> _searchPaths;
private IEnumerable<string> _executableExtensions;
public IEnumerable<string> ExecutableExtensions
{
get
{
if (_executableExtensions == null)
{
_executableExtensions = PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows
? Environment.GetEnvironmentVariable("PATHEXT")
.Split(';')
.Select(e => e.ToLower().Trim('"'))
: new [] { string.Empty };
}
return _executableExtensions;
}
}
private IEnumerable<string> SearchPaths
{
get
{
if (_searchPaths == null)
{
var searchPaths = new List<string> { PlatformServices.Default.Application.ApplicationBasePath };
searchPaths.AddRange(Environment
.GetEnvironmentVariable("PATH")
.Split(Path.PathSeparator)
.Select(p => p.Trim('"')));
_searchPaths = searchPaths;
}
return _searchPaths;
}
}
public EnvironmentProvider(
IEnumerable<string> extensionsOverride = null,
IEnumerable<string> searchPathsOverride = null)
{
_executableExtensions = extensionsOverride;
_searchPaths = searchPathsOverride;
}
public string GetCommandPath(string commandName, params string[] extensions)
{
if (!extensions.Any())
{
extensions = ExecutableExtensions.ToArray();
}
var commandPath = SearchPaths.Join(
extensions,
p => true, s => true,
(p, s) => Path.Combine(p, commandName + s))
.FirstOrDefault(File.Exists);
return commandPath;
}
public string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions)
{
if (!extensions.Any())
{
extensions = ExecutableExtensions.ToArray();
}
var commandPath = extensions.Select(e => Path.Combine(rootPath, commandName + e))
.FirstOrDefault(File.Exists);
return commandPath;
}
public string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions)
{
var extensionsArr = extensions.OrEmptyIfNull().ToArray();
return GetCommandPathFromRootPath(rootPath, commandName, extensionsArr);
}
}
}

View file

@ -1,6 +1,4 @@
// 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;

View file

@ -1,41 +0,0 @@
// 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;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils
{
public class FixedPathCommandFactory : ICommandFactory
{
private readonly NuGetFramework _nugetFramework;
private readonly string _configuration;
private readonly string _outputPath;
public FixedPathCommandFactory(NuGetFramework nugetFramework, string configuration, string outputPath)
{
_nugetFramework = nugetFramework;
_configuration = configuration;
_outputPath = outputPath;
}
public ICommand Create(
string commandName,
IEnumerable<string> args,
NuGetFramework framework = null,
string configuration = Constants.DefaultConfiguration)
{
if (string.IsNullOrEmpty(configuration))
{
configuration = _configuration;
}
if (framework == null)
{
framework = _nugetFramework;
}
return Command.Create(commandName, args, framework, configuration, _outputPath);
}
}
}

View file

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.DotNet.Cli.Utils
{
public interface IEnvironmentProvider
{
IEnumerable<string> ExecutableExtensions { get; }
string GetCommandPath(string commandName, params string[] extensions);
string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions);
string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions);
}
}

View file

@ -0,0 +1,92 @@
// 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;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils
{
public class ProjectDependenciesCommandFactory : ICommandFactory
{
private readonly NuGetFramework _nugetFramework;
private readonly string _configuration;
private readonly string _outputPath;
private readonly string _projectDirectory;
public ProjectDependenciesCommandFactory(
NuGetFramework nugetFramework,
string configuration,
string outputPath,
string projectDirectory)
{
_nugetFramework = nugetFramework;
_configuration = configuration;
_outputPath = outputPath;
_projectDirectory = projectDirectory;
}
public ICommand Create(
string commandName,
IEnumerable<string> args,
NuGetFramework framework = null,
string configuration = Constants.DefaultConfiguration)
{
if (string.IsNullOrEmpty(configuration))
{
configuration = _configuration;
}
if (framework == null)
{
framework = _nugetFramework;
}
var commandSpec = FindProjectDependencyCommands(
commandName,
args,
configuration,
framework,
_outputPath,
_projectDirectory);
return Command.Create(commandSpec);
}
private CommandSpec FindProjectDependencyCommands(
string commandName,
IEnumerable<string> commandArgs,
string configuration,
NuGetFramework framework,
string outputPath,
string projectDirectory)
{
var commandResolverArguments = new CommandResolverArguments
{
CommandName = commandName,
CommandArguments = commandArgs,
Framework = framework,
Configuration = configuration,
OutputPath = outputPath,
ProjectDirectory = projectDirectory
};
var commandResolver = GetProjectDependenciesCommandResolver();
var commandSpec = commandResolver.Resolve(commandResolverArguments);
if (commandSpec == null)
{
throw new CommandUnknownException(commandName);
}
return commandSpec;
}
private ICommandResolver GetProjectDependenciesCommandResolver()
{
var environment = new EnvironmentProvider();
var packagedCommandSpecFactory = new PackagedCommandSpecFactory();
return new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory);
}
}
}

View file

@ -19,7 +19,8 @@
"netstandard1.3": {
"imports": "dnxcore50",
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23901"
"NETStandard.Library": "1.0.0-rc2-23901",
"System.Diagnostics.Process": "4.1.0-rc2-23901"
}
}
}

View file

@ -39,11 +39,6 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
public void MakeCompilationOutputRunnable()
{
if (string.IsNullOrEmpty(_context.RuntimeIdentifier))
{
throw new InvalidOperationException($"Can not make output runnable for framework {_context.TargetFramework}, because it doesn't have runtime target");
}
CopyContentFiles();
ExportRuntimeAssets();
}
@ -72,9 +67,12 @@ namespace Microsoft.Dotnet.Cli.Compiler.Common
{
WriteDepsFileAndCopyProjectDependencies(_exporter);
if (!string.IsNullOrEmpty(_context.RuntimeIdentifier))
{
// TODO: Pick a host based on the RID
CoreHost.CopyTo(_runtimeOutputPath, _context.ProjectFile.Name + Constants.ExeSuffix);
}
}
private void CopyContentFiles()
{

View file

@ -10,7 +10,10 @@ namespace Microsoft.DotNet.ProjectModel.Loader
{
public static class LoaderProjectContextExtensions
{
public static AssemblyLoadContext CreateLoadContext(this ProjectContext context, string configuration = "Debug")
public static AssemblyLoadContext CreateLoadContext(
this ProjectContext context,
string configuration = "Debug",
string outputPath = null)
{
var exporter = context.CreateExporter(configuration);
var assemblies = new Dictionary<AssemblyName, string>(AssemblyNameComparer.OrdinalIgnoreCase);
@ -38,7 +41,7 @@ namespace Microsoft.DotNet.ProjectModel.Loader
dllImports,
// Add the project's output directory path to ensure project-to-project references get located
new[] { context.GetOutputPaths(configuration).CompilationOutputPath });
new[] { context.GetOutputPaths(configuration, outputPath: outputPath).CompilationOutputPath });
}
private class AssemblyNameComparer : IEqualityComparer<AssemblyName>

View file

@ -49,6 +49,16 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
/// </summary>
public IEnumerable<AnalyzerReference> AnalyzerReferences { get; }
/// <summary>
/// Get a list of runtime targets provided by this export.
/// </summary>
public IEnumerable<LibraryRuntimeTarget> RuntimeTargets { get; }
/// <summary>
/// Get a list of resource assemblies provided by this export.
/// </summary>
public IEnumerable<LibraryResourceAssembly> ResourceAssemblies { get; }
public LibraryExport(LibraryDescription library,
IEnumerable<LibraryAsset> compileAssemblies,
IEnumerable<LibraryAsset> sourceReferences,
@ -56,7 +66,9 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
IEnumerable<LibraryAsset> runtimeAssets,
IEnumerable<LibraryAsset> nativeLibraries,
IEnumerable<LibraryAsset> embeddedResources,
IEnumerable<AnalyzerReference> analyzers)
IEnumerable<AnalyzerReference> analyzers,
IEnumerable<LibraryRuntimeTarget> runtimeTargets,
IEnumerable<LibraryResourceAssembly> resourceAssemblies)
{
Library = library;
CompilationAssemblies = compileAssemblies;
@ -66,6 +78,8 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
NativeLibraries = nativeLibraries;
EmbeddedResources = embeddedResources;
AnalyzerReferences = analyzers;
RuntimeTargets = runtimeTargets;
ResourceAssemblies = resourceAssemblies;
}
private string DebuggerDisplay => Library.Identity.ToString();

View file

@ -26,6 +26,10 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
private IList<AnalyzerReference> _analyzerReferences;
private IList<LibraryRuntimeTarget> _runtimeTargets;
private IList<LibraryResourceAssembly> _resourceAssemblies;
public LibraryDescription Library { get; set; }
public IEnumerable<LibraryAsset> RuntimeAssemblies => _runtimeAssemblies;
@ -44,6 +48,10 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
public IEnumerable<AnalyzerReference> AnalyzerReferences => _analyzerReferences;
public IEnumerable<LibraryRuntimeTarget> RuntimeTargets => _runtimeTargets;
public IEnumerable<LibraryResourceAssembly> ResourceAssemblies => _resourceAssemblies;
public static LibraryExportBuilder Create(LibraryDescription library = null)
{
return new LibraryExportBuilder().WithLibrary(library);
@ -63,7 +71,9 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
RuntimeAssets ?? EmptyArray<LibraryAsset>.Value,
NativeLibraries ?? EmptyArray<LibraryAsset>.Value,
EmbeddedResources ?? EmptyArray<LibraryAsset>.Value,
AnalyzerReferences ?? EmptyArray<AnalyzerReference>.Value);
AnalyzerReferences ?? EmptyArray<AnalyzerReference>.Value,
RuntimeTargets ?? EmptyArray<LibraryRuntimeTarget>.Value,
ResourceAssemblies ?? EmptyArray<LibraryResourceAssembly>.Value);
}
public LibraryExportBuilder WithLibrary(LibraryDescription libraryDescription)
@ -114,6 +124,18 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
return this;
}
public LibraryExportBuilder WithRuntimeTargets(IEnumerable<LibraryRuntimeTarget> targets)
{
Replace(ref _runtimeTargets, targets);
return this;
}
public LibraryExportBuilder WithResourceAssemblies(IEnumerable<LibraryResourceAssembly> assemblies)
{
Replace(ref _resourceAssemblies, assemblies);
return this;
}
public LibraryExportBuilder AddRuntimeAssembly(LibraryAsset asset)
{
Add(ref _runtimeAssemblies, asset);
@ -156,10 +178,29 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
return this;
}
public LibraryExportBuilder AddRuntimeTarget(LibraryRuntimeTarget target)
{
Add(ref _runtimeTargets, target);
return this;
}
public LibraryExportBuilder AddResourceAssembly(LibraryResourceAssembly assembly)
{
Add(ref _resourceAssemblies, assembly);
return this;
}
private void Replace<T>(ref IList<T> list, IEnumerable<T> enumerable)
{
if (enumerable == null)
{
list = null;
}
else
{
list = new List<T>(enumerable);
}
}
private void Add<T>(ref IList<T> list, T item)
{

View file

@ -122,6 +122,8 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
.WithNativeLibraries(libraryExport.NativeLibraries)
.WithEmbedddedResources(libraryExport.EmbeddedResources)
.WithAnalyzerReference(analyzerReferences)
.WithResourceAssemblies(libraryExport.ResourceAssemblies)
.WithRuntimeTargets(libraryExport.RuntimeTargets)
.Build();
}
}
@ -199,6 +201,28 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
}
}
}
if (package.RuntimeTargets.Any())
{
foreach (var targetGroup in package.RuntimeTargets.GroupBy(t => t.Runtime))
{
var runtime = new List<LibraryAsset>();
var native = new List<LibraryAsset>();
foreach (var lockFileRuntimeTarget in targetGroup)
{
if (string.Equals(lockFileRuntimeTarget.AssetType, "native", StringComparison.OrdinalIgnoreCase))
{
native.Add(LibraryAsset.CreateFromRelativePath(package.Path, lockFileRuntimeTarget.Path));
}
else if (string.Equals(lockFileRuntimeTarget.AssetType, "runtime", StringComparison.OrdinalIgnoreCase))
{
runtime.Add(LibraryAsset.CreateFromRelativePath(package.Path, lockFileRuntimeTarget.Path));
}
}
builder.AddRuntimeTarget(new LibraryRuntimeTarget(targetGroup.Key, runtime, native));
}
}
return builder.Build();
}

View file

@ -0,0 +1,18 @@
// 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.ProjectModel.Compilation
{
public class LibraryResourceAssembly
{
public LibraryResourceAssembly(LibraryAsset asset, string locale)
{
Asset = asset;
Locale = locale;
}
public LibraryAsset Asset { get; }
public string Locale { get; }
}
}

View file

@ -0,0 +1,32 @@
// 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.Linq;
using System.Collections.Generic;
namespace Microsoft.DotNet.ProjectModel.Compilation
{
public class LibraryRuntimeTarget
{
public LibraryRuntimeTarget(string runtime,
IEnumerable<LibraryAsset> runtimeAssemblies,
IEnumerable<LibraryAsset> nativeLibraries)
{
Runtime = runtime;
RuntimeAssemblies = runtimeAssemblies.ToArray();
NativeLibraries = nativeLibraries.ToArray();
}
public string Runtime { get; }
/// <summary>
/// Gets a list of fully-qualified paths to MSIL binaries required to run
/// </summary>
public IReadOnlyList<LibraryAsset> RuntimeAssemblies { get; }
/// <summary>
/// Gets a list of fully-qualified paths to native binaries required to run
/// </summary>
public IReadOnlyList<LibraryAsset> NativeLibraries { get; }
}
}

View file

@ -49,8 +49,8 @@ namespace Microsoft.Extensions.DependencyModel
runtime,
false,
GetCompilationOptions(compilerOptions),
GetLibraries(compilationExports, dependencyLookup, runtime: false).Cast<CompilationLibrary>().ToArray(),
GetLibraries(runtimeExports, dependencyLookup, runtime: true).Cast<RuntimeLibrary>().ToArray(),
GetLibraries(compilationExports, dependencyLookup, runtime: false).Cast<CompilationLibrary>(),
GetLibraries(runtimeExports, dependencyLookup, runtime: true).Cast<RuntimeLibrary>(),
new KeyValuePair<string, string[]>[0]);
}
@ -107,14 +107,14 @@ namespace Microsoft.Extensions.DependencyModel
}
}
string[] assemblies;
IEnumerable<string> assemblies;
if (type == LibraryType.ReferenceAssembly)
{
assemblies = ResolveReferenceAssembliesPath(libraryAssets);
}
else
{
assemblies = libraryAssets.Select(libraryAsset => libraryAsset.RelativePath).ToArray();
assemblies = libraryAssets.Select(libraryAsset => libraryAsset.RelativePath);
}
if (runtime)
@ -124,9 +124,10 @@ namespace Microsoft.Extensions.DependencyModel
export.Library.Identity.Name,
export.Library.Identity.Version.ToString(),
export.Library.Hash,
assemblies.Select(RuntimeAssembly.Create).ToArray(),
new RuntimeTarget[0],
libraryDependencies.ToArray(),
assemblies.Select(RuntimeAssembly.Create),
export.ResourceAssemblies.Select(CreateResourceAssembly),
export.RuntimeTargets.Select(CreateRuntimeTarget),
libraryDependencies,
serviceable
);
}
@ -138,15 +139,31 @@ namespace Microsoft.Extensions.DependencyModel
export.Library.Identity.Version.ToString(),
export.Library.Hash,
assemblies,
libraryDependencies.ToArray(),
libraryDependencies,
serviceable
);
}
}
private string[] ResolveReferenceAssembliesPath(IEnumerable<LibraryAsset> libraryAssets)
private ResourceAssembly CreateResourceAssembly(LibraryResourceAssembly resourceAssembly)
{
return new ResourceAssembly(
path: resourceAssembly.Asset.RelativePath,
locale: resourceAssembly.Locale
);
}
private RuntimeTarget CreateRuntimeTarget(LibraryRuntimeTarget runtimeTarget)
{
return new RuntimeTarget(
runtime: runtimeTarget.Runtime,
assemblies: runtimeTarget.RuntimeAssemblies.Select(a => RuntimeAssembly.Create(a.RelativePath)),
nativeLibraries: runtimeTarget.NativeLibraries.Select(l => l.RelativePath)
);
}
private IEnumerable<string> ResolveReferenceAssembliesPath(IEnumerable<LibraryAsset> libraryAssets)
{
var resolvedPaths = new List<string>();
var referenceAssembliesPath =
PathUtility.EnsureTrailingSlash(_referenceAssembliesPath);
foreach (var libraryAsset in libraryAssets)
@ -155,14 +172,13 @@ namespace Microsoft.Extensions.DependencyModel
// 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));
yield return libraryAsset.ResolvedPath.Substring(referenceAssembliesPath.Length);
}
else
{
resolvedPaths.Add(Path.GetFileName(libraryAsset.ResolvedPath));
yield return Path.GetFileName(libraryAsset.ResolvedPath);
}
}
return resolvedPaths.ToArray();
}
}
}

View file

@ -170,6 +170,21 @@ namespace Microsoft.DotNet.ProjectModel.Graph
return library;
}
private static LockFileRuntimeTarget ReadRuntimeTarget(string property, JsonValue json)
{
var jsonObject = json as JsonObject;
if (jsonObject == null)
{
throw FileFormatException.Create("The value type is not an object.", json);
}
return new LockFileRuntimeTarget(
path: property,
runtime: jsonObject.ValueAsString("runtime"),
assetType: jsonObject.ValueAsString("assetType")
);
}
private static LockFileContentFile ReadContentFile(string property, JsonValue json)
{
var contentFile = new LockFileContentFile()

View file

@ -0,0 +1,21 @@
// 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.ProjectModel.Graph
{
public class LockFileRuntimeTarget
{
public LockFileRuntimeTarget(string path, string runtime, string assetType)
{
Path = path;
Runtime = runtime;
AssetType = assetType;
}
public string Path { get; }
public string Runtime { get; }
public string AssetType { get; }
}
}

View file

@ -32,5 +32,7 @@ namespace Microsoft.DotNet.ProjectModel.Graph
public IList<LockFileItem> NativeLibraries { get; set; } = new List<LockFileItem>();
public IList<LockFileContentFile> ContentFiles { get; set; } = new List<LockFileContentFile>();
public IList<LockFileRuntimeTarget> RuntimeTargets { get; set; } = new List<LockFileRuntimeTarget>();
}
}

View file

@ -48,12 +48,18 @@ namespace Microsoft.DotNet.ProjectModel
{
if (!string.IsNullOrEmpty(runtimeIdentifier))
{
runtimeOutputPath= PathUtility.EnsureTrailingSlash(Path.Combine(compilationOutputPath, runtimeIdentifier));
runtimeOutputPath = PathUtility.EnsureTrailingSlash(Path.Combine(compilationOutputPath, runtimeIdentifier));
}
else
{
// "Runtime" assets (i.e. the deps file) will be dropped to the compilation output path, because
// we are building a RID-less target.
runtimeOutputPath = compilationOutputPath;
}
}
else
{
runtimeOutputPath= PathUtility.EnsureTrailingSlash(Path.GetFullPath(outputPath));
runtimeOutputPath = PathUtility.EnsureTrailingSlash(Path.GetFullPath(outputPath));
}
var intermediateOutputPath = PathUtility.EnsureTrailingSlash(Path.Combine(

View file

@ -45,6 +45,8 @@ namespace Microsoft.DotNet.ProjectModel
public IEnumerable<LockFileContentFile> ContentFiles => Target.ContentFiles;
public IEnumerable<LockFileRuntimeTarget> RuntimeTargets => Target.RuntimeTargets;
private IEnumerable<LockFileItem> FilterPlaceholders(IList<LockFileItem> items)
{
return items.Where(a => !PackageDependencyProvider.IsPlaceholderFile(a));

View file

@ -124,12 +124,12 @@ namespace Microsoft.DotNet.ProjectModel
public bool HasRuntimeOutput(string configuration)
{
var compilationOptions = GetCompilerOptions(targetFramework: null, configurationName: configuration);
// TODO: Make this opt in via another mechanism
return compilationOptions.EmitEntryPoint.GetValueOrDefault() || IsTestProject;
}
private CommonCompilerOptions GetCompilerOptions()
{
return _defaultCompilerOptions;

View file

@ -120,15 +120,12 @@ namespace Microsoft.DotNet.ProjectModel
/// <summary>
/// Creates a project context for each target located in the project at <paramref name="projectPath"/>
/// </summary>
public static IEnumerable<ProjectContext> CreateContextForEachTarget(string projectPath)
public static IEnumerable<ProjectContext> CreateContextForEachTarget(string projectPath, ProjectReaderSettings settings = null)
{
if (!projectPath.EndsWith(Project.FileName))
{
projectPath = Path.Combine(projectPath, Project.FileName);
}
var project = ProjectReader.GetProject(projectPath);
return new ProjectContextBuilder()
.WithReaderSettings(settings)
.WithProject(project)
.BuildAllTargets();
}
@ -143,15 +140,20 @@ namespace Microsoft.DotNet.ProjectModel
public ProjectContext CreateRuntimeContext(IEnumerable<string> runtimeIdentifiers)
{
var context = Create(ProjectFile.ProjectFilePath, TargetFramework, runtimeIdentifiers);
if (context.RuntimeIdentifier == null)
// Check if there are any runtime targets (i.e. are we portable)
var standalone = LockFile.Targets
.Where(t => t.TargetFramework.Equals(TargetFramework))
.Any(t => !string.IsNullOrEmpty(t.RuntimeIdentifier));
var context = Create(ProjectFile.ProjectFilePath, TargetFramework, standalone ? runtimeIdentifiers : Enumerable.Empty<string>());
if (standalone && context.RuntimeIdentifier == null)
{
// We are standalone, but don't support this runtime
var rids = string.Join(", ", runtimeIdentifiers);
throw new InvalidOperationException($"Can not find runtime target for framework '{TargetFramework}' and RID's '{rids}'. " +
throw new InvalidOperationException($"Can not find runtime target for framework '{TargetFramework}' compatible with one of the target runtimes: '{rids}'. " +
"Possible causes:" + Environment.NewLine +
"1. Project is not restored or restore failed - run `dotnet restore`" + Environment.NewLine +
"2. Project is not targeting `runable` framework (`netstandardapp*` or `net*`)"
);
"1. The project has not been restored or restore failed - run `dotnet restore`" + Environment.NewLine +
$"2. The project does not list one of '{rids}' in the 'runtimes' section.");
}
return context;
}

View file

@ -61,18 +61,20 @@ namespace Microsoft.DotNet.ProjectModel
return true;
}
public static Project GetProject(string projectFile, ProjectReaderSettings settings = null)
public static Project GetProject(string projectPath, ProjectReaderSettings settings = null) => GetProject(projectPath, new List<DiagnosticMessage>(), settings);
public static Project GetProject(string projectPath, ICollection<DiagnosticMessage> diagnostics, ProjectReaderSettings settings = null)
{
return GetProject(projectFile, new List<DiagnosticMessage>(), settings);
if (!projectPath.EndsWith(Project.FileName))
{
projectPath = Path.Combine(projectPath, Project.FileName);
}
public static Project GetProject(string projectFile, ICollection<DiagnosticMessage> diagnostics, ProjectReaderSettings settings = null)
{
var name = Path.GetFileName(Path.GetDirectoryName(projectFile));
var name = Path.GetFileName(Path.GetDirectoryName(projectPath));
using (var stream = new FileStream(projectFile, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var stream = new FileStream(projectPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return new ProjectReader().ReadProject(stream, name, projectFile, diagnostics, settings);
return new ProjectReader().ReadProject(stream, name, projectPath, diagnostics, settings);
}
}
@ -546,7 +548,8 @@ namespace Microsoft.DotNet.ProjectModel
analyzerOptions.LanguageId = languageId;
break;
default:;
default:
;
throw FileFormatException.Create(
$"Unrecognized analyzerOption key: {key}",
project.ProjectFilePath);

View file

@ -6,7 +6,7 @@
"description": "Types to model a .NET Project",
"dependencies": {
"System.Reflection.Metadata": "1.2.0-rc2-23901",
"NuGet.Packaging": "3.4.0-beta-632",
"NuGet.Packaging": "3.4.0-rtm-0724",
"Microsoft.Extensions.FileSystemGlobbing": "1.0.0-rc2-15996",
"Microsoft.Extensions.JsonParser.Sources": {
"type": "build",

View file

@ -3,16 +3,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyModel.Resolution;
namespace Microsoft.Extensions.DependencyModel
{
public class CompilationLibrary : Library
{
public CompilationLibrary(string type, string name, string version, string hash, string[] assemblies, Dependency[] dependencies, bool serviceable)
public CompilationLibrary(string type,
string name,
string version,
string hash,
IEnumerable<string> assemblies,
IEnumerable<Dependency> dependencies,
bool serviceable)
: base(type, name, version, hash, dependencies, serviceable)
{
Assemblies = assemblies;
Assemblies = assemblies.ToArray();
}
public IReadOnlyList<string> Assemblies { get; }

View file

@ -46,9 +46,10 @@ namespace Microsoft.Extensions.DependencyModel
name: identity.Item2,
version: identity.Item3,
hash: identity.Item4,
assemblies: packageGroup.Select(l => RuntimeAssembly.Create(l.AssetPath)).ToArray(),
subTargets: new RuntimeTarget[0],
dependencies: new Dependency[] { },
assemblies: packageGroup.Select(l => RuntimeAssembly.Create(l.AssetPath)),
resourceAssemblies: Enumerable.Empty<ResourceAssembly>(),
subTargets: Enumerable.Empty<RuntimeTarget>(),
dependencies: Enumerable.Empty<Dependency>(),
serviceable: false
));
}
@ -58,9 +59,9 @@ namespace Microsoft.Extensions.DependencyModel
runtime: string.Empty,
isPortable: false,
compilationOptions: CompilationOptions.Default,
compileLibraries: new CompilationLibrary[] {},
compileLibraries: Enumerable.Empty<CompilationLibrary>(),
runtimeLibraries: runtimeLibraries.ToArray(),
runtimeGraph: new KeyValuePair<string, string[]>[0]);
runtimeGraph: Enumerable.Empty<KeyValuePair<string, string[]>>());
}
private Tuple<string, string, string, string> PackageIdentity(DepsFileLine line)

View file

@ -173,7 +173,18 @@ namespace Microsoft.Extensions.DependencyModel
.Select(RuntimeAssembly.Create)
.ToArray();
return new RuntimeLibrary(stub.Type, name, version, stub.Hash, assemblies, runtimeTargets.ToArray(), dependencies, stub.Serviceable);
var resourceAssemblies = ReadResourceAssemblies((JObject)libraryObject[DependencyContextStrings.ResourceAssembliesPropertyName]);
return new RuntimeLibrary(
type: stub.Type,
name: name,
version: version,
hash: stub.Hash,
assemblies: assemblies,
resourceAssemblies: resourceAssemblies,
subTargets: runtimeTargets.ToArray(),
dependencies: dependencies,
serviceable: stub.Serviceable);
}
else
{
@ -182,6 +193,21 @@ namespace Microsoft.Extensions.DependencyModel
}
}
private IEnumerable<ResourceAssembly> ReadResourceAssemblies(JObject resourcesObject)
{
if (resourcesObject == null)
{
yield break;
}
foreach (var resourceProperty in resourcesObject)
{
yield return new ResourceAssembly(
locale: resourceProperty.Value[DependencyContextStrings.LocalePropertyName]?.Value<string>(),
path: resourceProperty.Key
);
}
}
private static IEnumerable<RuntimeTargetEntryStub> ReadRuntimeTargetEntries(JObject runtimeTargetObject)
{
if (runtimeTargetObject == null)

View file

@ -66,5 +66,9 @@ namespace Microsoft.Extensions.DependencyModel
internal const string RuntimeAssetType = "runtime";
internal const string NativeAssetType = "native";
internal const string ResourceAssembliesPropertyName = "resources";
internal const string LocalePropertyName = "locale";
}
}

View file

@ -140,49 +140,70 @@ namespace Microsoft.Extensions.DependencyModel
return targetObject;
}
private void AddCompilationAssemblies(JObject libraryObject, IEnumerable<string> compilationAssemblies)
{
libraryObject.Add(new JProperty(DependencyContextStrings.CompileTimeAssembliesKey,
WriteAssemblies(compilationAssemblies))
);
}
private void AddRuntimeAssemblies(JObject libraryObject, IEnumerable<RuntimeAssembly> runtimeAssemblies)
{
libraryObject.Add(new JProperty(DependencyContextStrings.RuntimeAssembliesKey,
WriteAssemblies(runtimeAssemblies.Select(a => a.Path)))
);
}
private void AddDependencies(JObject libraryObject, IEnumerable<Dependency> dependencies)
{
libraryObject.Add(
new JProperty(DependencyContextStrings.DependenciesPropertyName,
new JObject(
dependencies.Select(dependency => new JProperty(dependency.Name, dependency.Version))))
);
}
private void AddResourceAssemblies(JObject libraryObject, IEnumerable<ResourceAssembly> resourceAssemblies)
{
libraryObject.Add(DependencyContextStrings.ResourceAssembliesPropertyName,
new JObject(resourceAssemblies.Select(a =>
new JProperty(a.Path, new JObject(new JProperty(DependencyContextStrings.LocalePropertyName, a.Locale))))
)
);
}
private JObject WriteTargetLibrary(Library library)
{
string propertyName;
string[] assemblies;
var runtimeLibrary = library as RuntimeLibrary;
if (runtimeLibrary != null)
{
propertyName = DependencyContextStrings.RuntimeAssembliesKey;
assemblies = runtimeLibrary.Assemblies.Select(assembly => assembly.Path).ToArray();
var libraryObject = new JObject();
AddDependencies(libraryObject, runtimeLibrary.Dependencies);
AddRuntimeAssemblies(libraryObject, runtimeLibrary.Assemblies);
AddResourceAssemblies(libraryObject, runtimeLibrary.ResourceAssemblies);
return libraryObject;
}
else
{
var compilationLibrary = library as CompilationLibrary;
if (compilationLibrary != null)
{
propertyName = DependencyContextStrings.CompileTimeAssembliesKey;
assemblies = compilationLibrary.Assemblies.ToArray();
var libraryObject = new JObject();
AddDependencies(libraryObject, compilationLibrary.Dependencies);
AddCompilationAssemblies(libraryObject, compilationLibrary.Assemblies);
return libraryObject;
}
else
{
throw new NotSupportedException();
}
}
return new JObject(
new JProperty(DependencyContextStrings.DependenciesPropertyName, WriteDependencies(library.Dependencies)),
new JProperty(propertyName,
WriteAssemblies(assemblies))
);
}
private JObject WritePortableTargetLibrary(RuntimeLibrary runtimeLibrary, CompilationLibrary compilationLibrary)
{
var libraryObject = new JObject();
var dependencies = new HashSet<Dependency>();
var libraryObject = new JObject();
var dependencies = new HashSet<Dependency>();
if (runtimeLibrary != null)
{
libraryObject.Add(new JProperty(DependencyContextStrings.RuntimeAssembliesKey,
WriteAssemblies(runtimeLibrary.Assemblies.Select(a => a.Path)))
);
if (runtimeLibrary.RuntimeTargets.Any())
{
libraryObject.Add(new JProperty(
@ -190,21 +211,20 @@ namespace Microsoft.Extensions.DependencyModel
new JObject(runtimeLibrary.RuntimeTargets.SelectMany(WriteRuntimeTarget)))
);
}
AddResourceAssemblies(libraryObject, runtimeLibrary.ResourceAssemblies);
AddRuntimeAssemblies(libraryObject, runtimeLibrary.Assemblies);
dependencies.UnionWith(runtimeLibrary.Dependencies);
}
if (compilationLibrary != null)
{
libraryObject.Add(new JProperty(DependencyContextStrings.CompileTimeAssembliesKey,
WriteAssemblies(compilationLibrary.Assemblies))
);
AddCompilationAssemblies(libraryObject, compilationLibrary.Assemblies);
dependencies.UnionWith(compilationLibrary.Dependencies);
}
libraryObject.Add(
new JProperty(DependencyContextStrings.DependenciesPropertyName, WriteDependencies(dependencies)));
AddDependencies(libraryObject, dependencies);
return libraryObject;
}
@ -241,13 +261,6 @@ namespace Microsoft.Extensions.DependencyModel
return new JObject(assemblies.Select(assembly => new JProperty(assembly, new JObject())));
}
private JObject WriteDependencies(IEnumerable<Dependency> dependencies)
{
return new JObject(
dependencies.Select(dependency => new JProperty(dependency.Name, dependency.Version))
);
}
private JObject WriteLibraries(DependencyContext context)
{
var allLibraries =

View file

@ -1,19 +1,20 @@
// 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.Linq;
using System.Collections.Generic;
namespace Microsoft.Extensions.DependencyModel
{
public class Library
{
public Library(string type, string name, string version, string hash, Dependency[] dependencies, bool serviceable)
public Library(string type, string name, string version, string hash, IEnumerable<Dependency> dependencies, bool serviceable)
{
Type = type;
Name = name;
Version = version;
Hash = hash;
Dependencies = dependencies;
Dependencies = dependencies.ToArray();
Serviceable = serviceable;
}

View file

@ -0,0 +1,19 @@
// 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.Extensions.DependencyModel
{
public class ResourceAssembly
{
public ResourceAssembly(string path, string locale)
{
Locale = locale;
Path = path;
}
public string Locale { get; set; }
public string Path { get; set; }
}
}

View file

@ -13,18 +13,22 @@ namespace Microsoft.Extensions.DependencyModel
string name,
string version,
string hash,
RuntimeAssembly[] assemblies,
RuntimeTarget[] subTargets,
Dependency[] dependencies,
IEnumerable<RuntimeAssembly> assemblies,
IEnumerable<ResourceAssembly> resourceAssemblies,
IEnumerable<RuntimeTarget> subTargets,
IEnumerable<Dependency> dependencies,
bool serviceable)
: base(type, name, version, hash, dependencies, serviceable)
{
Assemblies = assemblies;
RuntimeTargets = subTargets;
Assemblies = assemblies.ToArray();
ResourceAssemblies = resourceAssemblies.ToArray();
RuntimeTargets = subTargets.ToArray();
}
public IReadOnlyList<RuntimeAssembly> Assemblies { get; }
public IReadOnlyList<ResourceAssembly> ResourceAssemblies { get; }
public IReadOnlyList<RuntimeTarget> RuntimeTargets { get; }
}
}

View file

@ -1,14 +1,15 @@
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.Extensions.DependencyModel
{
public class RuntimeTarget
{
public RuntimeTarget(string runtime, IReadOnlyList<RuntimeAssembly> assemblies, IReadOnlyList<string> nativeLibraries)
public RuntimeTarget(string runtime, IEnumerable<RuntimeAssembly> assemblies, IEnumerable<string> nativeLibraries)
{
Runtime = runtime;
Assemblies = assemblies;
NativeLibraries = nativeLibraries;
Assemblies = assemblies.ToArray();
NativeLibraries = nativeLibraries.ToArray();
}
public string Runtime { get; }

View file

@ -48,7 +48,7 @@ namespace Microsoft.DotNet.Tools.Build
{
CreateOutputDirectories();
return CompileDendencies(incremental) && CompileRootProject(incremental);
return CompileDependencies(incremental) && CompileRootProject(incremental);
}
private bool CompileRootProject(bool incremental)
@ -66,7 +66,7 @@ namespace Microsoft.DotNet.Tools.Build
return success;
}
private bool CompileDendencies(bool incremental)
private bool CompileDependencies(bool incremental)
{
if (_args.ShouldSkipDependencies)
{
@ -394,17 +394,9 @@ namespace Microsoft.DotNet.Tools.Build
var succeeded = compileResult == 0;
if (succeeded)
{
if (_rootProject.ProjectFile.HasRuntimeOutput(_args.ConfigValue))
{
MakeRunnable();
}
else if (!string.IsNullOrEmpty(_args.OutputValue))
{
var outputPaths = _rootProject.GetOutputPaths(_args.ConfigValue, _args.BuildBasePathValue, _args.OutputValue);
CopyCompilationOutput(outputPaths);
}
}
return succeeded;
}
@ -428,9 +420,22 @@ namespace Microsoft.DotNet.Tools.Build
private void MakeRunnable()
{
var runtimeContext = _rootProject.CreateRuntimeContext(_args.GetRuntimes());
if(_args.PortableMode)
{
// HACK: Force the use of the portable target
runtimeContext = _rootProject;
}
var outputPaths = runtimeContext.GetOutputPaths(_args.ConfigValue, _args.BuildBasePathValue, _args.OutputValue);
var libraryExporter = runtimeContext.CreateExporter(_args.ConfigValue, _args.BuildBasePathValue);
// If we're building for a specific RID, we need to copy the RID-less compilation output into
// the RID-specific output dir
if (!string.IsNullOrEmpty(runtimeContext.RuntimeIdentifier))
{
CopyCompilationOutput(outputPaths);
}
var executable = new Executable(runtimeContext, outputPaths, libraryExporter);
executable.MakeCompilationOutputRunnable();

View file

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Dnx.Runtime.Common.CommandLine;
@ -28,6 +29,7 @@ namespace Microsoft.DotNet.Tools.Compiler
private CommandOption _runtimeOption;
private CommandOption _versionSuffixOption;
private CommandOption _configurationOption;
private CommandOption _portableOption;
private CommandArgument _projectArgument;
private CommandOption _nativeOption;
private CommandOption _archOption;
@ -41,8 +43,8 @@ namespace Microsoft.DotNet.Tools.Compiler
// resolved values for the options and arguments
public string ProjectPathValue { get; set; }
public string BuildBasePathValue { get; set; }
public string OutputValue { get; set; }
public string RuntimeValue { get; set; }
public string OutputValue { get; set; }
public string VersionSuffixValue { get; set; }
public string ConfigValue { get; set; }
public bool IsNativeValue { get; set; }
@ -53,6 +55,7 @@ namespace Microsoft.DotNet.Tools.Compiler
public bool IsCppModeValue { get; set; }
public string AppDepSdkPathValue { get; set; }
public string CppCompilerFlagsValue { get; set; }
public bool PortableMode { get; set; }
// workaround: CommandLineApplication is internal therefore I cannot make _app protected so baseclasses can add their own params
private readonly Dictionary<string, CommandOption> baseClassOptions;
@ -77,12 +80,15 @@ namespace Microsoft.DotNet.Tools.Compiler
_outputOption = _app.Option("-o|--output <OUTPUT_DIR>", "Directory in which to place outputs", CommandOptionType.SingleValue);
_buildBasePath = _app.Option("-b|--build-base-path <OUTPUT_DIR>", "Directory in which to place temporary outputs", CommandOptionType.SingleValue);
_frameworkOption = _app.Option("-f|--framework <FRAMEWORK>", "Compile a specific framework", CommandOptionType.MultipleValue);
_frameworkOption = _app.Option("-f|--framework <FRAMEWORK>", "Compile a specific framework", CommandOptionType.SingleValue);
_runtimeOption = _app.Option("-r|--runtime <RUNTIME_IDENTIFIER>", "Produce runtime-specific assets for the specified runtime", CommandOptionType.SingleValue);
_configurationOption = _app.Option("-c|--configuration <CONFIGURATION>", "Configuration under which to build", CommandOptionType.SingleValue);
_runtimeOption = _app.Option("-r|--runtime <RUNTIME_IDENTIFIER>", "Target runtime to publish for", CommandOptionType.SingleValue);
_versionSuffixOption = _app.Option("--version-suffix <VERSION_SUFFIX>", "Defines what `*` should be replaced with in version field in project.json", CommandOptionType.SingleValue);
_projectArgument = _app.Argument("<PROJECT>", "The project to compile, defaults to the current directory. Can be a path to a project.json or a project directory");
// HACK: Allow us to treat a project as though it was portable by ignoring the runtime-specific targets. This is temporary until RID inference is removed from NuGet
_portableOption = _app.Option("--portable", "TEMPORARY: Enforces portable build/publish mode", CommandOptionType.NoValue);
// Native Args
_nativeOption = _app.Option("-n|--native", "Compiles source to native machine code.", CommandOptionType.NoValue);
_archOption = _app.Option("-a|--arch <ARCH>", "The architecture for which to compile. x64 only currently supported.", CommandOptionType.SingleValue);
@ -98,6 +104,12 @@ namespace Microsoft.DotNet.Tools.Compiler
{
_app.OnExecute(() =>
{
if (_outputOption.HasValue() && !_frameworkOption.HasValue())
{
Reporter.Error.WriteLine("When the '--output' option is provided, the '--framework' option must also be provided.");
return 1;
}
// Locate the project and get the name and full path
ProjectPathValue = _projectArgument.Value;
if (string.IsNullOrEmpty(ProjectPathValue))
@ -110,6 +122,7 @@ namespace Microsoft.DotNet.Tools.Compiler
ConfigValue = _configurationOption.Value() ?? Constants.DefaultConfiguration;
RuntimeValue = _runtimeOption.Value();
VersionSuffixValue = _versionSuffixOption.Value();
PortableMode = _portableOption.HasValue();
IsNativeValue = _nativeOption.HasValue();
ArchValue = _archOption.Value();
@ -120,8 +133,6 @@ namespace Microsoft.DotNet.Tools.Compiler
IsCppModeValue = _cppModeOption.HasValue();
CppCompilerFlagsValue = _cppCompilerFlagsOption.Value();
IEnumerable<ProjectContext> contexts;
// Set defaults based on the environment
var settings = ProjectReaderSettings.ReadFromEnvironment();
@ -130,29 +141,35 @@ namespace Microsoft.DotNet.Tools.Compiler
settings.VersionSuffix = VersionSuffixValue;
}
if (_frameworkOption.HasValue())
// Load the project file and construct all the targets
var targets = ProjectContext.CreateContextForEachFramework(ProjectPathValue, settings).ToList();
if (targets.Count == 0)
{
contexts = _frameworkOption.Values
.Select(f =>
{
return ProjectContext.CreateBuilder(ProjectPathValue, NuGetFramework.Parse(f))
.WithReaderSettings(settings)
.Build();
});
}
else
{
if (!string.IsNullOrEmpty(OutputValue))
{
throw new InvalidOperationException($"'{_frameworkOption.LongName}' is required when '{_outputOption.LongName}' is specified");
}
else
{
contexts = ProjectContext.CreateContextForEachFramework(ProjectPathValue, settings);
}
// Project is missing 'frameworks' section
Reporter.Error.WriteLine("Project does not have any frameworks listed in the 'frameworks' section.");
return 1;
}
var success = execute(contexts.ToList(), this);
// Filter the targets down based on the inputs
if (_frameworkOption.HasValue())
{
var fx = NuGetFramework.Parse(_frameworkOption.Value());
targets = targets.Where(t => fx.Equals(t.TargetFramework)).ToList();
if (targets.Count == 0)
{
// We filtered everything out
Reporter.Error.WriteLine($"Project does not support framework: {fx.DotNetFrameworkName}.");
return 1;
}
Debug.Assert(targets.Count == 1);
}
Debug.Assert(targets.All(t => string.IsNullOrEmpty(t.RuntimeIdentifier)));
var success = execute(targets, this);
return success ? 0 : 1;
});

View file

@ -9,7 +9,6 @@ using Microsoft.DotNet.Cli.Compiler.Common;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Compilation;
using Microsoft.DotNet.ProjectModel.Utilities;
using Microsoft.Extensions.DependencyModel;
namespace Microsoft.DotNet.Tools.Compiler
@ -161,6 +160,10 @@ namespace Microsoft.DotNet.Tools.Compiler
contextVariables.Add(
"compile:RuntimeOutputDir",
runtimeOutputPath.RuntimeOutputPath.TrimEnd('\\', '/'));
contextVariables.Add(
"compile:RuntimeIdentifier",
runtimeContext.RuntimeIdentifier);
}
_scriptRunner.RunScripts(context, ScriptNames.PreCompile, contextVariables);

View file

@ -180,7 +180,7 @@ namespace Microsoft.DotNet.Tools.Restore
Console.WriteLine($"Restoring Tool '{tooldep.Name}' for '{projectPath}' in '{tempPath}'");
File.WriteAllText(projectPath, GenerateProjectJsonContents(new[] {"netstandardapp1.5"}, tooldep));
return NuGet3.Restore(new [] { $"{projectPath}", "--runtime", $"{DefaultRid}"}.Concat(args), quiet) == 0;
return NuGet3.Restore(new[] { $"{projectPath}" }.Concat(args), quiet) == 0;
}
private static string GenerateProjectJsonContents(IEnumerable<string> frameworks, LibraryRange tooldep)
@ -194,9 +194,11 @@ namespace Microsoft.DotNet.Tools.Restore
foreach (var framework in frameworks)
{
var importsStatement = "\"imports\": [ \"dnxcore50\", \"portable-net452+win81\" ]";
sb.AppendLine($" \"{framework}\": {{ {importsStatement} }}");
}
sb.AppendLine(" },");
sb.AppendLine(" \"runtimes\": { ");
sb.AppendLine($" \"{DefaultRid}\": {{}}");
sb.AppendLine(" }");
sb.AppendLine("}");
var pjContents = sb.ToString();

View file

@ -102,12 +102,19 @@ namespace Microsoft.DotNet.Tools.Test
var commandArgs = new List<string> { GetAssemblyUnderTest(projectContext, configuration, outputPath) };
commandArgs.AddRange(app.RemainingArguments);
return Command.Create(
var commandFactory =
new ProjectDependenciesCommandFactory(
projectContext.TargetFramework,
configuration,
outputPath,
projectContext.ProjectDirectory);
return commandFactory.Create(
$"dotnet-{GetCommandName(testRunner)}",
commandArgs,
projectContext.TargetFramework,
configuration: configuration,
outputPath: outputPath)
configuration)
.ForwardStdErr()
.ForwardStdOut()
.Execute()
@ -160,7 +167,12 @@ namespace Microsoft.DotNet.Tools.Test
using (var dotnetTest = new DotnetTest(messages, assemblyUnderTest))
{
var commandFactory =
new FixedPathCommandFactory(projectContext.TargetFramework, configuration, outputPath);
new ProjectDependenciesCommandFactory(
projectContext.TargetFramework,
configuration,
outputPath,
projectContext.ProjectDirectory);
var testRunnerFactory = new TestRunnerFactory(GetCommandName(testRunner), commandFactory);
dotnetTest

View file

@ -24,7 +24,7 @@
"Microsoft.CodeAnalysis.CSharp": "1.3.0-beta1-20160225-02",
"Microsoft.DiaSymReader.Native": "1.3.3",
"NuGet.CommandLine.XPlat": "3.4.0-beta-632",
"NuGet.CommandLine.XPlat": "3.4.0-rtm-0724",
"System.CommandLine": "0.1.0-e160119-1",
"Microsoft.DotNet.ProjectModel": "1.0.0-*",

View file

@ -27,5 +27,5 @@
"testRunner": "xunit",
"scripts": { "precompile": "dotnet build ../ArgumentsReflector/project.json --framework netstandardapp1.5 --output %compile:RuntimeOutputDir%" }
"scripts": { "precompile": "dotnet build ../ArgumentsReflector/project.json --framework netstandardapp1.5 --runtime %compile:RuntimeIdentifier% --output %compile:RuntimeOutputDir%" }
}

View file

@ -83,15 +83,8 @@ namespace Microsoft.DotNet.Tests.EndToEnd
[Fact]
public void TestDotnetBuildNativeRyuJit()
{
if(IsCentOS())
if(!IsNativeCompilationSupported())
{
Console.WriteLine("Skipping native compilation tests on CentOS - https://github.com/dotnet/cli/issues/453");
return;
}
if (IsWinX86())
{
Console.WriteLine("Skipping native compilation tests on Windows x86 - https://github.com/dotnet/cli/issues/1550");
return;
}
@ -105,15 +98,8 @@ namespace Microsoft.DotNet.Tests.EndToEnd
[Fact]
public void TestDotnetBuildNativeCpp()
{
if(IsCentOS())
if(!IsNativeCompilationSupported())
{
Console.WriteLine("Skipping native compilation tests on CentOS - https://github.com/dotnet/cli/issues/453");
return;
}
if (IsWinX86())
{
Console.WriteLine("Skipping native compilation tests on Windows x86 - https://github.com/dotnet/cli/issues/1550");
return;
}
@ -127,15 +113,8 @@ namespace Microsoft.DotNet.Tests.EndToEnd
[Fact]
public void TestDotnetCompileNativeCppIncremental()
{
if (IsCentOS())
if(!IsNativeCompilationSupported())
{
Console.WriteLine("Skipping native compilation tests on CentOS - https://github.com/dotnet/cli/issues/453");
return;
}
if (IsWinX86())
{
Console.WriteLine("Skipping native compilation tests on Windows x86 - https://github.com/dotnet/cli/issues/1550");
return;
}
@ -236,25 +215,26 @@ namespace Microsoft.DotNet.Tests.EndToEnd
Directory.SetCurrentDirectory(currentDirectory);
}
private bool IsCentOS()
private bool IsNativeCompilationSupported()
{
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
bool isSupported = true;
var platform = PlatformServices.Default.Runtime.OperatingSystem.ToLower();
switch (platform)
{
const string OSIDFILE = "/etc/os-release";
if(File.Exists(OSIDFILE))
{
return File.ReadAllText(OSIDFILE).ToLower().Contains("centos");
}
case "centos":
case "rhel":
Console.WriteLine("Skipping native compilation tests on CentOS/RHEL - https://github.com/dotnet/cli/issues/453");
isSupported = false;
break;
case "windows":
Console.WriteLine("Skipping native compilation tests on Windows x86 - https://github.com/dotnet/cli/issues/1550");
isSupported = RuntimeInformation.ProcessArchitecture != Architecture.X86;
break;
default:
break;
}
return false;
}
private bool IsWinX86()
{
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
RuntimeInformation.ProcessArchitecture == Architecture.X86;
return isSupported;
}
private static DateTime GetLastWriteTimeUtcOfDirectoryFiles(string outputDirectory)

View file

@ -14,7 +14,7 @@
"xunit": "2.1.0",
"xunit.netcore.extensions": "1.0.0-prerelease-*",
"dotnet-test-xunit": "1.0.0-dev-79755-47"
"dotnet-test-xunit": "1.0.0-dev-91790-12"
},
"frameworks": {

View file

@ -42,7 +42,6 @@ pushd "$Stage2Dir\bin"
try {
.\dotnet restore `
--runtime win-anycpu `
$testDir `
-f https://www.myget.org/F/dotnet-buildtools/api/v3/index.json | Out-Host
@ -53,7 +52,6 @@ try {
.\dotnet publish `
--framework net46 `
--runtime win-anycpu `
--output $testBin `
$testDir | Out-Host

View 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;
using System.IO;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public static class CommandResolverTestUtils
{
public static string CreateNonRunnableTestCommand(string directory, string filename, string extension=".dll")
{
Directory.CreateDirectory(directory);
var filePath = Path.Combine(directory, filename + extension);
File.WriteAllText(filePath, "test command that does nothing.");
return filePath;
}
public static IEnvironmentProvider SetupEnvironmentProviderWhichFindsExtensions(params string[] extensions)
{
return new EnvironmentProvider(extensions);
}
}
}

View file

@ -0,0 +1,86 @@
// 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.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenACompositeCommandResolver
{
[Fact]
public void It_iterates_through_all_added_resolvers_in_order_when_they_return_null()
{
var compositeCommandResolver = new CompositeCommandResolver();
var resolverCalls = new List<int>();
var mockResolver1 = new Mock<ICommandResolver>();
mockResolver1.Setup(r => r
.Resolve(It.IsAny<CommandResolverArguments>()))
.Returns(default(CommandSpec))
.Callback(() => resolverCalls.Add(1));
var mockResolver2 = new Mock<ICommandResolver>();
mockResolver2.Setup(r => r
.Resolve(It.IsAny<CommandResolverArguments>()))
.Returns(default(CommandSpec))
.Callback(() => resolverCalls.Add(2));
compositeCommandResolver.AddCommandResolver(mockResolver1.Object);
compositeCommandResolver.AddCommandResolver(mockResolver2.Object);
compositeCommandResolver.Resolve(default(CommandResolverArguments));
resolverCalls.Should()
.HaveCount(2)
.And
.ContainInOrder(new [] {1, 2});
}
[Fact]
public void It_stops_iterating_through_added_resolvers_when_one_returns_nonnull()
{
var compositeCommandResolver = new CompositeCommandResolver();
var resolverCalls = new List<int>();
var mockResolver1 = new Mock<ICommandResolver>();
mockResolver1.Setup(r => r
.Resolve(It.IsAny<CommandResolverArguments>()))
.Returns(new CommandSpec(null, null, default(CommandResolutionStrategy)))
.Callback(() => resolverCalls.Add(1));
var mockResolver2 = new Mock<ICommandResolver>();
mockResolver2.Setup(r => r
.Resolve(It.IsAny<CommandResolverArguments>()))
.Returns(default(CommandSpec))
.Callback(() => resolverCalls.Add(2));
compositeCommandResolver.AddCommandResolver(mockResolver1.Object);
compositeCommandResolver.AddCommandResolver(mockResolver2.Object);
compositeCommandResolver.Resolve(default(CommandResolverArguments));
resolverCalls.Should()
.HaveCount(1)
.And
.ContainInOrder(new [] {1});
}
}
}

View file

@ -0,0 +1,44 @@
// 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.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenADefaultCommandResolver
{
[Fact]
public void It_contains_resolvers_in_the_right_order()
{
var defaultCommandResolver = DefaultCommandResolverPolicy.Create();
var resolvers = defaultCommandResolver.OrderedCommandResolvers;
resolvers.Should().HaveCount(4);
resolvers.Select(r => r.GetType())
.Should()
.ContainInOrder(
new []{
typeof(RootedCommandResolver),
typeof(ProjectToolsCommandResolver),
typeof(AppBaseCommandResolver),
typeof(PathCommandResolver)
});
}
}
}

View file

@ -0,0 +1,218 @@
// 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.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAPathCommandResolver
{
private static readonly string s_testDirectory = Path.Combine(AppContext.BaseDirectory, "pathTestDirectory");
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var pathCommandResolver = SetupPlatformPathCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_in_PATH()
{
var emptyPathEnvironmentMock = new Mock<IEnvironmentProvider>();
emptyPathEnvironmentMock.Setup(e => e
.GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns((string)null);
var pathCommandResolver = SetupPlatformPathCommandResolver(emptyPathEnvironmentMock.Object);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_in_PATH()
{
var testCommandPath = CommandResolverTestUtils.CreateNonRunnableTestCommand(
s_testDirectory,
"pathtestcommand1",
".exe");
var staticPathEnvironmentMock = new Mock<IEnvironmentProvider>();
staticPathEnvironmentMock.Setup(e => e
.GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(testCommandPath);
var pathCommandResolver = SetupPlatformPathCommandResolver(staticPathEnvironmentMock.Object, forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = Path.GetFileNameWithoutExtension(testCommandPath),
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be(Path.GetFileNameWithoutExtension(testCommandPath));
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var testCommandPath = CommandResolverTestUtils.CreateNonRunnableTestCommand(
s_testDirectory,
"pathtestcommand1",
".exe");
var staticPathEnvironmentMock = new Mock<IEnvironmentProvider>();
staticPathEnvironmentMock.Setup(e => e
.GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(testCommandPath);
var pathCommandResolver = SetupPlatformPathCommandResolver(staticPathEnvironmentMock.Object, forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = Path.GetFileNameWithoutExtension(testCommandPath),
CommandArguments = new [] {"arg with space"}
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Be("\"arg with space\"");
}
[Fact]
public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var testCommandPath = CommandResolverTestUtils.CreateNonRunnableTestCommand(
s_testDirectory,
"pathtestcommand1",
".exe");
var staticPathEnvironmentMock = new Mock<IEnvironmentProvider>();
staticPathEnvironmentMock.Setup(e => e
.GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
.Returns(testCommandPath);
var pathCommandResolver = SetupPlatformPathCommandResolver(staticPathEnvironmentMock.Object, forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = Path.GetFileNameWithoutExtension(testCommandPath),
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Be(string.Empty);
}
[Fact]
public void It_prefers_EXE_over_CMD_when_two_command_candidates_exist_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = new EnvironmentProvider(new [] {".exe", ".cmd"}, new[] { s_testDirectory });
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testDirectory, "extensionPreferenceCommand", ".exe");
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testDirectory, "extensionPreferenceCommand", ".cmd");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "extensionPreferenceCommand",
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("extensionPreferenceCommand.exe");
}
[Fact]
public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = new EnvironmentProvider(new [] {".cmd"}, new[] { s_testDirectory });
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
var testCommandPath =
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testDirectory, "cmdWrapCommand", ".cmd");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "cmdWrapCommand",
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("cmd.exe");
result.Args.Should().Contain(testCommandPath);
}
private PathCommandResolver SetupPlatformPathCommandResolver(
IEnvironmentProvider environment = null,
bool forceGeneric = false)
{
environment = environment ?? new EnvironmentProvider();
IPlatformCommandSpecFactory platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows
&& !forceGeneric)
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
}
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
return pathCommandResolver;
}
}
}

View file

@ -0,0 +1,221 @@
// 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.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAProjectDependenciesCommandResolver
{
private static readonly string s_liveProjectDirectory =
Path.Combine(AppContext.BaseDirectory, "TestAssets/TestProjects/AppWithDirectDependency");
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = new string[] {""},
ProjectDirectory = "/some/directory",
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_ProjectDirectory_is_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = null,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_Framework_is_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = null
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_Configuration_is_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = s_liveProjectDirectory,
Configuration = null,
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_in_ProjectDependencies()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CoreHost_as_FileName_and_CommandName_in_Args_when_CommandName_exists_in_ProjectDependencies()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be("corehost");
result.Args.Should().Contain(commandResolverArguments.CommandName);
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = new [] { "arg with space"},
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("\"arg with space\"");
}
[Fact]
public void It_passes_depsfile_arg_to_corehost_when_returning_a_commandspec()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("--depsfile");
}
[Fact]
public void It_returns_a_CommandSpec_with_CommandName_in_Args_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var projectDependenciesCommandResolver = SetupProjectDependenciesCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory,
Configuration = "Debug",
Framework = FrameworkConstants.CommonFrameworks.NetStandardApp15
};
var result = projectDependenciesCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("dotnet-hello");
}
private ProjectDependenciesCommandResolver SetupProjectDependenciesCommandResolver(
IEnvironmentProvider environment = null,
IPackagedCommandSpecFactory packagedCommandSpecFactory = null)
{
environment = environment ?? new EnvironmentProvider();
packagedCommandSpecFactory = packagedCommandSpecFactory ?? new PackagedCommandSpecFactory();
var projectDependenciesCommandResolver = new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory);
return projectDependenciesCommandResolver;
}
}
}

View file

@ -0,0 +1,264 @@
// 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.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAProjectPathCommandResolver
{
private static readonly string s_testProjectDirectory = Path.Combine(AppContext.BaseDirectory, "testprojectdirectory");
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = new string[] {""},
ProjectDirectory = "/some/directory"
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_ProjectDirectory_is_null()
{
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = null
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_in_ProjectDirectory()
{
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null,
ProjectDirectory = s_testProjectDirectory
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_exists_in_a_subdirectory_of_ProjectDirectory()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
var testDir = Path.Combine(s_testProjectDirectory, "projectpathtestsubdir");
CommandResolverTestUtils.CreateNonRunnableTestCommand(testDir, "projectpathtestsubdircommand", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "projectpathtestsubdircommand",
CommandArguments = null,
ProjectDirectory = s_testProjectDirectory
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_in_ProjectDirectory()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "projectpathtestcommand1",
CommandArguments = null,
ProjectDirectory = s_testProjectDirectory
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be("projectpathtestcommand1");
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "projectpathtestcommand1",
CommandArguments = new [] { "arg with space"},
ProjectDirectory = s_testProjectDirectory
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Be("\"arg with space\"");
}
[Fact]
public void It_resolves_commands_with_extensions_defined_in_InferredExtensions()
{
var extensions = new string[] {".sh", ".cmd", ".foo", ".exe"};
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(forceGeneric: true);
foreach (var extension in extensions)
{
var extensionTestDir = Path.Combine(s_testProjectDirectory, "testext" + extension);
CommandResolverTestUtils.CreateNonRunnableTestCommand(extensionTestDir, "projectpathexttest", extension);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "projectpathexttest",
CommandArguments = null,
ProjectDirectory = extensionTestDir,
InferredExtensions = extensions
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFileName = Path.GetFileName(result.Path);
commandFileName.Should().Be("projectpathexttest" + extension);
}
}
[Fact]
public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var projectPathCommandResolver = SetupPlatformProjectPathCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "projectpathtestcommand1",
CommandArguments = null,
ProjectDirectory = s_testProjectDirectory
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Be(string.Empty);
}
[Fact]
public void It_prefers_EXE_over_CMD_when_two_command_candidates_exist_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var projectPathCommandResolver = new ProjectPathCommandResolver(environment, platformCommandSpecFactory);
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".exe");
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "projectpathtestcommand1", ".cmd");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "projectpathtestcommand1",
CommandArguments = null,
ProjectDirectory = s_testProjectDirectory
};
var result = projectPathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("projectpathtestcommand1.exe");
}
public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = new EnvironmentProvider(new [] {".cmd"});
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
var testCommandPath =
CommandResolverTestUtils.CreateNonRunnableTestCommand(s_testProjectDirectory, "cmdWrapCommand", ".cmd");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "cmdWrapCommand",
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("cmd.exe");
result.Args.Should().Contain(testCommandPath);
}
private ProjectPathCommandResolver SetupPlatformProjectPathCommandResolver(
IEnvironmentProvider environment = null,
bool forceGeneric = false)
{
environment = environment ?? new EnvironmentProvider();
IPlatformCommandSpecFactory platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows
&& !forceGeneric)
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
}
var projectPathCommandResolver = new ProjectPathCommandResolver(environment, platformCommandSpecFactory);
return projectPathCommandResolver;
}
}
}

View file

@ -0,0 +1,152 @@
// 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.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAProjectToolsCommandResolver
{
private static readonly string s_liveProjectDirectory =
Path.Combine(AppContext.BaseDirectory, "TestAssets/TestProjects/AppWithToolDependency");
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = new string[] {""},
ProjectDirectory = "/some/directory"
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_ProjectDirectory_is_null()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "command",
CommandArguments = new string[] {""},
ProjectDirectory = null
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_in_ProjectTools()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CoreHost_as_FileName_and_CommandName_in_Args_when_CommandName_exists_in_ProjectTools()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be("corehost");
result.Args.Should().Contain(commandResolverArguments.CommandName);
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = new [] { "arg with space"},
ProjectDirectory = s_liveProjectDirectory
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Contain("\"arg with space\"");
}
[Fact]
public void It_returns_a_CommandSpec_with_Args_as_CommandPath_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var projectToolsCommandResolver = SetupProjectToolsCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "dotnet-hello",
CommandArguments = null,
ProjectDirectory = s_liveProjectDirectory
};
var result = projectToolsCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandPath = result.Args.Trim('"');
commandPath.Should().Contain("dotnet-hello");
File.Exists(commandPath).Should().BeTrue();
}
private ProjectToolsCommandResolver SetupProjectToolsCommandResolver(
IPackagedCommandSpecFactory packagedCommandSpecFactory = null)
{
packagedCommandSpecFactory = packagedCommandSpecFactory ?? new PackagedCommandSpecFactory();
var projectToolsCommandResolver = new ProjectToolsCommandResolver(packagedCommandSpecFactory);
return projectToolsCommandResolver;
}
}
}

View file

@ -0,0 +1,111 @@
// 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.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenARootedCommandResolver
{
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = null
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_is_not_rooted()
{
var rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "some/relative/path",
CommandArguments = null
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CommandName_as_Path_when_CommandName_is_rooted()
{
var rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "/some/rooted/path",
CommandArguments = null
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Path.Should().Be(commandResolverArguments.CommandName);
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "/some/rooted/path",
CommandArguments = new [] { "arg with space"}
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Path.Should().Be(commandResolverArguments.CommandName);
result.Args.Should().Be("\"arg with space\"");
}
[Fact]
public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var rootedCommandResolver = new RootedCommandResolver();
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "/some/rooted/path",
CommandArguments = null
};
var result = rootedCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Path.Should().Be(commandResolverArguments.CommandName);
result.Args.Should().Be(string.Empty);
}
}
}

View file

@ -0,0 +1,44 @@
// 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 System.Runtime.InteropServices;
using System.Text;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAScriptCommandResolver
{
[Fact]
public void It_contains_resolvers_in_the_right_order()
{
var scriptCommandResolver = ScriptCommandResolverPolicy.Create();
var resolvers = scriptCommandResolver.OrderedCommandResolvers;
resolvers.Should().HaveCount(4);
resolvers.Select(r => r.GetType())
.Should()
.ContainInOrder(
new []{
typeof(RootedCommandResolver),
typeof(ProjectPathCommandResolver),
typeof(AppBaseCommandResolver),
typeof(PathCommandResolver)
});
}
}
}

View file

@ -0,0 +1,209 @@
// 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.Runtime.InteropServices;
using System.Text;
using System.Linq;
using Xunit;
using Moq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.PlatformAbstractions;
using System.Threading;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Cli.Utils.Tests
{
public class GivenAnAppBaseCommandResolver
{
[Fact]
public void It_returns_null_when_CommandName_is_null()
{
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = null,
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_null_when_CommandName_does_not_exist_applocal()
{
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(forceGeneric: true);
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "nonexistent-command",
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_returns_a_CommandSpec_with_CommandName_as_FileName_when_CommandName_exists_applocal()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestcommand1",
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileNameWithoutExtension(result.Path);
commandFile.Should().Be("appbasetestcommand1");
}
[Fact]
public void It_returns_null_when_CommandName_exists_applocal_in_a_subdirectory()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment, forceGeneric: true);
var testDir = Path.Combine(AppContext.BaseDirectory, "appbasetestsubdir");
CommandResolverTestUtils.CreateNonRunnableTestCommand(testDir, "appbasetestsubdircommand", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestsubdircommand",
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().BeNull();
}
[Fact]
public void It_escapes_CommandArguments_when_returning_a_CommandSpec()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestcommand1",
CommandArguments = new [] { "arg with space"}
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Be("\"arg with space\"");
}
[Fact]
public void It_returns_a_CommandSpec_with_Args_as_stringEmpty_when_returning_a_CommandSpec_and_CommandArguments_are_null()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var appBaseCommandResolver = SetupPlatformAppBaseCommandResolver(environment, forceGeneric: true);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestcommand1",
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
result.Args.Should().Be(string.Empty);
}
[Fact]
public void It_prefers_EXE_over_CMD_when_two_command_candidates_exist_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = CommandResolverTestUtils.SetupEnvironmentProviderWhichFindsExtensions(".exe");
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var appBaseCommandResolver = new AppBaseCommandResolver(environment, platformCommandSpecFactory);
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".exe");
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "appbasetestcommand1", ".cmd");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "appbasetestcommand1",
CommandArguments = null
};
var result = appBaseCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("appbasetestcommand1.exe");
}
public void It_wraps_command_with_CMD_EXE_when_command_has_CMD_Extension_and_using_WindowsExePreferredCommandSpecFactory()
{
var environment = new EnvironmentProvider(new [] {".cmd"});
var platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
var pathCommandResolver = new PathCommandResolver(environment, platformCommandSpecFactory);
var testCommandPath =
CommandResolverTestUtils.CreateNonRunnableTestCommand(AppContext.BaseDirectory, "cmdWrapCommand", ".cmd");
var commandResolverArguments = new CommandResolverArguments()
{
CommandName = "cmdWrapCommand",
CommandArguments = null
};
var result = pathCommandResolver.Resolve(commandResolverArguments);
result.Should().NotBeNull();
var commandFile = Path.GetFileName(result.Path);
commandFile.Should().Be("cmd.exe");
result.Args.Should().Contain(testCommandPath);
}
private AppBaseCommandResolver SetupPlatformAppBaseCommandResolver(
IEnvironmentProvider environment = null,
bool forceGeneric = false)
{
environment = environment ?? new EnvironmentProvider();
IPlatformCommandSpecFactory platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows
&& !forceGeneric)
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
}
var appBaseCommandResolver = new AppBaseCommandResolver(environment, platformCommandSpecFactory);
return appBaseCommandResolver;
}
}
}

View file

@ -13,6 +13,7 @@
"Microsoft.DotNet.Tools.Tests.Utilities": { "target": "project" },
"moq.netcore": "4.4.0-beta8",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-dev-79755-47"
},
@ -27,7 +28,11 @@
},
"content": [
"../../TestAssets/TestProjects/OutputStandardOutputAndError/*"
"../../TestAssets/TestProjects/OutputStandardOutputAndError/*",
"../../TestAssets/TestProjects/TestAppWithArgs/*",
"../../TestAssets/TestProjects/AppWithDirectAndToolDependency/**/*",
"../../TestAssets/TestProjects/AppWithDirectDependency/**/*",
"../../TestAssets/TestProjects/AppWithToolDependency/**/*"
],
"testRunner": "xunit"

View file

@ -1,3 +1,6 @@
// 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;
@ -15,36 +18,6 @@ namespace Microsoft.DotNet.ProjectModel.Tests
{
private const string PackagePath = "PackagePath";
private LibraryExport ExportSingle(LibraryDescription description = null)
{
var rootProject = new Project()
{
Name = "RootProject",
CompilerName = "csc"
};
var rootProjectDescription = new ProjectDescription(
new LibraryRange(),
rootProject,
new LibraryRange[] { },
new TargetFrameworkInformation(),
true);
if (description == null)
{
description = rootProjectDescription;
}
else
{
description.Parents.Add(rootProjectDescription);
}
var libraryManager = new LibraryManager(new[] { description }, new DiagnosticMessage[] { }, "");
var allExports = new LibraryExporter(rootProjectDescription, libraryManager, "config", "runtime", "basepath", "solutionroot").GetAllExports();
var export = allExports.Single();
return export;
}
private PackageDescription CreateDescription(LockFileTargetLibrary target = null, LockFilePackageLibrary package = null)
{
return new PackageDescription(PackagePath,
@ -54,7 +27,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
}
[Fact]
private void ExportsPackageNativeLibraries()
public void ExportsPackageNativeLibraries()
{
var description = CreateDescription(
new LockFileTargetLibrary()
@ -76,7 +49,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
}
[Fact]
private void ExportsPackageCompilationAssebmlies()
public void ExportsPackageCompilationAssebmlies()
{
var description = CreateDescription(
new LockFileTargetLibrary()
@ -98,7 +71,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
}
[Fact]
private void ExportsPackageRuntimeAssebmlies()
public void ExportsPackageRuntimeAssebmlies()
{
var description = CreateDescription(
new LockFileTargetLibrary()
@ -120,7 +93,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
}
[Fact]
private void ExportsSources()
public void ExportsSources()
{
var description = CreateDescription(
package: new LockFilePackageLibrary()
@ -142,7 +115,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
}
[Fact]
private void ExportsCopyToOutputContentFiles()
public void ExportsCopyToOutputContentFiles()
{
var description = CreateDescription(
new LockFileTargetLibrary()
@ -170,7 +143,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
[Fact]
private void ExportsResourceContentFiles()
public void ExportsResourceContentFiles()
{
var description = CreateDescription(
new LockFileTargetLibrary()
@ -196,7 +169,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
}
[Fact]
private void ExportsCompileContentFiles()
public void ExportsCompileContentFiles()
{
var description = CreateDescription(
new LockFileTargetLibrary()
@ -224,7 +197,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
[Fact]
private void SelectsContentFilesOfProjectCodeLanguage()
public void SelectsContentFilesOfProjectCodeLanguage()
{
var description = CreateDescription(
new LockFileTargetLibrary()
@ -264,7 +237,7 @@ namespace Microsoft.DotNet.ProjectModel.Tests
}
[Fact]
private void SelectsContentFilesWithNoLanguageIfProjectLanguageNotMathed()
public void SelectsContentFilesWithNoLanguageIfProjectLanguageNotMathed()
{
var description = CreateDescription(
new LockFileTargetLibrary()
@ -295,5 +268,84 @@ namespace Microsoft.DotNet.ProjectModel.Tests
libraryAsset.RelativePath.Should().Be(Path.Combine("content", "file.any"));
libraryAsset.ResolvedPath.Should().Be(Path.Combine(PackagePath, "content", "file.any"));
}
[Fact]
public void ExportsRuntimeTargets()
{
var win8Native = Path.Combine("native", "win8-x64", "Native.dll");
var win8Runtime = Path.Combine("runtime", "win8-x64", "Runtime.dll");
var linuxNative = Path.Combine("native", "linux", "Native.dll");
var description = CreateDescription(
new LockFileTargetLibrary()
{
RuntimeTargets = new List<LockFileRuntimeTarget>()
{
new LockFileRuntimeTarget(
path: win8Native,
runtime: "win8-x64",
assetType: "native"
),
new LockFileRuntimeTarget(
path: win8Runtime,
runtime: "win8-x64",
assetType: "runtime"
),
new LockFileRuntimeTarget(
path: linuxNative,
runtime: "linux",
assetType: "native"
),
}
});
var result = ExportSingle(description);
result.RuntimeTargets.Should().HaveCount(2);
var runtimeTarget = result.RuntimeTargets.Should().Contain(t => t.Runtime == "win8-x64").Subject;
var runtime = runtimeTarget.RuntimeAssemblies.Single();
runtime.RelativePath.Should().Be(win8Runtime);
runtime.ResolvedPath.Should().Be(Path.Combine(PackagePath, win8Runtime));
var native = runtimeTarget.NativeLibraries.Single();
native.RelativePath.Should().Be(win8Native);
native.ResolvedPath.Should().Be(Path.Combine(PackagePath, win8Native));
runtimeTarget = result.RuntimeTargets.Should().Contain(t => t.Runtime == "linux").Subject;
native = runtimeTarget.NativeLibraries.Single();
native.RelativePath.Should().Be(linuxNative);
native.ResolvedPath.Should().Be(Path.Combine(PackagePath, linuxNative));
}
private LibraryExport ExportSingle(LibraryDescription description = null)
{
var rootProject = new Project()
{
Name = "RootProject",
CompilerName = "csc"
};
var rootProjectDescription = new ProjectDescription(
new LibraryRange(),
rootProject,
new LibraryRange[] { },
new TargetFrameworkInformation(),
true);
if (description == null)
{
description = rootProjectDescription;
}
else
{
description.Parents.Add(rootProjectDescription);
}
var libraryManager = new LibraryManager(new[] { description }, new DiagnosticMessage[] { }, "");
var allExports = new LibraryExporter(rootProjectDescription, libraryManager, "config", "runtime", "basepath", "solutionroot").GetAllExports();
var export = allExports.Single();
return export;
}
}
}

View file

@ -6,7 +6,7 @@
"Microsoft.DotNet.ProjectModel": { "target": "project" },
"Microsoft.DotNet.Tools.Tests.Utilities": { "target": "project" },
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-dev-79755-47"
"dotnet-test-xunit": "1.0.0-dev-91790-12"
},
"frameworks": {
"netstandardapp1.5": {

View file

@ -32,6 +32,13 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> NotPass()
{
Execute.Assertion.ForCondition(_commandResult.ExitCode != 0)
.FailWith(AppendDiagnosticsTo($"Expected command to fail but it did not."));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> Fail()
{
Execute.Assertion.ForCondition(_commandResult.ExitCode != 0)
@ -53,7 +60,14 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> StdOutMatchPattern(string pattern, RegexOptions options = RegexOptions.None)
public AndConstraint<CommandResultAssertions> HaveStdOutContaining(string pattern)
{
Execute.Assertion.ForCondition(_commandResult.StdOut.Contains(pattern))
.FailWith(AppendDiagnosticsTo($"The command output did not contain expected result: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> HaveStdOutMatching(string pattern, RegexOptions options = RegexOptions.None)
{
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdOut, pattern, options).Success)
.FailWith(AppendDiagnosticsTo($"Matching the command output failed. Pattern: {pattern}{Environment.NewLine}"));
@ -67,6 +81,20 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> HaveStdErrContaining(string pattern)
{
Execute.Assertion.ForCondition(_commandResult.StdErr.Contains(pattern))
.FailWith(AppendDiagnosticsTo($"The command error output did not contain expected result: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> HaveStdErrMatching(string pattern, RegexOptions options = RegexOptions.None)
{
Execute.Assertion.ForCondition(Regex.Match(_commandResult.StdErr, pattern, options).Success)
.FailWith(AppendDiagnosticsTo($"Matching the command error output failed. Pattern: {pattern}{Environment.NewLine}"));
return new AndConstraint<CommandResultAssertions>(this);
}
public AndConstraint<CommandResultAssertions> NotHaveStdOut()
{
Execute.Assertion.ForCondition(string.IsNullOrEmpty(_commandResult.StdOut))

View file

@ -11,23 +11,25 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
public sealed class BuildCommand : TestCommand
{
private Project _project;
private string _projectPath;
private string _outputDirectory;
private string _buidBasePathDirectory;
private string _configuration;
private string _framework;
private string _versionSuffix;
private bool _noHost;
private bool _native;
private string _architecture;
private string _ilcArgs;
private string _ilcPath;
private string _appDepSDKPath;
private bool _nativeCppMode;
private string _cppCompilerFlags;
private bool _buildProfile;
private bool _noIncremental;
private bool _noDependencies;
private readonly string _projectPath;
private readonly string _outputDirectory;
private readonly string _buidBasePathDirectory;
private readonly string _configuration;
private readonly string _framework;
private readonly string _versionSuffix;
private readonly bool _noHost;
private readonly bool _native;
private readonly string _architecture;
private readonly string _ilcArgs;
private readonly string _ilcPath;
private readonly string _appDepSDKPath;
private readonly bool _nativeCppMode;
private readonly string _cppCompilerFlags;
private readonly bool _buildProfile;
private readonly bool _noIncremental;
private readonly bool _noDependencies;
private readonly string _runtime;
private readonly bool _forcePortable;
private string OutputOption
{
@ -39,6 +41,16 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
}
}
private string ForcePortableOption
{
get
{
return _forcePortable ?
"--portable" :
string.Empty;
}
}
private string BuildBasePathOption
{
get
@ -98,6 +110,16 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
}
}
private string RuntimeOption
{
get
{
return _runtime == string.Empty ?
"" :
$"--runtime {_runtime}";
}
}
private string ArchitectureOption
{
get
@ -194,6 +216,7 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
string buidBasePath="",
string configuration="",
string framework="",
string runtime="",
string versionSuffix="",
bool noHost=false,
bool native=false,
@ -205,7 +228,8 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
string cppCompilerFlags="",
bool buildProfile=true,
bool noIncremental=false,
bool noDependencies=false
bool noDependencies=false,
bool forcePortable=false
)
: base("dotnet")
{
@ -217,6 +241,7 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
_configuration = configuration;
_versionSuffix = versionSuffix;
_framework = framework;
_runtime = runtime;
_noHost = noHost;
_native = native;
_architecture = architecture;
@ -228,6 +253,7 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
_buildProfile = buildProfile;
_noIncremental = noIncremental;
_noDependencies = noDependencies;
_forcePortable = forcePortable;
}
public override CommandResult Execute(string args = "")
@ -251,7 +277,7 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
private string BuildArgs()
{
return $"{BuildProfile} {NoDependencies} {NoIncremental} \"{_projectPath}\" {OutputOption} {BuildBasePathOption} {ConfigurationOption} {FrameworkOption} {VersionSuffixOption} {NoHostOption} {NativeOption} {ArchitectureOption} {IlcArgsOption} {IlcPathOption} {AppDepSDKPathOption} {NativeCppModeOption} {CppCompilerFlagsOption}";
return $"{BuildProfile} {ForcePortableOption} {NoDependencies} {NoIncremental} \"{_projectPath}\" {OutputOption} {BuildBasePathOption} {ConfigurationOption} {FrameworkOption} {RuntimeOption} {VersionSuffixOption} {NoHostOption} {NativeOption} {ArchitectureOption} {IlcArgsOption} {IlcPathOption} {AppDepSDKPathOption} {NativeCppModeOption} {CppCompilerFlagsOption}";
}
}
}

View file

@ -10,7 +10,7 @@
"System.IO.Compression": "4.1.0-rc2-23901",
"FluentAssertions": "4.0.0",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-dev-79755-47",
"dotnet-test-xunit": "1.0.0-dev-91790-12",
"Microsoft.DotNet.TestFramework": "1.0.0-*",
"Microsoft.DotNet.Cli.Utils": "1.0.0-*",

View file

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;
using Moq;
@ -54,7 +55,14 @@ namespace StreamForwarderTests
failTwo.Object
};
var library = new CompilationLibrary(string.Empty, string.Empty, string.Empty, string.Empty, null, null, false);
var library = new CompilationLibrary(
string.Empty,
string.Empty,
string.Empty,
string.Empty,
Enumerable.Empty<string>(),
Enumerable.Empty<Dependency>(),
false);
var resolver = new CompositeCompilationAssemblyResolver(resolvers);
var result = resolver.TryResolveAssemblyPaths(library, null);
@ -82,7 +90,14 @@ namespace StreamForwarderTests
};
var assemblies = new List<string>();
var library = new CompilationLibrary(string.Empty, string.Empty, string.Empty, string.Empty, null, null, false);
var library = new CompilationLibrary(
string.Empty,
string.Empty,
string.Empty,
string.Empty,
Enumerable.Empty<string>(),
Enumerable.Empty<Dependency>(),
false);
var resolver = new CompositeCompilationAssemblyResolver(resolvers);
var result = resolver.TryResolveAssemblyPaths(library, assemblies);

View file

@ -104,9 +104,24 @@ namespace Microsoft.Extensions.DependencyModel.Tests
LibraryType.ReferenceAssembly,
LibraryDependencyType.Default)
}),
resourceAssemblies: new[]
{
new LibraryResourceAssembly(
new LibraryAsset("Dll", "en-US/Pack.Age.resources.dll", ""),
"en-US"
)
},
runtimeAssemblies: new[]
{
new LibraryAsset("Dll", "lib/Pack.Age.dll", ""),
},
runtimeTargets: new []
{
new LibraryRuntimeTarget(
"win8-x64",
new [] { new LibraryAsset("Dll", "win8-x64/Pack.Age.dll", "") },
new [] { new LibraryAsset("Dll", "win8-x64/Pack.Age.native.dll", "") }
)
}
),
Export(ReferenceAssemblyDescription("System.Collections",
@ -126,6 +141,11 @@ namespace Microsoft.Extensions.DependencyModel.Tests
lib.Version.Should().Be("1.2.3");
lib.Dependencies.Should().OnlyContain(l => l.Name == "System.Collections" && l.Version == "3.3.3");
lib.Assemblies.Should().OnlyContain(l => l.Path == "lib/Pack.Age.dll");
lib.ResourceAssemblies.Should().OnlyContain(l => l.Path == "en-US/Pack.Age.resources.dll" && l.Locale == "en-US");
var target = lib.RuntimeTargets.Should().Contain(t => t.Runtime == "win8-x64").Subject;
target.Assemblies.Should().OnlyContain(l => l.Path == "win8-x64/Pack.Age.dll");
target.NativeLibraries.Should().OnlyContain(l => l == "win8-x64/Pack.Age.native.dll");
var asm = context.RuntimeLibraries.Should().Contain(l => l.Name == "System.Collections").Subject;
asm.Type.Should().Be("referenceassembly");
@ -225,18 +245,16 @@ namespace Microsoft.Extensions.DependencyModel.Tests
private LibraryExport Export(
LibraryDescription description,
IEnumerable<LibraryAsset> compilationAssemblies = null,
IEnumerable<LibraryAsset> runtimeAssemblies = null)
IEnumerable<LibraryAsset> runtimeAssemblies = null,
IEnumerable<LibraryRuntimeTarget> runtimeTargets = null,
IEnumerable<LibraryResourceAssembly> resourceAssemblies = null)
{
return new LibraryExport(
description,
compilationAssemblies ?? Enumerable.Empty<LibraryAsset>(),
Enumerable.Empty<LibraryAsset>(),
runtimeAssemblies ?? Enumerable.Empty<LibraryAsset>(),
Enumerable.Empty<LibraryAsset>(),
Enumerable.Empty<LibraryAsset>(),
Enumerable.Empty<LibraryAsset>(),
Enumerable.Empty<AnalyzerReference>()
);
return LibraryExportBuilder.Create(description)
.WithCompilationAssemblies(compilationAssemblies)
.WithRuntimeAssemblies(runtimeAssemblies)
.WithRuntimeTargets(runtimeTargets)
.WithResourceAssemblies(resourceAssemblies)
.Build();
}
private PackageDescription PackageDescription(

View file

@ -183,6 +183,9 @@ namespace Microsoft.Extensions.DependencyModel.Tests
""runtimeTargets"": {
""lib/win7/System.Banana.dll"": { ""assetType"": ""runtime"", ""rid"": ""win7-x64""},
""lib/win7/Banana.dll"": { ""assetType"": ""native"", ""rid"": ""win7-x64""}
},
""resources"": {
""System.Banana.resources.dll"": { ""locale"": ""en-US"" }
}
}
}
@ -211,6 +214,8 @@ namespace Microsoft.Extensions.DependencyModel.Tests
package.Type.Should().Be("package");
package.Serviceable.Should().Be(false);
package.Assemblies.Should().Contain(a => a.Path == "lib/dotnet5.4/System.Banana.dll");
package.ResourceAssemblies.Should().Contain(a => a.Path == "System.Banana.resources.dll")
.Subject.Locale.Should().Be("en-US");
var target = package.RuntimeTargets.Should().Contain(t => t.Runtime == "win7-x64").Subject;
target.Assemblies.Should().Contain(a => a.Path == "lib/win7/System.Banana.dll");

View file

@ -154,6 +154,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests
"1.2.3",
"HASH",
new [] { RuntimeAssembly.Create("Banana.dll")},
new [] { new ResourceAssembly("en-US\\Banana.Resource.dll", "en-US")},
new []
{
new RuntimeTarget("win7-x64",
@ -187,6 +188,10 @@ namespace Microsoft.Extensions.DependencyModel.Tests
nativeLibrary.Should().HavePropertyValue("rid", "win7-x64");
nativeLibrary.Should().HavePropertyValue("assetType", "native");
var resourceAssemblies = library.Should().HavePropertyAsObject("resources").Subject;
var resourceAssembly = resourceAssemblies.Should().HavePropertyAsObject("en-US\\Banana.Resource.dll").Subject;
resourceAssembly.Should().HavePropertyValue("locale", "en-US");
//libraries
var libraries = result.Should().HavePropertyAsObject("libraries").Subject;
library = libraries.Should().HavePropertyAsObject("PackageName/1.2.3").Subject;
@ -224,6 +229,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests
"1.2.3",
"HASH",
new [] { RuntimeAssembly.Create("Banana.dll")},
new ResourceAssembly[] {},
new []
{
new RuntimeTarget("win7-x64",
@ -283,6 +289,7 @@ namespace Microsoft.Extensions.DependencyModel.Tests
"1.2.3",
"HASH",
new [] { RuntimeAssembly.Create("Banana.dll")},
new ResourceAssembly[] {},
new RuntimeTarget[] {},
new [] {
new Dependency("Fruits.Abstract.dll","2.0.0")
@ -308,6 +315,74 @@ namespace Microsoft.Extensions.DependencyModel.Tests
library.Should().HavePropertyValue("serviceable", true);
}
[Fact]
public void WritesResourceAssembliesForNonPortable()
{
var result = Save(Create(
"Target",
"runtime",
false,
runtimeLibraries: new[]
{
new RuntimeLibrary(
"package",
"PackageName",
"1.2.3",
"HASH",
new RuntimeAssembly[] { },
new []
{
new ResourceAssembly("en-US/Fruits.resources.dll", "en-US")
},
new RuntimeTarget[] { },
new Dependency[] { },
true
),
}));
var targets = result.Should().HavePropertyAsObject("targets").Subject;
var target = targets.Should().HavePropertyAsObject("Target/runtime").Subject;
var library = target.Should().HavePropertyAsObject("PackageName/1.2.3").Subject;
var resources = library.Should().HavePropertyAsObject("resources").Subject;
var resource = resources.Should().HavePropertyAsObject("en-US/Fruits.resources.dll").Subject;
resource.Should().HavePropertyValue("locale", "en-US");
}
[Fact]
public void WritesResourceAssembliesForPortable()
{
var result = Save(Create(
"Target",
"runtime",
true,
runtimeLibraries: new[]
{
new RuntimeLibrary(
"package",
"PackageName",
"1.2.3",
"HASH",
new RuntimeAssembly[] { },
new []
{
new ResourceAssembly("en-US/Fruits.resources.dll", "en-US")
},
new RuntimeTarget[] { },
new Dependency[] { },
true
),
}));
var targets = result.Should().HavePropertyAsObject("targets").Subject;
var target = targets.Should().HavePropertyAsObject("Target").Subject;
var library = target.Should().HavePropertyAsObject("PackageName/1.2.3").Subject;
var resources = library.Should().HavePropertyAsObject("resources").Subject;
var resource = resources.Should().HavePropertyAsObject("en-US/Fruits.resources.dll").Subject;
resource.Should().HavePropertyValue("locale", "en-US");
}
[Fact]
public void WritesCompilationOptions()
{

View file

@ -12,7 +12,7 @@
"FluentAssertions": "4.0.0",
"moq.netcore": "4.4.0-beta8",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-dev-79755-47"
"dotnet-test-xunit": "1.0.0-dev-91790-12"
},
"frameworks": {
@ -24,6 +24,5 @@
}
},
"testRunner": "xunit"
}

Some files were not shown because too many files have changed in this diff Show more