PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/SPConstantGen/Program.cs

#
C# | 193 lines | 154 code | 39 blank | 0 comment | 11 complexity | 756cc5314f8c92a355c0be9e629d5482 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Microsoft.SharePoint;
  5. using System.CodeDom;
  6. using Plossum.CommandLine;
  7. using Refly.CodeDom;
  8. using System.IO;
  9. using System.Collections.Specialized;
  10. using System.Threading;
  11. using System.Collections;
  12. using Refly.CodeDom.Expressions;
  13. using System.Reflection;
  14. namespace AKJ.Sharepoint.SPConstantGen
  15. {
  16. class Program
  17. {
  18. static void Main(string[] args)
  19. {
  20. CmdOptions options = new CmdOptions();
  21. CommandLineParser parser = new CommandLineParser(options);
  22. parser.Parse();
  23. Console.WriteLine(parser.UsageInfo.GetHeaderAsString(78));
  24. if (parser.HasErrors)
  25. {
  26. Console.WriteLine(parser.UsageInfo.GetErrorsAsString(78));
  27. Console.WriteLine(parser.UsageInfo.GetOptionsAsString(78));
  28. return;
  29. }
  30. SPSecurity.RunWithElevatedPrivileges(delegate()
  31. {
  32. using (SPSite site = new SPSite(options.Url))
  33. {
  34. using (SPWeb web = site.RootWeb)
  35. {
  36. GenereateClass(web, options.Path, options.Namespace);
  37. }
  38. }
  39. }
  40. );
  41. }
  42. static void GenereateClass(SPWeb web, string filepath, string namesp)
  43. {
  44. if (filepath == "")
  45. filepath = Environment.CurrentDirectory;
  46. NamespaceDeclaration ns = new NamespaceDeclaration(namesp);
  47. ClassDeclaration aclass = ns.AddClass("SPConstant");
  48. aclass.OutputType = ClassOutputType.Struct;
  49. AddFieldsRoot(web, aclass);
  50. AddLists(web, aclass);
  51. AddWebs(web, aclass);
  52. CodeGenerator gen = new CodeGenerator();
  53. gen.Options.BlankLinesBetweenMembers = false;
  54. gen.CreateFolders = false;
  55. gen.Copyright = "Anders K. Jacobsen";
  56. gen.Options.VerbatimOrder = true;
  57. gen.GenerateCode(filepath, ns);
  58. FileInfo file = new FileInfo(filepath + "/" + "SPConstant.cs");
  59. StreamReader reader = file.OpenText();
  60. string str = reader.ReadToEnd();
  61. Console.WriteLine(str);
  62. }
  63. static void AddFieldsRoot(SPWeb web, ClassDeclaration aclass)
  64. {
  65. ClassDeclaration fieldsStruct = aclass.AddClass("Fields");
  66. fieldsStruct.OutputType = ClassOutputType.Struct;
  67. fieldsStruct.Conformer.Camelize = false;
  68. fieldsStruct.Doc.Summary.AddText("Contains all SPField's in the Sharepoint Applicaiton");
  69. AddFieldCollection(web.Fields, fieldsStruct);
  70. }
  71. static void AddFieldCollection(SPFieldCollection fields, ClassDeclaration fieldsStruct)
  72. {
  73. foreach (SPField field in fields)
  74. {
  75. if (fieldsStruct.NestedClasses.Contains(field.StaticName))
  76. continue;
  77. ClassDeclaration fieldStruct = fieldsStruct.AddClass(field.StaticName);
  78. fieldStruct.OutputType = ClassOutputType.Struct;
  79. fieldStruct.Conformer.Camelize = false;
  80. fieldStruct.Doc.Summary.AddText(field.Description == "" ? "No description has been specified" : field.Description);
  81. FieldDeclaration nameField = fieldStruct.AddField(typeof(string), "StaticName");
  82. nameField.Attributes = MemberAttributes.Public | MemberAttributes.Const;
  83. nameField.InitExpression = new PrimitiveExpression(field.StaticName);
  84. nameField.Doc.Summary.AddText(field.StaticName);
  85. FieldDeclaration descriptionField = fieldStruct.AddField(typeof(string), "DescriptionText");
  86. descriptionField.Attributes = MemberAttributes.Public | MemberAttributes.Const;
  87. descriptionField.InitExpression = new PrimitiveExpression(field.Description);
  88. descriptionField.Doc.Summary.AddText(field.Description == "" ? "No description has been specified" : field.Description);
  89. }
  90. }
  91. static void AddLists(SPWeb web, ClassDeclaration aclass)
  92. {
  93. ClassDeclaration lists = aclass.AddClass("Lists");
  94. lists.OutputType = ClassOutputType.Struct;
  95. lists.Conformer.Camelize = false;
  96. lists.Doc.Summary.AddText("Contains all SPList's in the Sharepoint Applicaiton");
  97. StringCollection existing = new StringCollection();
  98. foreach (SPList list in web.Lists)
  99. {
  100. string listTitleNoWhitespace = list.Title.Replace(" ", "");
  101. if (lists.NestedClasses.Contains(listTitleNoWhitespace))
  102. continue;
  103. ClassDeclaration listStruct = lists.AddClass(listTitleNoWhitespace);
  104. listStruct.OutputType = ClassOutputType.Struct;
  105. listStruct.Conformer.Camelize = false;
  106. listStruct.Doc.Summary.AddText(list.Description == "" ? "No description has been specified" : list.Description);
  107. FieldDeclaration nameField = listStruct.AddField(typeof(string), "Title");
  108. nameField.Attributes = MemberAttributes.Public | MemberAttributes.Const;
  109. nameField.InitExpression = new PrimitiveExpression(list.Title);
  110. nameField.Doc.Summary.AddText(list.Title);
  111. FieldDeclaration descriptionField = listStruct.AddField(typeof(string), "Description");
  112. descriptionField.Attributes = MemberAttributes.Public | MemberAttributes.Const;
  113. descriptionField.InitExpression = new PrimitiveExpression(list.Description);
  114. descriptionField.Doc.Summary.AddText(list.Description == "" ? "No description has been specified" : list.Description);
  115. ClassDeclaration fieldsStruct = listStruct.AddClass("Fields");
  116. fieldsStruct.OutputType = ClassOutputType.Struct;
  117. fieldsStruct.Conformer.Camelize = false;
  118. fieldsStruct.Doc.Summary.AddText("All fields for List \"" + list.Title + "\"");
  119. AddFieldCollection(list.Fields, fieldsStruct);
  120. }
  121. }
  122. static void AddWeb(SPWeb web, ClassDeclaration aclass, string name)
  123. {
  124. ClassDeclaration currentWeb = aclass.AddClass(name);
  125. currentWeb.OutputType = ClassOutputType.Struct;
  126. currentWeb.Conformer.Camelize = false;
  127. currentWeb.Doc.Summary.AddText(web.Description);
  128. FieldDeclaration nameField = currentWeb.AddField(typeof(string), "Name");
  129. nameField.Attributes = MemberAttributes.Public | MemberAttributes.Const;
  130. nameField.InitExpression = new PrimitiveExpression(web.Name);
  131. nameField.Doc.Summary.AddText(web.Name);
  132. FieldDeclaration descriptionField = currentWeb.AddField(typeof(string), "Description");
  133. descriptionField.Attributes = MemberAttributes.Public | MemberAttributes.Const;
  134. descriptionField.InitExpression = new PrimitiveExpression(web.Description);
  135. descriptionField.Doc.Summary.AddText(web.Description == "" ? "No description has been specified" : web.Description);
  136. }
  137. static void AddWebs(SPWeb web, ClassDeclaration aclass)
  138. {
  139. ClassDeclaration webs = aclass.AddClass("Webs");
  140. webs.OutputType = ClassOutputType.Struct;
  141. webs.Conformer.Camelize = false;
  142. webs.Doc.Summary.AddText("Contains all SPWeb's in the Sharepoint Applicaiton");
  143. AddWeb(web, webs, "RootWeb");
  144. StringCollection existing = new StringCollection();
  145. foreach (SPWeb currentweb in web.Webs)
  146. {
  147. string webNameNoWhitespace = web.Name.Replace(" ", "");
  148. if (webs.NestedClasses.Contains(webNameNoWhitespace))
  149. continue;
  150. AddWeb(currentweb, webs, currentweb.Name);
  151. }
  152. }
  153. }
  154. }