DependencyResolver.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using ET;
  7. using MongoDB.Bson.Serialization;
  8. using MongoDB.Bson.Serialization.Attributes;
  9. using UnityEditor;
  10. using UnityEditor.PackageManager;
  11. using PackageInfo = UnityEditor.PackageManager.PackageInfo;
  12. using UnityEditor.PackageManager.Requests;
  13. using Debug = UnityEngine.Debug;
  14. namespace Hibzz.DependencyResolver
  15. {
  16. [InitializeOnLoad]
  17. public class DependencyResolver
  18. {
  19. //[MenuItem("ET/MoveToPackage")]
  20. static void MoveToPackage(string package, string version)
  21. {
  22. string dir = Path.Combine("Library/PackageCache", $"{package}@{version}");
  23. if (!Directory.Exists(dir))
  24. {
  25. return;
  26. }
  27. Debug.Log($"move package: {package}@{version}");
  28. Process process = ProcessHelper.PowerShell($"-NoExit -ExecutionPolicy Bypass -File ./Packages/com.etetet.init/MoveToPackages.ps1 {package} {version}", waitExit: true);
  29. Debug.Log(process.StandardOutput.ReadToEnd());
  30. }
  31. static DependencyResolver()
  32. {
  33. Events.registeredPackages += OnPackagesRegistered;
  34. }
  35. // Invoked when the package manager completes registering new packages
  36. static void OnPackagesRegistered(PackageRegistrationEventArgs packageRegistrationInfo)
  37. {
  38. if (packageRegistrationInfo.added.Count == 0 && packageRegistrationInfo.changedFrom.Count == 0)
  39. {
  40. return;
  41. }
  42. Debug.Log($"Packages Registered: {string.Join(" ", packageRegistrationInfo.added.Select(x=>x.name))}");
  43. // loop through all of the added packages and get their git
  44. // dependencies and add it to the list that contains all the
  45. // dependencies that need to be installed
  46. foreach (var package in packageRegistrationInfo.added)
  47. {
  48. if (!package.name.StartsWith("cn.etetet."))
  49. {
  50. continue;
  51. }
  52. MoveToPackage(package.name, package.version);
  53. }
  54. foreach (var package in packageRegistrationInfo.changedFrom)
  55. {
  56. if (!package.name.StartsWith("cn.etetet."))
  57. {
  58. continue;
  59. }
  60. MoveToPackage(package.name, package.version);
  61. }
  62. AssetDatabase.Refresh();
  63. }
  64. [MenuItem("ET/Init/RepairDependencies")]
  65. static void RepairDependencies()
  66. {
  67. foreach (var directory in Directory.GetDirectories("Library/PackageCache", "cn.etetet.*"))
  68. {
  69. string baseName = Path.GetFileName(directory);
  70. if (!baseName.StartsWith("cn.etetet."))
  71. {
  72. continue;
  73. }
  74. string[] ss = baseName.Split("@");
  75. string packageName = ss[0];
  76. string version = ss[1];
  77. MoveToPackage(packageName, version);
  78. }
  79. AssetDatabase.Refresh();
  80. Debug.Log($"repaire package finish");
  81. }
  82. }
  83. }