Esempio n. 1
0
 public void Add(TaskGeneratorResult other)
 {
     this.Groups.AddRange(other.Groups);
     this.Inputs.AddRange(other.Inputs);
     this.SourceDefinitions.AddRange(other.SourceDefinitions);
 }
Esempio n. 2
0
        public static TaskGeneratorResult GetTaskInputs(Type programOptionsType, PropertyInfo parent)
        {
            var properties = programOptionsType.GetProperties(
                BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Instance).Where(p =>
                                                                                                   Attribute.IsDefined(p, typeof(OptionAttribute)) || Attribute.IsDefined(p, typeof(DisplayAttribute)))
                             .ToArray();

            //     var useServiceEndpoint = properties.Any(p => Attribute.IsDefined(p, typeof(ServiceEndpointAttribute)));


            var results = new TaskGeneratorResult();

            foreach (var property in properties)
            {
                var groupName    = GetGroupName(property);
                var resourceType = GetResourcetype(property);
                var variableName = GetVariableName(property);


                var defaultTask = new TaskInput()
                {
                    HelpMarkDown = GetHelpmarkDown(property),
                    Name         = variableName,
                    DefaultValue = GetTaskDefaultValue(property),
                    Label        = GetLabel(property),
                    GroupName    = groupName,
                    Required     = GetRequired(property, parent),
                    VisibleRule  = property.GetCustomAttribute <VisibleRuleAttribute>()?.VisibleRule,
                    Order        = GetOrder(property),
                };
                defaultTask.Properties.EditableOptions = "True";

                if (variableName.Contains(" "))
                {
                    throw new ArgumentException($"The generated name for the given TaskInput contains spaces, please fix. : {defaultTask.Name}");
                }

                var sd = property.GetCustomAttribute <SourceDefinitionAttribute>();
                if (sd != null)
                {
                    try
                    {
                        if (!sd.Ignore)
                        {
                            results.SourceDefinitions.Add(new SourceDefinition
                            {
                                Endpoint = sd.Endpoint,
                                AuthKey  = (Activator.CreateInstance(sd.ConnectedService ??
                                                                     parent?.GetCustomAttribute <SourceDefinitionAttribute>()?.ConnectedService ??
                                                                     programOptionsType.GetCustomAttribute <SourceDefinitionAttribute>()?.ConnectedService) as AuthKeyProvider).GetAuthKey(),
                                Selector    = sd.Selector,
                                KeySelector = sd.KeySelector ?? "",
                                Target      = variableName
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }

                if (typeof(ITaskInputFactory).IsAssignableFrom(resourceType))
                {
                    ITaskInputFactory fac = null;
                    if (resourceType == property.PropertyType)
                    {
                        fac = property.GetValue(Activator.CreateInstance(programOptionsType)) as ITaskInputFactory;
                    }
                    if (fac == null && resourceType.IsGenericTypeDefinition && resourceType == property.PropertyType.GetGenericTypeDefinition())
                    {
                        fac = property.GetValue(Activator.CreateInstance(programOptionsType)) as ITaskInputFactory;
                    }
                    if (fac == null)
                    {
                        fac = Activator.CreateInstance(resourceType) as ITaskInputFactory;
                    }
                    var tasks = fac.GenerateTasks(groupName, defaultTask, property);
                    foreach (var iput in tasks.Inputs)
                    {
                        iput.GroupName = iput.GroupName ?? defaultTask.GroupName;
                    }

                    results.Add(tasks);
                }
                else
                {
                    defaultTask.Type = GetTaskInputType(resourceType, property);
                    results.Inputs.Add(defaultTask);
                }
            }
            results.Groups.AddRange(
                programOptionsType.GetCustomAttributes <GroupAttribute>()
                .Select(g => new Group
            {
                DisplayName = g.DisplayName,
                Name        = g.Name,
                IsExpanded  = g.isExpanded
            }));

            return(results);
        }