Exemplo n.º 1
0
 internal static void Main(string[] args)
 {
     FirstTimeCliExperience();
     SetupGlobalExceptionHandler();
     ConsoleApp.Run <Program>(args, InitializeAutofacContainer());
 }
Exemplo n.º 2
0
        public static async Task RunAsync <T>(string[] args, IContainer container)
        {
            var stopWatch = Stopwatch.StartNew();

            // This will flush any old telemetry that was saved
            // We only do this for clients that have not opted out
            var telemetry = new Telemetry.Telemetry(Guid.NewGuid().ToString());

            telemetry.Flush();

            var exitCode = ExitCodes.Success;
            var app      = new ConsoleApp(args, typeof(T).Assembly, container);

            // If all goes well, we will have an action to run.
            // This action can be an actual action, or just a HelpAction, but this method doesn't care
            // since HelpAction is still an IAction.
            try
            {
                var action = app.Parse();
                if (action != null)
                {
                    Utilities.PrintUpgradeWarning();
                    await Task.Delay(TimeSpan.FromSeconds(1));

                    if (action is IInitializableAction)
                    {
                        var initializableAction = action as IInitializableAction;
                        await initializableAction.Initialize();
                    }
                    // All Actions are async. No return value is expected from any action.
                    await action.RunAsync();

                    TelemetryHelpers.UpdateTelemetryEvent(app._telemetryEvent, action.TelemetryCommandEvents);
                    app._telemetryEvent.IsSuccessful = true;
                }
            }
            catch (Exception ex)
            {
                if (StaticSettings.IsDebug)
                {
                    // If CLI is in debug mode, display full call stack.
                    ColoredConsole.Error.WriteLine(ErrorColor(ex.ToString()));
                }
                else
                {
                    ColoredConsole.Error.WriteLine(ErrorColor(ex.Message));
                }

                if (args.Any(a => a.Equals("--pause-on-error", StringComparison.OrdinalIgnoreCase)))
                {
                    ColoredConsole.Write("Press any to continue....");
                    Console.ReadKey(true);
                }

                app._telemetryEvent.IsSuccessful = false;
                exitCode = ExitCodes.GeneralError;
            }
            finally
            {
                stopWatch.Stop();

                // Log the event if we did recognize an event
                if (!string.IsNullOrEmpty(app._telemetryEvent?.CommandName))
                {
                    app._telemetryEvent.TimeTaken = stopWatch.ElapsedMilliseconds;
                    TelemetryHelpers.LogEventIfAllowedSafe(telemetry, app._telemetryEvent);
                }

                Environment.Exit(exitCode);
            }
        }