private SystemInformationTelemetryProvider CreateUserDataService( string culture = "", ModelsMode modelsMode = ModelsMode.InMemoryAuto, bool isDebug = true) { var localizationService = CreateILocalizationService(culture); var databaseMock = new Mock <IUmbracoDatabase>(); databaseMock.Setup(x => x.DatabaseType.GetProviderName()).Returns("SQL"); return(new SystemInformationTelemetryProvider( _umbracoVersion, localizationService, Mock.Of <IOptionsMonitor <ModelsBuilderSettings> >(x => x.CurrentValue == new ModelsBuilderSettings { ModelsMode = modelsMode }), Mock.Of <IOptionsMonitor <HostingSettings> >(x => x.CurrentValue == new HostingSettings { Debug = isDebug }), Mock.Of <IOptionsMonitor <GlobalSettings> >(x => x.CurrentValue == new GlobalSettings()), Mock.Of <IHostEnvironment>(), Mock.Of <IUmbracoDatabaseFactory>(x => x.CreateDatabase() == Mock.Of <IUmbracoDatabase>(y => y.DatabaseType == DatabaseType.SQLite)), Mock.Of <IServerRoleAccessor>())); }
private SystemInformationTelemetryProvider CreateProvider( ModelsMode modelsMode = ModelsMode.InMemoryAuto, bool isDebug = true, string umbracoPath = "", string environment = "") { var hostEnvironment = new Mock <IHostEnvironment>(); hostEnvironment.Setup(x => x.EnvironmentName).Returns(environment); var databaseMock = new Mock <IUmbracoDatabase>(); databaseMock.Setup(x => x.DatabaseType.GetProviderName()).Returns("SQL"); return(new SystemInformationTelemetryProvider( Mock.Of <IUmbracoVersion>(), Mock.Of <ILocalizationService>(), Mock.Of <IOptionsMonitor <ModelsBuilderSettings> >(x => x.CurrentValue == new ModelsBuilderSettings { ModelsMode = modelsMode }), Mock.Of <IOptionsMonitor <HostingSettings> >(x => x.CurrentValue == new HostingSettings { Debug = isDebug }), Mock.Of <IOptionsMonitor <GlobalSettings> >(x => x.CurrentValue == new GlobalSettings { UmbracoPath = umbracoPath }), hostEnvironment.Object, Mock.Of <IUmbracoDatabaseFactory>(x => x.CreateDatabase() == Mock.Of <IUmbracoDatabase>(y => y.DatabaseType == DatabaseType.SQLite)))); }
/// <summary> /// Gets a value indicating whether the mode is LiveAnything or PureLive. /// </summary> public static bool IsLive(this ModelsMode modelsMode) { return (modelsMode == ModelsMode.PureLive || modelsMode == ModelsMode.LiveDll || modelsMode == ModelsMode.LiveAppData); }
/// <summary> /// Initializes a new instance of the <see cref="Config"/> class. /// </summary> public Config( bool enable = false, ModelsMode modelsMode = ModelsMode.Nothing, bool enableApi = true, string modelsNamespace = null, bool enableFactory = true, LanguageVersion languageVersion = DefaultLanguageVersion, bool staticMixinGetters = true, string staticMixinGetterPattern = null, bool flagOutOfDateModels = true, ClrNameSource clrNameSource = DefaultClrNameSource, string modelsDirectory = null, bool acceptUnsafeModelsDirectory = false) { Enable = enable; ModelsMode = modelsMode; EnableApi = enableApi; ModelsNamespace = string.IsNullOrWhiteSpace(modelsNamespace) ? DefaultModelsNamespace : modelsNamespace; EnableFactory = enableFactory; LanguageVersion = languageVersion; StaticMixinGetters = staticMixinGetters; StaticMixinGetterPattern = string.IsNullOrWhiteSpace(staticMixinGetterPattern) ? DefaultStaticMixinGetterPattern : staticMixinGetterPattern; FlagOutOfDateModels = flagOutOfDateModels; ClrNameSource = clrNameSource; ModelsDirectory = string.IsNullOrWhiteSpace(modelsDirectory) ? DefaultModelsDirectory : modelsDirectory; AcceptUnsafeModelsDirectory = acceptUnsafeModelsDirectory; }
/// <summary> /// Gets a value indicating whether the mode supports explicit generation (as opposed to pure live). /// </summary> public static bool SupportsExplicitGeneration(this ModelsMode modelsMode) { return (modelsMode == ModelsMode.Dll || modelsMode == ModelsMode.LiveDll || modelsMode == ModelsMode.AppData || modelsMode == ModelsMode.LiveAppData); }
public void ReportsModelsModeCorrectly(ModelsMode modelsMode) { var userDataService = CreateUserDataService(modelsMode: modelsMode); var userData = userDataService.GetUserData().ToArray(); var actual = userData.FirstOrDefault(x => x.Name == "Models Builder Mode"); Assert.IsNotNull(actual?.Data); Assert.AreEqual(modelsMode.ToString(), actual.Data); }
public void ReportsModelsModeCorrectly(ModelsMode modelsMode) { var telemetryProvider = CreateProvider(modelsMode); var usageInformation = telemetryProvider.GetInformation().ToArray(); var actual = usageInformation.FirstOrDefault(x => x.Name == Constants.Telemetry.ModelsBuilderMode); Assert.IsNotNull(actual?.Data); Assert.AreEqual(modelsMode.ToString(), actual.Data); }
/// <summary> /// Initializes a new instance of the <see cref="ModelsBuilderConfig"/> class. /// </summary> public ModelsBuilderConfig( bool enable = false, ModelsMode modelsMode = ModelsMode.Nothing, string modelsNamespace = null, bool enableFactory = true, bool flagOutOfDateModels = true, string modelsDirectory = null, bool acceptUnsafeModelsDirectory = false, int debugLevel = 0) { Enable = enable; _modelsMode = modelsMode; ModelsNamespace = string.IsNullOrWhiteSpace(modelsNamespace) ? DefaultModelsNamespace : modelsNamespace; EnableFactory = enableFactory; _flagOutOfDateModels = flagOutOfDateModels; ModelsDirectory = string.IsNullOrWhiteSpace(modelsDirectory) ? DefaultModelsDirectory : modelsDirectory; AcceptUnsafeModelsDirectory = acceptUnsafeModelsDirectory; DebugLevel = debugLevel; }
/// <summary> /// Initializes a new instance of the <see cref="Config"/> class. /// </summary> private Config() { const string prefix = "Umbraco.ModelsBuilder."; // giant kill switch, default: false // must be explicitely set to true for anything else to happen Enable = ConfigurationManager.AppSettings[prefix + "Enable"] == "true"; // ensure defaults are initialized for tests StaticMixinGetterPattern = DefaultStaticMixinGetterPattern; LanguageVersion = DefaultLanguageVersion; ModelsNamespace = DefaultModelsNamespace; ClrNameSource = DefaultClrNameSource; ModelsDirectory = HostingEnvironment.IsHosted ? HostingEnvironment.MapPath(DefaultModelsDirectory) : DefaultModelsDirectory.TrimStart("~/"); BinDirectory = HostingEnvironment.IsHosted ? HostingEnvironment.MapPath(DefaultBinDirectory) : DefaultBinDirectory.TrimStart("~/"); DebugLevel = 0; // stop here, everything is false if (!Enable) { return; } // mode var modelsMode = ConfigurationManager.AppSettings[prefix + "ModelsMode"]; if (!string.IsNullOrWhiteSpace(modelsMode)) { switch (modelsMode) { case nameof(ModelsMode.Nothing): ModelsMode = ModelsMode.Nothing; break; case nameof(ModelsMode.PureLive): ModelsMode = ModelsMode.PureLive; break; case nameof(ModelsMode.Dll): ModelsMode = ModelsMode.Dll; break; case nameof(ModelsMode.LiveDll): ModelsMode = ModelsMode.LiveDll; break; case nameof(ModelsMode.AppData): ModelsMode = ModelsMode.AppData; break; case nameof(ModelsMode.LiveAppData): ModelsMode = ModelsMode.LiveAppData; break; default: throw new ConfigurationErrorsException($"ModelsMode \"{modelsMode}\" is not a valid mode." + " Note that modes are case-sensitive. Possible values are: " + string.Join(", ", Enum.GetNames(typeof(ModelsMode)))); } } // default: false EnableApi = ConfigurationManager.AppSettings[prefix + "EnableApi"].InvariantEquals("true"); AcceptUnsafeModelsDirectory = ConfigurationManager.AppSettings[prefix + "AcceptUnsafeModelsDirectory"].InvariantEquals("true"); // default: true EnableFactory = !ConfigurationManager.AppSettings[prefix + "EnableFactory"].InvariantEquals("false"); StaticMixinGetters = !ConfigurationManager.AppSettings[prefix + "StaticMixinGetters"].InvariantEquals("false"); FlagOutOfDateModels = !ConfigurationManager.AppSettings[prefix + "FlagOutOfDateModels"].InvariantEquals("false"); // default: initialized above with DefaultModelsNamespace const var value = ConfigurationManager.AppSettings[prefix + "ModelsNamespace"]; if (!string.IsNullOrWhiteSpace(value)) { ModelsNamespace = value; } // default: initialized above with DefaultStaticMixinGetterPattern const value = ConfigurationManager.AppSettings[prefix + "StaticMixinGetterPattern"]; if (!string.IsNullOrWhiteSpace(value)) { StaticMixinGetterPattern = value; } // default: initialized above with DefaultLanguageVersion const value = ConfigurationManager.AppSettings[prefix + "LanguageVersion"]; if (!string.IsNullOrWhiteSpace(value)) { LanguageVersion lv; if (!Enum.TryParse(value, true, out lv)) { throw new ConfigurationErrorsException($"Invalid language version \"{value}\"."); } LanguageVersion = lv; } // default: initialized above with DefaultClrNameSource const value = ConfigurationManager.AppSettings[prefix + "ClrNameSource"]; if (!string.IsNullOrWhiteSpace(value)) { switch (value) { case nameof(ClrNameSource.Nothing): ClrNameSource = ClrNameSource.Nothing; break; case nameof(ClrNameSource.Alias): ClrNameSource = ClrNameSource.Alias; break; case nameof(ClrNameSource.RawAlias): ClrNameSource = ClrNameSource.RawAlias; break; case nameof(ClrNameSource.Name): ClrNameSource = ClrNameSource.Name; break; default: throw new ConfigurationErrorsException($"ClrNameSource \"{value}\" is not a valid source." + " Note that sources are case-sensitive. Possible values are: " + string.Join(", ", Enum.GetNames(typeof(ClrNameSource)))); } } // default: initialized above with DefaultModelsDirectory const value = ConfigurationManager.AppSettings[prefix + "ModelsDirectory"]; if (!string.IsNullOrWhiteSpace(value)) { // GetModelsDirectory will ensure that the path is safe var root = GetRootDirectory(); ModelsDirectory = GetModelsDirectory(root, value, AcceptUnsafeModelsDirectory); } // default: initialized above with DefaultBinDirectory const value = ConfigurationManager.AppSettings[prefix + "BinDirectory"]; if (!string.IsNullOrWhiteSpace(value)) { // GetBinDirectory will ensure that the path is safe - no AcceptUnsafe here var root = GetRootDirectory(); BinDirectory = GetBinDirectory(root, value); } // default: 0 value = ConfigurationManager.AppSettings[prefix + "DebugLevel"]; if (!string.IsNullOrWhiteSpace(value)) { int debugLevel; if (!int.TryParse(value, out debugLevel)) { throw new ConfigurationErrorsException($"Invalid debug level \"{value}\"."); } DebugLevel = debugLevel; } // not flagging if not generating, or live (incl. pure) if (ModelsMode == ModelsMode.Nothing || ModelsMode.IsLive()) { FlagOutOfDateModels = false; } }
/// <summary> /// Gets a value indicating whether the mode supports explicit manual generation. /// </summary> public static bool SupportsExplicitGeneration(this ModelsMode modelsMode) => modelsMode == ModelsMode.SourceCodeManual || modelsMode == ModelsMode.SourceCodeAuto;
/// <summary> /// Gets a value indicating whether the mode is *Auto but not InMemory. /// </summary> public static bool IsAutoNotInMemory(this ModelsMode modelsMode) => modelsMode == ModelsMode.SourceCodeAuto;
/// <summary> /// Gets a value indicating whether the mode is *Auto. /// </summary> public static bool IsAuto(this ModelsMode modelsMode) => modelsMode == ModelsMode.InMemoryAuto || modelsMode == ModelsMode.SourceCodeAuto;
private SystemInformationTelemetryProvider CreateUserDataService(string culture = "", ModelsMode modelsMode = ModelsMode.InMemoryAuto, bool isDebug = true) { var localizationService = CreateILocalizationService(culture); var databaseMock = new Mock <IUmbracoDatabase>(); databaseMock.Setup(x => x.DatabaseType.GetProviderName()).Returns("SQL"); return(new SystemInformationTelemetryProvider( _umbracoVersion, localizationService, Mock.Of <IOptions <ModelsBuilderSettings> >(x => x.Value == new ModelsBuilderSettings { ModelsMode = modelsMode }), Mock.Of <IOptions <HostingSettings> >(x => x.Value == new HostingSettings { Debug = isDebug }), Mock.Of <IOptions <GlobalSettings> >(x => x.Value == new GlobalSettings()), Mock.Of <IHostEnvironment>(), new Lazy <IUmbracoDatabase>(databaseMock.Object))); }
/// <summary> /// Gets a value indicating whether the mode is LiveAnything but not PureLive. /// </summary> public static bool IsLiveNotPure(this ModelsMode modelsMode) { return (modelsMode == ModelsMode.LiveAppData); }
/// <summary> /// Gets a value indicating whether the mode is [Live]Dll. /// </summary> public static bool IsAnyDll(this ModelsMode modelsMode) { return (modelsMode == ModelsMode.Dll || modelsMode == ModelsMode.LiveDll); }
internal const ClrNameSource DefaultClrNameSource = ClrNameSource.Alias; // for legacy reasons /// <summary> /// Initializes a new instance of the <see cref="Config"/> class. /// </summary> private Config() { const string prefix = "Umbraco.ModelsBuilder."; // giant kill switch, default: false // must be explicitely set to true for anything else to happen Enable = ConfigurationManager.AppSettings[prefix + "Enable"] == "true"; // ensure defaults are initialized for tests StaticMixinGetterPattern = DefaultStaticMixinGetterPattern; LanguageVersion = DefaultLanguageVersion; ModelsNamespace = DefaultModelsNamespace; ClrNameSource = DefaultClrNameSource; // stop here, everything is false if (!Enable) { return; } // mode var modelsMode = ConfigurationManager.AppSettings[prefix + "ModelsMode"]; if (!string.IsNullOrWhiteSpace(modelsMode)) { switch (modelsMode) { case nameof(ModelsMode.Nothing): ModelsMode = ModelsMode.Nothing; break; case nameof(ModelsMode.PureLive): ModelsMode = ModelsMode.PureLive; break; case nameof(ModelsMode.Dll): ModelsMode = ModelsMode.Dll; break; case nameof(ModelsMode.LiveDll): ModelsMode = ModelsMode.LiveDll; break; case nameof(ModelsMode.AppData): ModelsMode = ModelsMode.AppData; break; case nameof(ModelsMode.LiveAppData): ModelsMode = ModelsMode.LiveAppData; break; default: throw new ConfigurationErrorsException($"ModelsMode \"{modelsMode}\" is not a valid mode." + " Note that modes are case-sensitive."); } } // default: false EnableApi = ConfigurationManager.AppSettings[prefix + "EnableApi"].InvariantEquals("true"); // default: true EnableFactory = !ConfigurationManager.AppSettings[prefix + "EnableFactory"].InvariantEquals("false"); StaticMixinGetters = !ConfigurationManager.AppSettings[prefix + "StaticMixinGetters"].InvariantEquals("false"); FlagOutOfDateModels = !ConfigurationManager.AppSettings[prefix + "FlagOutOfDateModels"].InvariantEquals("false"); // default: initialized above with DefaultModelsNamespace const var value = ConfigurationManager.AppSettings[prefix + "ModelsNamespace"]; if (!string.IsNullOrWhiteSpace(value)) { ModelsNamespace = value; } // default: initialized above with DefaultStaticMixinGetterPattern const value = ConfigurationManager.AppSettings[prefix + "StaticMixinGetterPattern"]; if (!string.IsNullOrWhiteSpace(value)) { StaticMixinGetterPattern = value; } // default: initialized above with DefaultLanguageVersion const value = ConfigurationManager.AppSettings[prefix + "LanguageVersion"]; if (!string.IsNullOrWhiteSpace(value)) { LanguageVersion lv; if (!Enum.TryParse(value, true, out lv)) { throw new ConfigurationErrorsException($"Invalid language version \"{value}\"."); } LanguageVersion = lv; } // default: initialized above with DefaultClrNameSource const value = ConfigurationManager.AppSettings[prefix + "ClrNameSource"]; if (!string.IsNullOrWhiteSpace(value)) { switch (value) { case nameof(ClrNameSource.Nothing): ClrNameSource = ClrNameSource.Nothing; break; case nameof(ClrNameSource.Alias): ClrNameSource = ClrNameSource.Alias; break; case nameof(ClrNameSource.RawAlias): ClrNameSource = ClrNameSource.RawAlias; break; case nameof(ClrNameSource.Name): ClrNameSource = ClrNameSource.Name; break; default: throw new ConfigurationErrorsException($"ClrNameSource \"{value}\" is not a valid source." + " Note that sources are case-sensitive."); } } // not flagging if not generating, or live (incl. pure) if (ModelsMode == ModelsMode.Nothing || ModelsMode.IsLive()) { FlagOutOfDateModels = false; } }
/// <summary> /// Initializes a new instance of the <see cref="ModelsBuilderConfig"/> class. /// </summary> public ModelsBuilderConfig() { const string prefix = "Umbraco.ModelsBuilder."; // giant kill switch, default: false // must be explicitely set to true for anything else to happen Enable = ConfigurationManager.AppSettings[prefix + "Enable"] == "true"; // ensure defaults are initialized for tests ModelsNamespace = DefaultModelsNamespace; ModelsDirectory = Current.IOHelper.MapPath(DefaultModelsDirectory); DebugLevel = 0; // stop here, everything is false if (!Enable) { return; } // mode var modelsMode = ConfigurationManager.AppSettings[prefix + "ModelsMode"]; if (!string.IsNullOrWhiteSpace(modelsMode)) { switch (modelsMode) { case nameof(ModelsMode.Nothing): ModelsMode = ModelsMode.Nothing; break; case nameof(ModelsMode.PureLive): ModelsMode = ModelsMode.PureLive; break; case nameof(ModelsMode.AppData): ModelsMode = ModelsMode.AppData; break; case nameof(ModelsMode.LiveAppData): ModelsMode = ModelsMode.LiveAppData; break; default: throw new ConfigurationErrorsException($"ModelsMode \"{modelsMode}\" is not a valid mode." + " Note that modes are case-sensitive. Possible values are: " + string.Join(", ", Enum.GetNames(typeof(ModelsMode)))); } } // default: false AcceptUnsafeModelsDirectory = ConfigurationManager.AppSettings[prefix + "AcceptUnsafeModelsDirectory"].InvariantEquals("true"); // default: true EnableFactory = !ConfigurationManager.AppSettings[prefix + "EnableFactory"].InvariantEquals("false"); FlagOutOfDateModels = !ConfigurationManager.AppSettings[prefix + "FlagOutOfDateModels"].InvariantEquals("false"); // default: initialized above with DefaultModelsNamespace const var value = ConfigurationManager.AppSettings[prefix + "ModelsNamespace"]; if (!string.IsNullOrWhiteSpace(value)) { ModelsNamespace = value; } // default: initialized above with DefaultModelsDirectory const value = ConfigurationManager.AppSettings[prefix + "ModelsDirectory"]; if (!string.IsNullOrWhiteSpace(value)) { var root = Current.IOHelper.MapPath("~/"); if (root == null) { throw new ConfigurationErrorsException("Could not determine root directory."); } // GetModelsDirectory will ensure that the path is safe ModelsDirectory = GetModelsDirectory(root, value, AcceptUnsafeModelsDirectory); } // default: 0 value = ConfigurationManager.AppSettings[prefix + "DebugLevel"]; if (!string.IsNullOrWhiteSpace(value)) { int debugLevel; if (!int.TryParse(value, out debugLevel)) { throw new ConfigurationErrorsException($"Invalid debug level \"{value}\"."); } DebugLevel = debugLevel; } // not flagging if not generating, or live (incl. pure) if (ModelsMode == ModelsMode.Nothing || ModelsMode.IsLive()) { FlagOutOfDateModels = false; } }