Skip to content

Commit 8207c80

Browse files
committed
Рефакторинг механизма конфигурирования
1 parent 2c3c530 commit 8207c80

14 files changed

Lines changed: 200 additions & 176 deletions

src/ScriptEngine.HostedScript/CfgFileConfigProvider.cs

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*----------------------------------------------------------
1+
/*----------------------------------------------------------
22
This Source Code Form is subject to the terms of the
33
Mozilla Public License, v.2.0. If a copy of the MPL
44
was not distributed with this file, You can obtain one
@@ -8,7 +8,7 @@ This Source Code Form is subject to the terms of the
88
using System;
99
using System.Collections.Generic;
1010
using System.IO;
11-
using System.Linq;
11+
using ScriptEngine.Hosting;
1212

1313
namespace ScriptEngine.HostedScript
1414
{
@@ -20,10 +20,17 @@ public class CfgFileConfigProvider : IConfigProvider
2020

2121
public bool Required { get; set; }
2222

23-
public Func<IDictionary<string, string>> GetProvider()
23+
public string SourceId => FilePath;
24+
25+
public IReadOnlyDictionary<string, string> Load()
26+
{
27+
return (IReadOnlyDictionary<string, string>)ReadConfigFile(FilePath);
28+
}
29+
30+
public string ResolveRelativePath(string path)
2431
{
25-
var localCopy = FilePath;
26-
return () => ReadConfigFile(localCopy);
32+
var confDir = Path.GetDirectoryName(FilePath);
33+
return Path.Combine(confDir, path);
2734
}
2835

2936
private IDictionary<string, string> ReadConfigFile(string configPath)
@@ -58,30 +65,7 @@ private IDictionary<string, string> ReadConfigFile(string configPath)
5865
}
5966
}
6067

61-
ExpandRelativePaths(conf, configPath);
62-
6368
return conf;
6469
}
65-
66-
private static void ExpandRelativePaths(IDictionary<string, string> conf, string configFile)
67-
{
68-
string sysDir = null;
69-
conf.TryGetValue(OneScriptLibraryOptions.SYSTEM_LIBRARY_DIR, out sysDir);
70-
71-
var confDir = System.IO.Path.GetDirectoryName(configFile);
72-
if (sysDir != null && !System.IO.Path.IsPathRooted(sysDir))
73-
{
74-
sysDir = System.IO.Path.GetFullPath(System.IO.Path.Combine(confDir, sysDir));
75-
conf[OneScriptLibraryOptions.SYSTEM_LIBRARY_DIR] = sysDir;
76-
}
77-
78-
string additionals;
79-
if (conf.TryGetValue(OneScriptLibraryOptions.ADDITIONAL_LIBRARIES, out additionals))
80-
{
81-
var fullPaths = additionals.Split(new[]{";"}, StringSplitOptions.RemoveEmptyEntries)
82-
.Select(x => Path.GetFullPath(Path.Combine(confDir, x)));
83-
conf[OneScriptLibraryOptions.ADDITIONAL_LIBRARIES] = string.Join(";",fullPaths);
84-
}
85-
}
8670
}
87-
}
71+
}

src/ScriptEngine.HostedScript/EngineConfigProvider.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/ScriptEngine.HostedScript/EnvironmentVariableConfigProvider.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This Source Code Form is subject to the terms of the
88
using System;
99
using System.Collections.Generic;
1010
using OneScript.Commons;
11+
using ScriptEngine.Hosting;
1112

1213
namespace ScriptEngine.HostedScript
1314
{
@@ -19,20 +20,22 @@ public EnvironmentVariableConfigProvider(string variableName)
1920
{
2021
_variableName = variableName;
2122
}
22-
23-
public Func<IDictionary<string, string>> GetProvider()
23+
24+
public string SourceId => _variableName;
25+
26+
public IReadOnlyDictionary<string, string> Load()
27+
{
28+
var envValue = Environment.GetEnvironmentVariable(_variableName);
29+
if (string.IsNullOrEmpty(envValue))
30+
return new Dictionary<string, string>();
31+
32+
var paramList = new FormatParametersList(envValue);
33+
return paramList.ToDictionary();
34+
}
35+
36+
public string ResolveRelativePath(string path)
2437
{
25-
var varName = _variableName;
26-
return () =>
27-
{
28-
// Читаем переменную окружения динамически при каждом вызове
29-
var envValue = Environment.GetEnvironmentVariable(varName);
30-
if (string.IsNullOrEmpty(envValue))
31-
return new Dictionary<string, string>();
32-
33-
var paramList = new FormatParametersList(envValue);
34-
return paramList.ToDictionary();
35-
};
38+
return path;
3639
}
3740
}
3841
}

src/ScriptEngine.HostedScript/Extensions/EngineBuilderExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static ConfigurationProviders UseConfigFile(this ConfigurationProviders p
2525
FilePath = configFile,
2626
Required = required
2727
};
28-
providers.Add(reader.GetProvider());
28+
providers.Add(reader);
2929
}
3030

3131
return providers;
@@ -61,7 +61,7 @@ public static ConfigurationProviders UseEntrypointConfigFile(this ConfigurationP
6161
public static ConfigurationProviders UseEnvironmentVariableConfig(this ConfigurationProviders providers, string varName)
6262
{
6363
var reader = new EnvironmentVariableConfigProvider(varName);
64-
providers.Add(reader.GetProvider());
64+
providers.Add(reader);
6565
return providers;
6666
}
6767

@@ -119,4 +119,4 @@ public static IEngineBuilder UseEventHandlers(this IEngineBuilder builder)
119119
return builder;
120120
}
121121
}
122-
}
122+
}

src/ScriptEngine.HostedScript/FormatStringConfigProvider.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/*----------------------------------------------------------
1+
/*----------------------------------------------------------
22
This Source Code Form is subject to the terms of the
33
Mozilla Public License, v.2.0. If a copy of the MPL
44
was not distributed with this file, You can obtain one
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
77

88
using System.Collections.Generic;
9+
using System.Linq;
910
using ScriptEngine.Hosting;
1011

1112
namespace ScriptEngine.HostedScript
@@ -17,18 +18,12 @@ public class OneScriptLibraryOptions : OneScriptCoreOptions
1718

1819
public OneScriptLibraryOptions(KeyValueConfig config) : base(config)
1920
{
20-
SystemLibraryDir = config[SYSTEM_LIBRARY_DIR];
21-
22-
var additionalDirsList = config[ADDITIONAL_LIBRARIES];
23-
if (additionalDirsList != null)
24-
{
25-
var addDirs = additionalDirsList.Split(';');
26-
AdditionalLibraries = new List<string>(addDirs);
27-
}
21+
SystemLibraryDir = config.GetEntry(SYSTEM_LIBRARY_DIR)?.ResolvePath();
22+
AdditionalLibraries = config.GetEntry(ADDITIONAL_LIBRARIES)?.ResolvePathList(';').ToList();
2823
}
2924

3025
public string SystemLibraryDir { get; set; }
3126

3227
public IEnumerable<string> AdditionalLibraries { get; set; }
3328
}
34-
}
29+
}

src/ScriptEngine.HostedScript/SystemConfigAccessor.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ namespace ScriptEngine.HostedScript.Library
1515
[GlobalContext(Category = "Работа с настройками системы")]
1616
public class SystemConfigAccessor : GlobalContextBase<SystemConfigAccessor>
1717
{
18-
private KeyValueConfig _config;
19-
private readonly ConfigurationProviders _providers;
18+
private readonly EngineConfiguration _activeConfig;
2019

21-
public SystemConfigAccessor(ConfigurationProviders providers)
20+
public SystemConfigAccessor(EngineConfiguration activeConfig)
2221
{
23-
_providers = providers;
24-
Refresh();
22+
_activeConfig = activeConfig;
2523
}
2624

2725
/// <summary>
@@ -30,7 +28,7 @@ public SystemConfigAccessor(ConfigurationProviders providers)
3028
[ContextMethod("ОбновитьНастройкиСистемы", "RefreshSystemConfig")]
3129
public void Refresh()
3230
{
33-
_config = _providers.CreateConfig();
31+
_activeConfig.Reload();
3432
}
3533

3634
/// <summary>
@@ -41,21 +39,16 @@ public void Refresh()
4139
[ContextMethod("ПолучитьЗначениеСистемнойНастройки", "GetSystemOptionValue")]
4240
public IValue GetSystemOptionValue(string optionKey)
4341
{
44-
string value = null;
45-
if (_config != null)
46-
{
47-
value = _config[optionKey];
48-
}
49-
50-
if (value != null)
51-
return ValueFactory.Create(value);
42+
var cfg = _activeConfig.GetConfig();
5243

53-
return ValueFactory.Create();
44+
var value = cfg[optionKey];
45+
46+
return value != null ? ValueFactory.Create(value) : ValueFactory.Create();
5447
}
5548

56-
public static IAttachableContext CreateInstance(ConfigurationProviders providers)
49+
public static IAttachableContext CreateInstance(EngineConfiguration configHolder)
5750
{
58-
return new SystemConfigAccessor(providers);
51+
return new SystemConfigAccessor(configHolder);
5952
}
6053
}
6154
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*----------------------------------------------------------
1+
/*----------------------------------------------------------
22
This Source Code Form is subject to the terms of the
33
Mozilla Public License, v.2.0. If a copy of the MPL
44
was not distributed with this file, You can obtain one
@@ -7,27 +7,29 @@ This Source Code Form is subject to the terms of the
77

88
using System;
99
using System.Collections.Generic;
10+
using System.Linq;
1011

1112
namespace ScriptEngine.Hosting
1213
{
1314
public class ConfigurationProviders
1415
{
15-
private List<Func<IDictionary<string, string>>> _providers = new List<Func<IDictionary<string, string>>>();
16+
private readonly List<IConfigProvider> _providers = new List<IConfigProvider>();
1617

17-
public void Add(Func<IDictionary<string, string>> configGetter)
18+
public void Add(IConfigProvider source)
1819
{
19-
_providers.Add(configGetter);
20+
_providers.Add(source);
2021
}
2122

2223
public KeyValueConfig CreateConfig()
2324
{
2425
var cfg = new KeyValueConfig();
2526
foreach (var provider in _providers)
2627
{
27-
cfg.Merge(provider());
28+
var values = provider.Load();
29+
cfg.Merge((IDictionary<string, string>)values, provider);
2830
}
2931

3032
return cfg;
3133
}
3234
}
33-
}
35+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.IO;
11+
using System.Linq;
12+
13+
namespace ScriptEngine.Hosting
14+
{
15+
public class ConfigurationValue
16+
{
17+
public string RawValue { get; }
18+
public IConfigProvider Source { get; }
19+
20+
public ConfigurationValue(string rawValue, IConfigProvider source)
21+
{
22+
RawValue = rawValue;
23+
Source = source;
24+
}
25+
26+
/// <summary>
27+
/// Разрешает значение как путь относительно источника конфигурации
28+
/// </summary>
29+
public string ResolvePath()
30+
{
31+
return string.IsNullOrEmpty(RawValue) ? RawValue : Source.ResolveRelativePath(RawValue.Trim());
32+
}
33+
34+
/// <summary>
35+
/// Разрешает значение как список путей
36+
/// </summary>
37+
public IEnumerable<string> ResolvePathList(char separator = ';')
38+
{
39+
if (string.IsNullOrEmpty(RawValue))
40+
return Enumerable.Empty<string>();
41+
42+
return RawValue.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries)
43+
.Select(path =>
44+
{
45+
var trimmed = path.Trim();
46+
return Source.ResolveRelativePath(trimmed);
47+
});
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)