コード例 #1
0
ファイル: GetFormatDataCommand.cs プロジェクト: nickchal/pash
 protected override void ProcessRecord()
 {
     List<ViewDefinition> viewDefinitionList = base.Context.FormatDBManager.Database.viewDefinitionsSection.viewDefinitionList;
     Dictionary<string, List<FormatViewDefinition>> dictionary = new Dictionary<string, List<FormatViewDefinition>>();
     foreach (ViewDefinition definition in viewDefinitionList)
     {
         foreach (TypeOrGroupReference reference in definition.appliesTo.referenceList)
         {
             PSControl control = null;
             if (definition.mainControl is TableControlBody)
             {
                 control = new TableControl((TableControlBody) definition.mainControl);
             }
             if (definition.mainControl is ListControlBody)
             {
                 control = new ListControl((ListControlBody) definition.mainControl);
             }
             if (definition.mainControl is WideControlBody)
             {
                 control = new WideControl((WideControlBody) definition.mainControl);
             }
             if (control != null)
             {
                 FormatViewDefinition item = new FormatViewDefinition(definition.name, control, definition.InstanceId);
                 foreach (WildcardPattern pattern in this._filter)
                 {
                     if (pattern.IsMatch(reference.name))
                     {
                         if (!dictionary.ContainsKey(reference.name))
                         {
                             dictionary.Add(reference.name, new List<FormatViewDefinition>());
                         }
                         dictionary[reference.name].Add(item);
                     }
                 }
             }
         }
     }
     foreach (string str in dictionary.Keys)
     {
         base.WriteObject(new ExtendedTypeDefinition(str, dictionary[str]));
     }
 }
コード例 #2
0
 private ViewDefinition LoadViewFromObjectModle(string typeName, FormatViewDefinition formatView, int viewIndex)
 {
     TypeReference reference = new TypeReference {
         name = typeName
     };
     AppliesTo to = new AppliesTo {
         referenceList = { reference }
     };
     ViewDefinition definition = new ViewDefinition {
         appliesTo = to,
         name = formatView.Name
     };
     PSControl control = formatView.Control;
     if (control is TableControl)
     {
         TableControl table = control as TableControl;
         definition.mainControl = this.LoadTableControlFromObjectModel(table, viewIndex, typeName);
     }
     else if (control is ListControl)
     {
         ListControl list = control as ListControl;
         definition.mainControl = this.LoadListControlFromObjectModel(list, viewIndex, typeName);
     }
     else if (control is WideControl)
     {
         WideControl wide = control as WideControl;
         definition.mainControl = this.LoadWideControlFromObjectModel(wide, viewIndex, typeName);
     }
     if (definition.mainControl == null)
     {
         return null;
     }
     return definition;
 }
コード例 #3
0
        /// <summary>
        /// Load the view into a ViewDefinition
        /// </summary>
        /// <param name="typeNames">the TypeName tag under SelectedBy tag</param>
        /// <param name="formatView"></param>
        /// <param name="viewIndex"></param>
        /// <returns></returns>
        private ViewDefinition LoadViewFromObjectModel(List<string> typeNames, FormatViewDefinition formatView, int viewIndex)
        {
            // Get AppliesTo information
            AppliesTo appliesTo = new AppliesTo();
            foreach (var typename in typeNames)
            {
                TypeReference tr = new TypeReference { name = typename };
                appliesTo.referenceList.Add(tr);
            }

            // Set AppliesTo and Name in the view
            ViewDefinition view = new ViewDefinition();
            view.appliesTo = appliesTo;
            view.name = formatView.Name;

            var firstTypeName = typeNames[0];
            PSControl control = formatView.Control;
            if (control is TableControl)
            {
                var tableControl = control as TableControl;
                view.mainControl = LoadTableControlFromObjectModel(tableControl, viewIndex, firstTypeName);
            }
            else if (control is ListControl)
            {
                var listControl = control as ListControl;
                view.mainControl = LoadListControlFromObjectModel(listControl, viewIndex, firstTypeName);
            }
            else if (control is WideControl)
            {
                var wideControl = control as WideControl;
                view.mainControl = LoadWideControlFromObjectModel(wideControl, viewIndex, firstTypeName);
            }
            else
            {
                view.mainControl = LoadCustomControlFromObjectModel((CustomControl)control, viewIndex, firstTypeName);
            }

            // Check if the PSControl is successfully loaded
            if (view.mainControl == null)
            {
                return null;
            }

            view.outOfBand = control.OutOfBand;

            if (control.GroupBy != null)
            {
                view.groupBy = new GroupBy
                {
                    startGroup = new StartGroup
                    {
                        expression = LoadExpressionFromObjectModel(control.GroupBy.Expression, viewIndex, firstTypeName)
                    }
                };
                if (control.GroupBy.Label != null)
                {
                    view.groupBy.startGroup.labelTextToken = new TextToken { text = control.GroupBy.Label };
                }
                if (control.GroupBy.CustomControl != null)
                {
                    view.groupBy.startGroup.control = LoadCustomControlFromObjectModel(control.GroupBy.CustomControl, viewIndex, firstTypeName);
                }
            }

            return view;
        }
