ProjectExtensions.cs 1.6 KB

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