Esempio n. 1
0
        public ComplexInterfaceInfo(string name, Type type, InfoAttribute infoAttribute, bool?required, List <Type> seenTypes)
        {
            Name          = name;
            Type          = type;
            InfoAttribute = infoAttribute;

            Required    = required ?? InfoAttribute.Required;
            ReadOnly    = InfoAttribute.ReadOnly;
            Description = InfoAttribute.Description.ToPsSingleLine();

            var unwrappedType = Type.Unwrap();
            var hasBeenSeen   = seenTypes?.Contains(unwrappedType) ?? false;

            (seenTypes ?? (seenTypes = new List <Type>())).Add(unwrappedType);
            NestedInfos = hasBeenSeen ? new ComplexInterfaceInfo[] {} :
            unwrappedType.GetInterfaces()
            .Concat(InfoAttribute.PossibleTypes)
            .SelectMany(pt => pt.GetProperties()
                        .SelectMany(pi => pi.GetCustomAttributes(true).OfType <InfoAttribute>()
                                    .Select(ia => ia.ToComplexInterfaceInfo(pi.Name, pi.PropertyType, seenTypes: seenTypes))))
            .Where(cii => !cii.ReadOnly).OrderByDescending(cii => cii.Required).ToArray();
            // https://stackoverflow.com/a/503359/294804
            var associativeArrayInnerType = Type.GetInterfaces()
                                            .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IAssociativeArray <>))
                                            ?.GetTypeInfo().GetGenericArguments().First();

            if (!hasBeenSeen && associativeArrayInnerType != null)
            {
                var anyInfo = new InfoAttribute {
                    Description = "This indicates any property can be added to this object."
                };
                NestedInfos = NestedInfos.Prepend(anyInfo.ToComplexInterfaceInfo("(Any)", associativeArrayInnerType)).ToArray();
            }
            IsComplexInterface = NestedInfos.Any();
        }
        public Parameter(string variantName, string parameterName, ParameterMetadata metadata)
        {
            VariantName   = variantName;
            ParameterName = parameterName;
            Metadata      = metadata;

            Attributes             = Metadata.Attributes.ToArray();
            ParameterType          = Attributes.OfType <ExportAsAttribute>().FirstOrDefault()?.Type ?? Metadata.ParameterType;
            Categories             = Attributes.OfType <CategoryAttribute>().SelectMany(ca => ca.Categories).Distinct().ToArray();
            DefaultValue           = Attributes.OfType <PSDefaultValueAttribute>().FirstOrDefault();
            ParameterAttribute     = Attributes.OfType <ParameterAttribute>().First();
            SupportsWildcards      = Attributes.OfType <SupportsWildcardsAttribute>().Any();
            InfoAttribute          = Attributes.OfType <InfoAttribute>().FirstOrDefault();
            CompleterInfoAttribute = Attributes.OfType <CompleterInfoAttribute>().FirstOrDefault();

            ValueFromPipeline = ParameterAttribute.ValueFromPipeline;
            ValueFromPipelineByPropertyName = ParameterAttribute.ValueFromPipelineByPropertyName;
            Position    = ParameterAttribute.Position == Int32.MinValue ? (int?)null : ParameterAttribute.Position;
            DontShow    = ParameterAttribute.DontShow;
            IsMandatory = ParameterAttribute.Mandatory;

            var complexParameterName = ParameterName.ToUpperInvariant();

            ComplexInterfaceInfo = InfoAttribute?.ToComplexInterfaceInfo(complexParameterName, ParameterType, true);
            IsComplexInterface   = ComplexInterfaceInfo?.IsComplexInterface ?? false;
            HelpMessage          = $"{ParameterAttribute.HelpMessage}{(IsComplexInterface ? $"{Environment.NewLine}To construct, see NOTES section for {complexParameterName} properties and create a hash table." : String.Empty)}";
        }
Esempio n. 3
0
        public Parameter(string variantName, string parameterName, ParameterMetadata metadata, PsParameterHelpInfo helpInfo = null)
        {
            VariantName   = variantName;
            ParameterName = parameterName;
            Metadata      = metadata;
            HelpInfo      = helpInfo ?? new PsParameterHelpInfo();

            Attributes            = Metadata.Attributes.ToArray();
            ParameterType         = Attributes.OfType <ExportAsAttribute>().FirstOrDefault()?.Type ?? Metadata.ParameterType;
            Categories            = Attributes.OfType <CategoryAttribute>().SelectMany(ca => ca.Categories).Distinct().ToArray();
            OrderCategory         = Categories.DefaultIfEmpty(ParameterCategory.Body).Min();
            DefaultValueAttribute = Attributes.OfType <PSDefaultValueAttribute>().FirstOrDefault();
            DefaultInfoAttribute  = Attributes.OfType <DefaultInfoAttribute>().FirstOrDefault();
            ParameterAttribute    = Attributes.OfType <ParameterAttribute>().FirstOrDefault(pa => pa.ParameterSetName == VariantName || pa.ParameterSetName == AllParameterSets);
            if (ParameterAttribute == null)
            {
                throw new ParsingMetadataException($"The variant '{VariantName}' has multiple parameter sets defined, which is not supported.");
            }
            SupportsWildcards          = Attributes.OfType <SupportsWildcardsAttribute>().Any();
            CompleterInfoAttribute     = Attributes.OfType <CompleterInfoAttribute>().FirstOrDefault();
            ArgumentCompleterAttribute = Attributes.OfType <ArgumentCompleterAttribute>().FirstOrDefault();

            ValueFromPipeline = ParameterAttribute.ValueFromPipeline;
            ValueFromPipelineByPropertyName = ParameterAttribute.ValueFromPipelineByPropertyName;
            Position    = ParameterAttribute.Position == Int32.MinValue ? (int?)null : ParameterAttribute.Position;
            DontShow    = ParameterAttribute.DontShow;
            IsMandatory = ParameterAttribute.Mandatory;

            var complexParameterName = ParameterName.ToUpperInvariant();
            var complexMessage       = $"{Environment.NewLine}To construct, see NOTES section for {complexParameterName} properties and create a hash table.";
            var description          = ParameterAttribute.HelpMessage.NullIfEmpty() ?? HelpInfo.Description.NullIfEmpty() ?? InfoAttribute?.Description.NullIfEmpty() ?? String.Empty;

            // Remove the complex type message as it will be reinserted if this is a complex type
            description = description.NormalizeNewLines().Replace(complexMessage, String.Empty).Replace(complexMessage.ToPsSingleLine(), String.Empty);
            // Make an InfoAttribute for processing only if one isn't provided
            InfoAttribute = Attributes.OfType <InfoAttribute>().FirstOrDefault() ?? new InfoAttribute {
                PossibleTypes = new[] { ParameterType.Unwrap() }, Required = IsMandatory
            };
            // Set the description if the InfoAttribute does not have one since they are exported without a description
            InfoAttribute.Description = String.IsNullOrEmpty(InfoAttribute.Description) ? description : InfoAttribute.Description;
            ComplexInterfaceInfo      = InfoAttribute.ToComplexInterfaceInfo(complexParameterName, ParameterType, true);
            IsComplexInterface        = ComplexInterfaceInfo.IsComplexInterface;
            Description = $"{description}{(IsComplexInterface ? complexMessage : String.Empty)}";
        }