PackageJsonHelper.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System.IO;
  2. using UnityEngine;
  3. namespace UnityEditor.PackageManager.UI
  4. {
  5. internal class PackageJsonHelper
  6. {
  7. [SerializeField]
  8. private string name = string.Empty;
  9. private string path = string.Empty;
  10. public static string GetPackagePath(string jsonPath)
  11. {
  12. return Path.GetDirectoryName(jsonPath).Replace("\\", "/");
  13. }
  14. public static PackageJsonHelper Load(string path)
  15. {
  16. // If the path is a directory, find the `package.json` file path
  17. var jsonPath = Directory.Exists(path) ? Path.Combine(path, "package.json") : path;
  18. if (!File.Exists(jsonPath))
  19. return null;
  20. var packageJson = JsonUtility.FromJson<PackageJsonHelper>(File.ReadAllText(jsonPath));
  21. packageJson.path = GetPackagePath(jsonPath);
  22. return string.IsNullOrEmpty(packageJson.name) ? null : packageJson;
  23. }
  24. public PackageInfo PackageInfo
  25. {
  26. get { return new PackageInfo {PackageId = string.Format("{0}@file:{1}", name, path)}; }
  27. }
  28. }
  29. }