Exemple #1
0
        private static CmdletParameterData ParseParameter(XElement parent)
        {
            CmdletParameterData ret = new CmdletParameterData();

            foreach (var elm in parent.Nodes())
            {
                if (elm is XElement)
                {
                    var xelm = elm as XElement;
                    switch (xelm.Name.LocalName)
                    {
                    case "name":
                        if (xelm.Name.NamespaceName.Equals("http://schemas.microsoft.com/maml/2004/10"))
                        {
                            ret.Name = xelm.Value;
                        }
                        break;

                    case "description":
                        if (xelm.Name.NamespaceName.Equals("http://schemas.microsoft.com/maml/2004/10"))
                        {
                            ret.Description = ConvertDesc(xelm);
                        }
                        break;
                    }
                }
            }

            return(ret);
        }
Exemple #2
0
        public static List <CmdletData> ParseCmdLets(string filePath)
        {
            List <CmdletData> ret  = new List <CmdletData>();
            XmlReaderSettings sett = new XmlReaderSettings();


            CmdletData             currentCmdLet = null;
            CmdletParameterSetData currentPSet   = null;
            CmdletParameterData    currentPm     = null;

            using (var st = File.Open(filePath, FileMode.Open, FileAccess.Read))
                using (XmlReader rdr = XmlReader.Create(st, sett))
                {
                    while (rdr.Read())
                    {
                        switch (rdr.NodeType)
                        {
                        case XmlNodeType.XmlDeclaration:
                            break;

                        case XmlNodeType.Comment:
                            var comment = rdr.Value;
                            if (string.IsNullOrEmpty(comment))
                            {
                                break;
                            }
                            comment = comment.Trim();
                            if (comment.StartsWith("Parameter set:", StringComparison.InvariantCultureIgnoreCase))
                            {
                                comment = comment.Substring(14);
                                if (currentPSet != null && currentCmdLet != null)
                                {
                                    currentCmdLet.ParameterSets.Add(currentPSet);
                                }

                                if (currentPm != null && !string.IsNullOrEmpty(currentPm.Name))
                                {
                                    if (!currentPSet.Parameters.Contains(currentPm))
                                    {
                                        currentPSet.Parameters.Add(currentPm);
                                    }
                                }

                                currentPSet      = new CmdletParameterSetData();
                                currentPSet.Name = comment;
                            }
                            break;

                        case XmlNodeType.Element:
                            switch (rdr.NamespaceURI)
                            {
                            case "http://schemas.microsoft.com/maml/dev/command/2004/10":
                                switch (rdr.LocalName)
                                {
                                case "command":
                                    if (currentCmdLet != null)
                                    {
                                        ret.Add(currentCmdLet);
                                        if (currentPSet != null && !string.IsNullOrEmpty(currentPSet.Name))
                                        {
                                            if (!currentCmdLet.ParameterSets.Contains(currentPSet))
                                            {
                                                currentCmdLet.ParameterSets.Add(currentPSet);
                                            }

                                            if (currentPm != null && !string.IsNullOrEmpty(currentPm.Name))
                                            {
                                                if (!currentPSet.Parameters.Contains(currentPm))
                                                {
                                                    currentPSet.Parameters.Add(currentPm);
                                                }
                                            }
                                        }
                                    }
                                    currentCmdLet = new CmdletData();
                                    break;

                                case "name":
                                    if (currentCmdLet != null)
                                    {
                                        currentCmdLet.Name = rdr.ReadInnerXml();
                                    }
                                    break;

                                case "parameter":
                                    if (currentPm != null && !string.IsNullOrEmpty(currentPm.Name))
                                    {
                                        if (!currentPSet.Parameters.Contains(currentPm))
                                        {
                                            currentPSet.Parameters.Add(currentPm);
                                        }
                                    }

                                    currentPm = ParseParameter(XElement.Parse(rdr.ReadOuterXml()));
                                    if (currentPSet != null)
                                    {
                                        currentPSet.Parameters.Add(currentPm);
                                    }
                                    break;
                                }
                                break;

                            case "http://schemas.microsoft.com/maml/2004/10":
                                switch (rdr.LocalName)
                                {
                                case "description":
                                    if (currentCmdLet != null)
                                    {
                                        currentCmdLet.Description = ConvertDesc(XElement.Parse(rdr.ReadOuterXml()));
                                    }
                                    break;
                                }
                                break;
                            }
                            break;
                        }
                    }
                }

            if (currentCmdLet != null && !string.IsNullOrEmpty(currentCmdLet.Name))
            {
                if (!ret.Contains(currentCmdLet))
                {
                    ret.Add(currentCmdLet);
                }
                if (currentPSet != null && !string.IsNullOrEmpty(currentPSet.Name))
                {
                    if (!currentCmdLet.ParameterSets.Contains(currentPSet))
                    {
                        currentCmdLet.ParameterSets.Add(currentPSet);
                    }
                }
            }

            return(ret);
        }