Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid useless config file saves #2520

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 42 additions & 29 deletions dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitClones.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PyRevitClone>();
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
Expand All @@ -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
Expand All @@ -96,34 +102,42 @@ public static void UnregisterAllClones() {
// return list of registered clones
// @handled @logs
public static List<PyRevitClone> GetRegisteredClones() {
var validatedClones = new List<PyRevitClone>();

// 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<PyRevitClone>();
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;
}

Expand Down Expand Up @@ -553,12 +567,11 @@ public static void UpdateAllClones(GitInstallerCredentials credentials) {
// updates the config value for registered clones
public static void SaveRegisteredClones(IEnumerable<PyRevitClone> clonesList) {
var cfg = PyRevitConfigs.GetConfigFile();
var newValueDic = new Dictionary<string, string>();
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);
}

}
}
46 changes: 26 additions & 20 deletions dev/pyRevitLabs/pyRevitLabs.PyRevit/PyRevitConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}

Expand Down Expand Up @@ -94,24 +95,29 @@ public Dictionary<string, string> 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
Expand Down