2015-11-16 11:21:57 -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 ;
2015-11-01 16:21:10 -08:00
using System.Collections.Generic ;
2016-02-18 01:09:23 -08:00
using System.IO ;
2015-10-06 02:19:27 -07:00
using System.Linq ;
2015-10-06 10:46:43 -07:00
using Microsoft.DotNet.Cli.Utils ;
2016-02-18 01:09:23 -08:00
using Microsoft.DotNet.Tools.Test ;
using Microsoft.Extensions.PlatformAbstractions ;
using NuGet.Frameworks ;
2016-01-30 21:47:50 -08:00
using Microsoft.DotNet.ProjectModel.Server ;
using Microsoft.DotNet.Tools.Build ;
using Microsoft.DotNet.Tools.Compiler ;
using Microsoft.DotNet.Tools.Compiler.Csc ;
using Microsoft.DotNet.Tools.Compiler.Fsc ;
using Microsoft.DotNet.Tools.Compiler.Native ;
2016-02-04 12:41:50 -08:00
using Microsoft.DotNet.Tools.Help ;
2016-01-30 21:47:50 -08:00
using Microsoft.DotNet.Tools.New ;
using Microsoft.DotNet.Tools.Publish ;
2016-02-18 01:09:23 -08:00
using Microsoft.DotNet.Tools.Repl ;
2016-01-30 21:47:50 -08:00
using Microsoft.DotNet.Tools.Resgen ;
2016-02-18 01:09:23 -08:00
using Microsoft.DotNet.Tools.Restore ;
2016-01-30 21:47:50 -08:00
using Microsoft.DotNet.Tools.Run ;
2015-10-03 11:34:08 -07:00
namespace Microsoft.DotNet.Cli
{
public class Program
{
2015-11-01 16:21:10 -08:00
2015-10-03 11:34:08 -07:00
public static int Main ( string [ ] args )
{
2015-11-28 00:28:45 -08:00
DebugHelper . HandleDebugSwitch ( ref args ) ;
2016-01-06 02:27:16 -08:00
try
{
return ProcessArgs ( args ) ;
}
catch ( CommandUnknownException e )
{
Console . WriteLine ( e . Message ) ;
return 1 ;
}
2016-01-30 21:47:50 -08:00
2016-01-06 02:27:16 -08:00
}
private static int ProcessArgs ( string [ ] args )
{
2015-11-01 16:21:10 -08:00
// CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA.
2016-01-06 02:27:16 -08:00
2016-02-05 12:55:09 -08:00
bool? verbose = null ;
2016-01-06 02:27:16 -08:00
var success = true ;
2015-11-01 16:21:10 -08:00
var command = string . Empty ;
var lastArg = 0 ;
for ( ; lastArg < args . Length ; lastArg + + )
2015-10-08 14:49:39 -07:00
{
2015-11-01 16:21:10 -08:00
if ( IsArg ( args [ lastArg ] , "v" , "verbose" ) )
2015-10-08 14:49:39 -07:00
{
2015-11-01 16:21:10 -08:00
verbose = true ;
}
2016-01-30 21:47:50 -08:00
else if ( IsArg ( args [ lastArg ] , "version" ) )
2015-12-18 16:39:43 -08:00
{
PrintVersionInfo ( ) ;
return 0 ;
}
2015-11-16 17:39:03 -08:00
else if ( IsArg ( args [ lastArg ] , "h" , "help" ) )
{
2016-02-04 12:41:50 -08:00
HelpCommand . PrintHelp ( ) ;
2015-11-16 17:39:03 -08:00
return 0 ;
}
2015-11-01 16:21:10 -08:00
else if ( args [ lastArg ] . StartsWith ( "-" ) )
{
2015-11-16 17:39:03 -08:00
Reporter . Error . WriteLine ( $"Unknown option: {args[lastArg]}" ) ;
success = false ;
2015-10-08 14:49:39 -07:00
}
else
{
2015-11-01 16:21:10 -08:00
// It's the command, and we're done!
command = args [ lastArg ] ;
break ;
2015-10-08 14:49:39 -07:00
}
}
2016-01-06 02:27:16 -08:00
if ( ! success )
{
2016-02-04 12:41:50 -08:00
HelpCommand . PrintHelp ( ) ;
2015-11-16 17:39:03 -08:00
return 1 ;
}
2015-11-01 16:21:10 -08:00
var appArgs = ( lastArg + 1 ) > = args . Length ? Enumerable . Empty < string > ( ) : args . Skip ( lastArg + 1 ) . ToArray ( ) ;
2016-02-05 12:55:09 -08:00
if ( verbose . HasValue )
{
Environment . SetEnvironmentVariable ( CommandContext . Variables . Verbose , verbose . ToString ( ) ) ;
}
2016-02-04 12:41:50 -08:00
if ( string . IsNullOrEmpty ( command ) )
2015-10-08 14:49:39 -07:00
{
2016-02-04 12:41:50 -08:00
command = "help" ;
2015-11-01 16:21:10 -08:00
}
2016-01-30 21:47:50 -08:00
var builtIns = new Dictionary < string , Func < string [ ] , int > >
{
["build"] = BuildCommand . Run ,
["compile"] = CommpileCommand . Run ,
["compile-csc"] = CompileCscCommand . Run ,
["compile-fsc"] = CompileFscCommand . Run ,
["compile-native"] = CompileNativeCommand . Run ,
2016-02-04 12:41:50 -08:00
["help"] = HelpCommand . Run ,
2016-01-30 21:47:50 -08:00
["new"] = NewCommand . Run ,
2016-02-01 11:49:25 -08:00
["pack"] = PackCommand . Run ,
2016-01-30 21:47:50 -08:00
["projectmodel-server"] = ProjectModelServerCommand . Run ,
["publish"] = PublishCommand . Run ,
2016-02-05 08:40:30 -08:00
["repl"] = ReplCommand . Run ,
2016-01-30 21:47:50 -08:00
["restore"] = RestoreCommand . Run ,
["resgen"] = ResgenCommand . Run ,
["run"] = RunCommand . Run ,
["test"] = TestCommand . Run
} ;
Func < string [ ] , int > builtIn ;
if ( builtIns . TryGetValue ( command , out builtIn ) )
{
return builtIn ( appArgs . ToArray ( ) ) ;
}
2016-01-06 02:27:16 -08:00
return Command . Create ( "dotnet-" + command , appArgs , FrameworkConstants . CommonFrameworks . DnxCore50 )
2015-11-01 16:21:10 -08:00
. ForwardStdErr ( )
. ForwardStdOut ( )
. Execute ( )
. ExitCode ;
}
2015-12-18 16:39:43 -08:00
private static void PrintVersionInfo ( )
{
2016-02-04 12:41:50 -08:00
HelpCommand . PrintVersionHeader ( ) ;
2015-12-18 16:39:43 -08:00
2016-02-18 01:09:23 -08:00
var commitSha = GetCommitSha ( ) ;
Reporter . Output . WriteLine ( ) ;
Reporter . Output . WriteLine ( "Product Information:" ) ;
Reporter . Output . WriteLine ( $" Version: {HelpCommand.ProductVersion}" ) ;
Reporter . Output . WriteLine ( $" Commit Sha: {commitSha.Substring(0, 10)}" ) ;
Reporter . Output . WriteLine ( ) ;
2015-12-18 16:39:43 -08:00
var runtimeEnvironment = PlatformServices . Default . Runtime ;
Reporter . Output . WriteLine ( "Runtime Environment:" ) ;
Reporter . Output . WriteLine ( $" OS Name: {runtimeEnvironment.OperatingSystem}" ) ;
Reporter . Output . WriteLine ( $" OS Version: {runtimeEnvironment.OperatingSystemVersion}" ) ;
Reporter . Output . WriteLine ( $" OS Platform: {runtimeEnvironment.OperatingSystemPlatform}" ) ;
Reporter . Output . WriteLine ( $" Runtime Id: {runtimeEnvironment.GetRuntimeIdentifier()}" ) ;
}
private static bool IsArg ( string candidate , string longName )
{
return IsArg ( candidate , shortName : null , longName : longName ) ;
2015-10-08 14:49:39 -07:00
}
2015-11-01 16:21:10 -08:00
private static bool IsArg ( string candidate , string shortName , string longName )
2015-10-08 14:49:39 -07:00
{
2015-12-18 16:39:43 -08:00
return ( shortName ! = null & & candidate . Equals ( "-" + shortName ) ) | | ( longName ! = null & & candidate . Equals ( "--" + longName ) ) ;
2015-10-06 02:19:27 -07:00
}
2016-02-18 01:09:23 -08:00
private static string GetCommitSha ( )
{
// The CLI ships with a .version file that stores the commit information
var versionFile = Path . GetFullPath ( Path . Combine ( AppContext . BaseDirectory , ".." , ".version" ) ) ;
if ( File . Exists ( versionFile ) )
{
return File . ReadLines ( versionFile ) . FirstOrDefault ( ) ;
}
return null ;
}
2015-10-03 11:34:08 -07:00
}
}