2015-10-03 11:34:08 -07:00
using System ;
using System.Collections.Generic ;
2015-10-06 02:19:27 -07:00
using System.Diagnostics ;
using System.Linq ;
2015-10-03 11:34:08 -07:00
using Microsoft.Dnx.Runtime.Common.CommandLine ;
namespace Microsoft.DotNet.Cli
{
public class Program
{
public static int Main ( string [ ] args )
{
var app = new CommandLineApplication ( ) ;
app . Name = "dotnet" ;
app . Description = "The .NET CLI" ;
2015-10-06 02:19:27 -07:00
// Most commonly used commands
2015-10-03 11:34:08 -07:00
app . Command ( "init" , c = >
{
c . Description = "Scaffold a basic .NET application" ;
2015-10-06 02:19:27 -07:00
c . OnExecute ( ( ) = > Exec ( "dotnet-init" , c . RemainingArguments ) ) ;
2015-10-03 11:34:08 -07:00
} ) ;
app . Command ( "compile" , c = >
{
c . Description = "Produce assemblies for the project in given directory" ;
2015-10-06 03:10:26 -07:00
c . OnExecute ( ( ) = > Exec ( "dotnet-compile" , c . RemainingArguments ) ) ;
2015-10-03 11:34:08 -07:00
} ) ;
app . Command ( "restore" , c = >
{
c . Description = "Restore packages" ;
2015-10-06 02:19:27 -07:00
c . OnExecute ( ( ) = > Exec ( "dotnet-restore" , c . RemainingArguments ) ) ;
2015-10-03 11:34:08 -07:00
} ) ;
app . Command ( "pack" , c = >
{
c . Description = "Produce a NuGet package" ;
2015-10-06 02:19:27 -07:00
c . OnExecute ( ( ) = > Exec ( "dotnet-pack" , c . RemainingArguments ) ) ;
2015-10-03 11:34:08 -07:00
} ) ;
app . Command ( "publish" , c = >
{
c . Description = "Produce deployable assets" ;
2015-10-06 02:19:27 -07:00
c . OnExecute ( ( ) = > Exec ( "dotnet-publish" , c . RemainingArguments ) ) ;
2015-10-03 11:34:08 -07:00
} ) ;
2015-10-06 03:10:26 -07:00
// TODO: Handle unknown commands
2015-10-03 11:34:08 -07:00
app . OnExecute ( ( ) = >
{
app . ShowHelp ( ) ;
return 2 ;
} ) ;
2015-10-06 03:10:26 -07:00
try
{
return app . Execute ( args ) ;
}
catch ( Exception ex )
{
Console . WriteLine ( ex . Message ) ;
}
return 1 ;
2015-10-03 11:34:08 -07:00
}
2015-10-06 02:19:27 -07:00
2015-10-06 03:10:26 -07:00
private static int Exec ( string executable , IList < string > args )
2015-10-06 02:19:27 -07:00
{
var comSpec = Environment . GetEnvironmentVariable ( "ComSpec" ) ;
if ( ! string . IsNullOrEmpty ( comSpec ) )
{
2015-10-06 03:10:26 -07:00
args =
2015-10-06 02:19:27 -07:00
new [ ] { "/C" , "\"" , executable }
2015-10-06 03:10:26 -07:00
. Concat ( args )
2015-10-06 02:19:27 -07:00
. Concat ( new [ ] { "\"" } )
. ToArray ( ) ;
executable = comSpec ;
}
2015-10-06 03:10:26 -07:00
else
{
// Temporary, we're doing this so that redirecting the output works
args = new [ ] { "bash" , "-c" , "\"" , executable }
. Concat ( args . Select ( argument = > argument . Replace ( "\"" , "\\\"" ) ) )
. Concat ( new [ ] { "\"" } )
. ToArray ( ) ;
executable = "/usr/bin/env" ;
}
2015-10-06 02:19:27 -07:00
var psi = new ProcessStartInfo
{
FileName = executable ,
2015-10-06 03:10:26 -07:00
Arguments = string . Join ( " " , args ) ,
2015-10-06 02:19:27 -07:00
UseShellExecute = false ,
CreateNoWindow = true ,
RedirectStandardError = true ,
RedirectStandardOutput = true
} ;
var process = Process . Start ( psi ) ;
process . ErrorDataReceived + = OnProcessErrorDataReceived ;
process . OutputDataReceived + = OnProcessOutputDataReceived ;
process . BeginErrorReadLine ( ) ;
process . BeginOutputReadLine ( ) ;
process . WaitForExit ( ) ;
return process . ExitCode ;
}
private static void OnProcessOutputDataReceived ( object sender , DataReceivedEventArgs e )
{
Console . WriteLine ( e . Data ) ;
}
private static void OnProcessErrorDataReceived ( object sender , DataReceivedEventArgs e )
{
Console . Error . WriteLine ( e . Data ) ;
}
2015-10-03 11:34:08 -07:00
}
}