/// <summary> /// Skips the specified task. /// </summary> /// <param name="task">The task to skip.</param> public void Skip(CakeTask task) { if (task != null) { _log.Verbose("Skipping task: {0}", task.Name); } }
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)); } }
/// <summary> /// Sets the task's finally handler. /// </summary> /// <param name="task">The task.</param> /// <param name="finallyHandler">The finally handler.</param> /// <exception cref="CakeException">There can only be one finally handler per task.</exception> /// <exception cref="ArgumentNullException"><paramref name="finallyHandler"/> is null.</exception> public static void SetFinallyHandler(this CakeTask task, Action finallyHandler) { if (task.FinallyHandler != null) { throw new CakeException("There can only be one finally handler per task."); } task.FinallyHandler = finallyHandler ?? throw new ArgumentNullException(nameof(finallyHandler)); }
/// <summary> /// Sets the task's error reporter. /// </summary> /// <param name="task">The task.</param> /// <param name="errorReporter">The error reporter.</param> /// <exception cref="CakeException">There can only be one error reporter per task.</exception> /// <exception cref="ArgumentNullException"><paramref name="errorReporter"/> is null.</exception> public static void SetErrorReporter(this CakeTask task, Action <Exception> errorReporter) { if (task.ErrorReporter != null) { throw new CakeException("There can only be one error reporter per task."); } task.ErrorReporter = errorReporter ?? throw new ArgumentNullException(nameof(errorReporter)); }
/// <summary> /// Adds the action to the task's delayed actions. /// </summary> /// <param name="task">The task.</param> /// <param name="action">The action.</param> /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> public static void AddDelayedAction(this CakeTask task, Action action) { if (action == null) { throw new ArgumentNullException(nameof(action)); } task.DelayedActions.Enqueue(action); }
/// <summary> /// Adds the action to the task's actions. /// </summary> /// <param name="task">The task.</param> /// <param name="action">The action.</param> /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> public static void AddAction(this CakeTask task, Func <ICakeContext, Task> action) { if (action == null) { throw new ArgumentNullException(nameof(action)); } task.Actions.Add(action); }
public static void AddCriteria(this CakeTask task, Func <ICakeContext, bool> criteria, string message = null) { if (criteria == null) { throw new ArgumentNullException(nameof(criteria)); } task.Criterias.Add(new CakeTaskCriteria(criteria, message)); }
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); } }
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, false); // Add the skipped task to the report. report.AddSkipped(task.Name); }
/// <summary> /// Adds the dependee to the task's dependees. /// </summary> /// <param name="task">The task.</param> /// <param name="name">The name of the dependee.</param> /// <param name="required">Whether or not the dependee is required.</param> /// <exception cref="CakeException">The task already is a dependee.</exception> public static void AddDependee(this CakeTask task, string name, bool required = true) { if (task.Dependees.Any(x => x.Name == name)) { const string format = "The task '{0}' already is a dependee of '{1}'."; var message = string.Format(CultureInfo.InvariantCulture, format, task.Name, name); throw new CakeException(message); } task.Dependees.Add(new CakeTaskDependency(name, required)); }
private static bool ShouldTaskExecute(CakeTask task) { foreach (var criteria in task.Criterias) { if (!criteria()) { return(false); } } return(true); }
/// <summary> /// Skips the specified task. /// </summary> /// <param name="task">The task to skip.</param> public void Skip(CakeTask task) { if (task != null) { _log.Verbose(string.Empty); _log.Verbose("========================================"); _log.Verbose(task.Name); _log.Verbose("========================================"); _log.Verbose("Skipping task: {0}", task.Name); } }
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> /// Creates and registers a new Cake task. /// </summary> /// <param name="name">The name of the task.</param> /// <returns> /// A <see cref="CakeTaskBuilder"/> used to configure the task. /// </returns> public CakeTaskBuilder RegisterTask(string name) { if (_tasks.Any(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase))) { const string format = "Another task with the name '{0}' has already been added."; throw new CakeException(string.Format(CultureInfo.InvariantCulture, format, name)); } var task = new CakeTask(name); _tasks.Add(task); return(new CakeTaskBuilder(task)); }
/// <summary> /// Adds a criteria to the task that is invoked when the task is invoked. /// </summary> /// <param name="task">The task.</param> /// <param name="criteria">The criteria.</param> public static void AddCriteria(this CakeTask task, Func <bool> criteria) { if (task == null) { throw new ArgumentNullException(nameof(task)); } if (criteria == null) { throw new ArgumentNullException(nameof(criteria)); } task.AddCriteria(context => criteria()); }
/// <summary> /// Sets the task's error handler. /// </summary> /// <param name="task">The task.</param> /// <param name="errorHandler">The error handler.</param> /// <exception cref="CakeException">There can only be one error handler per task.</exception> /// <exception cref="ArgumentNullException"><paramref name="errorHandler"/> is null.</exception> public static void SetErrorHandler(this CakeTask task, Func <Exception, ICakeContext, Task> errorHandler) { if (task.ErrorHandler != null) { throw new CakeException("There can only be one error handler per task."); } if (errorHandler is null) { throw new ArgumentNullException(nameof(errorHandler)); } task.ErrorHandler = errorHandler; }
/// <summary> /// Skips the specified task. /// </summary> /// <param name="task">The task to skip.</param> /// <param name="criteria">The criteria that caused the task to be skipped.</param> public void Skip(CakeTask task, CakeTaskCriteria criteria) { if (task != null) { _log.Verbose(string.Empty); _log.Verbose("========================================"); _log.Verbose(task.Name); _log.Verbose("========================================"); var message = string.IsNullOrWhiteSpace(criteria.Message) ? task.Name : criteria.Message; _log.Verbose("Skipping task: {0}", message); } }
/// <summary> /// Executes the specified task. /// </summary> /// <param name="task">The task to execute.</param> /// <param name="context">The context.</param> public void Execute(CakeTask task, ICakeContext context) { if (task != null) { _log.Information(string.Empty); _log.Information("========================================"); _log.Information(task.Name); _log.Information("========================================"); _log.Verbose("Executing task: {0}", task.Name); task.Execute(context); _log.Verbose("Finished executing task: {0}", task.Name); } }
/// <summary> /// Executes the specified task asynchronously. /// </summary> /// <param name="task">The task to execute.</param> /// <param name="context">The context.</param> public async Task ExecuteAsync(CakeTask task, ICakeContext context) { if (task != null) { _log.Information(string.Empty); _log.Information("========================================"); _log.Information(task.Name); _log.Information("========================================"); _log.Verbose("Executing task: {0}", task.Name); await Task.Run(() => task.Execute(context)); _log.Verbose("Finished executing task: {0}", task.Name); } }
/// <summary> /// Sets the task's error handler. /// </summary> /// <param name="task">The task.</param> /// <param name="errorHandler">The error handler.</param> /// <exception cref="CakeException">There can only be one error handler per task.</exception> /// <exception cref="ArgumentNullException"><paramref name="errorHandler"/> is null.</exception> public static void SetErrorHandler(this CakeTask task, Action <Exception, ICakeContext> errorHandler) { if (task.ErrorHandler != null) { throw new CakeException("There can only be one error handler per task."); } if (errorHandler is null) { throw new ArgumentNullException(nameof(errorHandler)); } #pragma warning disable CS1998 task.ErrorHandler = async(ex, context) => errorHandler(ex, context); #pragma warning restore CS1998 }
private static bool ShouldTaskExecute(ICakeContext context, CakeTask task, CakeTaskCriteria criteria, bool isTarget) { if (!criteria.Predicate(context)) { if (isTarget) { // It's not OK to skip the target task. // See issue #106 (https://github.com/cake-build/cake/issues/106) const string format = "Could not reach target '{0}' since it was skipped due to a criteria."; var message = string.Format(CultureInfo.InvariantCulture, format, task.Name); throw new CakeException(message); } return(false); } return(true); }
/// <summary> /// Sets the task's finally handler. /// </summary> /// <param name="task">The task.</param> /// <param name="finallyHandler">The finally handler.</param> /// <exception cref="CakeException">There can only be one finally handler per task.</exception> /// <exception cref="ArgumentNullException"><paramref name="finallyHandler"/> is null.</exception> public static void SetFinallyHandler(this CakeTask task, Action finallyHandler) { if (task.FinallyHandler != null) { throw new CakeException("There can only be one finally handler per task."); } if (finallyHandler is null) { throw new ArgumentNullException(nameof(finallyHandler)); } #pragma warning disable CS1998 task.FinallyHandler = async() => finallyHandler(); #pragma warning restore CS1998 }
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 static bool ShouldTaskExecute(ICakeContext context, CakeTask task, CakeTaskCriteria criteria, bool isTarget) { if (!criteria.Predicate(context)) { if (isTarget) { // It's not OK to skip the target task. // See issue #106 (https://github.com/cake-build/cake/issues/106) var message = string.IsNullOrWhiteSpace(criteria.Message) ? $"Could not reach target '{task.Name}' since it was skipped due to a criteria." : $"Could not reach target '{task.Name}' since it was skipped due to a criteria: {criteria.Message}."; throw new CakeException(message); } return(false); } return(true); }
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); }
private static bool IsDelegatedTask(CakeTask task) { return(task != null && !task.Actions.Any()); }
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); } }
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); }
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; } }
internal void SkipTask(ICakeContext context, IExecutionStrategy strategy, CakeTask task) { PerformTaskSetup(context, strategy, task, true); strategy.Skip(task); PerformTaskTeardown(context, strategy, task, TimeSpan.Zero, true, false); }