Example #1
0
        private static void Execute(
            NukeBuild build,
            ExecutableTarget target,
            IReadOnlyCollection <string> previouslyExecutedTargets,
            bool failureMode = false)
        {
            if (target.Status == ExecutionStatus.Skipped ||
                previouslyExecutedTargets.Contains(target.Name) ||
                HasSkippingCondition(target, target.DynamicConditions))
            {
                target.Status = ExecutionStatus.Skipped;
                build.OnTargetSkipped(target.Name);
                AppendToBuildAttemptFile(target.Name);
                return;
            }

            if (target.Actions.Count == 0)
            {
                target.Status = ExecutionStatus.Collective;
                return;
            }

            using (Logger.Block(target.Name))
            {
                target.Status = ExecutionStatus.Executing;
                build.OnTargetStart(target.Name);
                var stopwatch = Stopwatch.StartNew();
                try
                {
                    target.Actions.ForEach(x => x());
                    target.Status = ExecutionStatus.Executed;
                    build.OnTargetExecuted(target.Name);
                    AppendToBuildAttemptFile(target.Name);
                }
                catch (Exception exception)
                {
                    Logger.Error(exception);
                    target.Status = ExecutionStatus.Failed;
                    build.OnTargetFailed(target.Name);
                    if (!target.ProceedAfterFailure && !failureMode)
                    {
                        throw new TargetExecutionException(target.Name, exception);
                    }
                }
                finally
                {
                    target.Duration = stopwatch.Elapsed;
                }
            }
        }
Example #2
0
        private static bool IsMemberNull(MemberInfo member, NukeBuild build, ExecutableTarget target = null)
        {
            var from = target != null ? $"from target '{target.Name}' " : string.Empty;

            ControlFlow.Assert(member.HasCustomAttribute <InjectionAttributeBase>(),
                               $"Member '{member.Name}' is required {from}but not marked with an injection attribute.");

            if (NukeBuild.Host == HostType.Console)
            {
                InjectValueInteractive(build, member);
            }

            return(member.GetValue(build) == null);
        }
Example #3
0
        private static bool IsMemberNull(MemberInfo member, NukeBuild build, ExecutableTarget target = null)
        {
            member = member.DeclaringType != build.GetType()
                ? build.GetType().GetMember(member.Name).SingleOrDefault() ?? member
                : member;

            var from = target != null ? $"from target '{target.Name}' " : string.Empty;

            ControlFlow.Assert(member.HasCustomAttribute <ValueInjectionAttributeBase>(),
                               $"Member '{member.Name}' is required {from}but not marked with an injection attribute.");

            if (NukeBuild.Host == HostType.Console)
            {
                TryInjectValueInteractive(member, build);
            }

            return(member.GetValue(build) == null);
        }
Example #4
0
        private static void Execute(
            NukeBuild build,
            ExecutableTarget target,
            IReadOnlyCollection <string> previouslyExecutedTargets,
            bool failureMode = false)
        {
            if (target.Status == ExecutionStatus.Skipped ||
                previouslyExecutedTargets.Contains(target.Name) ||
                target.DynamicConditions.Any(x => !x()))
            {
                target.Status = ExecutionStatus.Skipped;
                build.OnTargetSkipped(target.Name);
                AppendToBuildAttemptFile(target.Name);
                return;
            }

            using (Logger.Block(target.Name))
            {
                target.Status = ExecutionStatus.Executing;
                build.OnTargetStart(target.Name);
                var stopwatch = Stopwatch.StartNew();
                try
                {
                    target.Actions.ForEach(x => x());
                    target.Status = ExecutionStatus.Executed;
                    build.OnTargetExecuted(target.Name);
                    AppendToBuildAttemptFile(target.Name);
                }
                catch
                {
                    target.Status = ExecutionStatus.Failed;
                    build.OnTargetFailed(target.Name);
                    if (!target.ContinueOnFailure && !failureMode)
                    {
                        throw;
                    }
                }
                finally
                {
                    target.Duration = stopwatch.Elapsed;
                }
            }
        }
