/// <summary> /// /// </summary> /// <param name="share"></param> /// <param name="fileInfo"></param> /// <param name="keyValueFinder"></param> public JsonConfigurationBuilder(IEnumerable <ShareConfigurationBuilder> share, ConfigFileInfo fileInfo, ICustomKeyValueFinder keyValueFinder) { this.share = share; this.File = fileInfo; this.Name = this.File.File.Name.Replace(this.File.File.Extension, "").Trim(new[] { ' ', '.' }); this.usingShareInfo = new List <ShareFileInfo>(); this.keyValueFinder = keyValueFinder; }
/// <summary> /// 处理app级 /// </summary> /// <param name="change"></param> /// <param name="outsideEncoding"></param> private void HandleAppFile(ChangeQueue change, Encoding outsideEncoding = null) { var oldFile = change.OldFullName == null ? null : new FileInfo(change.OldFullName); var newFile = change.FullName == null ? null : new FileInfo(change.FullName); if (newFile != null && !this.CanRefresh(newFile)) { return; } switch (change.Action) { //新加 case 0: { IShareFileReference newBuilder = null; switch (newFile.Extension.ToLower()) { case ".json": { var fileInfo = new ConfigFileInfo() { File = newFile, Encoding = outsideEncoding ?? Encoding.UTF8 }; var builder = new JsonConfigurationBuilder(this.shareConfiguration, fileInfo, this.keyValueFinder) { }.Build(); if (this.appConfiguration.Any(ta => ta.Builder.Name == builder.Name)) { } else { this.appConfiguration.Add(builder); } newBuilder = builder; } break; case ".conf": { var fileInfo = new ConfigFileInfo() { File = newFile, Encoding = outsideEncoding }; var builder = new XmlConfigurationBuilder(this.shareConfiguration, fileInfo, this.keyValueFinder) { }.Build(); if (this.appConfiguration.Any(ta => ta.Builder.Name == builder.Name)) { } else { this.appConfiguration.Add(builder); } newBuilder = builder; } break; } if (!change.PathChanged && !this.fileWather.ContainsKey(newFile.FullName)) { var watcher = new Never.IO.FileWatcher(newFile) { EnableRaisingEvents = true }; this.fileWather.Add(newFile.FullName, watcher); this.moreTimeLimit.Add(newFile.FullName, DateTime.Now); watcher.Created += AppWatcher_Created; watcher.Changed += AppWatcher_Changed; watcher.Deleted += AppWatcher_Deleted; watcher.Renamed += AppWatcher_Renamed; } this.EatException(() => { this.OnAppFileChanged?.Invoke(this, new ConfigurationWatcherEventArgs() { Builders = new[] { newBuilder.Builder } }); }); this.WriteAppToLog(change, newBuilder); } break; //修改 case 1: { var oldBuilder = this.appConfiguration.FirstOrDefault(ta => ta.Builder.File.File.FullName == newFile.FullName); oldBuilder.Builder.Rebuild(this.shareConfiguration); this.EatException(() => { this.OnAppFileChanged?.Invoke(this, new ConfigurationWatcherEventArgs() { Builders = new[] { oldBuilder.Builder } }); }); this.WriteAppToLog(change, oldBuilder); } break; //重命名 case 2: { var oldBuilder = this.appConfiguration.FirstOrDefault(ta => ta.Builder.File.File.FullName == oldFile.FullName); var refence = default(IConfigurationBuilder); switch (oldBuilder.Builder.FileType) { case ConfigFileType.Json: { var fileInfo = new ConfigFileInfo() { File = newFile, Encoding = oldBuilder.Builder.File.Encoding }; var newBuilder = new JsonConfigurationBuilder(this.shareConfiguration, fileInfo, this.keyValueFinder) { }.Build(); this.appConfiguration.Remove(oldBuilder); this.appConfiguration.Add(newBuilder); refence = newBuilder; } break; case ConfigFileType.Xml: { var fileInfo = new ConfigFileInfo() { File = newFile, Encoding = oldBuilder.Builder.File.Encoding }; var newBuilder = new XmlConfigurationBuilder(this.shareConfiguration, fileInfo, this.keyValueFinder) { }.Build(); this.appConfiguration.Remove(oldBuilder); this.appConfiguration.Add(newBuilder); refence = newBuilder; } break; } if (!change.PathChanged && this.fileWather.ContainsKey(oldFile.FullName)) { var watcher = this.fileWather[oldFile.FullName]; watcher.Path = System.IO.Path.GetDirectoryName(newFile.FullName); watcher.Filter = newFile.Name; } if (refence != null) { this.EatException(() => { this.OnAppFileRenamed?.Invoke(this, new ConfigurationWatcherEventArgs() { Builders = new[] { refence } }); }); } this.WriteAppToLog(change, refence as IShareFileReference); } break; //删除 case 3: { var oldBuilder = this.appConfiguration.FirstOrDefault(ta => ta.Builder.File.File.FullName == oldFile.FullName); this.appConfiguration.Remove(oldBuilder); if (!change.PathChanged && this.fileWather.ContainsKey(oldFile.FullName)) { this.fileWather[oldFile.FullName].Dispose(); this.fileWather.Remove(oldFile.FullName); } this.EatException(() => { this.OnAppFileDeleted?.Invoke(this, new ConfigurationWatcherEventArgs() { Builders = new[] { oldBuilder.Builder } }); }); this.WriteAppToLog(change, oldBuilder); } break; } }
/// <summary> /// 构建信息 /// </summary> protected string Build(ConfigFileInfo fileInfo) { var content = System.IO.File.ReadAllText(fileInfo.File.FullName, fileInfo.Encoding); if (content.IsNullOrWhiteSpace()) { return(content); } //replace find://eat@user content = Regex.Replace(content, "\"find://(eat)@(.*?)\"", m => { var split = m.Value.Trim(new[] { '\'', '\"' }).Split('@'); if (split == null || split.Length <= 1) { return(m.Value); } var value = this.Find("eat", split[1]); return(value.IsNotNullOrEmpty() ? value : m.Value); }); //replace find://cat@user content = Regex.Replace(content, "\"find://(cat)@(.*?)\"", m => { var split = m.Value.Trim(new[] { '\'', '\"' }).Split('@'); if (split == null || split.Length <= 1) { return(m.Value); } var value = this.Find("cat", split[1]); return(value.IsNotNullOrEmpty() ? string.Concat(m.Value[0], value, m.Value[m.Value.Length - 1]) : m.Value); }); //replace link://eat@user content = Regex.Replace(content, "\"link://eat@(.*?)\"", m => { var split = m.Value.Trim(new[] { '\'', '\"' }).Split('@'); if (split == null || split.Length <= 1) { return(m.Value); } var keys = new List <string>(split.Length); for (var k = 2; k < split.Length; k++) { keys.Add(split[k]); } var value = this.Link(split[1], keys); return(value.IsNotNullOrEmpty() ? value : m.Value); }); //replace link://cat@user content = Regex.Replace(content, "\"link://cat@(.*?)\"", m => { var split = m.Value.Trim(new[] { '\'', '\"' }).Split('@'); if (split == null || split.Length <= 1) { return(m.Value); } var keys = new List <string>(split.Length); for (var k = 2; k < split.Length; k++) { keys.Add(split[k]); } var value = this.Link(split[1], keys); return(value.IsNotNullOrEmpty() ? string.Concat(m.Value[0], value, m.Value[m.Value.Length - 1]) : m.Value); }); return(content); }
/// <summary> /// /// </summary> /// <param name="configFileInfo"></param> public ShareConfigurationBuilder(ConfigFileInfo configFileInfo) { this.File = configFileInfo.File; this.Encoding = configFileInfo.Encoding; this.events = new List <EventHandler <ShareFileEventArgs> >(); }