private List <StackedConfiguredBlock> GetChildrenBlocks(Type parentBlock, CommerceContext context)
        {
            List <StackedConfiguredBlock>   childBlocks        = new List <StackedConfiguredBlock>();
            IEnumerable <PropertyFieldInfo> proertiesAndFields = parentBlock?.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
                                                                 .Select(element => new PropertyFieldInfo()
            {
                Name = element.Name, GivenType = element.PropertyType
            })
                                                                 .Union(parentBlock?.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
                                                                        .Select(element => new PropertyFieldInfo()
            {
                Name = element.Name, GivenType = element.FieldType
            }));

            foreach (var property in proertiesAndFields)
            {
                if (typeof(IPipeline).IsAssignableFrom(property.GivenType))
                {
                    IPipelineBlockDescriptor instance = Activator.CreateInstance(typeof(PipelineBlockDescriptor <>).MakeGenericType(property.GivenType)) as IPipelineBlockDescriptor;
                    childBlocks.Add(new StackedConfiguredBlock()
                    {
                        PropertyName = string.Empty,
                        Namespace    = instance?.Type.Namespace,
                        Name         = instance?.Type.Name,
                        Receives     = instance.Receives.FullName,
                        Returns      = instance.Returns.FullName,
                        IsCustom     = Assembly.GetAssembly(instance.Type).GetCustomAttribute <CustomAssemblyAttribute>() != null
                    });
                    continue;
                }

                if (typeof(IPipelineBlock).IsAssignableFrom(property.GivenType))
                {
                    //
                }

                if (property.GivenType.IsSubclassOf(typeof(CommerceCommand)))
                {
                    var stackedConfigurationBlock = new StackedConfiguredBlock()
                    {
                        PropertyName = property.Name,
                        Namespace    = property.GivenType.Namespace,
                        Name         = property.GivenType.Name,
                        Receives     = string.Empty,
                        Returns      = string.Empty,
                        IsCustom     = Assembly.GetAssembly(property.GivenType).GetCustomAttribute <CustomAssemblyAttribute>() != null
                    };
                    foreach (var child in GetChildrenBlocks(property.GivenType, context))
                    {
                        stackedConfigurationBlock.ChildComponents.Add(child);
                    }
                    childBlocks.Add(stackedConfigurationBlock);
                }
            }

            return(childBlocks);
        }
Exemple #2
0
        private void CollectPipelineInformation(StreamWriter streamWriter, StackedConfiguredBlock block, int level)
        {
            string placeholder = new string(' ', level * 3);

            foreach (StackedConfiguredBlock childBlock in block.GetComponents <StackedConfiguredBlock>())
            {
                streamWriter.WriteLine($"{placeholder}------------------------------------------------------------");
                string str = block.Namespace.Replace("Sitecore.Commerce.", string.Empty);
                if (string.IsNullOrEmpty(childBlock.PropertyName))
                {
                    // Such case is given when block is BLOCK or PIPELINE
                    streamWriter.WriteLine($"{placeholder}{(childBlock.IsCustom ? "<CUSTOM>" : string.Empty)} {str}.{childBlock.Name}({childBlock.Receives} => {childBlock.Returns})");
                }
                else
                {
                    // Such case is given when block is COMMAND
                    streamWriter.WriteLine($"{placeholder}{(childBlock.IsCustom ? "<CUSTOM>" : string.Empty)} {str}.{childBlock.Name} {childBlock.PropertyName}");
                }

                CollectPipelineInformation(streamWriter, childBlock, level + 1);
            }
        }
        private List <StackedConfiguredBlock> GetBlocksOfPipeline(Type type, CommerceContext context)
        {
            List <StackedConfiguredBlock> blocks = new List <StackedConfiguredBlock>();

            try
            {
                object    requiredService = this._serviceProvider.GetRequiredService(typeof(IPipelineConfiguration <>).MakeGenericType(type));
                FieldInfo field           = requiredService.GetType().GetField("_collected", BindingFlags.Instance | BindingFlags.NonPublic);
                object    obj             = field?.GetValue(requiredService);
                if (obj is List <IBuildablePipelineBlockDefinition> pipelineBlockDefinitionList)
                {
                    foreach (IBuildablePipelineBlockDefinition pipelineBlockDefinition in pipelineBlockDefinitionList)
                    {
                        IPipelineBlockDescriptor pipelineBlockDescriptor = pipelineBlockDefinition.Describe();
                        StackedConfiguredBlock   configuredBlock         = new StackedConfiguredBlock
                        {
                            Namespace = pipelineBlockDescriptor.Type.Namespace,
                            Name      = pipelineBlockDescriptor.Type.Name,
                            Receives  = pipelineBlockDescriptor.Receives.FullName,
                            Returns   = pipelineBlockDescriptor.Returns.FullName,
                            IsCustom  = Assembly.GetAssembly(pipelineBlockDescriptor.Type).GetCustomAttribute <CustomAssemblyAttribute>() != null
                        };
                        foreach (var child in GetChildrenBlocks(pipelineBlockDescriptor.Type, context))
                        {
                            configuredBlock.ChildComponents.Add(child);
                        }

                        blocks.Add(configuredBlock);
                    }
                }
            }
            catch (Exception ex)
            {
                context.Logger.LogError(ex.Message);
            }

            return(blocks);
        }