2016-03-03 15:31:04 -08:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic ;
2016-08-05 16:54:41 -07:00
using Microsoft.DotNet.PlatformAbstractions ;
2016-03-03 15:31:04 -08:00
using NuGet.Frameworks ;
namespace Microsoft.DotNet.Cli.Utils
{
public class ProjectDependenciesCommandFactory : ICommandFactory
{
private readonly NuGetFramework _nugetFramework ;
private readonly string _configuration ;
private readonly string _outputPath ;
2016-03-11 15:30:37 -08:00
private readonly string _buildBasePath ;
2016-03-03 15:31:04 -08:00
private readonly string _projectDirectory ;
public ProjectDependenciesCommandFactory (
2016-03-11 15:30:37 -08:00
NuGetFramework nugetFramework ,
string configuration ,
2016-03-03 15:31:04 -08:00
string outputPath ,
2016-03-11 15:30:37 -08:00
string buildBasePath ,
2016-03-03 15:31:04 -08:00
string projectDirectory )
{
_nugetFramework = nugetFramework ;
_configuration = configuration ;
_outputPath = outputPath ;
2016-03-11 15:30:37 -08:00
_buildBasePath = buildBasePath ;
2016-03-03 15:31:04 -08:00
_projectDirectory = projectDirectory ;
2016-04-18 10:51:15 -07:00
if ( _configuration = = null )
{
_configuration = Constants . DefaultConfiguration ;
}
2016-03-03 15:31:04 -08:00
}
public ICommand Create (
string commandName ,
IEnumerable < string > args ,
NuGetFramework framework = null ,
2016-04-18 10:51:15 -07:00
string configuration = null )
2016-03-03 15:31:04 -08:00
{
if ( string . IsNullOrEmpty ( configuration ) )
{
configuration = _configuration ;
}
if ( framework = = null )
{
framework = _nugetFramework ;
}
var commandSpec = FindProjectDependencyCommands (
2016-03-11 15:30:37 -08:00
commandName ,
args ,
configuration ,
framework ,
2016-03-03 15:31:04 -08:00
_outputPath ,
2016-03-11 15:30:37 -08:00
_buildBasePath ,
2016-03-03 15:31:04 -08:00
_projectDirectory ) ;
return Command . Create ( commandSpec ) ;
}
private CommandSpec FindProjectDependencyCommands (
string commandName ,
IEnumerable < string > commandArgs ,
string configuration ,
NuGetFramework framework ,
string outputPath ,
2016-03-11 15:30:37 -08:00
string buildBasePath ,
2016-03-03 15:31:04 -08:00
string projectDirectory )
{
var commandResolverArguments = new CommandResolverArguments
{
CommandName = commandName ,
CommandArguments = commandArgs ,
Framework = framework ,
Configuration = configuration ,
OutputPath = outputPath ,
2016-03-11 15:30:37 -08:00
BuildBasePath = buildBasePath ,
2016-03-03 15:31:04 -08:00
ProjectDirectory = projectDirectory
} ;
2016-04-05 20:13:06 -07:00
var commandResolver = GetProjectDependenciesCommandResolver ( framework ) ;
2016-03-03 15:31:04 -08:00
var commandSpec = commandResolver . Resolve ( commandResolverArguments ) ;
if ( commandSpec = = null )
{
throw new CommandUnknownException ( commandName ) ;
}
return commandSpec ;
}
2016-04-05 20:13:06 -07:00
private ICommandResolver GetProjectDependenciesCommandResolver ( NuGetFramework framework )
2016-03-03 15:31:04 -08:00
{
var environment = new EnvironmentProvider ( ) ;
2016-04-05 20:13:06 -07:00
if ( framework . IsDesktop ( ) )
{
2016-04-06 15:29:10 -07:00
IPlatformCommandSpecFactory platformCommandSpecFactory = null ;
2016-04-28 16:30:32 -07:00
if ( RuntimeEnvironment . OperatingSystemPlatform = = Platform . Windows )
2016-04-05 20:13:06 -07:00
{
platformCommandSpecFactory = new WindowsExePreferredCommandSpecFactory ( ) ;
}
else
{
platformCommandSpecFactory = new GenericPlatformCommandSpecFactory ( ) ;
}
return new OutputPathCommandResolver ( environment , platformCommandSpecFactory ) ;
}
else
{
var packagedCommandSpecFactory = new PackagedCommandSpecFactory ( ) ;
return new ProjectDependenciesCommandResolver ( environment , packagedCommandSpecFactory ) ;
}
2016-03-03 15:31:04 -08:00
}
}
}