2016-01-06 10:27:16 +00:00
|
|
|
|
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;
|
2016-02-03 18:57:25 +00:00
|
|
|
|
using Microsoft.Extensions.PlatformAbstractions;
|
2016-02-11 00:13:30 +00:00
|
|
|
|
using NuGet.Frameworks;
|
2016-01-06 10:27:16 +00:00
|
|
|
|
using NuGet.Packaging;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli.Utils
|
|
|
|
|
{
|
|
|
|
|
internal static class CommandResolver
|
|
|
|
|
{
|
2016-02-25 00:05:55 +00:00
|
|
|
|
<<<<<<< HEAD
|
2016-03-02 05:15:07 +00:00
|
|
|
|
public static CommandSpec TryResolveCommandSpec(
|
|
|
|
|
string commandName,
|
|
|
|
|
IEnumerable<string> args,
|
|
|
|
|
NuGetFramework framework = null,
|
|
|
|
|
string configuration = Constants.DefaultConfiguration,
|
|
|
|
|
string outputPath = null)
|
2016-01-06 10:27:16 +00:00
|
|
|
|
{
|
2016-02-09 23:30:04 +00:00
|
|
|
|
return ResolveFromRootedCommand(commandName, args) ??
|
2016-03-02 05:15:07 +00:00
|
|
|
|
ResolveFromProjectDependencies(commandName, args, framework, configuration, outputPath) ??
|
2016-02-09 23:30:04 +00:00
|
|
|
|
ResolveFromProjectTools(commandName, args) ??
|
|
|
|
|
ResolveFromAppBase(commandName, args) ??
|
|
|
|
|
ResolveFromPath(commandName, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommandSpec TryResolveScriptCommandSpec(string commandName, IEnumerable<string> args, Project project, string[] inferredExtensionList)
|
|
|
|
|
{
|
|
|
|
|
return ResolveFromRootedCommand(commandName, args) ??
|
|
|
|
|
ResolveFromProjectPath(commandName, args, project, inferredExtensionList) ??
|
|
|
|
|
ResolveFromAppBase(commandName, args) ??
|
|
|
|
|
ResolveFromPath(commandName, args);
|
2016-01-06 10:27:16 +00:00
|
|
|
|
}
|
2016-02-06 02:55:15 +00:00
|
|
|
|
|
2016-01-06 10:27:16 +00:00
|
|
|
|
|
2016-02-09 23:30:04 +00:00
|
|
|
|
private static CommandSpec ResolveFromPath(string commandName, IEnumerable<string> args)
|
2016-01-06 10:27:16 +00:00
|
|
|
|
{
|
|
|
|
|
var commandPath = Env.GetCommandPath(commandName);
|
2016-01-28 04:35:43 +00:00
|
|
|
|
return commandPath == null
|
|
|
|
|
? null
|
2016-02-09 23:30:04 +00:00
|
|
|
|
: CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.Path);
|
2016-01-11 22:54:02 +00:00
|
|
|
|
}
|
2016-01-06 10:27:16 +00:00
|
|
|
|
|
2016-02-09 23:30:04 +00:00
|
|
|
|
private static CommandSpec ResolveFromAppBase(string commandName, IEnumerable<string> args)
|
|
|
|
|
{
|
|
|
|
|
var commandPath = Env.GetCommandPathFromRootPath(PlatformServices.Default.Application.ApplicationBasePath, commandName);
|
|
|
|
|
return commandPath == null
|
|
|
|
|
? null
|
|
|
|
|
: CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.BaseDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static CommandSpec ResolveFromProjectPath(string commandName, IEnumerable<string> args, Project project, string[] inferredExtensionList)
|
2016-01-11 22:54:02 +00:00
|
|
|
|
{
|
2016-02-09 23:30:04 +00:00
|
|
|
|
var commandPath = Env.GetCommandPathFromRootPath(project.ProjectDirectory, commandName, inferredExtensionList);
|
2016-01-28 04:35:43 +00:00
|
|
|
|
return commandPath == null
|
|
|
|
|
? null
|
2016-02-09 23:30:04 +00:00
|
|
|
|
: CreateCommandSpecPreferringExe(commandName, args, commandPath, CommandResolutionStrategy.ProjectLocal);
|
2016-01-06 10:27:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 23:30:04 +00:00
|
|
|
|
private static CommandSpec ResolveFromRootedCommand(string commandName, IEnumerable<string> args)
|
2016-01-06 10:27:16 +00:00
|
|
|
|
{
|
|
|
|
|
if (Path.IsPathRooted(commandName))
|
|
|
|
|
{
|
2016-02-09 23:30:04 +00:00
|
|
|
|
var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
|
|
|
|
|
return new CommandSpec(commandName, escapedArgs, CommandResolutionStrategy.Path);
|
2016-01-06 10:27:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-06 02:55:15 +00:00
|
|
|
|
public static CommandSpec ResolveFromProjectDependencies(
|
2016-03-02 05:15:07 +00:00
|
|
|
|
string commandName,
|
|
|
|
|
IEnumerable<string> args,
|
|
|
|
|
NuGetFramework framework,
|
|
|
|
|
string configuration,
|
|
|
|
|
string outputPath)
|
2016-01-06 10:27:16 +00:00
|
|
|
|
{
|
|
|
|
|
if (framework == null) return null;
|
|
|
|
|
|
|
|
|
|
var projectContext = GetProjectContext(framework);
|
|
|
|
|
|
|
|
|
|
if (projectContext == null) return null;
|
|
|
|
|
|
|
|
|
|
var commandPackage = GetCommandPackage(projectContext, commandName);
|
|
|
|
|
|
|
|
|
|
if (commandPackage == null) return null;
|
|
|
|
|
|
2016-03-02 05:15:07 +00:00
|
|
|
|
var depsPath = projectContext.GetOutputPaths(configuration, outputPath: outputPath).RuntimeFiles.Deps;
|
2016-01-06 10:27:16 +00:00
|
|
|
|
|
2016-02-09 23:30:04 +00:00
|
|
|
|
return ConfigureCommandFromPackage(commandName, args, commandPackage, projectContext, depsPath);
|
2016-01-06 10:27:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ProjectContext GetProjectContext(NuGetFramework framework)
|
|
|
|
|
{
|
|
|
|
|
var projectRootPath = Directory.GetCurrentDirectory();
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(Path.Combine(projectRootPath, Project.FileName)))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-03 18:57:25 +00:00
|
|
|
|
var projectContext = ProjectContext.Create(projectRootPath, framework, PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers());
|
2016-01-06 10:27:16 +00:00
|
|
|
|
return projectContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static PackageDescription GetCommandPackage(ProjectContext projectContext, string commandName)
|
|
|
|
|
{
|
|
|
|
|
return projectContext.LibraryManager.GetLibraries()
|
2016-01-28 04:35:43 +00:00
|
|
|
|
.Where(l => l.GetType() == typeof(PackageDescription))
|
2016-01-06 10:27:16 +00:00
|
|
|
|
.Select(l => l as PackageDescription)
|
|
|
|
|
.FirstOrDefault(p => p.Library.Files
|
|
|
|
|
.Select(Path.GetFileName)
|
|
|
|
|
.Where(f => Path.GetFileNameWithoutExtension(f) == commandName)
|
|
|
|
|
.Select(Path.GetExtension)
|
|
|
|
|
.Any(e => Env.ExecutableExtensions.Contains(e) ||
|
|
|
|
|
e == FileNameSuffixes.DotNet.DynamicLib));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 23:30:04 +00:00
|
|
|
|
public static CommandSpec ResolveFromProjectTools(string commandName, IEnumerable<string> args)
|
2016-01-06 10:27:16 +00:00
|
|
|
|
{
|
2016-03-01 23:35:32 +00:00
|
|
|
|
var context = GetProjectContext(FrameworkConstants.CommonFrameworks.NetStandardApp15);
|
2016-01-06 10:27:16 +00:00
|
|
|
|
|
|
|
|
|
if (context == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var commandLibrary = context.ProjectFile.Tools
|
|
|
|
|
.FirstOrDefault(l => l.Name == commandName);
|
2016-02-25 00:05:55 +00:00
|
|
|
|
=======
|
|
|
|
|
private static DefaultCommandResolver _defaultCommandResolver;
|
|
|
|
|
private static ScriptCommandResolver _scriptCommandResolver;
|
|
|
|
|
>>>>>>> 9c4329a... Refactor CommandResolver into individual CommandResolver Implementation
|
2016-01-06 10:27:16 +00:00
|
|
|
|
|
2016-02-25 00:05:55 +00:00
|
|
|
|
public static CommandSpec TryResolveCommandSpec(
|
|
|
|
|
string commandName,
|
|
|
|
|
IEnumerable<string> args,
|
|
|
|
|
NuGetFramework framework = null,
|
|
|
|
|
string configuration=Constants.DefaultConfiguration,
|
|
|
|
|
string outputPath=null)
|
2016-01-06 10:27:16 +00:00
|
|
|
|
{
|
2016-02-25 00:05:55 +00:00
|
|
|
|
var commandResolverArgs = new CommandResolverArguments
|
2016-01-06 10:27:16 +00:00
|
|
|
|
{
|
2016-02-25 00:05:55 +00:00
|
|
|
|
CommandName = commandName,
|
|
|
|
|
CommandArguments = args,
|
|
|
|
|
Framework = framework,
|
|
|
|
|
ProjectDirectory = Directory.GetCurrentDirectory(),
|
|
|
|
|
Configuration = configuration
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (_defaultCommandResolver == null)
|
2016-01-11 22:54:02 +00:00
|
|
|
|
{
|
2016-02-25 00:05:55 +00:00
|
|
|
|
_defaultCommandResolver = DefaultCommandResolver.Create();
|
2016-01-22 22:03:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 00:05:55 +00:00
|
|
|
|
return _defaultCommandResolver.Resolve(commandResolverArgs);
|
2016-01-22 22:03:40 +00:00
|
|
|
|
}
|
2016-02-25 00:05:55 +00:00
|
|
|
|
|
|
|
|
|
public static CommandSpec TryResolveScriptCommandSpec(
|
|
|
|
|
string commandName,
|
|
|
|
|
IEnumerable<string> args,
|
|
|
|
|
Project project,
|
|
|
|
|
string[] inferredExtensionList)
|
2016-01-22 22:03:40 +00:00
|
|
|
|
{
|
2016-02-25 00:05:55 +00:00
|
|
|
|
var commandResolverArgs = new CommandResolverArguments
|
2016-01-22 22:03:40 +00:00
|
|
|
|
{
|
2016-02-25 00:05:55 +00:00
|
|
|
|
CommandName = commandName,
|
|
|
|
|
CommandArguments = args,
|
|
|
|
|
ProjectDirectory = project.ProjectDirectory,
|
|
|
|
|
InferredExtensions = inferredExtensionList
|
|
|
|
|
};
|
2016-01-22 11:59:04 +00:00
|
|
|
|
|
2016-02-25 00:05:55 +00:00
|
|
|
|
if (_scriptCommandResolver == null)
|
2016-01-22 11:59:04 +00:00
|
|
|
|
{
|
2016-02-25 00:05:55 +00:00
|
|
|
|
_scriptCommandResolver = ScriptCommandResolver.Create();
|
2016-01-22 11:59:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 00:05:55 +00:00
|
|
|
|
return _scriptCommandResolver.Resolve(commandResolverArgs);
|
2016-01-11 22:54:02 +00:00
|
|
|
|
}
|
2016-01-06 10:27:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-22 22:03:40 +00:00
|
|
|
|
|