Skip to content

Commit

Permalink
Фикс чтения конфигурации точки входа.
Browse files Browse the repository at this point in the history
Ранее конфигурация читалась по относительному пути, что ломало ряд сценариев, когда рабочий каталог менялся в процессе работы
  • Loading branch information
EvilBeaver committed Oct 6, 2023
1 parent 7fd9b71 commit 06291a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
21 changes: 18 additions & 3 deletions src/ScriptEngine.HostedScript/CfgFileConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,32 @@ public class CfgFileConfigProvider : IConfigProvider
public const string CONFIG_FILE_NAME = "oscript.cfg";

public string FilePath { get; set; }


public bool Required { get; set; }

public Func<IDictionary<string, string>> GetProvider()
{
var localCopy = FilePath;
return () => ReadConfigFile(localCopy);
}

private static IDictionary<string, string> ReadConfigFile(string configPath)
private IDictionary<string, string> ReadConfigFile(string configPath)
{
var conf = new Dictionary<string, string>();
using (var reader = new StreamReader(configPath, true))
StreamReader reader;
try
{
reader = new StreamReader(configPath, true);
}
catch (IOException)
{
if (!Required)
return conf;

throw;
}

using (reader)
{
while (!reader.EndOfStream)
{
Expand Down
23 changes: 12 additions & 11 deletions src/ScriptEngine.HostedScript/Extensions/EngineBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ namespace ScriptEngine.HostedScript.Extensions
{
public static class EngineBuilderExtensions
{
public static ConfigurationProviders UseConfigFile(this ConfigurationProviders providers, string configFile)
public static ConfigurationProviders UseConfigFile(this ConfigurationProviders providers, string configFile, bool required = true)
{
if (System.IO.File.Exists(configFile))
if (File.Exists(configFile))
{
var reader = new CfgFileConfigProvider
{
FilePath = configFile
FilePath = configFile,
Required = required
};
providers.Add(reader.GetProvider());
}
Expand All @@ -36,22 +37,22 @@ public static ConfigurationProviders UseSystemConfigFile(this ConfigurationProvi
if (string.IsNullOrEmpty(asmLocation))
asmLocation = System.Reflection.Assembly.GetEntryAssembly()?.Location;

var pathPrefix = !string.IsNullOrWhiteSpace(asmLocation) ?
System.IO.Path.GetDirectoryName(asmLocation) :
System.Environment.CurrentDirectory;
var pathPrefix = (!string.IsNullOrWhiteSpace(asmLocation) ?
Path.GetDirectoryName(asmLocation) :
System.Environment.CurrentDirectory) ?? "";

var configFile = System.IO.Path.Combine(pathPrefix, CfgFileConfigProvider.CONFIG_FILE_NAME);
var configFile = Path.Combine(pathPrefix, CfgFileConfigProvider.CONFIG_FILE_NAME);

return providers.UseConfigFile(configFile);
}

public static ConfigurationProviders UseEntrypointConfigFile(this ConfigurationProviders providers, string entryPoint)
{
var dir = System.IO.Path.GetDirectoryName(entryPoint);
var cfgPath = System.IO.Path.Combine(dir, CfgFileConfigProvider.CONFIG_FILE_NAME);
if (System.IO.File.Exists(cfgPath))
var dir = Path.GetDirectoryName(entryPoint) ?? "";
var cfgPath = Path.GetFullPath(Path.Combine(dir, CfgFileConfigProvider.CONFIG_FILE_NAME));
if (File.Exists(cfgPath))
{
return providers.UseConfigFile(cfgPath);
return providers.UseConfigFile(cfgPath, false);
}

return providers;
Expand Down

0 comments on commit 06291a2

Please sign in to comment.