Exemplo n.º 1
0
        public static void ValidateRequirements(NukeBuild build, IReadOnlyCollection <ExecutableTarget> executingTargets)
        {
            foreach (var target in executingTargets)
            {
                foreach (var requirement in target.Requirements)
                {
                    if (requirement is Expression <Func <bool> > boolExpression)
                    {
                        // TODO: same as HasSkippingCondition.GetSkipReason
                        ControlFlow.Assert(boolExpression.Compile().Invoke(), $"Target '{target.Name}' requires '{requirement.Body}'.");
                    }
                    else if (IsMemberNull(requirement.GetMemberInfo(), build, target))
                    {
                        ControlFlow.Fail($"Target '{target.Name}' requires member '{requirement.GetMemberInfo().Name}' to be not null.");
                    }
                }
            }

            var requiredMembers = ValueInjectionUtility.GetInjectionMembers(build.GetType())
                                  .Select(x => x.Member)
                                  .Where(x => x.HasCustomAttribute <RequiredAttribute>());

            foreach (var member in requiredMembers)
            {
                if (IsMemberNull(member, build))
                {
                    ControlFlow.Fail($"Member '{member.Name}' is required to be not null.");
                }
            }
        }
Exemplo n.º 2
0
        private static void WriteCompletionFile(NukeBuild build)
        {
            var completionItems = new SortedDictionary <string, string[]>();

            var targets = build.ExecutableTargets.OrderBy(x => x.Name).ToList();

            completionItems[InvokedTargetsParameterName] = targets.Where(x => x.Listed).Select(x => x.Name).ToArray();
            completionItems[SkippedTargetsParameterName] = targets.Select(x => x.Name).ToArray();

            var parameters = ValueInjectionUtility.GetParameterMembers(build.GetType(), includeUnlisted: false);

            foreach (var parameter in parameters)
            {
                var parameterName = ParameterService.GetParameterMemberName(parameter);
                if (completionItems.ContainsKey(parameterName))
                {
                    continue;
                }

                var subItems = ParameterService.GetParameterValueSet(parameter, build)?.Select(x => x.Text);
                completionItems[parameterName] = subItems?.ToArray();
            }

            SerializationTasks.YamlSerializeToFile(completionItems, GetCompletionFile(NukeBuild.RootDirectory));
        }
Exemplo n.º 3
0
        private static IDictionary <string, string> GetBuildProperties(NukeBuild build)
        {
            var startTimeString = EnvironmentInfo.Variables.GetValueOrDefault(Constants.GlobalToolStartTimeEnvironmentKey);
            var compileTime     = startTimeString != null
                ? DateTime.Now.Subtract(DateTime.Parse(startTimeString))
                : default(TimeSpan?);

            return(new Dictionary <string, string>
            {
                ["compile_time"] = compileTime?.TotalSeconds.ToString("F0"),
                ["target_framework"] = EnvironmentInfo.Framework.ToString(),
                ["host"] = GetTypeName(NukeBuild.Host),
                ["build_type"] = NukeBuild.BuildProjectFile != null ? "Project" : "Global Tool",
                ["num_targets"] = build.ExecutableTargets.Count.ToString(),
                ["num_custom_extensions"] = build.BuildExtensions.Select(x => x.GetType()).Count(IsCustomType).ToString(),
                ["num_custom_components"] = build.GetType().GetInterfaces().Count(IsCustomType).ToString(),
                ["num_partitioned_targets"] = build.ExecutableTargets.Count(x => x.PartitionSize.HasValue).ToString(),
                ["num_secrets"] = ValueInjectionUtility.GetParameterMembers(build.GetType(), includeUnlisted: true)
                                  .Count(x => x.HasCustomAttribute <SecretAttribute>()).ToString(),
                ["config_generators"] = build.GetType().GetCustomAttributes <ConfigurationAttributeBase>()
                                        .Select(GetTypeName).Distinct().OrderBy(x => x).JoinCommaSpace(),
                ["build_components"] = build.GetType().GetInterfaces().Where(x => IsCommonType(x) && x != typeof(INukeBuild))
                                       .Select(GetTypeName).Distinct().OrderBy(x => x).JoinCommaSpace()
            });
        }
