|
| 1 | +using Newtonsoft.Json.Linq; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace TeamTools.Common.Linting |
| 8 | +{ |
| 9 | + public class AppConfigLoader : IAppConfigLoader |
| 10 | + { |
| 11 | + private const string PluginListConfigNode = "plugins"; |
| 12 | + private const string IgnoredFoldersConfigNode = "ignore.folders"; |
| 13 | + private const string IgnoredFileExtensionsConfigNode = "ignore.extensions"; |
| 14 | + private readonly IFileSystemWrapper fileSystem; |
| 15 | + private string rootPath; |
| 16 | + |
| 17 | + public AppConfigLoader(IFileSystemWrapper fileSystem, IAssemblyWrapper assembly) |
| 18 | + { |
| 19 | + this.fileSystem = fileSystem; |
| 20 | + this.rootPath = assembly.GetExecutingPath(); |
| 21 | + } |
| 22 | + |
| 23 | + public IDictionary<string, PluginInfo> Plugins { get; } = new Dictionary<string, PluginInfo>(StringComparer.OrdinalIgnoreCase); |
| 24 | + |
| 25 | + public ICollection<string> IgnoredFolders { get; } = new SortedSet<string>(StringComparer.OrdinalIgnoreCase); |
| 26 | + |
| 27 | + public ICollection<string> IgnoredExtensions { get; } = new SortedSet<string>(StringComparer.OrdinalIgnoreCase); |
| 28 | + |
| 29 | + public void Load(TextReader config) |
| 30 | + { |
| 31 | + Plugins.Clear(); |
| 32 | + |
| 33 | + if (TryParseJson(config.ReadToEnd(), out JToken json)) |
| 34 | + { |
| 35 | + ExtractPlugins(json); |
| 36 | + ExtractIgnores(json); |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + throw new FormatException("Config is not a valid JSON"); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public void LoadFromFile(string filePath) |
| 45 | + { |
| 46 | + if (!fileSystem.FileExists(filePath)) |
| 47 | + { |
| 48 | + throw new FileNotFoundException("Config file not found: " + filePath); |
| 49 | + } |
| 50 | + |
| 51 | + // switching to relative to config file path |
| 52 | + rootPath = Path.GetDirectoryName(filePath); |
| 53 | + |
| 54 | + using (var src = fileSystem.OpenFile(filePath)) |
| 55 | + { |
| 56 | + Load(src); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static bool TryParseJson(string jsonString, out JToken token) |
| 61 | + { |
| 62 | + try |
| 63 | + { |
| 64 | + token = JToken.Parse(jsonString); |
| 65 | + return true; |
| 66 | + } |
| 67 | + catch |
| 68 | + { |
| 69 | + token = null; |
| 70 | + return false; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private void ExtractPlugins(JToken jsonObject) |
| 75 | + { |
| 76 | + var plugins = jsonObject.SelectTokens(".." + PluginListConfigNode).ToList(); |
| 77 | + |
| 78 | + foreach (var plugin in plugins) |
| 79 | + { |
| 80 | + foreach (var jsonToken in plugin.Children()) |
| 81 | + { |
| 82 | + var prop = (JProperty)jsonToken; |
| 83 | + |
| 84 | + var pluginOptions = prop.Value.ToObject<Dictionary<string, string>>(); |
| 85 | + |
| 86 | + pluginOptions.TryGetValue("docsBasePath", out string docsBasePath); |
| 87 | + pluginOptions.TryGetValue("docsBaseUrl", out string docsBaseUrl); |
| 88 | + |
| 89 | + if (pluginOptions.TryGetValue("dll", out string pluginPath) |
| 90 | + && pluginOptions.TryGetValue("config", out string pluginConfigPath)) |
| 91 | + { |
| 92 | + Plugins.Add( |
| 93 | + prop.Name, |
| 94 | + new PluginInfo |
| 95 | + { |
| 96 | + AssemblyPath = fileSystem.MakeAbsolutePath(rootPath, pluginPath), |
| 97 | + ConfigPath = fileSystem.MakeAbsolutePath(rootPath, pluginConfigPath), |
| 98 | + BaseDocsPath = fileSystem.MakeAbsolutePath(rootPath, docsBasePath), |
| 99 | + BaseDocsUrl = docsBaseUrl, |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void ExtractIgnores(JToken jsonObject) |
| 107 | + { |
| 108 | + FillIgnoreList(IgnoredFolders, jsonObject, ".." + IgnoredFoldersConfigNode); |
| 109 | + FillIgnoreList(IgnoredExtensions, jsonObject, ".." + IgnoredFileExtensionsConfigNode); |
| 110 | + } |
| 111 | + |
| 112 | + private void FillIgnoreList(ICollection<string> ignoreCollection, JToken jsonObject, string path) |
| 113 | + { |
| 114 | + ignoreCollection.Clear(); |
| 115 | + foreach (string value in ExtractListFromJson(jsonObject, path)) |
| 116 | + { |
| 117 | + ignoreCollection.Add(value); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private IEnumerable<string> ExtractListFromJson(JToken jsonObject, string path) |
| 122 | + { |
| 123 | + return jsonObject |
| 124 | + .SelectTokens(path) |
| 125 | + .ToList() |
| 126 | + .ToList() |
| 127 | + .Children() |
| 128 | + .Select(jt => ((JValue)jt).Value.ToString()) |
| 129 | + .Distinct(); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments