2017-03-02 21:04:03 -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 ;
2016-03-28 02:32:31 -07:00
using System.IO ;
using System.Collections.Generic ;
using System.Linq ;
2017-05-09 16:09:38 -07:00
using Microsoft.DotNet.Cli.CommandLine ;
2016-03-28 02:32:31 -07:00
using Microsoft.DotNet.Cli.Utils ;
2016-10-13 21:42:19 -07:00
using NuGet.Frameworks ;
2017-05-09 16:09:38 -07:00
using Microsoft.DotNet.Tools.Common ;
2016-03-28 02:32:31 -07:00
namespace Microsoft.DotNet.Tools.DependencyInvoker
{
public class Program
{
2016-05-08 14:20:34 -07:00
public static int Main ( string [ ] args )
2016-03-28 02:32:31 -07:00
{
DebugHelper . HandleDebugSwitch ( ref args ) ;
2017-05-09 16:09:38 -07:00
args = new [ ] { "dotnet-dependency-tool-invoker" } . Concat ( args ) . ToArray ( ) ;
2017-03-02 21:04:03 -08:00
2017-05-09 16:09:38 -07:00
var parser = new Parser (
options : DotnetDependencyToolInvokerParser . DotnetDependencyToolInvoker ( ) ) ;
2017-03-02 21:04:03 -08:00
2017-05-09 16:09:38 -07:00
var parseResult = parser . Parse ( args ) ;
var appliedOptions = parseResult [ "dotnet-dependency-tool-invoker" ] ;
Console . WriteLine ( parseResult . Diagram ( ) ) ;
if ( appliedOptions . HasOption ( "help" ) )
2016-03-28 02:32:31 -07:00
{
2017-05-09 16:09:38 -07:00
Console . WriteLine ( parseResult . Command ( ) . HelpView ( ) ) ;
return 0 ;
}
2017-03-02 21:04:03 -08:00
2017-05-09 16:09:38 -07:00
var command = appliedOptions . Arguments . First ( ) ;
var framework = appliedOptions . ValueOrDefault < NuGetFramework > ( "framework" ) ;
var configuration = appliedOptions . ValueOrDefault < string > ( "configuration" ) ;
if ( string . IsNullOrEmpty ( configuration ) )
{
configuration = Constants . DefaultConfiguration ;
2016-03-28 02:32:31 -07:00
}
2016-10-27 18:46:43 -07:00
2017-05-09 16:09:38 -07:00
var output = appliedOptions . SingleArgumentOrDefault ( "output" ) ;
var projectPath = appliedOptions . ValueOrDefault < string > ( "project-path" ) ;
if ( string . IsNullOrEmpty ( projectPath ) )
{
projectPath = PathUtility . EnsureTrailingSlash ( Directory . GetCurrentDirectory ( ) ) ;
}
var appArguments = parseResult . UnmatchedTokens ;
2016-03-28 02:32:31 -07:00
var commandFactory =
new ProjectDependenciesCommandFactory (
2017-05-09 16:09:38 -07:00
framework ,
configuration ,
output ,
string . Empty ,
projectPath ) ;
2016-10-13 21:42:19 -07:00
2017-05-09 16:09:38 -07:00
var result =
InvokeDependencyToolForMSBuild ( commandFactory , command , framework , configuration , appArguments ) ;
2016-10-13 21:42:19 -07:00
return result ;
}
private static int InvokeDependencyToolForMSBuild (
ProjectDependenciesCommandFactory commandFactory ,
2017-05-09 16:09:38 -07:00
string command ,
NuGetFramework framework ,
string configuration ,
IEnumerable < string > appArguments )
2016-10-13 21:42:19 -07:00
{
2017-05-09 16:09:38 -07:00
Console . WriteLine ( $"Invoking '{command}' for '{framework.GetShortFolderName()}'." ) ;
2016-10-13 21:42:19 -07:00
2017-05-09 16:09:38 -07:00
return InvokeDependencyTool ( commandFactory , command , framework , configuration , appArguments ) ;
2016-10-13 21:42:19 -07:00
}
private static int InvokeDependencyTool (
ProjectDependenciesCommandFactory commandFactory ,
2017-05-09 16:09:38 -07:00
string command ,
NuGetFramework framework ,
string configuration ,
IEnumerable < string > appArguments )
2016-10-13 21:42:19 -07:00
{
try
{
var exitCode = commandFactory . Create (
2017-05-09 16:09:38 -07:00
$"dotnet-{command}" ,
appArguments ,
2016-10-13 21:42:19 -07:00
framework ,
2017-05-09 16:09:38 -07:00
configuration )
2016-10-13 21:42:19 -07:00
. ForwardStdErr ( )
. ForwardStdOut ( )
. Execute ( )
. ExitCode ;
Console . WriteLine ( $"Command returned {exitCode}" ) ;
}
catch ( CommandUnknownException )
{
Console . WriteLine ( $"Command not found" ) ;
return 1 ;
}
2016-05-08 14:20:34 -07:00
return 0 ;
2016-03-28 02:32:31 -07:00
}
}
}