Exemplo n.º 4
0
        public void OnBeforeLogo(
            NukeBuild build,
            IReadOnlyCollection <ExecutableTarget> executableTargets)
        {
            var completionItems = new SortedDictionary <string, string[]>();

            var targets = build.ExecutableTargets.OrderBy(x => x.Name).ToList();

            completionItems[InvokedTargetsParameterName] = targets.Where(x => x.Listed).Select(x => x.Name).ToArray();
            completionItems[SkippedTargetsParameterName] = targets.Select(x => x.Name).ToArray();

            var parameters = ValueInjectionUtility.GetParameterMembers(build.GetType(), includeUnlisted: false);

            foreach (var parameter in parameters)
            {
                var parameterName = ParameterService.GetParameterMemberName(parameter);
                if (completionItems.ContainsKey(parameterName))
                {
                    continue;
                }

                var subItems = ParameterService.GetParameterValueSet(parameter, build)?.Select(x => x.Text);
                completionItems[parameterName] = subItems?.ToArray();
            }

            SerializationTasks.YamlSerializeToFile(completionItems, GetCompletionFile(NukeBuild.RootDirectory));

            if (EnvironmentInfo.GetParameter <bool>(CompletionParameterName))
            {
                Environment.Exit(exitCode: 0);
            }
        }
Exemplo n.º 5
0
 protected virtual IEnumerable <TeamCityParameter> GetGlobalParameters(NukeBuild build, IReadOnlyCollection <ExecutableTarget> relevantTargets)
 {
     return(ValueInjectionUtility.GetParameterMembers(build.GetType(), includeUnlisted: false)
            .Except(relevantTargets.SelectMany(x => x.Requirements
                                               .Where(y => !(y is Expression <Func <bool> >))
                                               .Select(y => y.GetMemberInfo())))
            .Where(x => x.DeclaringType != typeof(NukeBuild) || x.Name == nameof(NukeBuild.Verbosity))
            .Select(x => GetParameter(x, build, required: false)));
 }
Exemplo n.º 6
0
 protected virtual IEnumerable <TeamCityParameter> GetGlobalParameters(NukeBuild build, IReadOnlyCollection <ExecutableTarget> relevantTargets)
 {
     return(ValueInjectionUtility.GetParameterMembers(build.GetType(), includeUnlisted: false)
            // TODO: except build.ExecutableTargets ?
            .Except(relevantTargets.SelectMany(x => x.Requirements
                                               .Where(y => y is not Expression <Func <bool> >)
                                               .Select(y => y.GetMemberInfo())))
            .Where(x => !x.HasCustomAttribute <SecretAttribute>())
            .Where(x => x.DeclaringType != typeof(NukeBuild) || x.Name == nameof(NukeBuild.Verbosity))
            .Select(x => GetParameter(x, build, required: false))
            .Concat(GetSecretParameters(build))
            .Concat(GetDefaultParameters()));
 }
Exemplo n.º 7
0
        public static string GetParametersText(NukeBuild build)
        {
            var defaultTarget = build.ExecutableTargets.SingleOrDefault(x => x.IsDefault);
            var builder       = new StringBuilder();

            var parameters = ValueInjectionUtility.GetParameterMembers(build.GetType(), includeUnlisted: false)
                             .OrderBy(x => x.Name).ToList();
            var padRightParameter = Math.Max(parameters.Max(x => x.Name.Length), val2: 16);

            void PrintParameter(MemberInfo parameter)
            {
                var description = SplitLines(
                    // TODO: remove
                    ParameterService.GetParameterDescription(parameter)
                    ?.Replace("{default_target}", defaultTarget?.Name).Append(".")
                    ?? "<no description>");
                var parameterName = ParameterService.GetParameterDashedName(parameter);

                builder.AppendLine($"  --{parameterName.PadRight(padRightParameter)}  {description.First()}");
                foreach (var line in description.Skip(count: 1))
                {
                    builder.AppendLine($"{new string(c: ' ', count: padRightParameter + 6)}{line}");
                }
            }

            builder.AppendLine("Parameters:");

            var customParameters = parameters.Where(x => x.DeclaringType != typeof(NukeBuild)).ToList();

            if (customParameters.Count > 0)
            {
                builder.AppendLine();
            }
            foreach (var parameter in customParameters)
            {
                PrintParameter(parameter);
            }

            builder.AppendLine();

            var inheritedParameters = parameters.Where(x => x.DeclaringType == typeof(NukeBuild));

            foreach (var parameter in inheritedParameters)
            {
                PrintParameter(parameter);
            }

            return(builder.ToString());
        }
