public void TestEqual() { string raw = @"test_demon = { condition = { equal = {1, 1} } }"; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsTrue(demo.condition.Rslt()); raw = @"test_demon = { condition = { equal = {1, 12} } }"; syntaxItem = SyntaxItem.RootParse(raw); demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsFalse(demo.condition.Rslt()); }
internal void TryStartNextGroup(Scanner s, SyntaxItem justMatchedItem, int idxStartAt) { if (!justMatchedItem.HasNextGroup) { return; } //TODO: delete this and make sure tests pass, I don't think we need it if (s.TopScope.PosAfter <= idxStartAt) { return; } Scope nextGroupScope = new Scope(); nextGroupScope.SyntaxItem = this; nextGroupScope.ActiveSyntaxItems = justMatchedItem.NextGroupItems; nextGroupScope.PosStart = idxStartAt; nextGroupScope.PosAfter = 1 + FindLastPositionForNextGroupSearch(s, idxStartAt, justMatchedItem); nextGroupScope.Extend = false; nextGroupScope.IsWithinKeepEnd = s.TopScope.IsWithinKeepEnd; s.StartNextGroup(nextGroupScope); }
internal static int FindLastPositionForNextGroupSearch(Scanner s, int posStartAt, SyntaxItem justMatchedItem) { int posCurrent = posStartAt; int maxIdxEnd = s.PosWindowEnd; bool hasSkippedNewLine = false; while (posCurrent < maxIdxEnd) { char c = s[posCurrent]; if (' ' == c || '\t' == c) { if (justMatchedItem.SkipWhite) { posCurrent++; continue; } } if ('\n' == c) { if (justMatchedItem.SkipEmptyLine) { posCurrent++; continue; } else if (justMatchedItem.SkipNewLine && !hasSkippedNewLine) { hasSkippedNewLine = true; posCurrent++; continue; } } break; } return posCurrent; }
public void TestReduce() { string raw = @"test_demon = { selected = { reduce = {sub.a, 1} } }"; DataVisit.Visitor.InitVisitMap(typeof(TestData)); DataVisit.Visitor.SetVisitData(TestData.inst); Parser.Semantic.Visitor.GetValueFunc = DataVisit.Visitor.Get; Parser.Semantic.Visitor.SetValueFunc = DataVisit.Visitor.Set; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); TestData.inst.sub.a = 1; demo.operation.Do(); Assert.AreEqual(0, TestData.inst.sub.a); }
private static void ParseSemanticPropertyList(SyntaxItem syntaxRoot, SemanticPropertyArray property, FieldInfo field, ref object obj) { var items = syntaxRoot.Finds(property.key); if (!items.Any()) { return; //throw new Exception($"can not find key:{property.key}"); } if (!field.FieldType.IsGenericType || field.FieldType.GetGenericTypeDefinition() != typeof(List <>)) { throw new Exception($"field type not list! {field.FieldType.FullName}"); } dynamic list = Activator.CreateInstance(field.FieldType); Type[] listParameters = field.FieldType.GetGenericArguments(); foreach (var item in items) { list.Add((dynamic)ConvertItem(item, listParameters[0])); } field.SetValue(obj, list); }
public void TestModify() { string raw = @"test_demon = { group = { base = 50 modifier = { value = 50 condition = { equal = {sub.a, 1} } } } }"; DataVisit.Visitor.InitVisitMap(typeof(TestData)); DataVisit.Visitor.SetVisitData(TestData.inst); Parser.Semantic.Visitor.GetValueFunc = DataVisit.Visitor.Get; Parser.Semantic.Visitor.SetValueFunc = DataVisit.Visitor.Set; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.AreEqual(100, demo.group.CalcValue()); TestData.inst.sub.a = 2; Assert.AreEqual(50, demo.group.CalcValue()); }
public static Condition Parse(SyntaxItem item) { try { if (item.values.Count() != 1) { throw new Exception($"have more than one values"); } var bValue = item.values[0] as BoolValue; if (bValue != null) { return(new ConditionDefault(bValue.data)); } var subItem = item.values[0] as SyntaxItem; if (subItem == null) { throw new Exception($"not support value type"); } return(ParseItem(subItem)); } catch (Exception e) { throw new Exception($"Parse conditon faild! key:{item.key}", e); } }
public static Select Parse(SyntaxItem item) { var rslt = new Select(); foreach (var elem in item.values) { var subItem = elem as SyntaxItem; if (subItem == null) { throw new Exception("must key-value pair in select"); } switch (subItem.key) { case "reduce": rslt.opList.Add(OperationReduce.Parse(subItem)); break; case "add": rslt.opList.Add(OperationAdd.Parse(subItem)); break; case "assign": rslt.opList.Add(OperationAssign.Parse(subItem)); break; default: throw new Exception($"can not support {subItem.key} in select"); } } return(rslt); }
internal static Condition ParseItem(SyntaxItem item) { switch (item.key) { case "equal": return(new ConditionEqual(item)); case "less": return(new ConditionLess(item)); case "greater": return(new ConditionGreater(item)); case "or": return(new ConditionOr(item)); case "and": return(new ConditionAnd(item)); case "not": return(new ConditionNot(item)); default: throw new Exception($"not support item '{item.key}'"); } }
public void Parse(string itemsStr, Dictionary <string, string> defDict, Dictionary <string, int> expDict) { foreach (var itemStr in itemsStr.Split(new[] { ".." }, StringSplitOptions.RemoveEmptyEntries)) { var item = new SyntaxItem(); item.Parse(itemStr.Trim(), defDict, expDict); items.Add(item); } }
public void TestAnd() { string raw = @"test_demon = { condition = { and = { less = {1, 12} greater = {1, 0} } } }"; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsTrue(demo.condition.Rslt()); raw = @"test_demon = { condition = { and = { less = {1, 12} greater = {1, 12} } } }"; syntaxItem = SyntaxItem.RootParse(raw); demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsFalse(demo.condition.Rslt()); raw = @"test_demon = { condition = { and = { less = {1, 0} greater = {1, 12} } } }"; syntaxItem = SyntaxItem.RootParse(raw); demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.IsFalse(demo.condition.Rslt()); }
public static T Load <T>(string fileName, string fileContent) { try { var syntaxItem = SyntaxItem.RootParse(fileContent); return(SemanticParser.DoParser <T>(syntaxItem)); } catch (Exception e) { throw new Exception($"Parse error in script:{fileName}", e); } }
private static void ParseSemanticProperty(SyntaxItem syntaxRoot, SemanticProperty property, FieldInfo field, ref object obj) { var item = syntaxRoot.Find(property.key); if (item == null) { return; //throw new Exception($"can not find key:{property.key}"); } field.SetValue(obj, ConvertItem(item, field.FieldType)); }
public void Parse(string str) { var splitArr = str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); foreach (var itemStr in splitArr.Select(t => t.Trim())) { var item = new SyntaxItem(); item.index = items.Count; item.Parse(itemStr); items.Add(item); } }
public static T ParserFile <T>(string path) { try { var syntaxRoot = SyntaxItem.RootParse(File.ReadAllText(path)); return(DoParser <T>(syntaxRoot)); } catch (Exception e) { throw new Exception($"Parse error in script:{path}", e); } }
public void TestBaseValue() { string raw = @"test_demon = { group = { base = 100 } }"; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); Assert.AreEqual(100, demo.group.CalcValue()); }
internal static Operation Parse(SyntaxItem subItem) { if (subItem.values.Count != 2 || !(subItem.values[0] is StringValue) || !(subItem.values[1] is SingleValue)) { throw new Exception("add sub iteam must be have one SingleValue!"); } return(new OperationReduce() { left = subItem.values[0] as SingleValue, right = subItem.values[1] as SingleValue }); }
public void Test1() { string raw = @"title = EVENT_TEST_TITLE desc = EVENT_TEST_DESC option = { desc = EVENT_TEST_OPTION_1_DESC }"; var syntaxItem = SyntaxItem.RootParse(raw); SemanticParser.DoParser <Modder.GEvent>(syntaxItem); }
public ConditionAnd(SyntaxItem item) { subList = new List <Condition>(); foreach (var value in item.values) { var subItem = value as SyntaxItem; if (subItem == null) { throw new Exception($"not support value type"); } subList.Add(Condition.ParseItem(subItem)); } }
public static Date Parse(SyntaxItem item) { if (item.values.Count == 1 && item.values[0] is StringValue) { var sValue = item.values[0] as StringValue; if (sValue.ToString() != "every_day") { throw new Exception($"date with single value expect be 'every_day', but curr is {sValue.ToString()}"); } return(new Date()); } return(SemanticParser.DoParser <Date>(item)); }
internal static Operation Parse(SyntaxItem subItem) { if (subItem.values.Count != 1 || !(subItem.values[0] is StringValue)) { throw new Exception("add risk_start iteam must be have one SingleValue!"); } return(new OperationRiskStart() { left = new StringValue() { data = "risk.start" }, right = subItem.values[0] as SingleValue }); }
private static void AppendCommonOptions(SyntaxItem item, StringBuilder firstLine) { if (item.IsContained) { firstLine.Append(" contained"); } if (item.IsTransparent) { firstLine.Append(" transparent"); } if (item is ContainerItem && (item as ContainerItem).Extend) { firstLine.Append(" extend"); } if (item is Region && (item as Region).IsOneLine) { firstLine.Append(" oneline"); } if (item is Region && (item as Region).KeepEnd) { firstLine.Append(" keepend"); } if (0 < item.ContainedIn.ContainedGroupsAndClusters.Count) { firstLine.AppendFormat(" containedin={0}", item.ContainedIn); } if (item is ContainerItem && 0 < (item as ContainerItem).Contains.ContainedGroupsAndClusters.Count) { firstLine.AppendFormat(" contains={0}", (item as ContainerItem).Contains); } if (0 < item.NextGroupCluster.ContainedGroupsAndClusters.Count) { firstLine.AppendFormat(" nextgroup={0}", item.NextGroupCluster); if (item.SkipEmptyLine) { firstLine.Append(" skipempty"); } if (item.SkipNewLine) { firstLine.Append(" skipnl"); } if (item.SkipWhite) { firstLine.Append(" skipwhite"); } } }
public static GroupValue Parse(SyntaxItem item) { var Params = new List <SingleValue>(); foreach (var value in item.values) { var stringValue = value as SingleValue; if (stringValue == null) { throw new Exception($"Semantic error, {item} value must be single value"); } Params.Add(stringValue); } return(new GroupValue(Params.ToArray())); }
public void TestRiskStart() { string raw = @"test_demon = { selected = { risk.start = RISK_TEST } }"; DataVisit.Visitor.InitVisitMap(typeof(TestData)); DataVisit.Visitor.SetVisitData(TestData.inst); Parser.Semantic.Visitor.GetValueFunc = DataVisit.Visitor.Get; Parser.Semantic.Visitor.SetValueFunc = DataVisit.Visitor.Set; var syntaxItem = SyntaxItem.RootParse(raw); TestDemon demo = SemanticParser.DoParser <TestDemon>(syntaxItem.Find("test_demon")); demo.operation.Do(); Assert.AreEqual("RISK_TEST", TestData.inst.risk.start); }
internal ConditionTuple(SyntaxItem item) { if (item == null && this is ConditionDefault) { return; } if (item.values.Count() != 2) { throw new Exception("conditon equal must have 2 values!"); } var value0 = item.values[0] as SingleValue; var value1 = item.values[1] as SingleValue; if (value0 == null || value1 == null) { throw new Exception("conditon equal value must be SingleValue"); } right = value0; left = value1; }
public static T DoParser <T>(SyntaxItem syntaxRoot) { object rslt = Activator.CreateInstance <T>(); var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (var field in fields) { var Properties = field.GetCustomAttributes(typeof(SemanticProperty), false); if (Properties.Count() != 0) { ParseSemanticProperty(syntaxRoot, Properties.First() as SemanticProperty, field, ref rslt); continue; } Properties = field.GetCustomAttributes(typeof(SemanticPropertyArray), false); if (Properties.Count() != 0) { ParseSemanticPropertyList(syntaxRoot, Properties.First() as SemanticPropertyArray, field, ref rslt); } } return((T)rslt); }
internal void AddChildItem(SyntaxItem syntaxItem) { ArgumentValidator.ThrowIfNull(syntaxItem, "syntaxItem"); this.Items.Add(syntaxItem); }
static void Main(string[] args) { string inFile = args[0]; string outFile = args[1]; XDocument doc = new XDocument(new XDeclaration("1.0", "UTF-8", string.Empty)); XNamespace ns = "http://msh"; XElement helpItems = new XElement(ns + "helpItems", new XAttribute("schema", "maml")); doc.Add(helpItems); XNamespace maml = "http://schemas.microsoft.com/maml/2004/10"; XNamespace command = "http://schemas.microsoft.com/maml/dev/command/2004/10"; XNamespace dev = "http://schemas.microsoft.com/maml/dev/2004/10"; XAttribute mamlNsAttr = new XAttribute(XNamespace.Xmlns + "maml", "http://schemas.microsoft.com/maml/2004/10"); XAttribute commandNsAttr = new XAttribute(XNamespace.Xmlns + "command", "http://schemas.microsoft.com/maml/dev/command/2004/10"); XAttribute devNsAttr = new XAttribute(XNamespace.Xmlns + "dev", "http://schemas.microsoft.com/maml/dev/2004/10"); string output = string.Empty; Assembly assembly = Assembly.LoadFrom(inFile); Type[] types = assembly.GetTypes(); foreach (Type t in types) { if (t.BaseType.Name == "SPOCmdlet" || t.BaseType.Name == "PSCmdlet" || t.BaseType.Name == "SPOWebCmdlet" || t.BaseType.Name == "SPOAdminCmdlet") { //XElement examples = new XElement(command + "examples"); string verb = string.Empty; string noun = string.Empty; string description = string.Empty; string detaileddescription = string.Empty; string details = string.Empty; string copyright = string.Empty; string version = string.Empty; var attrs = t.GetCustomAttributes(); List<CmdletExampleAttribute> examples = new List<CmdletExampleAttribute>(); //System.Attribute.GetCustomAttributes(t); foreach (System.Attribute attr in attrs) { if (attr is CmdletAttribute) { CmdletAttribute a = (CmdletAttribute)attr; verb = a.VerbName; noun = a.NounName; } if (attr is CmdletHelpAttribute) { CmdletHelpAttribute a = (CmdletHelpAttribute)attr; description = a.Description; details = a.Details; copyright = a.Copyright; version = a.Version; detaileddescription = a.DetailedDescription; } if (attr is CmdletExampleAttribute) { CmdletExampleAttribute a = (CmdletExampleAttribute)attr; examples.Add(a); } } XElement commandElement = new XElement(command + "command", mamlNsAttr, commandNsAttr, devNsAttr); XElement detailsElement = new XElement(command + "details"); commandElement.Add(detailsElement); detailsElement.Add(new XElement(command + "name", string.Format("{0}-{1}", verb, noun))); detailsElement.Add(new XElement(maml + "description", new XElement(maml + "para", description))); detailsElement.Add(new XElement(maml + "copyright", new XElement(maml + "para", copyright))); detailsElement.Add(new XElement(command + "verb", verb)); detailsElement.Add(new XElement(command + "noun", noun)); detailsElement.Add(new XElement(dev + "version", version)); commandElement.Add(new XElement(maml + "description", new XElement(maml + "para", detaileddescription))); XElement syntaxElement = new XElement(command + "syntax"); commandElement.Add(syntaxElement); FieldInfo[] fields = t.GetFields(); List<SyntaxItem> syntaxItems = new List<SyntaxItem>(); foreach (FieldInfo field in fields) { foreach (System.Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { SyntaxItem syntaxItem = null; ParameterAttribute a = (ParameterAttribute)attr; if (a.ParameterSetName != ParameterAttribute.AllParameterSets) { syntaxItem = syntaxItems.Where(x => x.Name == a.ParameterSetName).FirstOrDefault(); if (syntaxItem == null) { syntaxItem = new SyntaxItem(a.ParameterSetName); syntaxItems.Add(syntaxItem); } syntaxItem.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } // all parameters foreach (FieldInfo field in fields) { foreach (System.Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { ParameterAttribute a = (ParameterAttribute)attr; if (a.ParameterSetName == ParameterAttribute.AllParameterSets) { foreach (var si in syntaxItems) { si.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } } // foreach (var syntaxItem in syntaxItems) { XElement syntaxItemElement = new XElement(command + "syntaxItem"); syntaxElement.Add(syntaxItemElement); syntaxItemElement.Add(new XElement(maml + "name", string.Format("{0}-{1}", verb, noun))); foreach (var parameter in syntaxItem.Parameters) { XElement parameterElement = new XElement(command + "parameter", new XAttribute("required", parameter.Required), new XAttribute("position", parameter.Position > 0 ? parameter.Position.ToString() : "named")); parameterElement.Add(new XElement(maml + "name", parameter.Name)); parameterElement.Add(new XElement(maml + "description", new XElement(maml + "para", parameter.Description))); parameterElement.Add(new XElement(command + "parameterValue", parameter.Type)); syntaxItemElement.Add(parameterElement); } } XElement parametersElement = new XElement(command + "parameters"); commandElement.Add(parametersElement); foreach (FieldInfo field in fields) { foreach (System.Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { ParameterAttribute a = (ParameterAttribute)attr; XElement parameter2Element = new XElement(command + "parameter", new XAttribute("required", a.Mandatory), new XAttribute("position", a.Position > 0 ? a.Position.ToString() : "named")); parameter2Element.Add(new XElement(maml + "name", field.Name)); parameter2Element.Add(new XElement(maml + "description", new XElement(maml + "para", a.HelpMessage))); var parameterValueElement = new XElement(command + "parameterValue", field.FieldType.Name, new XAttribute("required", a.Mandatory)); parameter2Element.Add(parameterValueElement); var devElement = new XElement(dev + "type"); devElement.Add(new XElement(maml + "name", field.FieldType.Name)); devElement.Add(new XElement(maml + "uri")); parameter2Element.Add(devElement); parametersElement.Add(parameter2Element); break; } } } commandElement.Add( new XElement(command + "inputTypes", new XElement(command + "inputType", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); helpItems.Add(commandElement); commandElement.Add( new XElement(command + "returnValues", new XElement(command + "returnValue", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); XElement examplesElement = new XElement(command + "examples"); int exampleCount = 1; foreach (var exampleAttr in examples.OrderBy(e => e.SortOrder)) { XElement example = new XElement(command + "example"); string title = string.Format("------------------EXAMPLE {0}---------------------", exampleCount); example.Add(new XElement(maml + "title", title)); example.Add(new XElement(maml + "introduction", new XElement(maml + "para", exampleAttr.Introduction))); example.Add(new XElement(dev + "code", exampleAttr.Code)); example.Add(new XElement(maml + "remarks", new XElement(maml + "para", exampleAttr.Remarks))); example.Add(new XElement(command + "commandLines", new XElement(command + "commandLine", new XElement(command + "commandText")))); examplesElement.Add(example); exampleCount++; } commandElement.Add(examplesElement); } } doc.Save(outFile); }
internal void AddDirectItem(SyntaxItem syntaxItem) { this.m_directItems.Add(syntaxItem); }
static void Main(string[] args) { var cmdlets = new List <CmdletInfo>(); var inFile = args[0]; var outFile = args[1]; // Specify an additional (third) parameter pointing to the Solution folder to generate Markdown. The markdown // will be created in the Documentation folder underneath the solution folder. bool generateMarkdown = false; string solutionDir = null; if (args.Length > 2) { solutionDir = args[2]; generateMarkdown = true; } var doc = new XDocument(new XDeclaration("1.0", "UTF-8", string.Empty)); XNamespace ns = "http://msh"; var helpItems = new XElement(ns + "helpItems", new XAttribute("schema", "maml")); doc.Add(helpItems); XNamespace maml = "http://schemas.microsoft.com/maml/2004/10"; XNamespace command = "http://schemas.microsoft.com/maml/dev/command/2004/10"; XNamespace dev = "http://schemas.microsoft.com/maml/dev/2004/10"; var mamlNsAttr = new XAttribute(XNamespace.Xmlns + "maml", "http://schemas.microsoft.com/maml/2004/10"); var commandNsAttr = new XAttribute(XNamespace.Xmlns + "command", "http://schemas.microsoft.com/maml/dev/command/2004/10"); var devNsAttr = new XAttribute(XNamespace.Xmlns + "dev", "http://schemas.microsoft.com/maml/dev/2004/10"); var assembly = Assembly.LoadFrom(inFile); var types = assembly.GetTypes(); foreach (var t in types) { if (t.BaseType.Name == "SPOCmdlet" || t.BaseType.Name == "PSCmdlet" || t.BaseType.Name == "SPOWebCmdlet" || t.BaseType.Name == "SPOAdminCmdlet") { //XElement examples = new XElement(command + "examples"); var verb = string.Empty; var noun = string.Empty; var description = string.Empty; var detaileddescription = string.Empty; var copyright = string.Empty; var version = string.Empty; var attrs = t.GetCustomAttributes(); var examples = new List <CmdletExampleAttribute>(); //System.Attribute.GetCustomAttributes(t); foreach (var attr in attrs) { if (attr is CmdletAttribute) { var a = (CmdletAttribute)attr; verb = a.VerbName; noun = a.NounName; } if (attr is CmdletHelpAttribute) { var a = (CmdletHelpAttribute)attr; description = a.Description; copyright = a.Copyright; version = a.Version; detaileddescription = a.DetailedDescription; } if (attr is CmdletExampleAttribute) { var a = (CmdletExampleAttribute)attr; examples.Add(a); } } var cmdletInfo = new CmdletInfo(verb, noun); cmdletInfo.Description = description; cmdletInfo.DetailedDescription = detaileddescription; cmdletInfo.Version = version; cmdletInfo.Copyright = copyright; var commandElement = new XElement(command + "command", mamlNsAttr, commandNsAttr, devNsAttr); var detailsElement = new XElement(command + "details"); commandElement.Add(detailsElement); detailsElement.Add(new XElement(command + "name", string.Format("{0}-{1}", verb, noun))); detailsElement.Add(new XElement(maml + "description", new XElement(maml + "para", description))); detailsElement.Add(new XElement(maml + "copyright", new XElement(maml + "para", copyright))); detailsElement.Add(new XElement(command + "verb", verb)); detailsElement.Add(new XElement(command + "noun", noun)); detailsElement.Add(new XElement(dev + "version", version)); commandElement.Add(new XElement(maml + "description", new XElement(maml + "para", detaileddescription))); var syntaxElement = new XElement(command + "syntax"); commandElement.Add(syntaxElement); var fields = t.GetFields(); var syntaxItems = new List <SyntaxItem>(); foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (a.ParameterSetName != ParameterAttribute.AllParameterSets) { var cmdletSyntax = cmdletInfo.Syntaxes.FirstOrDefault(c => c.ParameterSetName == a.ParameterSetName); if (cmdletSyntax == null) { cmdletSyntax = new CmdletSyntax(); cmdletSyntax.ParameterSetName = a.ParameterSetName; cmdletInfo.Syntaxes.Add(cmdletSyntax); } cmdletSyntax.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); var syntaxItem = syntaxItems.FirstOrDefault(x => x.Name == a.ParameterSetName); if (syntaxItem == null) { syntaxItem = new SyntaxItem(a.ParameterSetName); syntaxItems.Add(syntaxItem); } syntaxItem.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } // all parameters foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (a.ParameterSetName == ParameterAttribute.AllParameterSets) { foreach (var si in syntaxItems) { si.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } if (cmdletInfo.Syntaxes.Count == 0) { cmdletInfo.Syntaxes.Add(new CmdletSyntax() { ParameterSetName = ParameterAttribute.AllParameterSets }); } foreach (var cmdletSyntax in cmdletInfo.Syntaxes) { cmdletSyntax.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } } // foreach (var syntaxItem in syntaxItems) { var syntaxItemElement = new XElement(command + "syntaxItem"); syntaxElement.Add(syntaxItemElement); syntaxItemElement.Add(new XElement(maml + "name", string.Format("{0}-{1}", verb, noun))); foreach (var parameter in syntaxItem.Parameters) { var parameterElement = new XElement(command + "parameter", new XAttribute("required", parameter.Required), new XAttribute("position", parameter.Position > 0 ? parameter.Position.ToString() : "named")); parameterElement.Add(new XElement(maml + "name", parameter.Name)); parameterElement.Add(new XElement(maml + "description", new XElement(maml + "para", parameter.Description))); parameterElement.Add(new XElement(command + "parameterValue", parameter.Type)); syntaxItemElement.Add(parameterElement); } } var parametersElement = new XElement(command + "parameters"); commandElement.Add(parametersElement); foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; cmdletInfo.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); var parameter2Element = new XElement(command + "parameter", new XAttribute("required", a.Mandatory), new XAttribute("position", a.Position > 0 ? a.Position.ToString() : "named")); parameter2Element.Add(new XElement(maml + "name", field.Name)); parameter2Element.Add(new XElement(maml + "description", new XElement(maml + "para", a.HelpMessage))); var parameterValueElement = new XElement(command + "parameterValue", field.FieldType.Name, new XAttribute("required", a.Mandatory)); parameter2Element.Add(parameterValueElement); var devElement = new XElement(dev + "type"); devElement.Add(new XElement(maml + "name", field.FieldType.Name)); devElement.Add(new XElement(maml + "uri")); parameter2Element.Add(devElement); parametersElement.Add(parameter2Element); break; } } } commandElement.Add( new XElement(command + "inputTypes", new XElement(command + "inputType", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); helpItems.Add(commandElement); commandElement.Add( new XElement(command + "returnValues", new XElement(command + "returnValue", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); var examplesElement = new XElement(command + "examples"); var exampleCount = 1; foreach (var exampleAttr in examples.OrderBy(e => e.SortOrder)) { var example = new XElement(command + "example"); var title = string.Format("------------------EXAMPLE {0}---------------------", exampleCount); example.Add(new XElement(maml + "title", title)); example.Add(new XElement(maml + "introduction", new XElement(maml + "para", exampleAttr.Introduction))); example.Add(new XElement(dev + "code", exampleAttr.Code)); example.Add(new XElement(maml + "remarks", new XElement(maml + "para", exampleAttr.Remarks))); example.Add(new XElement(command + "commandLines", new XElement(command + "commandLine", new XElement(command + "commandText")))); examplesElement.Add(example); exampleCount++; } commandElement.Add(examplesElement); if (generateMarkdown) { if (!string.IsNullOrEmpty(cmdletInfo.Verb) && !string.IsNullOrEmpty(cmdletInfo.Noun)) { using (var docfile = new System.IO.StreamWriter(string.Format("{0}\\Documentation\\{1}{2}.md", solutionDir, cmdletInfo.Verb, cmdletInfo.Noun))) { docfile.WriteLine("#{0}", cmdletInfo.FullCommand); docfile.WriteLine("*Topic automatically generated on: {0}*", DateTime.Now.ToString("yyyy-MM-dd")); docfile.WriteLine(""); docfile.WriteLine(cmdletInfo.Description); docfile.WriteLine("##Syntax"); foreach (var cmdletSyntax in cmdletInfo.Syntaxes) { var syntaxText = new StringBuilder(); syntaxText.AppendFormat(" {0}", cmdletInfo.FullCommand); foreach (var par in cmdletSyntax.Parameters.OrderBy(p => p.Position)) { syntaxText.Append(" "); if (!par.Required) { syntaxText.Append("["); } syntaxText.AppendFormat("-{0} [<{1}>]", par.Name, par.Type); if (!par.Required) { syntaxText.Append("]"); } } // Add All ParameterSet ones docfile.WriteLine(syntaxText); docfile.WriteLine(""); docfile.WriteLine(" "); docfile.WriteLine(""); } if (!string.IsNullOrEmpty(cmdletInfo.DetailedDescription)) { docfile.WriteLine("##Detailed Description"); docfile.WriteLine(cmdletInfo.DetailedDescription); docfile.WriteLine(""); } docfile.WriteLine("##Parameters"); docfile.WriteLine("Parameter|Type|Required|Description"); docfile.WriteLine("---------|----|--------|-----------"); foreach (var par in cmdletInfo.Parameters.OrderBy(x => x.Name)) { docfile.WriteLine("{0}|{1}|{2}|{3}", par.Name, par.Type, par.Required ? "True" : "False", par.Description); } if (examples.Any()) { docfile.WriteLine("##Examples"); } var examplesCount = 1; foreach (var example in examples.OrderBy(e => e.SortOrder)) { docfile.WriteLine(example.Introduction); docfile.WriteLine("###Example {0}", examplesCount); docfile.WriteLine(" {0}", example.Code); docfile.WriteLine(example.Remarks); examplesCount++; } } } } } } doc.Save(outFile); }
public AnalyzeReslut(SyntaxItem AItem, string ABlock) { FItem = AItem; FBlock = ABlock; }
/// <summary> /// Selected the class item that is nearest to the caret position. /// </summary> /// <param name="caretOffset">The caret offset.</param> private void DoSelectItem(int caretOffset) { ClassItem matchClassItemInside = null; ClassItem nearestClassItemMatch = null; int nearestMatchDistance = int.MaxValue; foreach (var item in _classItems) { var span = item.ClassDeclarationSyntax.GetLocation().SourceSpan; if (Intersects(caretOffset, span)) { // when there are multiple matches inside (nested classes), use the last one matchClassItemInside = item; } else { // Not a perfect match? // Try to first the nearest match. We want the classes combo box to always // have a class selected if possible. int matchDistance = Math.Min(Math.Abs(caretOffset - span.Start), Math.Abs(caretOffset - span.End)); if (matchDistance < nearestMatchDistance) { nearestMatchDistance = matchDistance; nearestClassItemMatch = item; } } } _doJumpOnSelectionChange = false; try { classComboBox.SelectedItem = matchClassItemInside ?? nearestClassItemMatch; // the SelectedItem setter will update the list of member items } finally { _doJumpOnSelectionChange = true; } // finished selection of class item // now find the nearest member item SyntaxItem matchMemberItemInside = null; SyntaxItem nearestMemberItemMatch = null; nearestMatchDistance = int.MaxValue; foreach (var item in _memberItems) { if (item.IsInSamePart) { var member = item.SyntaxNode; var memberSpan = item.SyntaxNode.GetLocation().SourceSpan; if (Intersects(caretOffset, memberSpan)) { matchMemberItemInside = item; } else { // Not a perfect match? // Try to first the nearest match. We want the classes combo box to always // have a class selected if possible. int matchDistance = Math.Min(Math.Abs(caretOffset - memberSpan.Start), Math.Abs(caretOffset - memberSpan.End)); if (matchDistance < nearestMatchDistance) { nearestMatchDistance = matchDistance; nearestMemberItemMatch = item; } } } } _doJumpOnSelectionChange = false; try { membersComboBox.SelectedItem = matchMemberItemInside ?? nearestMemberItemMatch; } finally { _doJumpOnSelectionChange = true; } }
public static Next Parse(SyntaxItem item) { return(new Next(item.values)); }
static void Main(string[] args) { var cmdlets = new List<CmdletInfo>(); var inFile = args[0]; var outFile = args[1]; var toc = new List<CmdletInfo>(); // Specify an additional (third) parameter pointing to the Solution folder to generate Markdown. The markdown // will be created in the Documentation folder underneath the solution folder. bool generateMarkdown = false; string solutionDir = null; if (args.Length > 2) { solutionDir = args[2]; generateMarkdown = true; } var doc = new XDocument(new XDeclaration("1.0", "UTF-8", string.Empty)); XNamespace ns = "http://msh"; var helpItems = new XElement(ns + "helpItems", new XAttribute("schema", "maml")); doc.Add(helpItems); XNamespace maml = "http://schemas.microsoft.com/maml/2004/10"; XNamespace command = "http://schemas.microsoft.com/maml/dev/command/2004/10"; XNamespace dev = "http://schemas.microsoft.com/maml/dev/2004/10"; var mamlNsAttr = new XAttribute(XNamespace.Xmlns + "maml", "http://schemas.microsoft.com/maml/2004/10"); var commandNsAttr = new XAttribute(XNamespace.Xmlns + "command", "http://schemas.microsoft.com/maml/dev/command/2004/10"); var devNsAttr = new XAttribute(XNamespace.Xmlns + "dev", "http://schemas.microsoft.com/maml/dev/2004/10"); var assembly = Assembly.LoadFrom(inFile); var types = assembly.GetTypes(); foreach (var t in types) { if (t.BaseType.Name == "SPOCmdlet" || t.BaseType.Name == "PSCmdlet" || t.BaseType.Name == "SPOWebCmdlet" || t.BaseType.Name == "SPOAdminCmdlet") { //XElement examples = new XElement(command + "examples"); var verb = string.Empty; var noun = string.Empty; var description = string.Empty; var detaileddescription = string.Empty; var copyright = string.Empty; var version = string.Empty; var category = string.Empty; var attrs = t.GetCustomAttributes(); var examples = new List<CmdletExampleAttribute>(); //System.Attribute.GetCustomAttributes(t); foreach (var attr in attrs) { if (attr is CmdletAttribute) { var a = (CmdletAttribute)attr; verb = a.VerbName; noun = a.NounName; } if (attr is CmdletHelpAttribute) { var a = (CmdletHelpAttribute)attr; description = a.Description; copyright = a.Copyright; version = a.Version; detaileddescription = a.DetailedDescription; category = a.Category; } if (attr is CmdletExampleAttribute) { var a = (CmdletExampleAttribute)attr; examples.Add(a); } } var cmdletInfo = new CmdletInfo(verb, noun); cmdletInfo.Description = description; cmdletInfo.DetailedDescription = detaileddescription; cmdletInfo.Version = version; cmdletInfo.Copyright = copyright; cmdletInfo.Category = category; var commandElement = new XElement(command + "command", mamlNsAttr, commandNsAttr, devNsAttr); var detailsElement = new XElement(command + "details"); commandElement.Add(detailsElement); detailsElement.Add(new XElement(command + "name", string.Format("{0}-{1}", verb, noun))); detailsElement.Add(new XElement(maml + "description", new XElement(maml + "para", description))); detailsElement.Add(new XElement(maml + "copyright", new XElement(maml + "para", copyright))); detailsElement.Add(new XElement(command + "verb", verb)); detailsElement.Add(new XElement(command + "noun", noun)); detailsElement.Add(new XElement(dev + "version", version)); commandElement.Add(new XElement(maml + "description", new XElement(maml + "para", detaileddescription))); var syntaxElement = new XElement(command + "syntax"); commandElement.Add(syntaxElement); var fields = t.GetFields(); var syntaxItems = new List<SyntaxItem>(); foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!a.DontShow) { if (a.ParameterSetName != ParameterAttribute.AllParameterSets) { var cmdletSyntax = cmdletInfo.Syntaxes.FirstOrDefault(c => c.ParameterSetName == a.ParameterSetName); if (cmdletSyntax == null) { cmdletSyntax = new CmdletSyntax(); cmdletSyntax.ParameterSetName = a.ParameterSetName; cmdletInfo.Syntaxes.Add(cmdletSyntax); } cmdletSyntax.Parameters.Add(new CmdletParameterInfo() {Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name}); } var syntaxItem = syntaxItems.FirstOrDefault(x => x.Name == a.ParameterSetName); if (syntaxItem == null) { syntaxItem = new SyntaxItem(a.ParameterSetName); syntaxItems.Add(syntaxItem); } syntaxItem.Parameters.Add(new SyntaxItem.Parameter() {Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name}); } } } } // all parameters foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!a.DontShow) { if (a.ParameterSetName == ParameterAttribute.AllParameterSets) { foreach (var si in syntaxItems) { si.Parameters.Add(new SyntaxItem.Parameter() {Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name}); } if (cmdletInfo.Syntaxes.Count == 0) { cmdletInfo.Syntaxes.Add(new CmdletSyntax() {ParameterSetName = ParameterAttribute.AllParameterSets}); } foreach (var cmdletSyntax in cmdletInfo.Syntaxes) { cmdletSyntax.Parameters.Add(new CmdletParameterInfo() {Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name}); } } } } } } // foreach (var syntaxItem in syntaxItems) { var syntaxItemElement = new XElement(command + "syntaxItem"); syntaxElement.Add(syntaxItemElement); syntaxItemElement.Add(new XElement(maml + "name", string.Format("{0}-{1}", verb, noun))); foreach (var parameter in syntaxItem.Parameters) { var parameterElement = new XElement(command + "parameter", new XAttribute("required", parameter.Required), new XAttribute("position", parameter.Position > 0 ? parameter.Position.ToString() : "named")); parameterElement.Add(new XElement(maml + "name", parameter.Name)); parameterElement.Add(new XElement(maml + "description", new XElement(maml + "para", parameter.Description))); parameterElement.Add(new XElement(command + "parameterValue", parameter.Type)); syntaxItemElement.Add(parameterElement); } } var parametersElement = new XElement(command + "parameters"); commandElement.Add(parametersElement); foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!a.DontShow) { cmdletInfo.Parameters.Add(new CmdletParameterInfo() {Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name}); var parameter2Element = new XElement(command + "parameter", new XAttribute("required", a.Mandatory), new XAttribute("position", a.Position > 0 ? a.Position.ToString() : "named")); parameter2Element.Add(new XElement(maml + "name", field.Name)); parameter2Element.Add(new XElement(maml + "description", new XElement(maml + "para", a.HelpMessage))); var parameterValueElement = new XElement(command + "parameterValue", field.FieldType.Name, new XAttribute("required", a.Mandatory)); parameter2Element.Add(parameterValueElement); var devElement = new XElement(dev + "type"); devElement.Add(new XElement(maml + "name", field.FieldType.Name)); devElement.Add(new XElement(maml + "uri")); parameter2Element.Add(devElement); parametersElement.Add(parameter2Element); } break; } } } commandElement.Add( new XElement(command + "inputTypes", new XElement(command + "inputType", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); helpItems.Add(commandElement); commandElement.Add( new XElement(command + "returnValues", new XElement(command + "returnValue", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); var examplesElement = new XElement(command + "examples"); var exampleCount = 1; foreach (var exampleAttr in examples.OrderBy(e => e.SortOrder)) { var example = new XElement(command + "example"); var title = string.Format("------------------EXAMPLE {0}---------------------", exampleCount); example.Add(new XElement(maml + "title", title)); example.Add(new XElement(maml + "introduction", new XElement(maml + "para", exampleAttr.Introduction))); example.Add(new XElement(dev + "code", exampleAttr.Code)); example.Add(new XElement(maml + "remarks", new XElement(maml + "para", exampleAttr.Remarks))); example.Add(new XElement(command + "commandLines", new XElement(command + "commandLine", new XElement(command + "commandText")))); examplesElement.Add(example); exampleCount++; } commandElement.Add(examplesElement); if (generateMarkdown) { if (!string.IsNullOrEmpty(cmdletInfo.Verb) && !string.IsNullOrEmpty(cmdletInfo.Noun)) { toc.Add(cmdletInfo); using (var docfile = new System.IO.StreamWriter(string.Format("{0}\\Documentation\\{1}{2}.md", solutionDir, cmdletInfo.Verb, cmdletInfo.Noun))) { docfile.WriteLine("#{0}", cmdletInfo.FullCommand); docfile.WriteLine("*Topic automatically generated on: {0}*", DateTime.Now.ToString("yyyy-MM-dd")); docfile.WriteLine(""); docfile.WriteLine(cmdletInfo.Description); docfile.WriteLine("##Syntax"); foreach (var cmdletSyntax in cmdletInfo.Syntaxes) { var syntaxText = new StringBuilder(); syntaxText.AppendFormat("```powershell\r\n{0}", cmdletInfo.FullCommand); foreach (var par in cmdletSyntax.Parameters.OrderBy(p => p.Position)) { syntaxText.Append(" "); if (!par.Required) { syntaxText.Append("["); } syntaxText.AppendFormat("-{0} [<{1}>]", par.Name, par.Type); if (!par.Required) { syntaxText.Append("]"); } } // Add All ParameterSet ones docfile.WriteLine(syntaxText); docfile.WriteLine("```"); docfile.WriteLine(" "); docfile.WriteLine(""); } if (!string.IsNullOrEmpty(cmdletInfo.DetailedDescription)) { docfile.WriteLine("##Detailed Description"); docfile.WriteLine(cmdletInfo.DetailedDescription); docfile.WriteLine(""); } docfile.WriteLine("##Parameters"); docfile.WriteLine("Parameter|Type|Required|Description"); docfile.WriteLine("---------|----|--------|-----------"); foreach (var par in cmdletInfo.Parameters.OrderBy(x => x.Name)) { docfile.WriteLine("{0}|{1}|{2}|{3}", par.Name, par.Type, par.Required ? "True" : "False", par.Description); } if (examples.Any()) docfile.WriteLine("##Examples"); var examplesCount = 1; foreach (var example in examples.OrderBy(e => e.SortOrder)) { docfile.WriteLine(example.Introduction); docfile.WriteLine("###Example {0}", examplesCount); docfile.WriteLine(" {0}", example.Code); docfile.WriteLine(example.Remarks); examplesCount++; } } } } } } doc.Save(outFile); if (generateMarkdown) { // Create the readme.md using (var readme = new System.IO.StreamWriter(string.Format("{0}\\Documentation\\readme.md", solutionDir))) { readme.WriteLine("# Cmdlet Documentation #"); readme.WriteLine("Below you can find a list of all the available cmdlets. Many commands provide built-in help and examples. Retrieve the detailed help with "); readme.WriteLine("\r\n```powershell\r\nGet-Help Connect-SPOnline -Detailed\r\n```\r\n\r\n"); // Get all unique categories var categories = toc.Select(c => c.Category).Distinct(); foreach (var category in categories.OrderBy(c => c)) { readme.WriteLine("##{0}",category); readme.WriteLine("Cmdlet|Description"); readme.WriteLine(":-----|:----------"); foreach (var cmdletInfo in toc.Where(c => c.Category == category).OrderBy(c => c.Noun)) { var description = cmdletInfo.Description.Replace("\r\n", " "); readme.WriteLine("**[{0}]({1}{2}.md)** |{3}", cmdletInfo.FullCommand.Replace("-", "‑"), cmdletInfo.Verb, cmdletInfo.Noun, description); } } } } }
static void Main(string[] args) { var cmdlets = new List <CmdletInfo>(); var inFile = args[0]; var outFile = args[1]; var toc = new List <CmdletInfo>(); // Specify an additional (third) parameter pointing to the Solution folder to generate Markdown. The markdown // will be created in the Documentation folder underneath the solution folder. bool generateMarkdown = false; string solutionDir = null; if (args.Length > 2) { solutionDir = args[2]; generateMarkdown = true; } var doc = new XDocument(new XDeclaration("1.0", "UTF-8", string.Empty)); XNamespace ns = "http://msh"; var helpItems = new XElement(ns + "helpItems", new XAttribute("schema", "maml")); doc.Add(helpItems); XNamespace maml = "http://schemas.microsoft.com/maml/2004/10"; XNamespace command = "http://schemas.microsoft.com/maml/dev/command/2004/10"; XNamespace dev = "http://schemas.microsoft.com/maml/dev/2004/10"; var mamlNsAttr = new XAttribute(XNamespace.Xmlns + "maml", "http://schemas.microsoft.com/maml/2004/10"); var commandNsAttr = new XAttribute(XNamespace.Xmlns + "command", "http://schemas.microsoft.com/maml/dev/command/2004/10"); var devNsAttr = new XAttribute(XNamespace.Xmlns + "dev", "http://schemas.microsoft.com/maml/dev/2004/10"); var assembly = Assembly.LoadFrom(inFile); var types = assembly.GetTypes(); foreach (var t in types) { if (t.BaseType.Name == "SPOCmdlet" || t.BaseType.Name == "PSCmdlet" || t.BaseType.Name == "SPOWebCmdlet" || t.BaseType.Name == "SPOAdminCmdlet") { //XElement examples = new XElement(command + "examples"); var verb = string.Empty; var noun = string.Empty; var description = string.Empty; var detaileddescription = string.Empty; var copyright = string.Empty; var version = string.Empty; var category = string.Empty; var attrs = t.GetCustomAttributes(); var examples = new List <CmdletExampleAttribute>(); //System.Attribute.GetCustomAttributes(t); // Get info from attributes foreach (var attr in attrs) { if (attr is CmdletAttribute) { var a = (CmdletAttribute)attr; verb = a.VerbName; noun = a.NounName; } if (attr is CmdletHelpAttribute) { var a = (CmdletHelpAttribute)attr; description = a.Description; copyright = a.Copyright; version = a.Version; detaileddescription = a.DetailedDescription; category = a.Category; } if (attr is CmdletExampleAttribute) { var a = (CmdletExampleAttribute)attr; examples.Add(a); } } // Store in CmdletInfo structure var cmdletInfo = new CmdletInfo(verb, noun); cmdletInfo.Description = description; cmdletInfo.DetailedDescription = detaileddescription; cmdletInfo.Version = version; cmdletInfo.Copyright = copyright; cmdletInfo.Category = category; // Build XElement for command var commandElement = new XElement(command + "command", mamlNsAttr, commandNsAttr, devNsAttr); var detailsElement = new XElement(command + "details"); commandElement.Add(detailsElement); detailsElement.Add(new XElement(command + "name", string.Format("{0}-{1}", verb, noun))); detailsElement.Add(new XElement(maml + "description", new XElement(maml + "para", description))); detailsElement.Add(new XElement(maml + "copyright", new XElement(maml + "para", copyright))); detailsElement.Add(new XElement(command + "verb", verb)); detailsElement.Add(new XElement(command + "noun", noun)); detailsElement.Add(new XElement(dev + "version", version)); if (!string.IsNullOrWhiteSpace(detaileddescription)) { commandElement.Add(new XElement(maml + "description", new XElement(maml + "para", detaileddescription))); } var syntaxElement = new XElement(command + "syntax"); commandElement.Add(syntaxElement); // Store syntaxes in CmdletInfo structure (if not AllParameterSets), and also in all syntaxItems list var fields = t.GetFields(); var syntaxItems = new List <SyntaxItem>(); foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!a.DontShow) { if (a.ParameterSetName != ParameterAttribute.AllParameterSets) { var cmdletSyntax = cmdletInfo.Syntaxes.FirstOrDefault(c => c.ParameterSetName == a.ParameterSetName); if (cmdletSyntax == null) { cmdletSyntax = new CmdletSyntax(); cmdletSyntax.ParameterSetName = a.ParameterSetName; cmdletInfo.Syntaxes.Add(cmdletSyntax); } cmdletSyntax.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); var syntaxItem = syntaxItems.FirstOrDefault(x => x.Name == a.ParameterSetName); if (syntaxItem == null) { syntaxItem = new SyntaxItem(a.ParameterSetName); syntaxItems.Add(syntaxItem); } syntaxItem.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } } // all parameters // Add AllParameterSets to all CmdletInfo syntax sets and syntaxItems sets (first checking there is at least one, i.e. if all are marked AllParameterSets) foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!a.DontShow) { if (a.ParameterSetName == ParameterAttribute.AllParameterSets) { if (syntaxItems.Count == 0) { syntaxItems.Add(new SyntaxItem(ParameterAttribute.AllParameterSets)); } foreach (var si in syntaxItems) { si.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } if (cmdletInfo.Syntaxes.Count == 0) { cmdletInfo.Syntaxes.Add(new CmdletSyntax() { ParameterSetName = ParameterAttribute.AllParameterSets }); } foreach (var cmdletSyntax in cmdletInfo.Syntaxes) { cmdletSyntax.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } } } // Build XElement for parameters from syntaxItems list (note: syntax element is set above) foreach (var syntaxItem in syntaxItems) { var syntaxItemElement = new XElement(command + "syntaxItem"); syntaxElement.Add(syntaxItemElement); syntaxItemElement.Add(new XElement(maml + "name", string.Format("{0}-{1}", verb, noun))); foreach (var parameter in syntaxItem.Parameters) { var parameterElement = new XElement(command + "parameter", new XAttribute("required", parameter.Required), new XAttribute("position", parameter.Position > 0 ? parameter.Position.ToString() : "named")); parameterElement.Add(new XElement(maml + "name", parameter.Name)); parameterElement.Add(new XElement(maml + "description", new XElement(maml + "para", parameter.Description))); parameterElement.Add(new XElement(command + "parameterValue", new XAttribute("required", parameter.Type != "SwitchParameter"), parameter.Type)); syntaxItemElement.Add(parameterElement); } } // Also store parameters in cmdletInfo.Parameters (all parameters) and XElement parameters var parametersElement = new XElement(command + "parameters"); commandElement.Add(parametersElement); foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!a.DontShow) { cmdletInfo.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); var parameter2Element = new XElement(command + "parameter", new XAttribute("required", a.Mandatory), new XAttribute("position", a.Position > 0 ? a.Position.ToString() : "named")); parameter2Element.Add(new XElement(maml + "name", field.Name)); parameter2Element.Add(new XElement(maml + "description", new XElement(maml + "para", a.HelpMessage))); var parameterValueElement = new XElement(command + "parameterValue", field.FieldType.Name, new XAttribute("required", a.Mandatory)); parameter2Element.Add(parameterValueElement); var devElement = new XElement(dev + "type"); devElement.Add(new XElement(maml + "name", field.FieldType.Name)); devElement.Add(new XElement(maml + "uri")); parameter2Element.Add(devElement); parametersElement.Add(parameter2Element); } break; } } } // XElement inputTypes commandElement.Add( new XElement(command + "inputTypes", new XElement(command + "inputType", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); helpItems.Add(commandElement); // XElement return values commandElement.Add( new XElement(command + "returnValues", new XElement(command + "returnValue", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); // XElement examples var examplesElement = new XElement(command + "examples"); var exampleCount = 1; foreach (var exampleAttr in examples.OrderBy(e => e.SortOrder)) { var example = new XElement(command + "example"); var title = string.Format("------------------EXAMPLE {0}---------------------", exampleCount); example.Add(new XElement(maml + "title", title)); example.Add(new XElement(maml + "introduction", new XElement(maml + "para", exampleAttr.Introduction))); example.Add(new XElement(dev + "code", exampleAttr.Code)); example.Add(new XElement(maml + "remarks", new XElement(maml + "para", exampleAttr.Remarks))); example.Add(new XElement(command + "commandLines", new XElement(command + "commandLine", new XElement(command + "commandText")))); examplesElement.Add(example); exampleCount++; } commandElement.Add(examplesElement); // Markdown from CmdletInfo if (generateMarkdown) { if (!string.IsNullOrEmpty(cmdletInfo.Verb) && !string.IsNullOrEmpty(cmdletInfo.Noun)) { string mdFilePath = string.Format("{0}\\Documentation\\{1}{2}.md", solutionDir, cmdletInfo.Verb, cmdletInfo.Noun); toc.Add(cmdletInfo); var existingHashCode = string.Empty; if (System.IO.File.Exists(mdFilePath)) { // Calculate hashcode var existingFileText = System.IO.File.ReadAllText(mdFilePath); var refPosition = existingFileText.IndexOf("<!-- Ref:"); if (refPosition > -1) { var refEndPosition = existingFileText.IndexOf("-->", refPosition); if (refEndPosition > -1) { var refCode = existingFileText.Substring(refPosition + 9, refEndPosition - refPosition - 9).Trim(); if (!string.IsNullOrEmpty(refCode)) { existingHashCode = refCode; } } } } var docHeaderBuilder = new StringBuilder(); // Separate header from body to calculate the hashcode later docHeaderBuilder.AppendFormat("#{0}{1}", cmdletInfo.FullCommand, Environment.NewLine); docHeaderBuilder.AppendFormat("*Topic automatically generated on: {0}*{1}", DateTime.Now.ToString("yyyy'-'MM'-'dd"), Environment.NewLine); docHeaderBuilder.Append(Environment.NewLine); // Body var docBuilder = new StringBuilder(); docBuilder.AppendFormat("{0}{1}", cmdletInfo.Description, Environment.NewLine); docBuilder.AppendFormat("##Syntax{0}", Environment.NewLine); foreach (var cmdletSyntax in cmdletInfo.Syntaxes.OrderBy(s => s.ParameterSetName)) { var syntaxText = new StringBuilder(); syntaxText.AppendFormat("```powershell\r\n{0}", cmdletInfo.FullCommand); foreach (var par in cmdletSyntax.Parameters.OrderBy(p => p.Position)) { syntaxText.Append(" "); if (!par.Required) { syntaxText.Append("["); } if (par.Type == "SwitchParameter") { syntaxText.AppendFormat("-{0} [<{1}>]", par.Name, par.Type); } else { syntaxText.AppendFormat("-{0} <{1}>", par.Name, par.Type); } if (!par.Required) { syntaxText.Append("]"); } } // Add All ParameterSet ones docBuilder.Append(syntaxText); docBuilder.AppendFormat("\n```\n\n\n"); } if (!string.IsNullOrEmpty(cmdletInfo.DetailedDescription)) { docBuilder.Append("##Detailed Description\n"); docBuilder.AppendFormat("{0}\n\n", cmdletInfo.DetailedDescription); } docBuilder.Append("##Parameters\n"); docBuilder.Append("Parameter|Type|Required|Description\n"); docBuilder.Append("---------|----|--------|-----------\n"); foreach (var par in cmdletInfo.Parameters.OrderBy(x => x.Name)) { docBuilder.AppendFormat("|{0}|{1}|{2}|{3}|\n", par.Name, par.Type, par.Required ? "True" : "False", par.Description); } if (examples.Any()) { docBuilder.Append("##Examples\n"); } var examplesCount = 1; foreach (var example in examples.OrderBy(e => e.SortOrder)) { docBuilder.AppendFormat("{0}\n", example.Introduction); docBuilder.AppendFormat("###Example {0}\n", examplesCount); docBuilder.AppendFormat(" {0}\n", example.Code); docBuilder.AppendFormat("{0}\n", example.Remarks); examplesCount++; } var newHashCode = CalculateMD5Hash(docBuilder.ToString()); docBuilder.AppendFormat("<!-- Ref: {0} -->", newHashCode); // Add hashcode of generated text to the file as hidden entry if (newHashCode != existingHashCode) { System.IO.File.WriteAllText(mdFilePath, docHeaderBuilder.Append(docBuilder).ToString()); } } } } } doc.Save(outFile); if (generateMarkdown) { // Create the readme.md var existingHashCode = string.Empty; var readmePath = string.Format("{0}\\Documentation\\readme.md", solutionDir); if (System.IO.File.Exists(readmePath)) { existingHashCode = CalculateMD5Hash(System.IO.File.ReadAllText(readmePath)); } var docBuilder = new StringBuilder(); docBuilder.AppendFormat("# Cmdlet Documentation #{0}", Environment.NewLine); docBuilder.AppendFormat("Below you can find a list of all the available cmdlets. Many commands provide built-in help and examples. Retrieve the detailed help with {0}", Environment.NewLine); docBuilder.AppendFormat("{0}```powershell{0}Get-Help Connect-SPOnline -Detailed{0}```{0}{0}", Environment.NewLine); // Get all unique categories var categories = toc.Select(c => c.Category).Distinct(); foreach (var category in categories.OrderBy(c => c)) { docBuilder.AppendFormat("##{0}{1}", category, Environment.NewLine); docBuilder.AppendFormat("Cmdlet|Description{0}", Environment.NewLine); docBuilder.AppendFormat(":-----|:----------{0}", Environment.NewLine); foreach (var cmdletInfo in toc.Where(c => c.Category == category).OrderBy(c => c.Noun)) { var description = cmdletInfo.Description.Replace("\r\n", " "); docBuilder.AppendFormat("**[{0}]({1}{2}.md)** |{3}{4}", cmdletInfo.FullCommand.Replace("-", "‑"), cmdletInfo.Verb, cmdletInfo.Noun, description, Environment.NewLine); } } var newHashCode = CalculateMD5Hash(docBuilder.ToString()); if (newHashCode != existingHashCode) { System.IO.File.WriteAllText(readmePath, docBuilder.ToString()); } } }
static void Main(string[] args) { var cmdlets = new List <CmdletInfo>(); var inFile = args[0]; var outFile = args[1]; var toc = new List <CmdletInfo>(); // Specify an additional (third) parameter pointing to the Solution folder to generate Markdown. The markdown // will be created in the Documentation folder underneath the solution folder. bool generateMarkdown = false; string solutionDir = null; string spVersion = "Online"; if (args.Length > 2) { var configuration = args[2]; solutionDir = args[3]; generateMarkdown = true; switch (configuration.ToLowerInvariant()) { case "debug": case "release": { spVersion = "Online"; break; } case "debug15": case "release15": { spVersion = "2013"; break; } case "debug16": case "release16": { spVersion = "2016"; break; } } } var doc = new XDocument(new XDeclaration("1.0", "UTF-8", string.Empty)); XNamespace ns = "http://msh"; var helpItems = new XElement(ns + "helpItems", new XAttribute("schema", "maml")); doc.Add(helpItems); XNamespace maml = "http://schemas.microsoft.com/maml/2004/10"; XNamespace command = "http://schemas.microsoft.com/maml/dev/command/2004/10"; XNamespace dev = "http://schemas.microsoft.com/maml/dev/2004/10"; var mamlNsAttr = new XAttribute(XNamespace.Xmlns + "maml", "http://schemas.microsoft.com/maml/2004/10"); var commandNsAttr = new XAttribute(XNamespace.Xmlns + "command", "http://schemas.microsoft.com/maml/dev/command/2004/10"); var devNsAttr = new XAttribute(XNamespace.Xmlns + "dev", "http://schemas.microsoft.com/maml/dev/2004/10"); var assembly = Assembly.LoadFrom(inFile); var types = assembly.GetTypes(); // System.Diagnostics.Debugger.Launch(); var cmdletsToExport = new List <string>(); var aliasesToCreate = new List <KeyValuePair <string, string> >(); foreach (var t in types) { if (t.BaseType.Name == "SPOCmdlet" || t.BaseType.Name == "PSCmdlet" || t.BaseType.Name == "SPOWebCmdlet" || t.BaseType.Name == "SPOAdminCmdlet" || t.BaseType.Name == "PnPGraphCmdlet") { var verb = string.Empty; var noun = string.Empty; var description = string.Empty; var detaileddescription = string.Empty; var copyright = string.Empty; var version = string.Empty; var category = string.Empty; var attrs = t.GetCustomAttributes(); var examples = new List <CmdletExampleAttribute>(); var relatedLinks = new List <CmdletRelatedLinkAttribute>(); Type outputType = null; var outputTypeLink = string.Empty; var outputTypeDescription = string.Empty; var aliases = new List <string>(); var additionalParameters = new List <CmdletAdditionalParameter>(); // Get info from attributes foreach (var attr in attrs) { if (attr is CmdletAttribute) { var a = (CmdletAttribute)attr; verb = a.VerbName; noun = a.NounName; } if (attr is CmdletAliasAttribute) { var a = (CmdletAliasAttribute)attr; aliases.Add(a.Alias); } if (attr is CmdletHelpAttribute) { var a = (CmdletHelpAttribute)attr; description = a.Description; copyright = a.Copyright; version = a.Version; detaileddescription = a.DetailedDescription; category = ToEnumString(a.Category); outputType = a.OutputType; outputTypeLink = a.OutputTypeLink; outputTypeDescription = a.OutputTypeDescription; } if (attr is CmdletExampleAttribute) { var a = (CmdletExampleAttribute)attr; examples.Add(a); } if (attr is CmdletRelatedLinkAttribute) { var l = (CmdletRelatedLinkAttribute)attr; relatedLinks.Add(l); } if (attr is CmdletAdditionalParameter) { var a = (CmdletAdditionalParameter)attr; additionalParameters.Add(a); } } // Store in CmdletInfo structure var cmdletInfo = new CmdletInfo(verb, noun); cmdletInfo.Description = description; cmdletInfo.DetailedDescription = detaileddescription; cmdletInfo.Version = version; cmdletInfo.Copyright = copyright; cmdletInfo.Category = category; cmdletInfo.OutputType = outputType; cmdletInfo.OutputTypeLink = outputTypeLink; cmdletInfo.OutputTypeDescription = outputTypeDescription; cmdletInfo.Aliases = aliases; if (!string.IsNullOrEmpty(cmdletInfo.Verb) && !string.IsNullOrEmpty(cmdletInfo.Noun)) { cmdletsToExport.Add(cmdletInfo.FullCommand); } if (cmdletInfo.Aliases.Any()) { foreach (var alias in cmdletInfo.Aliases) { aliasesToCreate.Add(new KeyValuePair <string, string>(cmdletInfo.FullCommand, alias)); } } // Build XElement for command var commandElement = new XElement(command + "command", mamlNsAttr, commandNsAttr, devNsAttr); var detailsElement = new XElement(command + "details"); commandElement.Add(detailsElement); detailsElement.Add(new XElement(command + "name", $"{verb}-{noun}")); detailsElement.Add(new XElement(maml + "description", new XElement(maml + "para", description))); detailsElement.Add(new XElement(maml + "copyright", new XElement(maml + "para", copyright))); detailsElement.Add(new XElement(command + "verb", verb)); detailsElement.Add(new XElement(command + "noun", noun)); detailsElement.Add(new XElement(dev + "version", version)); if (!string.IsNullOrWhiteSpace(detaileddescription)) { commandElement.Add(new XElement(maml + "description", new XElement(maml + "para", detaileddescription))); } var syntaxElement = new XElement(command + "syntax"); commandElement.Add(syntaxElement); // Store syntaxes in CmdletInfo structure (if not AllParameterSets), and also in all syntaxItems list var fields = GetFields(t); //var fields = t.GetFields(); var syntaxItems = new List <SyntaxItem>(); foreach (var field in fields) { var dontShow = false; foreach (Attribute attr in field.GetCustomAttributes(typeof(ObsoleteAttribute), true)) { if (attr is ObsoleteAttribute) { dontShow = true; break; } } foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!dontShow) { if (a.ParameterSetName != ParameterAttribute.AllParameterSets) { var cmdletSyntax = cmdletInfo.Syntaxes.FirstOrDefault(c => c.ParameterSetName == a.ParameterSetName); if (cmdletSyntax == null) { cmdletSyntax = new CmdletSyntax(); cmdletSyntax.ParameterSetName = a.ParameterSetName; cmdletInfo.Syntaxes.Add(cmdletSyntax); } cmdletSyntax.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); var syntaxItem = syntaxItems.FirstOrDefault(x => x.Name == a.ParameterSetName); if (syntaxItem == null) { syntaxItem = new SyntaxItem(a.ParameterSetName); syntaxItems.Add(syntaxItem); } syntaxItem.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } } foreach (var additionalParameter in additionalParameters) { if (additionalParameter.ParameterSetName != ParameterAttribute.AllParameterSets) { var cmdletSyntax = cmdletInfo.Syntaxes.FirstOrDefault(c => c.ParameterSetName == additionalParameter.ParameterSetName); if (cmdletSyntax == null) { cmdletSyntax = new CmdletSyntax(); cmdletSyntax.ParameterSetName = additionalParameter.ParameterSetName; cmdletInfo.Syntaxes.Add(cmdletSyntax); } cmdletSyntax.Parameters.Add(new CmdletParameterInfo() { Name = additionalParameter.ParameterName, Description = additionalParameter.HelpMessage, Position = additionalParameter.Position, Required = additionalParameter.Mandatory, Type = additionalParameter.ParameterType.Name }); var syntaxItem = syntaxItems.FirstOrDefault(x => x.Name == additionalParameter.ParameterSetName); if (syntaxItem == null) { syntaxItem = new SyntaxItem(additionalParameter.ParameterSetName); syntaxItems.Add(syntaxItem); } syntaxItem.Parameters.Add(new SyntaxItem.Parameter() { Name = additionalParameter.ParameterName, Description = additionalParameter.HelpMessage, Position = additionalParameter.Position, Required = additionalParameter.Mandatory, Type = additionalParameter.ParameterType.Name }); } } // all parameters // Add AllParameterSets to all CmdletInfo syntax sets and syntaxItems sets (first checking there is at least one, i.e. if all are marked AllParameterSets) foreach (var field in fields) { var dontShow = false; foreach (Attribute attr in field.GetCustomAttributes(typeof(ObsoleteAttribute), true)) { if (attr is ObsoleteAttribute) { dontShow = true; break; } } foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!dontShow) { if (a.ParameterSetName == ParameterAttribute.AllParameterSets) { if (syntaxItems.Count == 0) { syntaxItems.Add(new SyntaxItem(ParameterAttribute.AllParameterSets)); } foreach (var si in syntaxItems) { si.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } if (cmdletInfo.Syntaxes.Count == 0) { cmdletInfo.Syntaxes.Add(new CmdletSyntax() { ParameterSetName = ParameterAttribute.AllParameterSets }); } foreach (var cmdletSyntax in cmdletInfo.Syntaxes) { cmdletSyntax.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } } } // Build XElement for parameters from syntaxItems list (note: syntax element is set above) foreach (var syntaxItem in syntaxItems) { var syntaxItemElement = new XElement(command + "syntaxItem"); syntaxElement.Add(syntaxItemElement); syntaxItemElement.Add(new XElement(maml + "name", $"{verb}-{noun}")); foreach (var parameter in syntaxItem.Parameters) { var parameterElement = new XElement(command + "parameter", new XAttribute("required", parameter.Required), new XAttribute("position", parameter.Position > 0 ? parameter.Position.ToString() : "named")); parameterElement.Add(new XElement(maml + "name", parameter.Name)); parameterElement.Add(new XElement(maml + "description", new XElement(maml + "para", parameter.Description))); parameterElement.Add(new XElement(command + "parameterValue", new XAttribute("required", parameter.Type != "SwitchParameter"), parameter.Type)); syntaxItemElement.Add(parameterElement); } } // Also store parameters in cmdletInfo.Parameters (all parameters) and XElement parameters var parametersElement = new XElement(command + "parameters"); commandElement.Add(parametersElement); foreach (var field in fields) { var dontShow = false; foreach (Attribute attr in field.GetCustomAttributes(typeof(ObsoleteAttribute), true)) { if (attr is ObsoleteAttribute) { dontShow = true; break; } } foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!dontShow) { cmdletInfo.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); var parameter2Element = new XElement(command + "parameter", new XAttribute("required", a.Mandatory), new XAttribute("position", a.Position > 0 ? a.Position.ToString() : "named")); parameter2Element.Add(new XElement(maml + "name", field.Name)); parameter2Element.Add(new XElement(maml + "description", new XElement(maml + "para", a.HelpMessage))); var parameterValueElement = new XElement(command + "parameterValue", field.FieldType.Name, new XAttribute("required", a.Mandatory)); parameter2Element.Add(parameterValueElement); var devElement = new XElement(dev + "type"); devElement.Add(new XElement(maml + "name", field.FieldType.Name)); devElement.Add(new XElement(maml + "uri")); parameter2Element.Add(devElement); parametersElement.Add(parameter2Element); } break; } } } foreach (var additionalParameter in additionalParameters.GroupBy(o => new { o.ParameterName }).Select(o => o.FirstOrDefault())) { cmdletInfo.Parameters.Add(new CmdletParameterInfo() { Name = additionalParameter.ParameterName, Description = additionalParameter.HelpMessage, Position = additionalParameter.Position, Required = additionalParameter.Mandatory, Type = additionalParameter.ParameterType.Name }); var parameter2Element = new XElement(command + "parameter", new XAttribute("required", additionalParameter.Mandatory), new XAttribute("position", additionalParameter.Position > 0 ? additionalParameter.Position.ToString() : "named")); parameter2Element.Add(new XElement(maml + "name", additionalParameter.ParameterName)); parameter2Element.Add(new XElement(maml + "description", new XElement(maml + "para", additionalParameter.HelpMessage))); var parameterValueElement = new XElement(command + "parameterValue", additionalParameter.ParameterType.Name, new XAttribute("required", additionalParameter.Mandatory)); parameter2Element.Add(parameterValueElement); var devElement = new XElement(dev + "type"); devElement.Add(new XElement(maml + "name", additionalParameter.ParameterType.Name)); devElement.Add(new XElement(maml + "uri")); parameter2Element.Add(devElement); parametersElement.Add(parameter2Element); } // XElement inputTypes commandElement.Add( new XElement(command + "inputTypes", new XElement(command + "inputType", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); helpItems.Add(commandElement); // XElement return values commandElement.Add( new XElement(command + "returnValues", new XElement(command + "returnValue", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); // XElement examples var examplesElement = new XElement(command + "examples"); var exampleCount = 1; foreach (var exampleAttr in examples.OrderBy(e => e.SortOrder)) { var example = new XElement(command + "example"); var title = string.Format("------------------EXAMPLE {0}---------------------", exampleCount); example.Add(new XElement(maml + "title", title)); example.Add(new XElement(maml + "introduction", new XElement(maml + "para", exampleAttr.Introduction))); example.Add(new XElement(dev + "code", exampleAttr.Code)); example.Add(new XElement(maml + "remarks", new XElement(maml + "para", exampleAttr.Remarks))); example.Add(new XElement(command + "commandLines", new XElement(command + "commandLine", new XElement(command + "commandText")))); examplesElement.Add(example); exampleCount++; } commandElement.Add(examplesElement); // Related links var relatedLinksElement = new XElement(maml + "relatedLinks"); relatedLinks.Insert(0, new CmdletRelatedLinkAttribute() { Text = "Office 365 Developer Patterns and Practices", Url = "http://aka.ms/officedevpnp" }); foreach (var link in relatedLinks) { var navigationLinksElement = new XElement(maml + "navigationLink"); var linkText = new XElement(maml + "linkText"); linkText.Value = link.Text + ":"; navigationLinksElement.Add(linkText); var uriElement = new XElement(maml + "uri"); uriElement.Value = link.Url; navigationLinksElement.Add(uriElement); relatedLinksElement.Add(navigationLinksElement); } commandElement.Add(relatedLinksElement); // Markdown from CmdletInfo if (generateMarkdown) { GenerateMarkDown(toc, solutionDir, examples, cmdletInfo); } } } doc.Save(outFile); if (generateMarkdown) { CreateReadme(toc, solutionDir); } DirectoryInfo di = new DirectoryInfo($"{solutionDir}\\Documentation"); var mdFiles = di.GetFiles("*.md"); // Clean up old MD files foreach (var mdFile in mdFiles) { if (mdFile.Name.ToLowerInvariant() != "readme.md") { var index = toc.FindIndex(t => $"{t.Verb}{t.Noun}.md" == mdFile.Name); if (index == -1) { mdFile.Delete(); } } } // Generate PSM1 file var aliasesToExport = new List <string>(); var psm1Path = $"{new FileInfo(inFile).Directory}\\ModuleFiles\\SharePointPnPPowerShell{spVersion}.psm1"; if (aliasesToCreate.Any()) { var aliasBuilder = new StringBuilder(); foreach (var alias in aliasesToCreate.Where(a => !string.IsNullOrEmpty(a.Key)).OrderBy(a => a.Key)) { var aliasLine = $"Set-Alias -Name {alias.Value} -Value {alias.Key}"; aliasBuilder.AppendLine(aliasLine); aliasesToExport.Add(alias.Value); } Console.WriteLine($"Writing {psm1Path}"); File.WriteAllText(psm1Path, aliasBuilder.ToString()); } else { File.WriteAllText(psm1Path, ""); } // Create Module Manifest var psd1Path = $"{new FileInfo(inFile).Directory}\\ModuleFiles\\SharePointPnPPowerShell{spVersion}.psd1"; var cmdletsToExportString = string.Join(",", cmdletsToExport.Select(x => "'" + x + "'")); var aliasesToExportString = string.Join(",", aliasesToExport.Select(x => "'" + x + "'")); WriteModuleManifest(psd1Path, spVersion, cmdletsToExportString, aliasesToExportString); }
private void DoKeywordMatch(SyntaxItem matchedKeyword, int posAfterKeyword) { int posKeywordStart = this.Reader.PosCurrent; if (!matchedKeyword.IsTransparent) { this.TrimModesAtAndAfter(posKeywordStart); this.m_modes.Add(new KeyValuePair<int, HighlightMode>(posKeywordStart, matchedKeyword.HighlightMode)); this.BuildHighlightModes(posAfterKeyword); } if (this.TopScope.SyntaxItem is NextGroup) { this.PopScope(posAfterKeyword); } else { this.TopScope.TryClearStaleEnd(); } NextGroup.Instance.TryStartNextGroup(this, matchedKeyword, posAfterKeyword); return; }
private void ProcessOptions(SyntaxItem syntaxItem, Match m) { int idxArgument = 0; int idxRegionDelimiter = 0; string matchGroup = null; bool excludeNl = false; syntaxItem.LineNumberInSyntaxFile = this.SourceFile.LineNumber; ContainerItem container = (syntaxItem as ContainerItem); Region region = (syntaxItem as Region); foreach (Capture c in m.Groups["options"].Captures) { if (IsOption(c, "oneline") && syntaxItem is Region) { (syntaxItem as Region).IsOneLine = true; } else if (IsOption(c, "keepend")) { region.KeepEnd = true; } else if (IsOption(c, "extend")) { container.Extend = true; } else if (IsOption(c, "skipwhite")) { syntaxItem.SkipWhite = true; } else if (IsOption(c, "skipnl")) { syntaxItem.SkipNewLine = true; } else if (IsOption(c, "skipempty")) { syntaxItem.SkipEmptyLine = true; } else if (IsOption(c, "transparent")) { syntaxItem.IsTransparent = true; } else if (IsOption(c, "excludenl")) { excludeNl = true; } else if (IsOption(c, "matchgroup")) { matchGroup = m.Groups["argument"].Captures[idxArgument].Value; idxArgument++; if (matchGroup == "NONE") { matchGroup = null; } } else if (IsOption(c, "start")) { Pattern p = this.MakePatternForRegion(m, matchGroup, excludeNl, ref idxRegionDelimiter); region.StartPatterns.Add(p); } else if (IsOption(c, "end")) { Pattern p = this.MakePatternForRegion(m, matchGroup, excludeNl, ref idxRegionDelimiter); region.EndPatterns.Add(p); } else if (IsOption(c, "skip")) { Pattern p = this.MakePatternForRegion(m, matchGroup, excludeNl, ref idxRegionDelimiter); region.SkipPatterns.Add(p); } else if (IsOption(c, "contains")) { string groupNames = m.Groups["argument"].Captures[idxArgument].Value; idxArgument++; container.Contains.SetContentsTo(ConvertRegexesInGroupNames(groupNames)); } else if (IsOption(c, "containedin")) { string groupNames = m.Groups["argument"].Captures[idxArgument].Value; idxArgument++; syntaxItem.ContainedIn.SetContentsTo(ConvertRegexesInGroupNames(groupNames)); } else if (IsOption(c, "nextgroup")) { string groupNames = m.Groups["argument"].Captures[idxArgument].Value; idxArgument++; syntaxItem.NextGroupCluster.SetContentsTo(ConvertRegexesInGroupNames(groupNames)); // IMPORTANT: contained must be checked after 'containedin', otherwise it will usurp containedin } else if (IsOption(c, "contained")) { syntaxItem.IsContained = true; } } }
static void Main(string[] args) { var cmdlets = new List<CmdletInfo>(); var inFile = args[0]; var outFile = args[1]; var toc = new List<CmdletInfo>(); // Specify an additional (third) parameter pointing to the Solution folder to generate Markdown. The markdown // will be created in the Documentation folder underneath the solution folder. bool generateMarkdown = false; string solutionDir = null; if (args.Length > 2) { solutionDir = args[2]; generateMarkdown = true; } var doc = new XDocument(new XDeclaration("1.0", "UTF-8", string.Empty)); XNamespace ns = "http://msh"; var helpItems = new XElement(ns + "helpItems", new XAttribute("schema", "maml")); doc.Add(helpItems); XNamespace maml = "http://schemas.microsoft.com/maml/2004/10"; XNamespace command = "http://schemas.microsoft.com/maml/dev/command/2004/10"; XNamespace dev = "http://schemas.microsoft.com/maml/dev/2004/10"; var mamlNsAttr = new XAttribute(XNamespace.Xmlns + "maml", "http://schemas.microsoft.com/maml/2004/10"); var commandNsAttr = new XAttribute(XNamespace.Xmlns + "command", "http://schemas.microsoft.com/maml/dev/command/2004/10"); var devNsAttr = new XAttribute(XNamespace.Xmlns + "dev", "http://schemas.microsoft.com/maml/dev/2004/10"); var assembly = Assembly.LoadFrom(inFile); var types = assembly.GetTypes(); foreach (var t in types) { if (t.BaseType.Name == "SPOCmdlet" || t.BaseType.Name == "PSCmdlet" || t.BaseType.Name == "SPOWebCmdlet" || t.BaseType.Name == "SPOAdminCmdlet") { //XElement examples = new XElement(command + "examples"); var verb = string.Empty; var noun = string.Empty; var description = string.Empty; var detaileddescription = string.Empty; var copyright = string.Empty; var version = string.Empty; var category = string.Empty; var attrs = t.GetCustomAttributes(); var examples = new List<CmdletExampleAttribute>(); //System.Attribute.GetCustomAttributes(t); // Get info from attributes foreach (var attr in attrs) { if (attr is CmdletAttribute) { var a = (CmdletAttribute)attr; verb = a.VerbName; noun = a.NounName; } if (attr is CmdletHelpAttribute) { var a = (CmdletHelpAttribute)attr; description = a.Description; copyright = a.Copyright; version = a.Version; detaileddescription = a.DetailedDescription; category = a.Category; } if (attr is CmdletExampleAttribute) { var a = (CmdletExampleAttribute)attr; examples.Add(a); } } // Store in CmdletInfo structure var cmdletInfo = new CmdletInfo(verb, noun); cmdletInfo.Description = description; cmdletInfo.DetailedDescription = detaileddescription; cmdletInfo.Version = version; cmdletInfo.Copyright = copyright; cmdletInfo.Category = category; // Build XElement for command var commandElement = new XElement(command + "command", mamlNsAttr, commandNsAttr, devNsAttr); var detailsElement = new XElement(command + "details"); commandElement.Add(detailsElement); detailsElement.Add(new XElement(command + "name", string.Format("{0}-{1}", verb, noun))); detailsElement.Add(new XElement(maml + "description", new XElement(maml + "para", description))); detailsElement.Add(new XElement(maml + "copyright", new XElement(maml + "para", copyright))); detailsElement.Add(new XElement(command + "verb", verb)); detailsElement.Add(new XElement(command + "noun", noun)); detailsElement.Add(new XElement(dev + "version", version)); if (!string.IsNullOrWhiteSpace(detaileddescription)) { commandElement.Add(new XElement(maml + "description", new XElement(maml + "para", detaileddescription))); } var syntaxElement = new XElement(command + "syntax"); commandElement.Add(syntaxElement); // Store syntaxes in CmdletInfo structure (if not AllParameterSets), and also in all syntaxItems list var fields = t.GetFields(); var syntaxItems = new List<SyntaxItem>(); foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!a.DontShow) { if (a.ParameterSetName != ParameterAttribute.AllParameterSets) { var cmdletSyntax = cmdletInfo.Syntaxes.FirstOrDefault(c => c.ParameterSetName == a.ParameterSetName); if (cmdletSyntax == null) { cmdletSyntax = new CmdletSyntax(); cmdletSyntax.ParameterSetName = a.ParameterSetName; cmdletInfo.Syntaxes.Add(cmdletSyntax); } cmdletSyntax.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); var syntaxItem = syntaxItems.FirstOrDefault(x => x.Name == a.ParameterSetName); if (syntaxItem == null) { syntaxItem = new SyntaxItem(a.ParameterSetName); syntaxItems.Add(syntaxItem); } syntaxItem.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } } // all parameters // Add AllParameterSets to all CmdletInfo syntax sets and syntaxItems sets (first checking there is at least one, i.e. if all are marked AllParameterSets) foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!a.DontShow) { if (a.ParameterSetName == ParameterAttribute.AllParameterSets) { if (syntaxItems.Count == 0) { syntaxItems.Add(new SyntaxItem(ParameterAttribute.AllParameterSets)); } foreach (var si in syntaxItems) { si.Parameters.Add(new SyntaxItem.Parameter() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } if (cmdletInfo.Syntaxes.Count == 0) { cmdletInfo.Syntaxes.Add(new CmdletSyntax() { ParameterSetName = ParameterAttribute.AllParameterSets }); } foreach (var cmdletSyntax in cmdletInfo.Syntaxes) { cmdletSyntax.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); } } } } } } // Build XElement for parameters from syntaxItems list (note: syntax element is set above) foreach (var syntaxItem in syntaxItems) { var syntaxItemElement = new XElement(command + "syntaxItem"); syntaxElement.Add(syntaxItemElement); syntaxItemElement.Add(new XElement(maml + "name", string.Format("{0}-{1}", verb, noun))); foreach (var parameter in syntaxItem.Parameters) { var parameterElement = new XElement(command + "parameter", new XAttribute("required", parameter.Required), new XAttribute("position", parameter.Position > 0 ? parameter.Position.ToString() : "named")); parameterElement.Add(new XElement(maml + "name", parameter.Name)); parameterElement.Add(new XElement(maml + "description", new XElement(maml + "para", parameter.Description))); parameterElement.Add(new XElement(command + "parameterValue", new XAttribute("required", parameter.Type != "SwitchParameter"), parameter.Type)); syntaxItemElement.Add(parameterElement); } } // Also store parameters in cmdletInfo.Parameters (all parameters) and XElement parameters var parametersElement = new XElement(command + "parameters"); commandElement.Add(parametersElement); foreach (var field in fields) { foreach (Attribute attr in field.GetCustomAttributes(typeof(ParameterAttribute), true)) { if (attr is ParameterAttribute) { var a = (ParameterAttribute)attr; if (!a.DontShow) { cmdletInfo.Parameters.Add(new CmdletParameterInfo() { Name = field.Name, Description = a.HelpMessage, Position = a.Position, Required = a.Mandatory, Type = field.FieldType.Name }); var parameter2Element = new XElement(command + "parameter", new XAttribute("required", a.Mandatory), new XAttribute("position", a.Position > 0 ? a.Position.ToString() : "named")); parameter2Element.Add(new XElement(maml + "name", field.Name)); parameter2Element.Add(new XElement(maml + "description", new XElement(maml + "para", a.HelpMessage))); var parameterValueElement = new XElement(command + "parameterValue", field.FieldType.Name, new XAttribute("required", a.Mandatory)); parameter2Element.Add(parameterValueElement); var devElement = new XElement(dev + "type"); devElement.Add(new XElement(maml + "name", field.FieldType.Name)); devElement.Add(new XElement(maml + "uri")); parameter2Element.Add(devElement); parametersElement.Add(parameter2Element); } break; } } } // XElement inputTypes commandElement.Add( new XElement(command + "inputTypes", new XElement(command + "inputType", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); helpItems.Add(commandElement); // XElement return values commandElement.Add( new XElement(command + "returnValues", new XElement(command + "returnValue", new XElement(dev + "type", new XElement(maml + "name", "String"), new XElement(maml + "uri"), new XElement(maml + "description", new XElement(maml + "para", "description")))))); // XElement examples var examplesElement = new XElement(command + "examples"); var exampleCount = 1; foreach (var exampleAttr in examples.OrderBy(e => e.SortOrder)) { var example = new XElement(command + "example"); var title = string.Format("------------------EXAMPLE {0}---------------------", exampleCount); example.Add(new XElement(maml + "title", title)); example.Add(new XElement(maml + "introduction", new XElement(maml + "para", exampleAttr.Introduction))); example.Add(new XElement(dev + "code", exampleAttr.Code)); example.Add(new XElement(maml + "remarks", new XElement(maml + "para", exampleAttr.Remarks))); example.Add(new XElement(command + "commandLines", new XElement(command + "commandLine", new XElement(command + "commandText")))); examplesElement.Add(example); exampleCount++; } commandElement.Add(examplesElement); // Markdown from CmdletInfo if (generateMarkdown) { if (!string.IsNullOrEmpty(cmdletInfo.Verb) && !string.IsNullOrEmpty(cmdletInfo.Noun)) { string mdFilePath = string.Format("{0}\\Documentation\\{1}{2}.md", solutionDir, cmdletInfo.Verb, cmdletInfo.Noun); toc.Add(cmdletInfo); var existingHashCode = string.Empty; if (System.IO.File.Exists(mdFilePath)) { // Calculate hashcode var existingFileText = System.IO.File.ReadAllText(mdFilePath); var refPosition = existingFileText.IndexOf("<!-- Ref:"); if (refPosition > -1) { var refEndPosition = existingFileText.IndexOf("-->", refPosition); if (refEndPosition > -1) { var refCode = existingFileText.Substring(refPosition + 9, refEndPosition - refPosition - 9).Trim(); if (!string.IsNullOrEmpty(refCode)) { existingHashCode = refCode; } } } } var docHeaderBuilder = new StringBuilder(); // Separate header from body to calculate the hashcode later docHeaderBuilder.AppendFormat("#{0}{1}", cmdletInfo.FullCommand, Environment.NewLine); docHeaderBuilder.AppendFormat("*Topic automatically generated on: {0}*{1}", DateTime.Now.ToString("yyyy'-'MM'-'dd"), Environment.NewLine); docHeaderBuilder.Append(Environment.NewLine); // Body var docBuilder = new StringBuilder(); docBuilder.AppendFormat("{0}{1}", cmdletInfo.Description, Environment.NewLine); docBuilder.AppendFormat("##Syntax{0}", Environment.NewLine); foreach (var cmdletSyntax in cmdletInfo.Syntaxes.OrderBy(s => s.ParameterSetName)) { var syntaxText = new StringBuilder(); syntaxText.AppendFormat("```powershell\r\n{0}", cmdletInfo.FullCommand); foreach (var par in cmdletSyntax.Parameters.OrderBy(p => p.Position)) { syntaxText.Append(" "); if (!par.Required) { syntaxText.Append("["); } if (par.Type == "SwitchParameter") { syntaxText.AppendFormat("-{0} [<{1}>]", par.Name, par.Type); } else { syntaxText.AppendFormat("-{0} <{1}>", par.Name, par.Type); } if (!par.Required) { syntaxText.Append("]"); } } // Add All ParameterSet ones docBuilder.Append(syntaxText); docBuilder.AppendFormat("\n```\n\n\n"); } if (!string.IsNullOrEmpty(cmdletInfo.DetailedDescription)) { docBuilder.Append("##Detailed Description\n"); docBuilder.AppendFormat("{0}\n\n", cmdletInfo.DetailedDescription); } docBuilder.Append("##Parameters\n"); docBuilder.Append("Parameter|Type|Required|Description\n"); docBuilder.Append("---------|----|--------|-----------\n"); foreach (var par in cmdletInfo.Parameters.OrderBy(x => x.Name)) { docBuilder.AppendFormat("|{0}|{1}|{2}|{3}|\n", par.Name, par.Type, par.Required ? "True" : "False", par.Description); } if (examples.Any()) docBuilder.Append("##Examples\n"); var examplesCount = 1; foreach (var example in examples.OrderBy(e => e.SortOrder)) { docBuilder.AppendFormat("{0}\n", example.Introduction); docBuilder.AppendFormat("###Example {0}\n", examplesCount); docBuilder.AppendFormat(" {0}\n", example.Code); docBuilder.AppendFormat("{0}\n", example.Remarks); examplesCount++; } var newHashCode = CalculateMD5Hash(docBuilder.ToString()); docBuilder.AppendFormat("<!-- Ref: {0} -->", newHashCode); // Add hashcode of generated text to the file as hidden entry if (newHashCode != existingHashCode) { System.IO.File.WriteAllText(mdFilePath, docHeaderBuilder.Append(docBuilder).ToString()); } } } } } doc.Save(outFile); if (generateMarkdown) { // Create the readme.md var existingHashCode = string.Empty; var readmePath = string.Format("{0}\\Documentation\\readme.md", solutionDir); if (System.IO.File.Exists(readmePath)) { existingHashCode = CalculateMD5Hash(System.IO.File.ReadAllText(readmePath)); } var docBuilder = new StringBuilder(); docBuilder.AppendFormat("# Cmdlet Documentation #{0}", Environment.NewLine); docBuilder.AppendFormat("Below you can find a list of all the available cmdlets. Many commands provide built-in help and examples. Retrieve the detailed help with {0}", Environment.NewLine); docBuilder.AppendFormat("{0}```powershell{0}Get-Help Connect-SPOnline -Detailed{0}```{0}{0}", Environment.NewLine); // Get all unique categories var categories = toc.Select(c => c.Category).Distinct(); foreach (var category in categories.OrderBy(c => c)) { docBuilder.AppendFormat("##{0}{1}", category, Environment.NewLine); docBuilder.AppendFormat("Cmdlet|Description{0}", Environment.NewLine); docBuilder.AppendFormat(":-----|:----------{0}", Environment.NewLine); foreach (var cmdletInfo in toc.Where(c => c.Category == category).OrderBy(c => c.Noun)) { var description = cmdletInfo.Description.Replace("\r\n", " "); docBuilder.AppendFormat("**[{0}]({1}{2}.md)** |{3}{4}", cmdletInfo.FullCommand.Replace("-", "‑"), cmdletInfo.Verb, cmdletInfo.Noun, description, Environment.NewLine); } } var newHashCode = CalculateMD5Hash(docBuilder.ToString()); if (newHashCode != existingHashCode) { System.IO.File.WriteAllText(readmePath, docBuilder.ToString()); } } }
private SetOfSyntaxItems GetMemberItems(List<string> clustersBeingResolved) { if (this.m_cachedMemberItems != null) { // sometimes we get called after final membership was calculated and cached. This happens when CacheFinalSetMembership() is // being called for each cluster, and some have already have had their final memberships calculated, but others haven't. // Sometimes a cluster gets called later on, and then calls GetMemberItems() for a cluster that already has final cached // information available. If that's the case, we just return it. return this.m_cachedMemberItems; } if (this.IsNamedCluster) { clustersBeingResolved.Add(this.m_clusterName); } IEnumerable<SyntaxItem> items; switch (this.ClusterType) { case ClusterType.ALL: items = this.SyntaxContext.AllItems; break; case ClusterType.ALLBUT: items = Extensions.Except(this.SyntaxContext.AllItems, this.GetItemsInMySets(clustersBeingResolved)); break; case ClusterType.CONTAINED: items = Extensions.Except(this.SyntaxContext.NonTopItems.Items, this.GetItemsInMySets(clustersBeingResolved)); break; case ClusterType.NONE: items = new SyntaxItem[0]; break; case ClusterType.NONMAGIC: items = this.GetItemsInMySets(clustersBeingResolved); break; case ClusterType.TOP: items = Extensions.Except(this.SyntaxContext.TopItems.Items, this.GetItemsInMySets(clustersBeingResolved)); break; default: throw new AssertionViolationException(StringExtensions.Fi("Unknown ClusterType {0}", this.ClusterType)); } clustersBeingResolved.Remove(this.m_clusterName); SetOfSyntaxItems myItems = new SetOfSyntaxItems(items); myItems.AddRange(this.m_directItems.Items); return myItems; }