コード例 #4
0
        /// <summary>
        /// Takes out the content from the database and writes them
        /// out
        /// </summary>
        protected override void ProcessRecord()
        {
            bool writeOldWay = PowerShellVersion == null ||
                               PowerShellVersion.Major < 5 ||
                               PowerShellVersion.Build < 11086;

            TypeInfoDataBase db = this.Context.FormatDBManager.Database;

            List<ViewDefinition> viewdefinitions = db.viewDefinitionsSection.viewDefinitionList;
            Dictionary<string, List<string>> typeGroupMap = GetTypeGroupMap(db.typeGroupSection.typeGroupDefinitionList);

            var typedefs = new Dictionary<ConsolidatedString, List<FormatViewDefinition>>(ConsolidatedString.EqualityComparer);

            foreach (ViewDefinition definition in viewdefinitions)
            {
                if (definition.isHelpFormatter)
                    continue;

                var consolidatedTypeName = CreateConsolidatedTypeName(definition, typeGroupMap);

                if (!ShouldGenerateView(consolidatedTypeName))
                    continue;

                PSControl control;

                var tableControlBody = definition.mainControl as TableControlBody;
                if (tableControlBody != null)
                {
                    control = new TableControl(tableControlBody, definition);
                }
                else
                {
                    var listControlBody = definition.mainControl as ListControlBody;
                    if (listControlBody != null)
                    {
                        control = new ListControl(listControlBody, definition);
                    }
                    else
                    {
                        var wideControlBody = definition.mainControl as WideControlBody;
                        if (wideControlBody != null)
                        {
                            control = new WideControl(wideControlBody, definition);
                            if (writeOldWay)
                            {
                                // Alignment was added to WideControl in V2, but wasn't
                                // used.  It was removed in V5, but old PowerShell clients
                                // expect this property or fail to rehydrate the remote object.
                                var psobj = new PSObject(control);
                                psobj.Properties.Add(new PSNoteProperty("Alignment", 0));
                            }
                        }
                        else
                        {
                            var complexControlBody = (ComplexControlBody)definition.mainControl;
                            control = new CustomControl(complexControlBody, definition);
                        }
                    }
                }

                // Older version of PowerShell do not know about something in the control, so
                // don't return it.
                if (writeOldWay && !control.CompatibleWithOldPowerShell())
                    continue;

                var formatdef = new FormatViewDefinition(definition.name, control, definition.InstanceId);

                List<FormatViewDefinition> viewList;
                if (!typedefs.TryGetValue(consolidatedTypeName, out viewList))
                {
                    viewList = new List<FormatViewDefinition>();
                    typedefs.Add(consolidatedTypeName, viewList);
                }
                viewList.Add(formatdef);
            }// foreach(ViewDefinition...

            // write out all the available type definitions
            foreach (var pair in typedefs)
            {
                var typeNames = pair.Key;

                if (writeOldWay)
                {
                    foreach (var typeName in typeNames)
                    {
                        var etd = new ExtendedTypeDefinition(typeName, pair.Value);
                        WriteObject(etd);
                    }
                }
                else
                {
                    var etd = new ExtendedTypeDefinition(typeNames[0], pair.Value);
                    for (int i = 1; i < typeNames.Count; i++)
                    {
                        etd.TypeNames.Add(typeNames[i]);
                    }
                    WriteObject(etd);
                }
            }
        }