2015-11-16 19:21:57 +00: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-02 00:21:10 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-02-18 09:09:23 +00:00
|
|
|
|
using System.IO;
|
2015-10-06 09:19:27 +00:00
|
|
|
|
using System.Linq;
|
2016-04-21 00:53:38 +00:00
|
|
|
|
using System.Runtime.Loader;
|
2016-04-22 19:20:50 +00:00
|
|
|
|
using System.Text;
|
2015-10-06 17:46:43 +00:00
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
2016-06-06 22:41:14 +00:00
|
|
|
|
using Microsoft.DotNet.Configurer;
|
2016-04-28 23:30:32 +00:00
|
|
|
|
using Microsoft.DotNet.InternalAbstractions;
|
2016-01-31 05:47:50 +00:00
|
|
|
|
using Microsoft.DotNet.ProjectModel.Server;
|
|
|
|
|
using Microsoft.DotNet.Tools.Build;
|
|
|
|
|
using Microsoft.DotNet.Tools.Compiler;
|
|
|
|
|
using Microsoft.DotNet.Tools.Compiler.Csc;
|
2016-02-04 20:41:50 +00:00
|
|
|
|
using Microsoft.DotNet.Tools.Help;
|
2016-01-31 05:47:50 +00:00
|
|
|
|
using Microsoft.DotNet.Tools.New;
|
|
|
|
|
using Microsoft.DotNet.Tools.Publish;
|
2016-02-18 09:09:23 +00:00
|
|
|
|
using Microsoft.DotNet.Tools.Restore;
|
2016-01-31 05:47:50 +00:00
|
|
|
|
using Microsoft.DotNet.Tools.Run;
|
2016-04-15 04:33:41 +00:00
|
|
|
|
using Microsoft.DotNet.Tools.Test;
|
|
|
|
|
using NuGet.Frameworks;
|
2015-10-03 18:34:08 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2016-04-15 04:33:41 +00:00
|
|
|
|
private static Dictionary<string, Func<string[], int>> s_builtIns = new Dictionary<string, Func<string[], int>>
|
|
|
|
|
{
|
|
|
|
|
["build"] = BuildCommand.Run,
|
|
|
|
|
["compile-csc"] = CompileCscCommand.Run,
|
|
|
|
|
["help"] = HelpCommand.Run,
|
|
|
|
|
["new"] = NewCommand.Run,
|
|
|
|
|
["pack"] = PackCommand.Run,
|
|
|
|
|
["projectmodel-server"] = ProjectModelServerCommand.Run,
|
|
|
|
|
["publish"] = PublishCommand.Run,
|
|
|
|
|
["restore"] = RestoreCommand.Run,
|
|
|
|
|
["run"] = RunCommand.Run,
|
|
|
|
|
["test"] = TestCommand.Run
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-03 18:34:08 +00:00
|
|
|
|
public static int Main(string[] args)
|
|
|
|
|
{
|
2015-11-28 08:28:45 +00:00
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
2016-04-25 18:29:29 +00:00
|
|
|
|
|
2016-04-22 19:23:26 +00:00
|
|
|
|
new MulticoreJitActivator().TryActivateMulticoreJit();
|
2016-04-25 18:29:29 +00:00
|
|
|
|
|
2016-04-27 23:04:26 +00:00
|
|
|
|
if (Env.GetEnvironmentVariableAsBool("DOTNET_CLI_CAPTURE_TIMING", false))
|
|
|
|
|
{
|
|
|
|
|
PerfTrace.Enabled = true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-22 19:20:50 +00:00
|
|
|
|
InitializeProcess();
|
|
|
|
|
|
2016-01-06 10:27:16 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2016-06-10 20:02:48 +00:00
|
|
|
|
ConfigureDotNetForFirstTimeUse();
|
|
|
|
|
|
2016-04-27 23:04:26 +00:00
|
|
|
|
using (PerfTrace.Current.CaptureTiming())
|
|
|
|
|
{
|
|
|
|
|
return ProcessArgs(args, new Telemetry());
|
|
|
|
|
}
|
2016-01-06 10:27:16 +00:00
|
|
|
|
}
|
2016-04-12 23:44:34 +00:00
|
|
|
|
catch (GracefulException e)
|
2016-01-06 10:27:16 +00:00
|
|
|
|
{
|
2016-04-29 20:46:16 +00:00
|
|
|
|
Reporter.Error.WriteLine(CommandContext.IsVerbose() ? e.ToString().Red().Bold() : e.Message.Red().Bold());
|
2016-04-29 00:49:04 +00:00
|
|
|
|
|
2016-01-06 10:27:16 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2016-04-27 23:04:26 +00:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (PerfTrace.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Output.WriteLine("Performance Summary:");
|
|
|
|
|
PerfTraceOutput.Print(Reporter.Output, PerfTrace.GetEvents());
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-06 10:27:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-05 01:58:22 +00:00
|
|
|
|
internal static int ProcessArgs(string[] args, ITelemetry telemetryClient)
|
2016-01-06 10:27:16 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
2016-02-05 20:55:09 +00:00
|
|
|
|
bool? verbose = null;
|
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;
|
|
|
|
|
}
|
2016-01-31 05:47:50 +00:00
|
|
|
|
else if (IsArg(args[lastArg], "version"))
|
2015-12-19 00:39:43 +00:00
|
|
|
|
{
|
2016-03-24 20:36:58 +00:00
|
|
|
|
PrintVersion();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else if (IsArg(args[lastArg], "info"))
|
|
|
|
|
{
|
|
|
|
|
PrintInfo();
|
2015-12-19 00:39:43 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2015-11-17 01:39:03 +00:00
|
|
|
|
else if (IsArg(args[lastArg], "h", "help"))
|
|
|
|
|
{
|
2016-02-04 20:41:50 +00:00
|
|
|
|
HelpCommand.PrintHelp();
|
2015-11-17 01:39:03 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2016-02-04 20:41:50 +00:00
|
|
|
|
HelpCommand.PrintHelp();
|
2015-11-17 01:39:03 +00:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
2015-11-02 00:21:10 +00:00
|
|
|
|
|
|
|
|
|
var appArgs = (lastArg + 1) >= args.Length ? Enumerable.Empty<string>() : args.Skip(lastArg + 1).ToArray();
|
|
|
|
|
|
2016-02-05 20:55:09 +00:00
|
|
|
|
if (verbose.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Environment.SetEnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString());
|
2016-04-27 23:04:26 +00:00
|
|
|
|
Console.WriteLine($"Telemetry is: {(telemetryClient.Enabled ? "Enabled" : "Disabled")}");
|
2016-02-05 20:55:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-04 20:41:50 +00:00
|
|
|
|
if (string.IsNullOrEmpty(command))
|
2015-10-08 21:49:39 +00:00
|
|
|
|
{
|
2016-02-04 20:41:50 +00:00
|
|
|
|
command = "help";
|
2015-11-02 00:21:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 00:28:44 +00:00
|
|
|
|
telemetryClient.TrackEvent(command, null, null);
|
|
|
|
|
|
2016-04-22 22:01:56 +00:00
|
|
|
|
int exitCode;
|
2016-01-31 05:47:50 +00:00
|
|
|
|
Func<string[], int> builtIn;
|
2016-04-15 04:33:41 +00:00
|
|
|
|
if (s_builtIns.TryGetValue(command, out builtIn))
|
2016-01-31 05:47:50 +00:00
|
|
|
|
{
|
2016-03-25 20:15:36 +00:00
|
|
|
|
exitCode = builtIn(appArgs.ToArray());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CommandResult result = Command.Create("dotnet-" + command, appArgs, FrameworkConstants.CommonFrameworks.NetStandardApp15)
|
|
|
|
|
.ForwardStdErr()
|
|
|
|
|
.ForwardStdOut()
|
|
|
|
|
.Execute();
|
|
|
|
|
exitCode = result.ExitCode;
|
2016-01-31 05:47:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 20:15:36 +00:00
|
|
|
|
|
|
|
|
|
return exitCode;
|
|
|
|
|
|
2015-11-02 00:21:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-10 03:52:49 +00:00
|
|
|
|
private static void ConfigureDotNetForFirstTimeUse()
|
|
|
|
|
{
|
2016-06-10 20:02:48 +00:00
|
|
|
|
using (PerfTrace.Current.CaptureTiming())
|
2016-06-10 03:52:49 +00:00
|
|
|
|
{
|
2016-06-10 20:02:48 +00:00
|
|
|
|
using (var nugetPackagesArchiver = new NuGetPackagesArchiver())
|
|
|
|
|
{
|
2016-06-10 22:06:48 +00:00
|
|
|
|
using (var nugetCacheSentinel = new NuGetCacheSentinel())
|
|
|
|
|
{
|
|
|
|
|
var environmentProvider = new EnvironmentProvider();
|
|
|
|
|
var commandFactory = new DotNetCommandFactory();
|
|
|
|
|
var nugetCachePrimer =
|
|
|
|
|
new NuGetCachePrimer(commandFactory, nugetPackagesArchiver, nugetCacheSentinel);
|
|
|
|
|
var dotnetConfigurer = new DotnetFirstTimeUseConfigurer(
|
|
|
|
|
nugetCachePrimer,
|
|
|
|
|
nugetCacheSentinel,
|
|
|
|
|
environmentProvider);
|
|
|
|
|
|
|
|
|
|
dotnetConfigurer.Configure();
|
|
|
|
|
}
|
2016-06-10 20:02:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-10 03:52:49 +00:00
|
|
|
|
}
|
2016-04-27 23:04:26 +00:00
|
|
|
|
|
2016-04-22 19:20:50 +00:00
|
|
|
|
private static void InitializeProcess()
|
|
|
|
|
{
|
|
|
|
|
// by default, .NET Core doesn't have all code pages needed for Console apps.
|
|
|
|
|
// see the .NET Core Notes in https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx
|
|
|
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-15 04:33:41 +00:00
|
|
|
|
internal static bool TryGetBuiltInCommand(string commandName, out Func<string[], int> builtInCommand)
|
|
|
|
|
{
|
|
|
|
|
return s_builtIns.TryGetValue(commandName, out builtInCommand);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-29 00:17:21 +00:00
|
|
|
|
private static void PrintVersion()
|
2016-03-24 20:36:58 +00:00
|
|
|
|
{
|
2016-03-25 20:15:36 +00:00
|
|
|
|
Reporter.Output.WriteLine(Product.Version);
|
2016-03-24 20:36:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void PrintInfo()
|
2015-12-19 00:39:43 +00:00
|
|
|
|
{
|
2016-02-04 20:41:50 +00:00
|
|
|
|
HelpCommand.PrintVersionHeader();
|
2015-12-19 00:39:43 +00:00
|
|
|
|
|
2016-02-20 04:01:14 +00:00
|
|
|
|
var commitSha = GetCommitSha() ?? "N/A";
|
2016-02-18 09:09:23 +00:00
|
|
|
|
Reporter.Output.WriteLine();
|
|
|
|
|
Reporter.Output.WriteLine("Product Information:");
|
2016-05-04 18:02:52 +00:00
|
|
|
|
Reporter.Output.WriteLine($" Version: {Product.Version}");
|
|
|
|
|
Reporter.Output.WriteLine($" Commit SHA-1 hash: {commitSha}");
|
2016-02-18 09:09:23 +00:00
|
|
|
|
Reporter.Output.WriteLine();
|
2015-12-19 00:39:43 +00:00
|
|
|
|
Reporter.Output.WriteLine("Runtime Environment:");
|
2016-04-28 23:30:32 +00:00
|
|
|
|
Reporter.Output.WriteLine($" OS Name: {RuntimeEnvironment.OperatingSystem}");
|
|
|
|
|
Reporter.Output.WriteLine($" OS Version: {RuntimeEnvironment.OperatingSystemVersion}");
|
|
|
|
|
Reporter.Output.WriteLine($" OS Platform: {RuntimeEnvironment.OperatingSystemPlatform}");
|
|
|
|
|
Reporter.Output.WriteLine($" RID: {RuntimeEnvironment.GetRuntimeIdentifier()}");
|
2015-12-19 00:39:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2015-12-19 00:39:43 +00:00
|
|
|
|
return (shortName != null && candidate.Equals("-" + shortName)) || (longName != null && candidate.Equals("--" + longName));
|
2015-10-06 09:19:27 +00:00
|
|
|
|
}
|
2016-04-22 22:01:56 +00:00
|
|
|
|
|
2016-02-18 09:09:23 +00:00
|
|
|
|
private static string GetCommitSha()
|
|
|
|
|
{
|
2016-03-04 06:57:43 +00:00
|
|
|
|
var versionFile = DotnetFiles.VersionFile;
|
2016-04-22 22:01:56 +00:00
|
|
|
|
|
2016-02-18 09:09:23 +00:00
|
|
|
|
if (File.Exists(versionFile))
|
|
|
|
|
{
|
2016-02-20 04:01:14 +00:00
|
|
|
|
return File.ReadLines(versionFile).FirstOrDefault()?.Substring(0, 10);
|
2016-02-18 09:09:23 +00:00
|
|
|
|
}
|
2016-04-22 22:01:56 +00:00
|
|
|
|
|
2016-02-18 09:09:23 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-10-03 18:34:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|