dotnet-installer/src/dotnet/Program.cs

209 lines
7.5 KiB
C#
Raw Normal View History

// 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-02 00:21:10 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.DotNet.Cli.Utils;
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;
using Microsoft.DotNet.Tools.New;
using Microsoft.DotNet.Tools.Publish;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.DotNet.Tools.Restore;
using NuGet.Frameworks;
using Microsoft.DotNet.Tools.Resgen;
using Microsoft.DotNet.Tools.Run;
using Microsoft.DotNet.Tools.Test;
2015-10-03 18:34:08 +00:00
namespace Microsoft.DotNet.Cli
{
public class Program
{
private const string ProductLongName = ".NET Command Line Tools";
private const string UsageText = @"Usage: dotnet [common-options] [command] [arguments]
2015-11-02 00:21:10 +00:00
Arguments:
[command] The command to execute
[arguments] Arguments to pass to the command
Common Options (passed before the command):
-v|--verbose Enable verbose output
--version Display .NET CLI Version Info
2015-11-02 00:21:10 +00:00
Common Commands:
new Initialize a basic .NET project
restore Restore dependencies specified in the .NET project
build Builds a .NET project
publish Publishes a .NET project for deployment (including the runtime)
run Compiles and immediately executes a .NET project
2015-12-07 22:18:09 +00:00
repl Launch an interactive session (read, eval, print, loop)
2015-12-02 19:19:41 +00:00
pack Creates a NuGet package";
private static readonly string ProductVersion = GetProductVersion();
private static string GetProductVersion()
{
var attr = typeof(Program).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
return attr?.InformationalVersion;
}
2015-11-02 00:21:10 +00:00
2015-10-03 18:34:08 +00:00
public static int Main(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
2016-01-06 10:27:16 +00:00
try
{
return ProcessArgs(args);
}
catch (CommandUnknownException e)
{
Console.WriteLine(e.Message);
return 1;
}
2016-01-06 10:27:16 +00:00
}
private static int ProcessArgs(string[] args)
{
2015-11-02 00:21:10 +00:00
// CommandLineApplication is a bit restrictive, so we parse things ourselves here. Individual apps should use CLA.
2016-01-06 10:27:16 +00:00
2015-11-02 00:21:10 +00:00
var verbose = false;
2016-01-06 10:27:16 +00:00
var success = true;
2015-11-02 00:21:10 +00:00
var command = string.Empty;
var lastArg = 0;
for (; lastArg < args.Length; lastArg++)
2015-10-08 21:49:39 +00:00
{
2015-11-02 00:21:10 +00:00
if (IsArg(args[lastArg], "v", "verbose"))
2015-10-08 21:49:39 +00:00
{
2015-11-02 00:21:10 +00:00
verbose = true;
}
else if (IsArg(args[lastArg], "version"))
{
PrintVersionInfo();
return 0;
}
2015-11-17 01:39:03 +00:00
else if (IsArg(args[lastArg], "h", "help"))
{
PrintHelp();
return 0;
}
2015-11-02 00:21:10 +00:00
else if (args[lastArg].StartsWith("-"))
{
2015-11-17 01:39:03 +00:00
Reporter.Error.WriteLine($"Unknown option: {args[lastArg]}");
success = false;
2015-10-08 21:49:39 +00:00
}
else
{
2015-11-02 00:21:10 +00:00
// It's the command, and we're done!
command = args[lastArg];
break;
2015-10-08 21:49:39 +00:00
}
}
2016-01-06 10:27:16 +00:00
if (!success)
{
2015-11-17 01:39:03 +00:00
PrintHelp();
return 1;
}
2015-11-02 00:21:10 +00:00
var appArgs = (lastArg + 1) >= args.Length ? Enumerable.Empty<string>() : args.Skip(lastArg + 1).ToArray();
if (string.IsNullOrEmpty(command) || command.Equals("help", StringComparison.OrdinalIgnoreCase))
2015-10-08 21:49:39 +00:00
{
2015-11-02 00:21:10 +00:00
return RunHelpCommand(appArgs);
}
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,
["new"] = NewCommand.Run,
["projectmodel-server"] = ProjectModelServerCommand.Run,
["publish"] = PublishCommand.Run,
["restore"] = RestoreCommand.Run,
["resgen"] = ResgenCommand.Run,
["run"] = RunCommand.Run,
["test"] = TestCommand.Run
};
Func<string[], int> builtIn;
if (builtIns.TryGetValue(command, out builtIn))
{
// mimic the env variable
Environment.SetEnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString());
Environment.SetEnvironmentVariable(CommandContext.Variables.AnsiPassThru, bool.TrueString);
return builtIn(appArgs.ToArray());
}
2016-01-06 10:27:16 +00:00
return Command.Create("dotnet-" + command, appArgs, FrameworkConstants.CommonFrameworks.DnxCore50)
2015-11-02 00:21:10 +00:00
.EnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString())
.EnvironmentVariable(CommandContext.Variables.AnsiPassThru, bool.TrueString)
.ForwardStdErr()
.ForwardStdOut()
.Execute()
.ExitCode;
}
private static int RunHelpCommand(IEnumerable<string> appArgs)
{
if (appArgs.Any())
{
return Command.Create("dotnet-" + appArgs.First(), new string[] { "--help" })
2015-10-08 21:49:39 +00:00
.ForwardStdErr()
.ForwardStdOut()
.Execute()
2015-10-08 21:49:39 +00:00
.ExitCode;
}
2015-11-02 00:21:10 +00:00
else
{
PrintHelp();
return 0;
}
}
private static void PrintHelp()
2015-11-02 00:21:10 +00:00
{
PrintVersionHeader();
Reporter.Output.WriteLine(UsageText);
}
private static void PrintVersionHeader()
{
var versionString = string.IsNullOrEmpty(ProductVersion) ?
string.Empty :
$" ({ProductVersion})";
Reporter.Output.WriteLine(ProductLongName + versionString);
}
private static void PrintVersionInfo()
{
PrintVersionHeader();
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 21:49:39 +00:00
}
2015-11-02 00:21:10 +00:00
private static bool IsArg(string candidate, string shortName, string longName)
2015-10-08 21:49:39 +00:00
{
return (shortName != null && candidate.Equals("-" + shortName)) || (longName != null && candidate.Equals("--" + longName));
}
2015-10-03 18:34:08 +00:00
}
}