ProjectExtensions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using GodotTools.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using DotNet.Globbing;
  6. using Microsoft.Build.Construction;
  7. namespace GodotTools.ProjectEditor
  8. {
  9. public static class ProjectExtensions
  10. {
  11. public static ProjectItemElement FindItemOrNull(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
  12. {
  13. GlobOptions globOptions = new GlobOptions {Evaluation = {CaseInsensitive = false}};
  14. string normalizedInclude = include.NormalizePath();
  15. foreach (var itemGroup in root.ItemGroups)
  16. {
  17. if (noCondition && itemGroup.Condition.Length != 0)
  18. continue;
  19. foreach (var item in itemGroup.Items)
  20. {
  21. if (item.ItemType != itemType)
  22. continue;
  23. var glob = Glob.Parse(item.Include.NormalizePath(), globOptions);
  24. if (glob.IsMatch(normalizedInclude))
  25. return item;
  26. }
  27. }
  28. return null;
  29. }
  30. public static ProjectItemElement FindItemOrNullAbs(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
  31. {
  32. GlobOptions globOptions = new GlobOptions {Evaluation = {CaseInsensitive = false}};
  33. string normalizedInclude = Path.GetFullPath(include).NormalizePath();
  34. foreach (var itemGroup in root.ItemGroups)
  35. {
  36. if (noCondition && itemGroup.Condition.Length != 0)
  37. continue;
  38. foreach (var item in itemGroup.Items)
  39. {
  40. if (item.ItemType != itemType)
  41. continue;
  42. var glob = Glob.Parse(Path.GetFullPath(item.Include).NormalizePath(), globOptions);
  43. if (glob.IsMatch(normalizedInclude))
  44. return item;
  45. }
  46. }
  47. return null;
  48. }
  49. public static IEnumerable<ProjectItemElement> FindAllItemsInFolder(this ProjectRootElement root, string itemType, string folder)
  50. {
  51. string absFolderNormalizedWithSep = Path.GetFullPath(folder).NormalizePath() + Path.DirectorySeparatorChar;
  52. foreach (var itemGroup in root.ItemGroups)
  53. {
  54. foreach (var item in itemGroup.Items)
  55. {
  56. if (item.ItemType != itemType)
  57. continue;
  58. string absPathNormalized = Path.GetFullPath(item.Include).NormalizePath();
  59. if (absPathNormalized.StartsWith(absFolderNormalizedWithSep))
  60. yield return item;
  61. }
  62. }
  63. }
  64. public static bool HasItem(this ProjectRootElement root, string itemType, string include, bool noCondition = false)
  65. {
  66. return root.FindItemOrNull(itemType, include, noCondition) != null;
  67. }
  68. public static bool AddItemChecked(this ProjectRootElement root, string itemType, string include)
  69. {
  70. if (!root.HasItem(itemType, include, noCondition: true))
  71. {
  72. root.AddItem(itemType, include);
  73. return true;
  74. }
  75. return false;
  76. }
  77. public static bool RemoveItemChecked(this ProjectRootElement root, string itemType, string include)
  78. {
  79. var item = root.FindItemOrNullAbs(itemType, include);
  80. if (item != null)
  81. {
  82. item.Parent.RemoveChild(item);
  83. return true;
  84. }
  85. return false;
  86. }
  87. public static Guid GetGuid(this ProjectRootElement root)
  88. {
  89. foreach (var property in root.Properties)
  90. {
  91. if (property.Name == "ProjectGuid")
  92. return Guid.Parse(property.Value);
  93. }
  94. return Guid.Empty;
  95. }
  96. }
  97. }