Exemplo n.º 8
0
        public void OnBuildCreated(NukeBuild build, IReadOnlyCollection <ExecutableTarget> executableTargets)
        {
            // TODO: probably remove
            if (!Directory.Exists(Constants.GetNukeDirectory(NukeBuild.RootDirectory)))
            {
                return;
            }

            var parameterMembers = ValueInjectionUtility.GetParameterMembers(build.GetType(), includeUnlisted: true);
            var passwords        = new Dictionary <string, string>();

            IEnumerable <string> ConvertToArguments(string profile, string name, string[] values)
            {
                var member          = parameterMembers.SingleOrDefault(x => ParameterService.GetParameterMemberName(x).EqualsOrdinalIgnoreCase(name));
                var scalarType      = member?.GetMemberType().GetScalarType();
                var mustDecrypt     = (member?.HasCustomAttribute <SecretAttribute>() ?? false) && !BuildServerConfigurationGeneration.IsActive;
                var decryptedValues = values.Select(x => mustDecrypt ? DecryptValue(profile, name, x) : x);
                var convertedValues = decryptedValues.Select(x => ConvertValue(scalarType, x)).ToList();

                Log.Verbose("Passing value for {Member} ({Value})",
                            member?.GetDisplayName() ?? "<unresolved>",
                            !mustDecrypt ? convertedValues.JoinComma() : "secret");
                return(new[] { $"--{ParameterService.GetParameterDashedName(name)}" }.Concat(convertedValues));
            }

            string DecryptValue(string profile, string name, string value)
            => EncryptionUtility.Decrypt(
                value,
                passwords[profile] = passwords.GetValueOrDefault(profile) ?? EncryptionUtility.GetPassword(profile),
                name);

            // TODO: Abstract AbsolutePath/Solution/Project etc.
            string ConvertValue(Type scalarType, string value)
            => scalarType == typeof(AbsolutePath) ||
            typeof(Solution).IsAssignableFrom(scalarType) ||
            scalarType == typeof(Project)
                    ? EnvironmentInfo.WorkingDirectory.GetUnixRelativePathTo(NukeBuild.RootDirectory / value)
                    : value;

            var arguments = GetParameters().SelectMany(x => ConvertToArguments(x.Profile, x.Name, x.Values)).ToArray();

            ParameterService.Instance.ArgumentsFromFilesService = new ParameterService(() => arguments, () => throw new NotSupportedException());
        }
        public void OnBuildCreated(NukeBuild build, IReadOnlyCollection <ExecutableTarget> executableTargets)
        {
            // TODO: probably remove
            if (!Directory.Exists(Constants.GetNukeDirectory(NukeBuild.RootDirectory)))
            {
                return;
            }

            var parameterMembers = ValueInjectionUtility.GetParameterMembers(build.GetType(), includeUnlisted: true);
            var passwords        = new Dictionary <string, byte[]>();

            IEnumerable <string> ConvertToArguments(string profile, string name, string[] values)
            {
                var member          = parameterMembers.SingleOrDefault(x => x.Name.EqualsOrdinalIgnoreCase(name));
                var scalarType      = member?.GetMemberType().GetScalarType();
                var mustDecrypt     = (member?.HasCustomAttribute <SecretAttribute>() ?? false) && !GenerationMode;
                var decryptedValues = values.Select(x => mustDecrypt ? DecryptValue(profile, name, x) : x);
                var convertedValues = decryptedValues.Select(x => ConvertValue(scalarType, x));

                Logger.Trace($"Passing argument for '{name}'{(member != null ? $" on '{member.DeclaringType.NotNull().Name}'" : string.Empty)}.");
                return(new[] { $"--{ParameterService.GetParameterDashedName(name)}" }.Concat(convertedValues));
            }

            string DecryptValue(string profile, string name, string value)
            => EncryptionUtility.Decrypt(
                value,
                passwords[profile] = passwords.GetValueOrDefault(profile) ?? Encoding.UTF8.GetBytes(EncryptionUtility.GetPassword(profile)),
                name);

            // TODO: Abstract AbsolutePath/Solution/Project etc.
            string ConvertValue(Type scalarType, string value)
            => scalarType == typeof(AbsolutePath) ||
            typeof(Solution).IsAssignableFrom(scalarType) ||
            scalarType == typeof(Project)
                    ? EnvironmentInfo.WorkingDirectory.GetUnixRelativePathTo(NukeBuild.RootDirectory / value)
                    : value;

            var arguments = GetParameters().SelectMany(x => ConvertToArguments(x.Profile, x.Name, x.Values)).ToArray();

            ParameterService.Instance.ArgumentsFromFilesService = new ParameterService(() => arguments, () => throw new NotSupportedException());
        }
