EdmInstallPolicy.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Text.RegularExpressions;
  2. namespace GoogleMobileAds.Editor
  3. {
  4. /// <summary>
  5. /// Decides what <see cref="EdmDependencyInstaller"/> should do with Unity's External Dependency
  6. /// Manager package, based on the project's `Packages/manifest.json` and the publisher's
  7. /// opt-out. The Unity Editor APIs that the installer calls are static and can't be mocked
  8. /// without wrappers, so the rules are kept free of them and unit tested directly.
  9. /// </summary>
  10. internal static class EdmInstallPolicy
  11. {
  12. /// <summary>
  13. /// The action to take for the EDM package. Defaults to <see cref="Install"/>: EDM is
  14. /// installed unless the publisher opts out by removing it.
  15. /// </summary>
  16. internal enum Action
  17. {
  18. // EDM has not been installed yet or needs to be updated to the required version. This
  19. // is the default value, so keep it first.
  20. Install,
  21. // EDM was seen before and is now missing: the publisher removed it, so opt out.
  22. OptOut,
  23. // Leave the project as is.
  24. None
  25. }
  26. /// <summary>
  27. /// The state of the project that <see cref="Decide"/> bases its decision on.
  28. /// </summary>
  29. internal struct ProjectState
  30. {
  31. // Whether EDM is declared in `Packages/manifest.json`.
  32. internal bool IsDeclared;
  33. // Whether Unity's External Dependency Manager is enabled.
  34. internal bool EnableEdm;
  35. // Whether EDM has been seen in this project before.
  36. internal bool PackageDetected;
  37. // Whether the plugin already added the EDM version this release requires.
  38. internal bool EdmVersionAdded;
  39. }
  40. /// <summary>
  41. /// Returns the action to take for the EDM package.
  42. /// </summary>
  43. internal static Action Decide(ProjectState state)
  44. {
  45. // An opted-out project is never modified, including on re-import of the plugin.
  46. if (!state.EnableEdm)
  47. {
  48. return Action.None;
  49. }
  50. if (!state.IsDeclared)
  51. {
  52. return state.PackageDetected ? Action.OptOut : Action.Install;
  53. }
  54. return state.EdmVersionAdded ? Action.None : Action.Install;
  55. }
  56. /// <summary>
  57. /// Returns whether <paramref name="packageName"/> is declared in the contents of a
  58. /// `Packages/manifest.json` file.
  59. /// </summary>
  60. internal static bool IsDeclared(string manifestJson, string packageName)
  61. {
  62. if (string.IsNullOrEmpty(manifestJson) || string.IsNullOrEmpty(packageName))
  63. {
  64. return false;
  65. }
  66. // Matches an entry such as `"com.unity.external-dependency-manager": "2.1.0"`.
  67. // JSON allows whitespace, or none, on either side of the colon.
  68. return Regex.IsMatch(
  69. manifestJson, "\"" + Regex.Escape(packageName) + "\"\\s*:\\s*\"[^\"]+\"");
  70. }
  71. }
  72. }