ProjectUtils.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using GodotTools.Core;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using DotNet.Globbing;
  5. using Microsoft.Build.Construction;
  6. namespace GodotTools.ProjectEditor
  7. {
  8. public static class ProjectUtils
  9. {
  10. public static void AddItemToProjectChecked(string projectPath, string itemType, string include)
  11. {
  12. var dir = Directory.GetParent(projectPath).FullName;
  13. var root = ProjectRootElement.Open(projectPath);
  14. var normalizedInclude = include.RelativeToPath(dir).Replace("/", "\\");
  15. if (root.AddItemChecked(itemType, normalizedInclude))
  16. root.Save();
  17. }
  18. private static string[] GetAllFilesRecursive(string rootDirectory, string mask)
  19. {
  20. string[] files = Directory.GetFiles(rootDirectory, mask, SearchOption.AllDirectories);
  21. // We want relative paths
  22. for (int i = 0; i < files.Length; i++) {
  23. files[i] = files[i].RelativeToPath(rootDirectory);
  24. }
  25. return files;
  26. }
  27. public static string[] GetIncludeFiles(string projectPath, string itemType)
  28. {
  29. var result = new List<string>();
  30. var existingFiles = GetAllFilesRecursive(Path.GetDirectoryName(projectPath), "*.cs");
  31. GlobOptions globOptions = new GlobOptions();
  32. globOptions.Evaluation.CaseInsensitive = false;
  33. var root = ProjectRootElement.Open(projectPath);
  34. foreach (var itemGroup in root.ItemGroups)
  35. {
  36. if (itemGroup.Condition.Length != 0)
  37. continue;
  38. foreach (var item in itemGroup.Items)
  39. {
  40. if (item.ItemType != itemType)
  41. continue;
  42. string normalizedInclude = item.Include.NormalizePath();
  43. var glob = Glob.Parse(normalizedInclude, globOptions);
  44. // TODO Check somehow if path has no blob to avoid the following loop...
  45. foreach (var existingFile in existingFiles)
  46. {
  47. if (glob.IsMatch(existingFile))
  48. {
  49. result.Add(existingFile);
  50. }
  51. }
  52. }
  53. }
  54. return result.ToArray();
  55. }
  56. }
  57. }