Exemplo n.º 10
0
        // ReSharper disable once CognitiveComplexity
        public static JObject GetBuildSchema(NukeBuild build)
        {
            var parameters = ValueInjectionUtility
                             .GetParameterMembers(build.GetType(), includeUnlisted: true)
                             // .Where(x => x.DeclaringType != typeof(NukeBuild))
                             .Select(x =>
                                     new
            {
                Name        = ParameterService.GetParameterMemberName(x),
                Description = ParameterService.GetParameterDescription(x),
                MemberType  = x.GetMemberType(),
                ScalarType  = x.GetMemberType().GetScalarType(),
                EnumValues  = ParameterService.GetParameterValueSet(x, build)?.Select(x => x.Text),
                IsRequired  = x.HasCustomAttribute <RequiredAttribute>(),
                IsSecret    = x.HasCustomAttribute <SecretAttribute>()
            }).ToList();

            string GetJsonType(Type type)
            => type.IsCollectionLike()
                    ? "array"
                    : type.GetScalarType() == typeof(int)
                        ? "integer"
                        : type.GetScalarType() == typeof(bool)
                            ? "boolean"
                            : "string";

            var schema = JObject.Parse(@"
{
  ""$schema"": ""http://json-schema.org/draft-04/schema#"",
  ""title"": ""Build Schema"",
  ""$ref"": ""#/definitions/build"",
  ""definitions"": {
    ""build"": {
      ""type"": ""object"",
      ""properties"": {
      }
    }
  }
}
");

            var properties = schema["definitions"].NotNull()["build"].NotNull()["properties"].NotNull();

            foreach (var parameter in parameters)
            {
                var property = properties[parameter.Name] = new JObject();
                property["type"] = GetJsonType(parameter.MemberType);

                if (parameter.Description != null)
                {
                    property["description"] = parameter.Description;
                }

                if (parameter.IsSecret)
                {
                    property["default"] = "Secrets must be entered via 'nuke :secrets [profile]'";
                }

                if (parameter.EnumValues != null && !parameter.MemberType.IsCollectionLike())
                {
                    property["enum"] = new JArray(parameter.EnumValues);
                }

                if (parameter.MemberType.IsCollectionLike())
                {
                    property["items"] = new JObject();
                    property["items"].NotNull()["type"] = GetJsonType(parameter.ScalarType);
                    if (parameter.EnumValues != null)
                    {
                        property["items"].NotNull()["enum"] = new JArray(parameter.EnumValues);
                    }
                }
            }

            return(schema);
        }
Exemplo n.º 11
0
        public static string GetParametersText(NukeBuild build)
        {
            var defaultTargets = build.ExecutableTargets.Where(x => x.IsDefault).Select(x => x.Name).ToList();
            var builder        = new StringBuilder();

            var parameters        = ValueInjectionUtility.GetParameterMembers(build.GetType(), includeUnlisted: false);
            var padRightParameter = Math.Max(parameters.Max(x => ParameterService.GetParameterDashedName(x).Length), val2: 16);

            List <string> SplitLines(string text)
            {
                var words = new Queue <string>(text.Split(' ').ToList());
                var lines = new List <string> {
                    string.Empty
                };

                foreach (var word in words)
                {
                    var nextLength = padRightParameter + 6 + lines.Last().Length + word.Length;
                    if (nextLength >= Console.BufferWidth || nextLength > 90)
                    {
                        lines.Add(string.Empty);
                    }

                    lines[lines.Count - 1] = $"{lines.Last()} {word}";
                }

                return(lines);
            }

            void PrintParameter(MemberInfo parameter)
            {
                var description = SplitLines(
                    // TODO: remove
                    ParameterService.GetParameterDescription(parameter)
                    ?.Replace("{default_target}", defaultTargets.Count > 0 ? defaultTargets.JoinCommaSpace() : "<none>")
                    .TrimEnd(".").Append(".")
                    ?? "<no description>");
                var parameterName = ParameterService.GetParameterDashedName(parameter);

                builder.AppendLine($"  --{parameterName.PadRight(padRightParameter)}  {description.First()}");
                foreach (var line in description.Skip(count: 1))
                {
                    builder.AppendLine($"{new string(c: ' ', count: padRightParameter + 6)}{line}");
                }
            }

            builder.AppendLine("Parameters:");

            var customParameters = parameters.Where(x => x.DeclaringType != typeof(NukeBuild)).ToList();

            if (customParameters.Count > 0)
            {
                builder.AppendLine();
            }
            foreach (var parameter in customParameters)
            {
                PrintParameter(parameter);
            }

            builder.AppendLine();

            var inheritedParameters = parameters.Where(x => x.DeclaringType == typeof(NukeBuild));

            foreach (var parameter in inheritedParameters)
            {
                PrintParameter(parameter);
            }

            return(builder.ToString());
        }
Exemplo n.º 12
0
 T INukeBuild.TryGetValue <T>(Expression <Func <object> > parameterExpression)
 {
     return(ValueInjectionUtility.TryGetValue <T>(parameterExpression));
 }