/// <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; } }
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; } }