ProjectExtensions.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using GodotTools.Core;
  2. using System;
  3. using DotNet.Globbing;
  4. using Microsoft.Build.Construction;
  5. namespace GodotTools.ProjectEditor
  6. {
  7. public static class ProjectExtensions
  8. {
  9. public static bool HasItem(this ProjectRootElement root, string itemType, string include)
  10. {
  11. GlobOptions globOptions = new GlobOptions();
  12. globOptions.Evaluation.CaseInsensitive = false;
  13. string normalizedInclude = include.NormalizePath();
  14. foreach (var itemGroup in root.ItemGroups)
  15. {
  16. if (itemGroup.Condition.Length != 0)
  17. continue;
  18. foreach (var item in itemGroup.Items)
  19. {
  20. if (item.ItemType != itemType)
  21. continue;
  22. var glob = Glob.Parse(item.Include.NormalizePath(), globOptions);
  23. if (glob.IsMatch(normalizedInclude))
  24. {
  25. return true;
  26. }
  27. }
  28. }
  29. return false;
  30. }
  31. public static bool AddItemChecked(this ProjectRootElement root, string itemType, string include)
  32. {
  33. if (!root.HasItem(itemType, include))
  34. {
  35. root.AddItem(itemType, include);
  36. return true;
  37. }
  38. return false;
  39. }
  40. public static Guid GetGuid(this ProjectRootElement root)
  41. {
  42. foreach (var property in root.Properties)
  43. {
  44. if (property.Name == "ProjectGuid")
  45. return Guid.Parse(property.Value);
  46. }
  47. return Guid.Empty;
  48. }
  49. }
  50. }