CoveragePreferences.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using UnityEditor.SettingsManagement;
  2. using UnityEditor.TestTools.CodeCoverage.Utils;
  3. namespace UnityEditor.TestTools.CodeCoverage
  4. {
  5. internal class CoveragePreferences : CoveragePreferencesImplementation
  6. {
  7. private static CoveragePreferences s_Instance = null;
  8. private const string k_PackageName = "com.unity.testtools.codecoverage";
  9. public static CoveragePreferences instance
  10. {
  11. get
  12. {
  13. if (s_Instance == null)
  14. s_Instance = new CoveragePreferences();
  15. return s_Instance;
  16. }
  17. }
  18. protected CoveragePreferences() : base(k_PackageName)
  19. {
  20. }
  21. }
  22. internal class CoveragePreferencesImplementation
  23. {
  24. private const string k_ProjectPathAlias = "{ProjectPath}";
  25. protected Settings m_Settings;
  26. public CoveragePreferencesImplementation(string packageName)
  27. {
  28. m_Settings = new Settings(packageName);
  29. }
  30. public bool GetBool(string key, bool defaultValue, SettingsScope scope = SettingsScope.Project)
  31. {
  32. if (m_Settings.ContainsKey<bool>(key, scope))
  33. {
  34. return m_Settings.Get<bool>(key, scope, defaultValue);
  35. }
  36. return defaultValue;
  37. }
  38. public int GetInt(string key, int defaultValue, SettingsScope scope = SettingsScope.Project)
  39. {
  40. if (m_Settings.ContainsKey<int>(key, scope))
  41. {
  42. return m_Settings.Get<int>(key, scope, defaultValue);
  43. }
  44. return defaultValue;
  45. }
  46. public string GetStringForPaths(string key, string defaultValue, SettingsScope scope = SettingsScope.Project)
  47. {
  48. string value = GetString(key, defaultValue, scope);
  49. value = value.Replace(k_ProjectPathAlias, CoverageUtils.GetProjectPath());
  50. return value;
  51. }
  52. public string GetString(string key, string defaultValue, SettingsScope scope = SettingsScope.Project)
  53. {
  54. if (m_Settings.ContainsKey<string>(key, scope))
  55. {
  56. return m_Settings.Get<string>(key, scope, defaultValue);
  57. }
  58. return defaultValue;
  59. }
  60. public void SetBool(string key, bool value, SettingsScope scope = SettingsScope.Project)
  61. {
  62. m_Settings.Set<bool>(key, value, scope);
  63. m_Settings.Save();
  64. }
  65. public void SetInt(string key, int value, SettingsScope scope = SettingsScope.Project)
  66. {
  67. m_Settings.Set<int>(key, value, scope);
  68. m_Settings.Save();
  69. }
  70. public void SetStringForPaths(string key, string value, SettingsScope scope = SettingsScope.Project)
  71. {
  72. value = CoverageUtils.NormaliseFolderSeparators(value, false);
  73. value = value.Replace(CoverageUtils.GetProjectPath(), k_ProjectPathAlias);
  74. SetString(key, value, scope);
  75. }
  76. public void SetString(string key, string value, SettingsScope scope = SettingsScope.Project)
  77. {
  78. m_Settings.Set<string>(key, value, scope);
  79. m_Settings.Save();
  80. }
  81. public void DeleteBool(string key, SettingsScope scope = SettingsScope.Project)
  82. {
  83. m_Settings.DeleteKey<bool>(key, scope);
  84. m_Settings.Save();
  85. }
  86. public void DeleteInt(string key, SettingsScope scope = SettingsScope.Project)
  87. {
  88. m_Settings.DeleteKey<int>(key, scope);
  89. m_Settings.Save();
  90. }
  91. public void DeleteString(string key, SettingsScope scope = SettingsScope.Project)
  92. {
  93. m_Settings.DeleteKey<string>(key, scope);
  94. m_Settings.Save();
  95. }
  96. }
  97. }