Beispiel #1
0
        public static void InjectValues(NukeBuild build)
        {
            var anyInjected = false;

            var injectionMembers = build.GetInjectionMembers()
                                   .OrderByDescending(x => x.GetCustomAttribute <ParameterAttribute>() != null);

            foreach (var member in injectionMembers)
            {
                var attributes = member.GetCustomAttributes().OfType <InjectionAttributeBase>().ToList();
                if (attributes.Count == 0)
                {
                    continue;
                }
                ControlFlow.Assert(attributes.Count == 1, $"Member '{member.Name}' has multiple injection attributes applied.");

                var attribute  = attributes.Single();
                var memberType = (member as FieldInfo)?.FieldType ?? ((PropertyInfo)member).PropertyType;
                var value      = attribute.GetValue(member.Name, memberType);
                if (value == null)
                {
                    continue;
                }

                var valueType = value.GetType();
                ControlFlow.Assert(memberType.IsAssignableFrom(valueType),
                                   $"Field '{member.Name}' must be of type '{valueType.Name}' to get its valued injected from '{attribute.GetType().Name}'.");
                ReflectionService.SetValue(build, member, value);

                anyInjected = true;
            }

            if (anyInjected)
            {
                Logger.Log();
            }
        }
        public static IReadOnlyCollection <TargetDefinition> GetExecutingTargets(NukeBuild build)
        {
            ControlFlow.Assert(build.TargetDefinitions.All(x => !x.Name.EqualsOrdinalIgnoreCase(BuildExecutor.DefaultTarget)),
                               "The name 'default' cannot be used as target name.");

            var invokedTargets   = build.InvokedTargets.Select(x => GetDefinition(x, build)).ToList();
            var executingTargets = GetUnfilteredExecutingTargets(build, invokedTargets);
            var skippedTargets   = executingTargets
                                   .Where(x => !invokedTargets.Contains(x) &&
                                          build.SkippedTargets != null &&
                                          (build.SkippedTargets.Length == 0 ||
                                           build.SkippedTargets.Contains(x.Name, StringComparer.OrdinalIgnoreCase))).ToList();

            skippedTargets.ForEach(x => x.Conditions.Add(() => false));

            string[] GetNames(IEnumerable <TargetDefinition> targets)
            => targets.Select(x => x.Name).ToArray();

            ReflectionService.SetValue(build, nameof(NukeBuild.InvokedTargets), GetNames(invokedTargets));
            ReflectionService.SetValue(build, nameof(NukeBuild.SkippedTargets), GetNames(skippedTargets));
            ReflectionService.SetValue(build, nameof(NukeBuild.ExecutingTargets), GetNames(executingTargets.Except(skippedTargets)));

            return(executingTargets);
        }