Пример #1
0
        /// <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);
            }
        }
Пример #2
0
        /// <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);
            }
        }