/NancyAppGenerator/ProjectParser/Parsercsproj.cs

https://github.com/majimenezp/AppGen
C# | 108 lines | 104 code | 4 blank | 0 comment | 9 complexity | cd426d36439e4ecaf241c8fe25cd88e2 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.IO;
  7. namespace NancyAppGenerator.ProjectParser
  8. {
  9. class Parsercsproj
  10. {
  11. string FilePath;
  12. static XDocument projDefinition;
  13. XNamespace msbuild;
  14. int changes = 0;
  15. public Parsercsproj(string currentPath)
  16. {
  17. string[] files = Directory.GetFiles(currentPath, "*.csproj");
  18. this.FilePath = files[0];
  19. msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
  20. projDefinition = XDocument.Load(FilePath);
  21. IEnumerable<string> references = projDefinition
  22. .Element(msbuild + "Project")
  23. .Elements(msbuild + "ItemGroup")
  24. .Elements(msbuild + "Reference")
  25. .Select(refElem => refElem.Value);
  26. RootNameSpace = projDefinition
  27. .Element(msbuild + "Project")
  28. .Elements(msbuild + "PropertyGroup")
  29. .Elements(msbuild + "RootNamespace")
  30. .Select(refElem => refElem.Value).FirstOrDefault();
  31. }
  32. public string RootNameSpace { get; set; }
  33. public void AddCompileFile(string fileName)
  34. {
  35. var Exist = projDefinition
  36. .Element(msbuild + "Project")
  37. .Elements(msbuild + "ItemGroup")
  38. .Elements(msbuild + "Compile")
  39. .Attributes("Include")
  40. .Where(x => x.Value == fileName).Count() > 0;
  41. var test1 = projDefinition
  42. .Element(msbuild + "Project")
  43. .Elements(msbuild + "ItemGroup")
  44. .Elements(msbuild + "Compile")
  45. .Last();
  46. var test2 = projDefinition
  47. .Element(msbuild + "Project")
  48. .Elements(msbuild + "ItemGroup")
  49. .Elements(msbuild + "Compile");
  50. if (!Exist)
  51. {
  52. XElement elem = new XElement(msbuild + "Compile");
  53. elem.SetAttributeValue("Include", fileName);
  54. projDefinition
  55. .Element(msbuild + "Project")
  56. .Elements(msbuild + "ItemGroup")
  57. .Elements(msbuild + "Compile")
  58. .Last().AddAfterSelf(elem);
  59. ++changes;
  60. }
  61. }
  62. public void AddContentFile(string fileName, CopyOutPutOptions option)
  63. {
  64. var Exist = projDefinition
  65. .Element(msbuild + "Project")
  66. .Elements(msbuild + "ItemGroup")
  67. .Elements(msbuild + "Content")
  68. .Attributes("Include")
  69. .Where(x => x.Value == fileName).Count() > 0;
  70. if (!Exist)
  71. {
  72. XElement elem = new XElement(msbuild + "Content");
  73. elem.SetAttributeValue("Include", fileName);
  74. if (option == CopyOutPutOptions.PreserveNewest || option == CopyOutPutOptions.Always)
  75. {
  76. XElement copyoutput=new XElement(msbuild +"CopyToOutputDirectory");
  77. copyoutput.Value = option.ToString();
  78. elem.Add(copyoutput);
  79. }
  80. var itemGroup=projDefinition
  81. .Element(msbuild + "Project")
  82. .Elements(msbuild + "ItemGroup")
  83. .Where(x =>( x.Elements(msbuild + "None").Count() + x.Elements(msbuild + "Content").Count() )>0).FirstOrDefault();
  84. var itemgroups = projDefinition
  85. .Element(msbuild + "Project")
  86. .Elements(msbuild + "ItemGroup");
  87. itemGroup.Elements().Last().AddAfterSelf(elem);
  88. ++changes;
  89. }
  90. }
  91. public void Save()
  92. {
  93. if (changes > 0)
  94. {
  95. projDefinition.Save(FilePath);
  96. projDefinition = null;
  97. FileStream arch = new FileStream(FilePath, FileMode.Open );
  98. projDefinition = XDocument.Load(arch);
  99. arch.Close();
  100. }
  101. changes = 0;
  102. }
  103. }
  104. }