diff --git a/src/Cake/Commands/DefaultCommand.cs b/src/Cake/Commands/DefaultCommand.cs index 81b26dfae7..27e00a06ee 100644 --- a/src/Cake/Commands/DefaultCommand.cs +++ b/src/Cake/Commands/DefaultCommand.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; +using Cake.Core; using Cake.Core.Diagnostics; using Cake.Features.Bootstrapping; using Cake.Features.Building; @@ -34,40 +36,70 @@ public sealed class DefaultCommand : Command public override int Execute(CommandContext context, DefaultCommandSettings settings) { - // Set log verbosity. - _log.Verbosity = settings.Verbosity; - - if (settings.ShowVersion) + try { - return _version.Run(); + // Set log verbosity. + _log.Verbosity = settings.Verbosity; + + if (settings.ShowVersion) + { + return _version.Run(); + } + else if (settings.ShowInfo) + { + return _info.Run(); + } + + // Get the build host type. + var host = GetBuildHostKind(settings); + + // Run the bootstrapper? + if (!settings.SkipBootstrap || settings.Bootstrap) + { + int bootstrapperResult = PerformBootstrapping(context, settings, host); + if (bootstrapperResult != 0 || settings.Bootstrap) + { + return bootstrapperResult; + } + } + + // Run the build feature. + return _builder.Run(context.Remaining, new BuildFeatureSettings(host) + { + Script = settings.Script, + Verbosity = settings.Verbosity, + Exclusive = settings.Exclusive, + Debug = settings.Debug, + NoBootstrapping = settings.SkipBootstrap, + }); } - else if (settings.ShowInfo) + catch (Exception ex) { - return _info.Run(); + return LogException(_log, ex); } + } - // Get the build host type. - var host = GetBuildHostKind(settings); + private static int LogException(ICakeLog log, T ex) where T : Exception + { + log = log ?? new CakeBuildLog( + new CakeConsole(new CakeEnvironment(new CakePlatform(), new CakeRuntime()))); - // Run the bootstrapper? - if (!settings.SkipBootstrap || settings.Bootstrap) + if (log.Verbosity == Verbosity.Diagnostic) + { + log.Error("Error: {0}", ex); + } + else { - int bootstrapperResult = PerformBootstrapping(context, settings, host); - if (bootstrapperResult != 0 || settings.Bootstrap) + log.Error("Error: {0}", ex.Message); + if (ex is AggregateException aex) { - return bootstrapperResult; + foreach (var exception in aex.Flatten().InnerExceptions) + { + log.Error("\t{0}", exception.Message); + } } } - - // Run the build feature. - return _builder.Run(context.Remaining, new BuildFeatureSettings(host) - { - Script = settings.Script, - Verbosity = settings.Verbosity, - Exclusive = settings.Exclusive, - Debug = settings.Debug, - NoBootstrapping = settings.SkipBootstrap, - }); + return 1; } private BuildHostKind GetBuildHostKind(DefaultCommandSettings settings)