From a7be3d93a8b4197eab9e81c14ae2acfd2766ce5b Mon Sep 17 00:00:00 2001 From: Andrea Ghensi Date: Tue, 14 Jan 2025 23:57:09 +0100 Subject: [PATCH] fix: avoid useless config file saves --- .../pyRevitLabs.PyRevit/PyRevitClones.cs | 71 +++++++++++-------- .../pyRevitLabs.PyRevit/PyRevitConfig.cs | 46 ++++++------ 2 files changed, 68 insertions(+), 49 deletions(-) diff --git a/dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitClones.cs b/dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitClones.cs index 8d58d8028..0df7ac038 100644 --- a/dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitClones.cs +++ b/dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitClones.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.IO.Compression; +using System.Linq; using System.Text.RegularExpressions; using System.Security.Principal; using System.Text; @@ -60,17 +61,20 @@ public static void RegisterClone(string cloneName, string repoPath, bool forceUp // @handled @logs public static void RenameClone(string cloneName, string newName) { logger.Debug("Renaming clone \"{0}\" to \"{1}\"", cloneName, newName); + var renamed = false; var renamedClones = new List(); foreach (var clone in GetRegisteredClones()) { if (clone.Name == cloneName) { clone.Rename(newName); logger.Debug("Renamed clone \"{0}\" to \"{1}\"", cloneName, clone.Name); + renamed = true; } - renamedClones.Add(clone); } - - SaveRegisteredClones(renamedClones); + if (renamed) + { + SaveRegisteredClones(renamedClones); + } } // unregister a clone from configs @@ -80,8 +84,10 @@ public static void UnregisterClone(PyRevitClone clone) { // remove the clone path from list var clones = GetRegisteredClones(); - clones.Remove(clone); - SaveRegisteredClones(clones); + if (clones.Remove(clone)) + { + SaveRegisteredClones(clones); + } } // unregister all clone from configs @@ -96,34 +102,42 @@ public static void UnregisterAllClones() { // return list of registered clones // @handled @logs public static List GetRegisteredClones() { - var validatedClones = new List(); // safely get clone list var cfg = PyRevitConfigs.GetConfigFile(); - var clonesList = cfg.GetDictValue(PyRevitConsts.EnvConfigsSectionName, PyRevitConsts.EnvConfigsInstalledClonesKey); + var clonesDict = cfg.GetDictValue(PyRevitConsts.EnvConfigsSectionName, PyRevitConsts.EnvConfigsInstalledClonesKey); - if (clonesList != null) { - // verify all registered clones, protect against tampering - foreach (var cloneKeyValue in clonesList) { - var clonePath = cloneKeyValue.Value.NormalizeAsPath(); - if (CommonUtils.VerifyPath(clonePath)) { - try { - var clone = new PyRevitClone(clonePath, name: cloneKeyValue.Key); - if (clone.IsValid && !validatedClones.Contains(clone)) { - logger.Debug("Verified clone \"{0}={1}\"", clone.Name, clone.ClonePath); - validatedClones.Add(clone); - } - } - catch { - logger.Debug("Error occured when processing registered clone \"{0}\" at \"{1}\"", cloneKeyValue.Key, clonePath); - } + var validatedClones = new List(); + if (clonesDict is null) { + return validatedClones; + } + var listChanged = false; + // verify all registered clones, protect against tampering + foreach (var cloneKeyValue in clonesDict) { + var clonePath = cloneKeyValue.Value.NormalizeAsPath(); + if (!CommonUtils.VerifyPath(clonePath)) { + listChanged = true; + continue; + } + if (clonePath != cloneKeyValue.Value) { + listChanged = true; + } + try { + var clone = new PyRevitClone(clonePath, name: cloneKeyValue.Key); + if (clone.IsValid && !validatedClones.Contains(clone)) { + logger.Debug("Verified clone \"{0}={1}\"", clone.Name, clone.ClonePath); + validatedClones.Add(clone); } } + catch { + logger.Debug("Error occured when processing registered clone \"{0}\" at \"{1}\"", cloneKeyValue.Key, clonePath); + } + } + if (listChanged){ // rewrite the verified clones list back to config file SaveRegisteredClones(validatedClones); } - return validatedClones; } @@ -553,12 +567,11 @@ public static void UpdateAllClones(GitInstallerCredentials credentials) { // updates the config value for registered clones public static void SaveRegisteredClones(IEnumerable clonesList) { var cfg = PyRevitConfigs.GetConfigFile(); - var newValueDic = new Dictionary(); - foreach (var clone in clonesList) - newValueDic[clone.Name] = clone.ClonePath; - - cfg.SetValue(PyRevitConsts.EnvConfigsSectionName, PyRevitConsts.EnvConfigsInstalledClonesKey, newValueDic); + var newValueDic = clonesList.ToDictionary(x => x.Name, x => x.ClonePath); + cfg.SetValue( + PyRevitConsts.EnvConfigsSectionName, + PyRevitConsts.EnvConfigsInstalledClonesKey, + newValueDic); } - } } diff --git a/dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitConfig.cs b/dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitConfig.cs index 8ccc23e7d..8bce435a6 100644 --- a/dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitConfig.cs +++ b/dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitConfig.cs @@ -52,9 +52,10 @@ public void SaveConfigFile() { try { _config.Save(PyRevitConsts.ConfigFilePath); } - catch (Exception ex) { - throw new PyRevitException(string.Format("Failed to save config to \"{0}\". | {1}", - PyRevitConsts.ConfigFilePath, ex.Message)); + catch (Exception ex) + { + throw new PyRevitException( + $"Failed to save config to \"{PyRevitConsts.ConfigFilePath}\". | {ex.Message}"); } } @@ -94,24 +95,29 @@ public Dictionary GetDictValue(string sectionName, string keyNam // set config key value, creates the config if not set yet public void SetValue(string sectionName, string keyName, string stringValue) { - if (stringValue != null) { - if (!_config.Sections.Contains(sectionName)) { - logger.Debug("Adding config section \"{0}\"", sectionName); - _config.Sections.Add(sectionName); - } - - if (!_config.Sections[sectionName].Keys.Contains(keyName)) { - logger.Debug("Adding config key \"{0}:{1}\"", sectionName, keyName); - _config.Sections[sectionName].Keys.Add(keyName); - } - - logger.Debug("Updating config \"{0}:{1} = {2}\"", sectionName, keyName, stringValue); - _config.Sections[sectionName].Keys[keyName].Value = stringValue; - - SaveConfigFile(); - } - else + if (stringValue is null) + { logger.Debug("Can not set null value for \"{0}:{1}\"", sectionName, keyName); + return; + } + if (!_config.Sections.Contains(sectionName)) + { + logger.Debug("Adding config section \"{0}\"", sectionName); + _config.Sections.Add(sectionName); + } + if (!_config.Sections[sectionName].Keys.Contains(keyName)) + { + logger.Debug("Adding config key \"{0}:{1}\"", sectionName, keyName); + _config.Sections[sectionName].Keys.Add(keyName); + } + if (_config.Sections[sectionName].Keys[keyName].Value == stringValue) + { + logger.Debug("Config \"{0}:{1}\" already set to \"{2}\"", sectionName, keyName, stringValue); + return; + } + logger.Debug("Updating config \"{0}:{1} = {2}\"", sectionName, keyName, stringValue); + _config.Sections[sectionName].Keys[keyName].Value = stringValue; + SaveConfigFile(); } // sets config key value as bool