private TypeSetting GetTypeSettingFromFile( NamespaceSetting namespaceSetting, Type type) { // if it's null here, try looking for a file from the namespace. Require <ArgumentNullException>(namespaceSetting.Path != null, ErrorMessages.NoTypeSettingAndNoPathInNamespace, type.FullName, namespaceSetting.Namespace); var path = $"{namespaceSetting.Path.Directory}\\{type.FullName}.{namespaceSetting.Path.Extension}"; // must have a path. Require <ArgumentNullException>(!string.IsNullOrWhiteSpace(path), ErrorMessages.RequiredPathEmptyInNamespace, type.FullName, namespaceSetting.Namespace); Require <FileNotFoundException>(File.Exists(path), ErrorMessages.FileNotFoundWithNamespaceSetting, type.FullName, path, namespaceSetting.Namespace); // this should probably be in a try/catch but i'm leaving it exposed // so that the deserialization exception is bubbled back up. var result = JsonConvert.DeserializeObject <TypeSetting>(File.ReadAllText(path)); Require <ArgumentNullException>(result != null, ErrorMessages.FileDeserializedToNull); return(result); }
private CommandSetting GetCommandSettingFromFile( NamespaceSetting namespaceSetting, TypeSetting typeSetting, string key) { // or maybe in a file. Require <ArgumentNullException>(typeSetting.Path != null, ErrorMessages.NoCommandSettingAndNoPathInTypeSetting, key, typeSetting.Name); var path = $"{typeSetting.Path.File}"; // check that the path is cool & file exists. Require <ArgumentNullException>(!string.IsNullOrWhiteSpace(path), ErrorMessages.RequiredPathEmptyInTypeSetting, key, typeSetting.Name); // convert from relative to absolute (if it is). path = _utilities.GetAbsolutePath(path, namespaceSetting?.Path?.Directory); Require <FileNotFoundException>(File.Exists(path), ErrorMessages.FileNotFoundWithTypeSetting, key, path, typeSetting.Name); // parse... var definitions = JsonConvert.DeserializeObject <IDictionary <string, CommandSetting> >(File.ReadAllText(path)); Require <ArgumentNullException>(definitions != null, ErrorMessages.CommandSettingsDictionaryIsNull, typeSetting.Name); var result = definitions.SingleOrDefault(x => x.Key == key).Value; Require <ArgumentNullException>(result != null, ErrorMessages.CommandSettingIsNull, typeSetting.Name, key); return(result); }