Example #5
0
        private static bool HasSkippingCondition(ExecutableTarget target, IEnumerable <Expression <Func <bool> > > conditions)
        {
            // TODO: trim outer parenthesis
            string GetSkipReason(Expression <Func <bool> > condition) =>
            condition.Body.ToString()
            .Replace("False", "false")
            .Replace("True", "true")
            .Replace("OrElse", "||")
            .Replace("AndAlso", "&&")
            // TODO: should get actual build type name
            .Replace("value(Build).", string.Empty);

            target.SkipReason = null; // solely for testing

            foreach (var condition in conditions)
            {
                if (!condition.Compile().Invoke())
                {
                    target.SkipReason = GetSkipReason(condition);
                }
            }

            return(target.SkipReason != null);
        }
Example #6
0
 private static bool HasSkippingCondition(ExecutableTarget target, IEnumerable <Expression <Func <bool> > > conditions)
 {
Example #7
0
 private static IDictionary <string, string> GetTargetProperties(NukeBuild build, ExecutableTarget target)
 {
     return(new Dictionary <string, string>
     {
         ["target_name"] = target.Name,
         ["target_duration"] = target.Duration.TotalSeconds.ToString("F0"),
         ["target_current_partition"] = build.Partition.Part.ToString(),
         ["target_total_partitions"] = build.Partition.Total.ToString()
     });
 }
Example #8
0
        public static IReadOnlyCollection <ExecutableTarget> CreateAll <T>(
            T build,
            params Expression <Func <T, Target> >[] defaultTargetExpressions)
            where T : NukeBuild
        {
            var defaultTargets = defaultTargetExpressions.Select(x => x.Compile().Invoke(build)).ToList();
            var properties     = build.GetType()
                                 .GetProperties(ReflectionService.Instance)
                                 .Where(x => x.PropertyType == typeof(Target)).ToList();

            var executables = new List <ExecutableTarget>();

            foreach (var property in properties)
            {
                var factory    = (Target)property.GetValue(build);
                var definition = new TargetDefinition();
                factory.Invoke(definition);

                var target = new ExecutableTarget
                {
                    Name                = property.Name,
                    Member              = property,
                    Definition          = definition,
                    Description         = definition.Description,
                    Factory             = factory,
                    IsDefault           = defaultTargets.Contains(factory),
                    DynamicConditions   = definition.DynamicConditions,
                    StaticConditions    = definition.StaticConditions,
                    DependencyBehavior  = definition.DependencyBehavior,
                    ProceedAfterFailure = definition.IsProceedAfterFailure,
                    AssuredAfterFailure = definition.IsAssuredAfterFailure,
                    Requirements        = definition.Requirements,
                    Actions             = definition.Actions,
                    Listed              = !definition.IsInternal
                };

                executables.Add(target);
            }

            foreach (var executable in executables)
            {
                IEnumerable <ExecutableTarget> GetDependencies(
                    Func <TargetDefinition, IReadOnlyList <Target> > directDependenciesSelector,
                    Func <TargetDefinition, IReadOnlyList <Target> > indirectDependenciesSelector)
                {
                    foreach (var factoryDependency in directDependenciesSelector(executable.Definition))
                    {
                        yield return(executables.Single(x => x.Factory == factoryDependency));
                    }

                    foreach (var otherExecutables in executables.Where(x => x != executable))
                    {
                        var otherDependencies = indirectDependenciesSelector(otherExecutables.Definition);
                        if (otherDependencies.Any(x => x == executable.Factory))
                        {
                            yield return(otherExecutables);
                        }
                    }
                }

                executable.ExecutionDependencies.AddRange(GetDependencies(x => x.DependsOnTargets, x => x.DependentForTargets));
                executable.OrderDependencies.AddRange(GetDependencies(x => x.AfterTargets, x => x.BeforeTargets));
                executable.TriggerDependencies.AddRange(GetDependencies(x => x.TriggeredByTargets, x => x.TriggersTargets));
                executable.Triggers.AddRange(GetDependencies(x => x.TriggersTargets, x => x.TriggeredByTargets));
            }

            return(executables);
        }
Example #9
0
 public void OnTargetFailed(NukeBuild build, ExecutableTarget target)
 {
     build.OnTargetFailed(target.Name);
 }
Example #10
0
 public void OnTargetRunning(NukeBuild build, ExecutableTarget target)
 {
     build.OnTargetRunning(target.Name);
 }
Example #11
0
 public void OnTargetSucceeded(NukeBuild build, ExecutableTarget target)
 {
     Telemetry.TargetSucceeded(target, build);
 }