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