/// <summary> /// Runs the specified target. /// </summary> /// <param name="target">The target to run.</param> /// <returns>The resulting report.</returns> public CakeReport RunTarget(string target) { var graph = CakeGraphBuilder.Build(_tasks); // Make sure target exist. if (!graph.Exist(target)) { const string format = "The target '{0}' was not found."; throw new CakeException(string.Format(CultureInfo.InvariantCulture, format, target)); } var stopWatch = new Stopwatch(); var report = new CakeReport(); foreach (var task in graph.Traverse(target)) { var taskNode = _tasks.FirstOrDefault(x => x.Name.Equals(task, StringComparison.OrdinalIgnoreCase)); Debug.Assert(taskNode != null, "Node should not be null."); if (ShouldTaskExecute(taskNode)) { _log.Verbose("Executing task: {0}", taskNode.Name); ExecuteTask(stopWatch, taskNode, report); _log.Verbose("Finished executing task: {0}", taskNode.Name); } } return(report); }
private void PerformSetup(IExecutionStrategy strategy, ICakeContext context, CakeTask targetTask, CakeTask[] tasks, Stopwatch stopWatch, CakeReport report) { stopWatch.Restart(); PublishEvent(BeforeSetup, new BeforeSetupEventArgs(context)); #pragma warning disable 618 PublishEvent(Setup, new SetupEventArgs(context)); #pragma warning restore 618 try { if (_actions.Setups.Count > 0) { foreach (var setup in _actions.Setups) { strategy.PerformSetup(setup, new SetupContext(context, targetTask, tasks)); } report.Add("Setup", CakeReportEntryCategory.Setup, stopWatch.Elapsed); } } finally { PublishEvent(AfterSetup, new AfterSetupEventArgs(context)); } }
private void PerformTeardown(IExecutionStrategy strategy, ICakeContext context, Stopwatch stopWatch, CakeReport report, bool exceptionWasThrown, Exception thrownException) { stopWatch.Restart(); var teardownContext = new TeardownContext(context, thrownException); PublishEvent(BeforeTeardown, new BeforeTeardownEventArgs(teardownContext)); #pragma warning disable 618 PublishEvent(Teardown, new TeardownEventArgs(teardownContext)); #pragma warning restore 618 try { if (_actions.Teardowns.Count > 0) { var exceptions = new List <Exception>(); try { foreach (var teardown in _actions.Teardowns) { try { strategy.PerformTeardown(teardown, teardownContext); } catch (Exception ex) { // No other exceptions were thrown and this is the only teardown? if (!exceptionWasThrown && _actions.Teardowns.Count == 1) { // If no other exception was thrown, we throw this one. // By doing this we preserve the original stack trace which is always nice. _log.Error("An error occurred in a custom teardown action."); throw; } // Add this exception to the list. exceptions.Add(ex); } } } finally { report.Add("Teardown", CakeReportEntryCategory.Teardown, stopWatch.Elapsed); } // If, any exceptions occurred, process them now. if (exceptions.Count > 0) { ProcessTeardownExceptions(exceptions, exceptionWasThrown); } } } finally { PublishEvent(AfterTeardown, new AfterTeardownEventArgs(teardownContext)); } }
private async Task ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report) { stopWatch.Restart(); PerformTaskSetup(context, strategy, task, false); var exceptionWasThrown = false; try { // Execute the task. await strategy.ExecuteAsync(task, context).ConfigureAwait(false); } catch (Exception exception) { _log.Error("An error occurred when executing task '{0}'.", task.Name); exceptionWasThrown = true; // Got an error reporter? if (task.ErrorReporter != null) { ReportErrors(strategy, task.ErrorReporter, exception); } // Got an error handler? if (task.ErrorHandler != null) { HandleErrors(strategy, task.ErrorHandler, exception); } else { // No error handler defined for this task. // Rethrow the exception and let it propagate. throw; } } finally { if (task.FinallyHandler != null) { strategy.InvokeFinally(task.FinallyHandler); } PerformTaskTeardown(context, strategy, task, stopWatch.Elapsed, false, exceptionWasThrown); } // Add the task results to the report if (IsDelegatedTask(task)) { report.AddDelegated(task.Name, stopWatch.Elapsed); } else { report.Add(task.Name, CakeReportEntryCategory.Task, stopWatch.Elapsed); } }
/// <summary> /// Runs the specified target. /// </summary> /// <param name="context">The context.</param> /// <param name="strategy">The execution strategy.</param> /// <param name="target">The target to run.</param> /// <returns>The resulting report.</returns> public override CakeReport RunTarget(ICakeContext context, IExecutionStrategy strategy, string target) { if (target == null) { throw new ArgumentNullException("target"); } if (strategy == null) { throw new ArgumentNullException("strategy"); } var graph = CakeGraphBuilder.Build(Tasks.ToList()); ThrowIfTargetNotFound(target, graph); var exceptionWasThrown = false; try { PerformSetup(strategy); var stopWatch = new Stopwatch(); var report = new CakeReport(); foreach (IEnumerable <string> parallelNodes in graph.TraverseAndGroup(target)) { var runningTasks = new List <Task>(); foreach (var taskNode in parallelNodes) { bool isTarget; var task = GetTask(target, taskNode, out isTarget); var newTask = Runner.ExecuteTaskAsync(context, strategy, stopWatch, task, isTarget, report); runningTasks.Add(newTask); } Task.WaitAll(runningTasks.ToArray()); } return(report); } catch { exceptionWasThrown = true; throw; } finally { PerformTeardown(strategy, exceptionWasThrown); } }
/// <summary> /// Runs the specified target. /// </summary> /// <param name="context">The context.</param> /// <param name="strategy">The execution strategy.</param> /// <param name="target">The target to run.</param> /// <returns>The resulting report.</returns> public override CakeReport RunTarget(ICakeContext context, IExecutionStrategy strategy, string target) { if (target == null) { throw new ArgumentNullException("target"); } if (strategy == null) { throw new ArgumentNullException("strategy"); } var graph = CakeGraphBuilder.Build(Tasks.ToList()); ThrowIfTargetNotFound(target, graph); // This isn't pretty, but we need to keep track of exceptions thrown // while running a setup action, or a task. We do this since we don't // want to throw teardown exceptions if an exception was thrown previously. var exceptionWasThrown = false; try { PerformSetup(strategy); var stopWatch = new Stopwatch(); var report = new CakeReport(); foreach (var taskNode in graph.Traverse(target)) { bool isTarget; var task = GetTask(target, taskNode, out isTarget); Runner.ExecuteTask(context, strategy, stopWatch, task, isTarget, report); } return(report); } catch { exceptionWasThrown = true; throw; } finally { PerformTeardown(strategy, exceptionWasThrown); } }
/// <inheritdoc/> public void Write(CakeReport report) { if (report == null) { throw new ArgumentNullException(nameof(report)); } try { var maxTaskNameLength = 29; foreach (var item in report) { if (item.TaskName.Length > maxTaskNameLength) { maxTaskNameLength = item.TaskName.Length; } } maxTaskNameLength++; string lineFormat = "{0,-" + maxTaskNameLength + "}{1,-20}"; _console.ForegroundColor = ConsoleColor.Green; // Write header. _console.WriteLine(); _console.WriteLine(lineFormat, "Task", "Duration"); _console.WriteLine(new string('-', 20 + maxTaskNameLength)); // Write task status. foreach (var item in report) { if (ShouldWriteTask(item)) { _console.ForegroundColor = GetItemForegroundColor(item); _console.WriteLine(lineFormat, item.TaskName, FormatDuration(item)); } } // Write footer. _console.ForegroundColor = ConsoleColor.Green; _console.WriteLine(new string('-', 20 + maxTaskNameLength)); _console.WriteLine(lineFormat, "Total:", FormatTime(GetTotalTime(report))); } finally { _console.ResetColor(); } }
private void PerformSetup(IExecutionStrategy strategy, ICakeContext context, CakeTask targetTask, CakeTask[] tasks, Stopwatch stopWatch, CakeReport report) { stopWatch.Restart(); PublishEvent(Setup, new SetupEventArgs(context)); if (_actions.Setups.Count > 0) { foreach (var setup in _actions.Setups) { strategy.PerformSetup(setup, new SetupContext(context, targetTask, tasks)); } report.Add("Setup", CakeReportEntryCategory.Setup, stopWatch.Elapsed); } }
private void ExecuteTask(Stopwatch stopWatch, CakeTask task, CakeReport report) { // Reset the stop watch. stopWatch.Reset(); stopWatch.Start(); try { // Execute the task. task.Execute(this); } catch (Exception ex) { _log.Error("An error occured in task {0}.", ex.Message); if (!task.ContinueOnError) { throw; } } // Add the task results to the report. report.Add(task.Name, stopWatch.Elapsed); }
internal void ExecuteTask(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, bool isTarget, CakeReport report) { try { ExecuteTaskAsync(context, strategy, stopWatch, task, isTarget, report).Wait(); } catch (AggregateException ae) { throw ae.InnerException; } }
private void SkipTask(ICakeContext context, IExecutionStrategy strategy, CakeTask task, CakeReport report, CakeTaskCriteria criteria) { PerformTaskSetup(context, strategy, task, true); strategy.Skip(task, criteria); PerformTaskTeardown(context, strategy, task, TimeSpan.Zero, true, null); // Add the skipped task to the report. report.AddSkipped(task.Name); }
private async Task RunTask(ICakeContext context, IExecutionStrategy strategy, CakeTask task, string target, Stopwatch stopWatch, CakeReport report) { // Is this the current target? var isTarget = task.Name.Equals(target, StringComparison.OrdinalIgnoreCase); // Should we execute the task? var skipped = false; foreach (var criteria in task.Criterias) { if (!ShouldTaskExecute(context, task, criteria, isTarget)) { SkipTask(context, strategy, task, report, criteria); skipped = true; break; } } if (!skipped) { await ExecuteTaskAsync(context, strategy, stopWatch, task, report).ConfigureAwait(false); } }
/// <inheritdoc/> public async Task <CakeReport> RunTargetAsync(ICakeContext context, IExecutionStrategy strategy, ExecutionSettings settings) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } if (string.IsNullOrWhiteSpace(settings.Target)) { throw new ArgumentException("No target specified.", nameof(settings)); } if (strategy == null) { throw new ArgumentNullException(nameof(strategy)); } // Ensure that registered actions are valid. _actions.Validate(); // Create a graph out of the tasks. var graph = CakeGraphBuilder.Build(_tasks); // Make sure target exist. var target = settings.Target; if (!graph.Exist(target)) { const string format = "The target '{0}' was not found."; throw new CakeException(string.Format(CultureInfo.InvariantCulture, format, target)); } // This isn't pretty, but we need to keep track of exceptions thrown // while running a setup action, or a task. We do this since we don't // want to throw teardown exceptions if an exception was thrown previously. var exceptionWasThrown = false; Exception thrownException = null; var stopWatch = new Stopwatch(); var report = new CakeReport(); try { // Get all nodes to traverse in the correct order. var orderedTasks = graph.Traverse(target) .Select(y => _tasks.FirstOrDefault(x => x.Name.Equals(y, StringComparison.OrdinalIgnoreCase))).ToArray(); // Get target node var targetNode = orderedTasks .FirstOrDefault(node => node.Name.Equals(target, StringComparison.OrdinalIgnoreCase)); PerformSetup(strategy, context, targetNode, orderedTasks, stopWatch, report); if (settings.Exclusive) { // Execute only the target task. var task = _tasks.FirstOrDefault(x => x.Name.Equals(settings.Target, StringComparison.OrdinalIgnoreCase)); await RunTask(context, strategy, task, target, stopWatch, report); } else { // Execute all scheduled tasks. foreach (var task in orderedTasks) { await RunTask(context, strategy, task, target, stopWatch, report); } } return(report); } catch (Exception ex) { exceptionWasThrown = true; thrownException = ex; throw; } finally { PerformTeardown(strategy, context, stopWatch, report, exceptionWasThrown, thrownException); } }
private void PerformTeardown(IExecutionStrategy strategy, ICakeContext context, Stopwatch stopWatch, CakeReport report, bool exceptionWasThrown, Exception thrownException) { stopWatch.Restart(); var teardownContext = new TeardownContext(context, thrownException); PublishEvent(Teardown, new TeardownEventArgs(teardownContext)); if (_actions.Teardown != null) { try { strategy.PerformTeardown(_actions.Teardown, teardownContext); } catch (Exception ex) { _log.Error("An error occurred in the custom teardown action."); if (!exceptionWasThrown) { // If no other exception was thrown, we throw this one. throw; } _log.Error("Teardown error: {0}", ex.ToString()); } finally { report.Add("**Teardown**", stopWatch.Elapsed); } } }
private void PerformSetup(IExecutionStrategy strategy, ICakeContext context, CakeTask targetTask, IEnumerable <CakeTask> tasks, Stopwatch stopWatch, CakeReport report) { stopWatch.Restart(); PublishEvent(Setup, new SetupEventArgs(context)); if (_actions.Setup != null) { strategy.PerformSetup(_actions.Setup, new SetupContext(context, targetTask, tasks)); report.Add("**Setup**", stopWatch.Elapsed); } }
/// <summary> /// Runs the specified target. /// </summary> /// <param name="context">The context.</param> /// <param name="strategy">The execution strategy.</param> /// <param name="target">The target to run.</param> /// <returns>The resulting report.</returns> public async Task <CakeReport> RunTargetAsync(ICakeContext context, IExecutionStrategy strategy, string target) { if (target == null) { throw new ArgumentNullException(nameof(target)); } if (strategy == null) { throw new ArgumentNullException(nameof(strategy)); } // Ensure that registered actions are valid. _actions.Validate(); // Create a graph out of the tasks. var graph = CakeGraphBuilder.Build(_tasks); // Make sure target exist. if (!graph.Exist(target)) { const string format = "The target '{0}' was not found."; throw new CakeException(string.Format(CultureInfo.InvariantCulture, format, target)); } // This isn't pretty, but we need to keep track of exceptions thrown // while running a setup action, or a task. We do this since we don't // want to throw teardown exceptions if an exception was thrown previously. var exceptionWasThrown = false; Exception thrownException = null; var stopWatch = new Stopwatch(); var report = new CakeReport(); try { // Get all nodes to traverse in the correct order. var orderedTasks = graph.Traverse(target) .Select(y => _tasks.FirstOrDefault(x => x.Name.Equals(y, StringComparison.OrdinalIgnoreCase))).ToArray(); // Get target node var targetNode = orderedTasks .FirstOrDefault(node => node.Name.Equals(target, StringComparison.OrdinalIgnoreCase)); PerformSetup(strategy, context, targetNode, orderedTasks, stopWatch, report); foreach (var task in orderedTasks) { // Is this the current target? var isTarget = task.Name.Equals(target, StringComparison.OrdinalIgnoreCase); // Should we execute the task? var skipped = false; foreach (var criteria in task.Criterias) { if (!ShouldTaskExecute(context, task, criteria, isTarget)) { SkipTask(context, strategy, task, report, criteria); skipped = true; } } if (!skipped) { await ExecuteTaskAsync(context, strategy, stopWatch, task, report).ConfigureAwait(false); } } return(report); } catch (Exception ex) { exceptionWasThrown = true; thrownException = ex; throw; } finally { PerformTeardown(strategy, context, stopWatch, report, exceptionWasThrown, thrownException); } }
internal async Task ExecuteTaskAsync(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, bool isTarget, CakeReport report) { if (!ShouldTaskExecute(task, isTarget)) { SkipTask(context, strategy, task); return; } // Reset the stop watch. stopWatch.Reset(); stopWatch.Start(); PerformTaskSetup(context, strategy, task, false); bool exceptionWasThrown = false; try { // Execute the task. await strategy.ExecuteAsync(task, context); } catch (Exception exception) { _log.Error("An error occured when executing task '{0}'.", task.Name); exceptionWasThrown = true; // Got an error reporter? if (task.ErrorReporter != null) { ReportErrors(strategy, task.ErrorReporter, exception); } // Got an error handler? if (task.ErrorHandler != null) { HandleErrors(strategy, task.ErrorHandler, exception); } else { // No error handler defined for this task. // Rethrow the exception and let it propagate. throw; } } finally { if (task.FinallyHandler != null) { strategy.InvokeFinally(task.FinallyHandler); } PerformTaskTeardown(context, strategy, task, stopWatch.Elapsed, false, exceptionWasThrown); } // Add the task results to the report. report.Add(task.Name, stopWatch.Elapsed); }
private void PerformSetup(IExecutionStrategy strategy, ICakeContext context, Stopwatch stopWatch, CakeReport report) { stopWatch.Restart(); PublishEvent(Setup, new SetupEventArgs(context)); if (_setupAction != null) { strategy.PerformSetup(_setupAction, context); report.Add("**Setup**", stopWatch.Elapsed); } }
/// <summary> /// Runs the specified target. /// </summary> /// <param name="context">The context.</param> /// <param name="strategy">The execution strategy.</param> /// <param name="target">The target to run.</param> /// <returns>The resulting report.</returns> public CakeReport RunTarget(ICakeContext context, IExecutionStrategy strategy, string target) { if (target == null) { throw new ArgumentNullException("target"); } if (strategy == null) { throw new ArgumentNullException("strategy"); } var graph = CakeGraphBuilder.Build(_tasks); // Make sure target exist. if (!graph.Exist(target)) { const string format = "The target '{0}' was not found."; throw new CakeException(string.Format(CultureInfo.InvariantCulture, format, target)); } // This isn't pretty, but we need to keep track of exceptions thrown // while running a setup action, or a task. We do this since we don't // want to throw teardown exceptions if an exception was thrown previously. var exceptionWasThrown = false; try { PerformSetup(strategy); var stopWatch = new Stopwatch(); var report = new CakeReport(); foreach (var taskNode in graph.Traverse(target)) { // Get the task. var task = _tasks.FirstOrDefault(x => x.Name.Equals(taskNode, StringComparison.OrdinalIgnoreCase)); Debug.Assert(task != null, "Node should not be null."); // Is this the current target? var isTarget = task.Name.Equals(target, StringComparison.OrdinalIgnoreCase); // Should we execute the task? if (ShouldTaskExecute(task, isTarget)) { ExecuteTask(context, strategy, stopWatch, task, report); } else { SkipTask(strategy, task); } } return(report); } catch { exceptionWasThrown = true; throw; } finally { PerformTeardown(strategy, exceptionWasThrown); } }
private void ExecuteTask(ICakeContext context, IExecutionStrategy strategy, Stopwatch stopWatch, CakeTask task, CakeReport report) { // Reset the stop watch. stopWatch.Reset(); stopWatch.Start(); try { // Execute the task. strategy.Execute(task, context); } catch (Exception exception) { _log.Error("An error occured when executing task.", task.Name); // Got an error reporter? if (task.ErrorReporter != null) { ReportErrors(strategy, task.ErrorReporter, exception); } // Got an error handler? if (task.ErrorHandler != null) { HandleErrors(strategy, task.ErrorHandler, exception); } else { // No error handler defined for this task. // Rethrow the exception and let it propagate. throw; } } finally { if (task.FinallyHandler != null) { strategy.InvokeFinally(task.FinallyHandler); } } // Add the task results to the report. report.Add(task.Name, stopWatch.Elapsed); }