Skip to content
Open
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
12 changes: 11 additions & 1 deletion src/ConfigSettings/Settings/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ private static string AppConfigFilePath
/// <returns>Экземпляр класса. Также создаёт новый конфиг согласно правилам.</returns>
public static AppConfig Change()
{
var liveConfigPath = ChangeConfig.Execute(AppConfigFilePath);
return Change(null);
}

/// <summary>
/// Сменить имя.
/// </summary>
/// <param name="resolveForcedAppDataPath">Функция разрешения пути до папки в appdata, когда флаг forceReturnAppDataPath включен.</param>
/// <returns>Экземпляр класса. Также создаёт новый конфиг согласно правилам.</returns>
public static AppConfig Change(Func<string, string> resolveForcedAppDataPath)
{
var liveConfigPath = ChangeConfig.Execute(AppConfigFilePath, resolveForcedAppDataPath);
return new ChangeAppConfig(liveConfigPath);
}

Expand Down
46 changes: 37 additions & 9 deletions src/ConfigSettings/Settings/ChangeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
using System.IO;
using System.Linq;
using System.Xml.Linq;
using ConfigSettings.Utils;
using ConfigSettings.Internal;
using ConfigSettings.Patch;
using ConfigSettings.Utils;

namespace ConfigSettings
{
Expand Down Expand Up @@ -158,16 +157,17 @@ public static bool IsFileLocked(string filePath)
/// <returns>Путь к реальному (live) файлу-конфигу.</returns>
public static string GetLiveConfigFilePath(string currentConfigPath)
{
return GetLiveConfigFilePath(currentConfigPath, false);
return GetLiveConfigFilePath(currentConfigPath, false, null);
}

/// <summary>
/// Получить путь к реальному (live) файлу-конфигу.
/// </summary>
/// <param name="currentConfigPath">Путь к оригинальному файлу-конфигу.</param>
/// <param name="forceReturnAppDataPath">Принудительно возвращать путь к appdata.</param>
/// <param name="resolveForcedAppDataPath">Функция разрешения пути до папки в appdata, когда флаг forceReturnAppDataPath включен.</param>
/// <returns>Путь к реальному (live) файлу-конфигу.</returns>
private static string GetLiveConfigFilePath(string currentConfigPath, bool forceReturnAppDataPath)
private static string GetLiveConfigFilePath(string currentConfigPath, bool forceReturnAppDataPath, Func<string, string> resolveForcedAppDataPath)
{
var nameWithoutExt = Path.GetFileNameWithoutExtension(currentConfigPath);
var liveConfigName = nameWithoutExt + ".live" + Path.GetExtension(currentConfigPath);
Expand All @@ -177,9 +177,14 @@ private static string GetLiveConfigFilePath(string currentConfigPath, bool force
if (!IsFileLocked(liveConfigBesideCurrent))
return liveConfigBesideCurrent;
}
var appDataPath = Path.Combine(SpecialFolders.ProductUserApplicationData("Configs",
Path.GetDirectoryName(currentConfigPath).GetMD5Hash().Substring(0, 8)), liveConfigName);
return appDataPath;

if (resolveForcedAppDataPath == null)
throw new ArgumentNullException(nameof(resolveForcedAppDataPath), "Remove FORCE_USE_APPDATA_PATH meta tag or specify resolveForcedAppDataPath function on ChangeConfig.Execute");

var appDataPath = resolveForcedAppDataPath(currentConfigPath);
if (!Directory.Exists(appDataPath))
Directory.CreateDirectory(appDataPath);
return Path.Combine(appDataPath, liveConfigName);
}

/// <summary>
Expand All @@ -189,7 +194,18 @@ private static string GetLiveConfigFilePath(string currentConfigPath, bool force
/// <returns>Путь к измененному конфигу.</returns>
public static string Execute(string currentConfigPath)
{
return Execute(currentConfigPath, null);
return Execute(currentConfigPath, null, null);
}

/// <summary>
/// Выполнить изменение конфига на основе правил.
/// </summary>
/// <param name="currentConfigPath">Путь к текущем конфигу.</param>
/// <param name="resolveForcedAppDataPath">Функция разрешения пути до папки в appdata, когда указан мета тег FORCE_USE_APPDATA_PATH.</param>
/// <returns>Путь к измененному конфигу.</returns>
public static string Execute(string currentConfigPath, Func<string, string> resolveForcedAppDataPath)
{
return Execute(currentConfigPath, null, resolveForcedAppDataPath);
}

/// <summary>
Expand All @@ -199,6 +215,18 @@ public static string Execute(string currentConfigPath)
/// <param name="settingsFileName">Имя файла с настройками конфига.</param>
/// <returns>Путь к измененному конфигу.</returns>
public static string Execute(string currentConfigPath, string settingsFileName)
{
return Execute(currentConfigPath, settingsFileName, null);
}

/// <summary>
/// Выполнить изменение конфига на основе правил.
/// </summary>
/// <param name="currentConfigPath">Путь к текущем конфигу.</param>
/// <param name="settingsFileName">Имя файла с настройками конфига.</param>
/// <param name="resolveForcedAppDataPath">Функция разрешения пути до папки в appdata, когда флаг forceReturnAppDataPath включен.</param>
/// <returns>Путь к измененному конфигу.</returns>
public static string Execute(string currentConfigPath, string settingsFileName, Func<string, string> resolveForcedAppDataPath)
{
lock (lockInstance)
{
Expand All @@ -212,7 +240,7 @@ public static string Execute(string currentConfigPath, string settingsFileName)
var liveConfigPath = parser.HasMetaVariable(ForceUseAppDataPathMetaVariable) &&
bool.TryParse(parser.GetMetaVariableValue(ForceUseAppDataPathMetaVariable),
out forceUseAppDataPath)
? GetLiveConfigFilePath(currentConfigPath, forceUseAppDataPath)
? GetLiveConfigFilePath(currentConfigPath, forceUseAppDataPath, resolveForcedAppDataPath)
: GetLiveConfigFilePath(currentConfigPath);
if (HasGeneratedBlock(config) && File.Exists(liveConfigPath))
ReplaceConfigWithoutGeneratedBlocks(config, liveConfigPath);
Expand Down
48 changes: 0 additions & 48 deletions src/ConfigSettings/Settings/SpecialFolders.cs

This file was deleted.

37 changes: 0 additions & 37 deletions src/ConfigSettings/Utils/AssemblyProductInfo.cs

This file was deleted.

29 changes: 0 additions & 29 deletions src/Tests/AssemblyProductInfoTests.cs

This file was deleted.

51 changes: 37 additions & 14 deletions src/Tests/ChangeConfig/ChangeConfigWithEtalonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ConfigSettings.Utils;
using FluentAssertions;
using NUnit.Framework;
using TestStack.BDDfy;
using ConfigSettings.Internal;

namespace ConfigSettings.Tests
{
Expand Down Expand Up @@ -53,43 +53,52 @@ public void FindPathByMaskTest()
[Test]
public void ApplyLogSettings()
{
this.When(_ => _.ApplySettingsToLogСonfig())
var currentConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "source_log.config");
this.When(_ => _.ApplySettingsToLogСonfig(currentConfigPath))
.Then(_ => _.ConfigShouldContainsFullPath())
.And(_ => _.ConfigFullPathShouldNotContainsAppdata())
.And(_ => _.ConfigFullPathShouldNotContainsAppdata(currentConfigPath))
.BDDfy();
}

[Test]
public void ApplyConfigSettingsAppDataPath()
{
this.When(_ => _.ApplySettingsToLogСonfigWithAppDataPath())
.Then(_ => _.ConfigFullPathShouldContainsAppdata())
var currentConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "source_log.config");
this.When(_ => _.ApplySettingsToLogСonfigWithAppDataPath(currentConfigPath))
.Then(_ => _.ConfigFullPathShouldContainsAppdata(currentConfigPath))
.BDDfy();
}

[Test]
public void ApplyConfigSettingsAppDataPathWithoutResolveFunc()
{
var currentConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "source_log.config");
Assert.Throws<ArgumentNullException>(() => ApplySettingsToLogСonfigWithAppDataPathWithoutResolvePathFunc(currentConfigPath));
}

private void ConfigShouldContainsFullPath()
{
var existingPathPart = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetFullPath(this.liveConfigPath)));
File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "source_log.config")).Contains(existingPathPart).Should().BeFalse();
File.ReadAllText(this.liveConfigPath).Contains(existingPathPart).Should().BeTrue();
}

