Refactor CommandResolver into individual CommandResolver Implementation
classes. Write Unit Tests covering Composite DefaultCommandResolver and ScriptCommandResolver. baseline Baseline2
This commit is contained in:
parent
1fccdbd6ec
commit
42cc39252e
37 changed files with 2361 additions and 206 deletions
|
@ -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.EmptyIfNull(),
|
||||||
|
commandPath,
|
||||||
|
GetCommandResolutionStrategy(),
|
||||||
|
_environment);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal abstract string ResolveCommandPath(CommandResolverArguments commandResolverArguments);
|
||||||
|
internal abstract CommandResolutionStrategy GetCommandResolutionStrategy();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 ProjectDirectory { get; set; }
|
||||||
|
|
||||||
|
public string Configuration { get; set; }
|
||||||
|
|
||||||
|
public IEnumerable<string> InferredExtensions { get; set; }
|
||||||
|
|
||||||
|
public IEnvironmentProvider Environment { get; set; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
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 DefaultCommandResolver : CompositeCommandResolver
|
||||||
|
{
|
||||||
|
public static DefaultCommandResolver 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 new DefaultCommandResolver(environment, packagedCommandSpecFactory, platformCommandSpecFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DefaultCommandResolver(
|
||||||
|
IEnvironmentProvider environment,
|
||||||
|
IPackagedCommandSpecFactory packagedCommandSpecFactory,
|
||||||
|
IPlatformCommandSpecFactory platformCommandSpecFactory) : base()
|
||||||
|
{
|
||||||
|
AddCommandResolver(new RootedCommandResolver());
|
||||||
|
AddCommandResolver(new ProjectDependenciesCommandResolver(environment, packagedCommandSpecFactory));
|
||||||
|
AddCommandResolver(new ProjectToolsCommandResolver(packagedCommandSpecFactory));
|
||||||
|
AddCommandResolver(new AppBaseCommandResolver(environment, platformCommandSpecFactory));
|
||||||
|
AddCommandResolver(new PathCommandResolver(environment, platformCommandSpecFactory));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,179 @@
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CommandSpec ResolveFromProjectDependencies(
|
||||||
|
string projectDirectory,
|
||||||
|
NuGetFramework framework,
|
||||||
|
string configuration,
|
||||||
|
string commandName,
|
||||||
|
IEnumerable<string> commandArguments)
|
||||||
|
{
|
||||||
|
var allowedExtensions = GetAllowedCommandExtensionsFromEnvironment(_environment);
|
||||||
|
|
||||||
|
var projectContext = GetProjectContextFromDirectory(
|
||||||
|
projectDirectory,
|
||||||
|
framework);
|
||||||
|
|
||||||
|
if (projectContext == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var depsFilePath = projectContext.GetOutputPaths(configuration).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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.EmptyIfNull());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override CommandResolutionStrategy GetCommandResolutionStrategy()
|
||||||
|
{
|
||||||
|
return CommandResolutionStrategy.ProjectLocal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,141 @@
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
return ResolveFromProjectTools(
|
||||||
|
commandResolverArguments.CommandName,
|
||||||
|
commandResolverArguments.CommandArguments,
|
||||||
|
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.EmptyIfNull();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,141 @@
|
||||||
|
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.DnxCore50;
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
return ResolveFromProjectTools(
|
||||||
|
commandResolverArguments.CommandName,
|
||||||
|
commandResolverArguments.CommandArguments,
|
||||||
|
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.EmptyIfNull();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.EmptyIfNull());
|
||||||
|
|
||||||
|
return new CommandSpec(commandResolverArguments.CommandName, escapedArgs, CommandResolutionStrategy.RootedPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
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 ScriptCommandResolver : CompositeCommandResolver
|
||||||
|
{
|
||||||
|
public static ScriptCommandResolver Create()
|
||||||
|
{
|
||||||
|
var environment = new EnvironmentProvider();
|
||||||
|
|
||||||
|
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory);
|
||||||
|
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
|
||||||
|
{
|
||||||
|
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ScriptCommandResolver(environment, platformCommandSpecFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScriptCommandResolver(
|
||||||
|
IEnvironmentProvider environment,
|
||||||
|
IPlatformCommandSpecFactory platformCommandSpecFactory)
|
||||||
|
{
|
||||||
|
AddCommandResolver(new RootedCommandResolver());
|
||||||
|
AddCommandResolver(new ProjectPathCommandResolver(environment, platformCommandSpecFactory));
|
||||||
|
AddCommandResolver(new AppBaseCommandResolver(environment, platformCommandSpecFactory));
|
||||||
|
AddCommandResolver(new PathCommandResolver(environment, platformCommandSpecFactory));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -13,6 +13,7 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
{
|
{
|
||||||
internal static class CommandResolver
|
internal static class CommandResolver
|
||||||
{
|
{
|
||||||
|
<<<<<<< HEAD
|
||||||
public static CommandSpec TryResolveCommandSpec(
|
public static CommandSpec TryResolveCommandSpec(
|
||||||
string commandName,
|
string commandName,
|
||||||
IEnumerable<string> args,
|
IEnumerable<string> args,
|
||||||
|
@ -130,148 +131,55 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
|
|
||||||
var commandLibrary = context.ProjectFile.Tools
|
var commandLibrary = context.ProjectFile.Tools
|
||||||
.FirstOrDefault(l => l.Name == commandName);
|
.FirstOrDefault(l => l.Name == commandName);
|
||||||
|
=======
|
||||||
|
private static DefaultCommandResolver _defaultCommandResolver;
|
||||||
|
private static ScriptCommandResolver _scriptCommandResolver;
|
||||||
|
>>>>>>> 9c4329a... Refactor CommandResolver into individual CommandResolver Implementation
|
||||||
|
|
||||||
if (commandLibrary == default(LibraryRange))
|
public static CommandSpec TryResolveCommandSpec(
|
||||||
{
|
string commandName,
|
||||||
return null;
|
IEnumerable<string> args,
|
||||||
}
|
NuGetFramework framework = null,
|
||||||
|
string configuration=Constants.DefaultConfiguration,
|
||||||
var lockPath = Path.Combine(context.ProjectDirectory, "artifacts", "Tools", commandName,
|
string outputPath=null)
|
||||||
"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 commandResolverArgs = new CommandResolverArguments
|
||||||
|
{
|
||||||
|
CommandName = commandName,
|
||||||
|
CommandArguments = args,
|
||||||
|
Framework = framework,
|
||||||
|
ProjectDirectory = Directory.GetCurrentDirectory(),
|
||||||
|
Configuration = configuration
|
||||||
|
};
|
||||||
|
|
||||||
var files = commandPackage.GetFiles();
|
if (_defaultCommandResolver == null)
|
||||||
|
{
|
||||||
|
_defaultCommandResolver = DefaultCommandResolver.Create();
|
||||||
|
}
|
||||||
|
|
||||||
return ConfigureCommandFromPackage(commandName, args, files, packageDir);
|
return _defaultCommandResolver.Resolve(commandResolverArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static CommandSpec ConfigureCommandFromPackage(string commandName, IEnumerable<string> args,
|
public static CommandSpec TryResolveScriptCommandSpec(
|
||||||
PackageDescription commandPackage, ProjectContext projectContext, string depsPath = null)
|
string commandName,
|
||||||
|
IEnumerable<string> args,
|
||||||
|
Project project,
|
||||||
|
string[] inferredExtensionList)
|
||||||
{
|
{
|
||||||
var files = commandPackage.Library.Files;
|
var commandResolverArgs = new CommandResolverArguments
|
||||||
|
|
||||||
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
|
CommandName = commandName,
|
||||||
.Where(f => Path.GetFileName(f) == commandName + FileNameSuffixes.DotNet.DynamicLib)
|
CommandArguments = args,
|
||||||
.Select(f => Path.Combine(packageDir, f))
|
ProjectDirectory = project.ProjectDirectory,
|
||||||
.FirstOrDefault();
|
InferredExtensions = inferredExtensionList
|
||||||
|
};
|
||||||
|
|
||||||
fileName = CoreHost.HostExePath;
|
if (_scriptCommandResolver == null)
|
||||||
|
|
||||||
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);
|
_scriptCommandResolver = ScriptCommandResolver.Create();
|
||||||
}
|
}
|
||||||
|
|
||||||
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
|
return _scriptCommandResolver.Resolve(commandResolverArgs);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
namespace Microsoft.DotNet.Cli.Utils
|
namespace Microsoft.DotNet.Cli.Utils
|
||||||
{
|
{
|
||||||
internal class CommandSpec
|
public class CommandSpec
|
||||||
{
|
{
|
||||||
public CommandSpec(string path, string args, CommandResolutionStrategy resolutionStrategy)
|
public CommandSpec(string path, string args, CommandResolutionStrategy resolutionStrategy)
|
||||||
{
|
{
|
||||||
|
|
|
@ -9,74 +9,29 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
{
|
{
|
||||||
public static class Env
|
public static class Env
|
||||||
{
|
{
|
||||||
private static IEnumerable<string> _searchPaths;
|
private static IEnvironmentProvider _environment = new EnvironmentProvider();
|
||||||
private static IEnumerable<string> _executableExtensions;
|
|
||||||
|
|
||||||
public static IEnumerable<string> ExecutableExtensions
|
public static IEnumerable<string> ExecutableExtensions
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if (_executableExtensions == null)
|
return _environment.ExecutableExtensions;
|
||||||
{
|
|
||||||
|
|
||||||
_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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetCommandPath(string commandName, params string[] extensions)
|
public static string GetCommandPath(string commandName, params string[] extensions)
|
||||||
{
|
{
|
||||||
if (!extensions.Any())
|
return _environment.GetCommandPath(commandName, extensions);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions)
|
public static string GetCommandPathFromRootPath(string rootPath, string commandName, params string[] extensions)
|
||||||
{
|
{
|
||||||
if (!extensions.Any())
|
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
|
||||||
{
|
}
|
||||||
extensions = Env.ExecutableExtensions.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
var commandPath = extensions.Select(e => Path.Combine(rootPath, commandName + e))
|
public static string GetCommandPathFromRootPath(string rootPath, string commandName, IEnumerable<string> extensions)
|
||||||
.FirstOrDefault(File.Exists);
|
{
|
||||||
|
return _environment.GetCommandPathFromRootPath(rootPath, commandName, extensions);
|
||||||
return commandPath;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
97
src/Microsoft.DotNet.Cli.Utils/EnvironmentProvider.cs
Normal file
97
src/Microsoft.DotNet.Cli.Utils/EnvironmentProvider.cs
Normal 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)
|
||||||
|
{
|
||||||
|
if (extensionsOverride != null)
|
||||||
|
{
|
||||||
|
_executableExtensions = extensionsOverride;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.EmptyIfNull().ToArray();
|
||||||
|
|
||||||
|
return GetCommandPathFromRootPath(rootPath, commandName, extensionsArr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.Cli.Utils
|
||||||
|
{
|
||||||
|
public static class CollectionsExtensions
|
||||||
|
{
|
||||||
|
public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> enumerable)
|
||||||
|
{
|
||||||
|
return enumerable == null
|
||||||
|
? Enumerable.Empty<T>()
|
||||||
|
: enumerable;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
src/Microsoft.DotNet.Cli.Utils/IEnvironmentProvider.cs
Normal file
20
src/Microsoft.DotNet.Cli.Utils/IEnvironmentProvider.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.Cli.Utils.Tests
|
||||||
|
{
|
||||||
|
public static class CommandResolverTestUtils
|
||||||
|
{
|
||||||
|
public static void 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.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnvironmentProvider SetupEnvironmentProviderWhichFindsExtensions(params string[] extensions)
|
||||||
|
{
|
||||||
|
return new EnvironmentProvider(extensions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,206 @@
|
||||||
|
// 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 = DefaultCommandResolver.Create();
|
||||||
|
|
||||||
|
var resolvers = defaultCommandResolver.OrderedCommandResolvers;
|
||||||
|
|
||||||
|
resolvers.Should().HaveCount(5);
|
||||||
|
|
||||||
|
resolvers.Select(r => r.GetType())
|
||||||
|
.Should()
|
||||||
|
.ContainInOrder(
|
||||||
|
new []{
|
||||||
|
typeof(RootedCommandResolver),
|
||||||
|
typeof(ProjectDependenciesCommandResolver),
|
||||||
|
typeof(ProjectToolsCommandResolver),
|
||||||
|
typeof(AppBaseCommandResolver),
|
||||||
|
typeof(PathCommandResolver)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_Resolves_Rooted_Commands_Correctly()
|
||||||
|
// {
|
||||||
|
// var path = Path.Combine(AppContext.BaseDirectory, "rooteddir");
|
||||||
|
// Directory.CreateDirectory(path);
|
||||||
|
|
||||||
|
// var testCommandPath = CreateTestCommandFile(path, ".dll", "rootedcommand");
|
||||||
|
|
||||||
|
// var defaultCommandResolver = new DefaultCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = testCommandPath,
|
||||||
|
// CommandArguments = new string[] {}
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().NotBeNull();
|
||||||
|
|
||||||
|
// commandSpec.Path.Should().Be(testCommandPath);
|
||||||
|
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.RootedPath);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_Resolves_AppBase_Commands_Correctly()
|
||||||
|
// {
|
||||||
|
// var testCommandPath = CreateTestCommandFile(AppContext.BaseDirectory, ".exe", "appbasecommand");
|
||||||
|
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
|
||||||
|
|
||||||
|
// var defaultCommandResolver = new DefaultCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = testCommandName,
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// Environment = new EnvironmentProvider()
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().NotBeNull();
|
||||||
|
|
||||||
|
// commandSpec.Path.Should().Be(testCommandPath);
|
||||||
|
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.BaseDirectory);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_Resolves_PATH_Commands_Correctly()
|
||||||
|
// {
|
||||||
|
// var path = Path.Combine(AppContext.BaseDirectory, "pathdir");
|
||||||
|
// var testCommandPath = CreateTestCommandFile(path, ".dll", "pathcommmand");
|
||||||
|
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
|
||||||
|
|
||||||
|
// Mock<IEnvironmentProvider> mockEnvironment = new Mock<IEnvironmentProvider>();
|
||||||
|
// mockEnvironment.Setup(c => c
|
||||||
|
// .GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
|
||||||
|
// .Returns(testCommandPath);
|
||||||
|
|
||||||
|
// var defaultCommandResolver = new DefaultCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = testCommandName,
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// Environment = mockEnvironment.Object
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().NotBeNull();
|
||||||
|
|
||||||
|
// commandSpec.Path.Should().Be(testCommandPath);
|
||||||
|
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.Path);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_Resolves_Project_Tools_Commands_Correctly()
|
||||||
|
// {
|
||||||
|
// var testAppPath = Path.Combine(AppContext.BaseDirectory,
|
||||||
|
// "TestAssets/TestProjects/AppWithToolDependency");
|
||||||
|
|
||||||
|
// var defaultCommandResolver = new DefaultCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = "dotnet-hello",
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// ProjectDirectory = testAppPath,
|
||||||
|
// Environment = new EnvironmentProvider()
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().NotBeNull();
|
||||||
|
|
||||||
|
// commandSpec.Path.Should().NotBeNull();
|
||||||
|
// commandSpec.Args.Should().NotContain("--depsfile");
|
||||||
|
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.NugetPackage);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_Resolves_Project_Dependencies_Commands_Correctly()
|
||||||
|
// {
|
||||||
|
// var testAppPath = Path.Combine(AppContext.BaseDirectory,
|
||||||
|
// "TestAssets/TestProjects/AppWithDirectDependency");
|
||||||
|
|
||||||
|
// var defaultCommandResolver = new DefaultCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = "dotnet-hello",
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// ProjectDirectory = testAppPath,
|
||||||
|
// Environment = new EnvironmentProvider(),
|
||||||
|
// Framework = FrameworkConstants.CommonFrameworks.DnxCore50,
|
||||||
|
// Configuration = "Debug"
|
||||||
|
// };
|
||||||
|
|
||||||
|
|
||||||
|
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().NotBeNull();
|
||||||
|
|
||||||
|
// commandSpec.Path.Should().NotBeNull();
|
||||||
|
// commandSpec.Args.Should().Contain("--depsfile");
|
||||||
|
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.NugetPackage);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_does_not_resolve_ProjectLocal_commands()
|
||||||
|
// {
|
||||||
|
// var path = Path.Combine(AppContext.BaseDirectory,
|
||||||
|
// "testdir");
|
||||||
|
|
||||||
|
// var testCommandPath = CreateTestCommandFile(path, ".exe", "projectlocalcommand");
|
||||||
|
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
|
||||||
|
|
||||||
|
// var defaultCommandResolver = new DefaultCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = testCommandName,
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// ProjectDirectory = path,
|
||||||
|
// Environment = new EnvironmentProvider()
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = defaultCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
|
||||||
|
// commandSpec.Should().Be(null);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public string CreateTestCommandFile(string path, string extension, string name = "testcommand")
|
||||||
|
// {
|
||||||
|
// Directory.CreateDirectory(path);
|
||||||
|
|
||||||
|
// var filename = name + extension;
|
||||||
|
// var filepath = Path.Combine(path, filename);
|
||||||
|
|
||||||
|
// File.WriteAllText(filepath, "hello world");
|
||||||
|
|
||||||
|
// return filepath;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,239 @@
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
private ProjectPathCommandResolver SetupPlatformProjectPathCommandResolver(IEnvironmentProvider environment = null)
|
||||||
|
{
|
||||||
|
environment = environment ?? new EnvironmentProvider();
|
||||||
|
|
||||||
|
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory);
|
||||||
|
|
||||||
|
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
|
||||||
|
{
|
||||||
|
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
var projectPathCommandResolver = new ProjectPathCommandResolver(environment, platformCommandSpecFactory);
|
||||||
|
|
||||||
|
return projectPathCommandResolver;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,199 @@
|
||||||
|
// 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 = ScriptCommandResolver.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)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_Resolves_Rooted_Commands_Correctly()
|
||||||
|
// {
|
||||||
|
// var path = Path.Combine(AppContext.BaseDirectory, "rooteddir");
|
||||||
|
// Directory.CreateDirectory(path);
|
||||||
|
|
||||||
|
// var testCommandPath = CreateTestCommandFile(path, ".dll", "scriptrootedcommand");
|
||||||
|
|
||||||
|
// var scriptCommandResolver = new ScriptCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = testCommandPath,
|
||||||
|
// CommandArguments = new string[] {}
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().NotBeNull();
|
||||||
|
|
||||||
|
// commandSpec.Path.Should().Be(testCommandPath);
|
||||||
|
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.RootedPath);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_Resolves_AppBase_Commands_Correctly()
|
||||||
|
// {
|
||||||
|
// var testCommandPath = CreateTestCommandFile(AppContext.BaseDirectory, ".exe", "scriptappbasecommand");
|
||||||
|
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
|
||||||
|
|
||||||
|
// var scriptCommandResolver = new ScriptCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = testCommandName,
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// Environment = new EnvironmentProvider()
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().NotBeNull();
|
||||||
|
|
||||||
|
// commandSpec.Path.Should().Be(testCommandPath);
|
||||||
|
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.BaseDirectory);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_Resolves_PATH_Commands_Correctly()
|
||||||
|
// {
|
||||||
|
// var path = Path.Combine(AppContext.BaseDirectory, "pathdir");
|
||||||
|
// var testCommandPath = CreateTestCommandFile(path, ".dll", "scriptpathcommmand");
|
||||||
|
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
|
||||||
|
|
||||||
|
// Mock<IEnvironmentProvider> mockEnvironment = new Mock<IEnvironmentProvider>();
|
||||||
|
// mockEnvironment.Setup(c => c
|
||||||
|
// .GetCommandPath(It.IsAny<string>(), It.IsAny<string[]>()))
|
||||||
|
// .Returns(testCommandPath);
|
||||||
|
|
||||||
|
// var scriptCommandResolver = new ScriptCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = testCommandName,
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// Environment = mockEnvironment.Object
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().NotBeNull();
|
||||||
|
|
||||||
|
// commandSpec.Path.Should().Be(testCommandPath);
|
||||||
|
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.Path);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_does_NOT_Resolve_Project_Tools_Commands()
|
||||||
|
// {
|
||||||
|
// var testAppPath = Path.Combine(AppContext.BaseDirectory,
|
||||||
|
// "TestAssets/TestProjects/AppWithToolDependency");
|
||||||
|
|
||||||
|
// var scriptCommandResolver = new ScriptCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = "dotnet-hello",
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// ProjectDirectory = testAppPath,
|
||||||
|
// Environment = new EnvironmentProvider()
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().BeNull();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_does_NOT_Resolve_Project_Dependencies_Commands()
|
||||||
|
// {
|
||||||
|
// var testAppPath = Path.Combine(AppContext.BaseDirectory,
|
||||||
|
// "TestAssets/TestProjects/AppWithDirectDependency");
|
||||||
|
|
||||||
|
// var scriptCommandResolver = new ScriptCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = "dotnet-hello",
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// ProjectDirectory = testAppPath,
|
||||||
|
// Environment = new EnvironmentProvider(),
|
||||||
|
// Framework = FrameworkConstants.CommonFrameworks.DnxCore50,
|
||||||
|
// Configuration = "Debug"
|
||||||
|
// };
|
||||||
|
|
||||||
|
|
||||||
|
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
// commandSpec.Should().BeNull();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// [Fact]
|
||||||
|
// public void It_resolves_ProjectLocal_commands_correctly()
|
||||||
|
// {
|
||||||
|
// var path = Path.Combine(AppContext.BaseDirectory,
|
||||||
|
// "testdir");
|
||||||
|
|
||||||
|
// var testCommandPath = CreateTestCommandFile(path, ".exe", "scriptprojectlocalcommand");
|
||||||
|
// var testCommandName = Path.GetFileNameWithoutExtension(testCommandPath);
|
||||||
|
|
||||||
|
// var scriptCommandResolver = new ScriptCommandResolver();
|
||||||
|
|
||||||
|
// var commandResolverArgs = new CommandResolverArguments
|
||||||
|
// {
|
||||||
|
// CommandName = testCommandName,
|
||||||
|
// CommandArguments = new string[] {},
|
||||||
|
// ProjectDirectory = path,
|
||||||
|
// Environment = new EnvironmentProvider()
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var commandSpec = scriptCommandResolver.Resolve(commandResolverArgs);
|
||||||
|
|
||||||
|
// commandSpec.Should().NotBeNull();
|
||||||
|
// commandSpec.Path.Should().Be(testCommandPath);
|
||||||
|
// commandSpec.ResolutionStrategy.Should().Be(CommandResolutionStrategy.ProjectLocal);
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// public string CreateTestCommandFile(string path, string extension, string name = "testcommand")
|
||||||
|
// {
|
||||||
|
// Directory.CreateDirectory(path);
|
||||||
|
|
||||||
|
// var filename = name + extension;
|
||||||
|
// var filepath = Path.Combine(path, filename);
|
||||||
|
|
||||||
|
// File.WriteAllText(filepath, "hello world");
|
||||||
|
|
||||||
|
// return filepath;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,184 @@
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
|
||||||
|
private AppBaseCommandResolver SetupPlatformAppBaseCommandResolver(IEnvironmentProvider environment = null)
|
||||||
|
{
|
||||||
|
environment = environment ?? new EnvironmentProvider();
|
||||||
|
|
||||||
|
var platformCommandSpecFactory = default(IPlatformCommandSpecFactory);
|
||||||
|
|
||||||
|
if (PlatformServices.Default.Runtime.OperatingSystemPlatform == Platform.Windows)
|
||||||
|
{
|
||||||
|
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
var appBaseCommandResolver = new AppBaseCommandResolver(environment, platformCommandSpecFactory);
|
||||||
|
|
||||||
|
return appBaseCommandResolver;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
"Microsoft.DotNet.Tools.Tests.Utilities": { "target": "project" },
|
"Microsoft.DotNet.Tools.Tests.Utilities": { "target": "project" },
|
||||||
|
|
||||||
|
"moq.netcore": "4.4.0-beta8",
|
||||||
"xunit": "2.1.0",
|
"xunit": "2.1.0",
|
||||||
"dotnet-test-xunit": "1.0.0-dev-79755-47"
|
"dotnet-test-xunit": "1.0.0-dev-79755-47"
|
||||||
},
|
},
|
||||||
|
@ -27,7 +28,11 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
"content": [
|
"content": [
|
||||||
"../../TestAssets/TestProjects/OutputStandardOutputAndError/*"
|
"../../TestAssets/TestProjects/OutputStandardOutputAndError/*",
|
||||||
|
"../../TestAssets/TestProjects/TestAppWithArgs/*",
|
||||||
|
"../../TestAssets/TestProjects/AppWithDirectAndToolDependency/**/*",
|
||||||
|
"../../TestAssets/TestProjects/AppWithDirectDependency/**/*",
|
||||||
|
"../../TestAssets/TestProjects/AppWithToolDependency/**/*"
|
||||||
],
|
],
|
||||||
|
|
||||||
"testRunner": "xunit"
|
"testRunner": "xunit"
|
||||||
|
|
Loading…
Reference in a new issue