private void ConfigFullPathShouldContainsAppdata()
private void ConfigFullPathShouldContainsAppdata(string currentConfigPath)
{
this.liveConfigPath.Contains(SpecialFolders.ProductUserApplicationData("Configs")).Should().BeTrue();
this.liveConfigPath.Contains(ResolveForcedAppDataPath(currentConfigPath)).Should().BeTrue();
}

private void ConfigFullPathShouldNotContainsAppdata()
private void ConfigFullPathShouldNotContainsAppdata(string currentConfigPath)
{
this.liveConfigPath.Contains(SpecialFolders.ProductUserApplicationData("Configs")).Should().BeFalse();
this.liveConfigPath.Contains(ResolveForcedAppDataPath(currentConfigPath)).Should().BeFalse();
}


private void FindPathByMask()
{
var fn = Guid.NewGuid().ToString();
ChangeConfig.FindFirstPathByMask(this.tempPath, fn).Should().BeNull();
File.WriteAllText(Path.Combine(this.tempPath, "prefix_"+fn), "some content");
File.WriteAllText(Path.Combine(this.tempPath, "prefix_" + fn), "some content");
ChangeConfig.FindFirstPathByMask(this.tempPath, fn).Should().EndWith(fn);
}

Expand Down Expand Up @@ -140,14 +149,28 @@ private void LiveConfigShouldBeEqualToEtalon()
diff.Should().BeEmpty();
}

private void ApplySettingsToLogСonfig()
private void ApplySettingsToLogСonfig(string currentConfigPath)
{
this.liveConfigPath = ChangeConfig.Execute(currentConfigPath, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "settings.xml"), ResolveForcedAppDataPath);
}

private void ApplySettingsToLogСonfigWithAppDataPath(string currentConfigPath)
{
this.liveConfigPath = ChangeConfig.Execute(currentConfigPath, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "settings_with_appdata_path.xml"), ResolveForcedAppDataPath);
}

private void ApplySettingsToLogСonfigWithAppDataPathWithoutResolvePathFunc(string currentConfigPath)
{
this.liveConfigPath = ChangeConfig.Execute(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "source_log.config"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "settings.xml"));
this.liveConfigPath = ChangeConfig.Execute(currentConfigPath, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "settings_with_appdata_path.xml"));
}

private void ApplySettingsToLogСonfigWithAppDataPath()
private static string ResolveForcedAppDataPath(string currentConfigPath)
{
this.liveConfigPath = ChangeConfig.Execute(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "source_log.config"), Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ChangeConfig", "Etalon", "settings_with_appdata_path.xml"));
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"ConfigSettings",
"Configs",
Path.GetDirectoryName(currentConfigPath).GetMD5Hash().Substring(0, 8));
}
}
}