Fix #3833: introduce token replace class.
This commit is contained in:
parent
766a190015
commit
77ce31128c
|
@ -16,6 +16,7 @@ using Microsoft.Extensions.DependencyInjection;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using Oqtane.Modules;
|
using Oqtane.Modules;
|
||||||
|
using Oqtane.Infrastructure.Interfaces;
|
||||||
|
|
||||||
namespace Oqtane.Controllers
|
namespace Oqtane.Controllers
|
||||||
{
|
{
|
||||||
|
@ -319,52 +320,33 @@ namespace Oqtane.Controllers
|
||||||
|
|
||||||
private void ProcessTemplatesRecursively(DirectoryInfo current, string rootPath, string rootFolder, string templatePath, ModuleDefinition moduleDefinition)
|
private void ProcessTemplatesRecursively(DirectoryInfo current, string rootPath, string rootFolder, string templatePath, ModuleDefinition moduleDefinition)
|
||||||
{
|
{
|
||||||
|
var tokenReplace = InitializeTokenReplace(rootPath, rootFolder, moduleDefinition);
|
||||||
|
|
||||||
// process folder
|
// process folder
|
||||||
string folderPath = Utilities.PathCombine(rootPath, current.FullName.Replace(templatePath, ""));
|
var folderPath = Utilities.PathCombine(rootPath, current.FullName.Replace(templatePath, ""));
|
||||||
folderPath = folderPath.Replace("[Owner]", moduleDefinition.Owner);
|
folderPath = tokenReplace.ReplaceTokens(folderPath);
|
||||||
folderPath = folderPath.Replace("[Module]", moduleDefinition.Name);
|
|
||||||
if (!Directory.Exists(folderPath))
|
if (!Directory.Exists(folderPath))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(folderPath);
|
Directory.CreateDirectory(folderPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo[] files = current.GetFiles("*.*");
|
tokenReplace.AddSource("Folder", folderPath);
|
||||||
|
var files = current.GetFiles("*.*");
|
||||||
if (files != null)
|
if (files != null)
|
||||||
{
|
{
|
||||||
foreach (FileInfo file in files)
|
foreach (FileInfo file in files)
|
||||||
{
|
{
|
||||||
// process file
|
// process file
|
||||||
string filePath = Path.Combine(folderPath, file.Name);
|
var filePath = Path.Combine(folderPath, file.Name);
|
||||||
filePath = filePath.Replace("[Owner]", moduleDefinition.Owner);
|
filePath = tokenReplace.ReplaceTokens(filePath);
|
||||||
filePath = filePath.Replace("[Module]", moduleDefinition.Name);
|
tokenReplace.AddSource("File", Path.GetFileName(filePath));
|
||||||
|
|
||||||
string text = System.IO.File.ReadAllText(file.FullName);
|
var text = System.IO.File.ReadAllText(file.FullName);
|
||||||
text = text.Replace("[Owner]", moduleDefinition.Owner);
|
text = tokenReplace.ReplaceTokens(text);
|
||||||
text = text.Replace("[Module]", moduleDefinition.Name);
|
|
||||||
text = text.Replace("[Description]", moduleDefinition.Description);
|
|
||||||
text = text.Replace("[RootPath]", rootPath);
|
|
||||||
text = text.Replace("[RootFolder]", rootFolder);
|
|
||||||
text = text.Replace("[ServerManagerType]", moduleDefinition.ServerManagerType);
|
|
||||||
text = text.Replace("[Folder]", folderPath);
|
|
||||||
text = text.Replace("[File]", Path.GetFileName(filePath));
|
|
||||||
if (moduleDefinition.Version == "local")
|
|
||||||
{
|
|
||||||
text = text.Replace("[FrameworkVersion]", Constants.Version);
|
|
||||||
text = text.Replace("[ClientReference]", $"<Reference Include=\"Oqtane.Client\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Client.dll</HintPath></Reference>");
|
|
||||||
text = text.Replace("[ServerReference]", $"<Reference Include=\"Oqtane.Server\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Server.dll</HintPath></Reference>");
|
|
||||||
text = text.Replace("[SharedReference]", $"<Reference Include=\"Oqtane.Shared\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Shared.dll</HintPath></Reference>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text = text.Replace("[FrameworkVersion]", moduleDefinition.Version);
|
|
||||||
text = text.Replace("[ClientReference]", "<PackageReference Include=\"Oqtane.Client\" Version=\"" + moduleDefinition.Version + "\" />");
|
|
||||||
text = text.Replace("[ServerReference]", "<PackageReference Include=\"Oqtane.Server\" Version=\"" + moduleDefinition.Version + "\" />");
|
|
||||||
text = text.Replace("[SharedReference]", "<PackageReference Include=\"Oqtane.Shared\" Version=\"" + moduleDefinition.Version + "\" />");
|
|
||||||
}
|
|
||||||
System.IO.File.WriteAllText(filePath, text);
|
System.IO.File.WriteAllText(filePath, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
DirectoryInfo[] folders = current.GetDirectories();
|
var folders = current.GetDirectories();
|
||||||
|
|
||||||
foreach (DirectoryInfo folder in folders.Reverse())
|
foreach (DirectoryInfo folder in folders.Reverse())
|
||||||
{
|
{
|
||||||
|
@ -372,5 +354,51 @@ namespace Oqtane.Controllers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ITokenReplace InitializeTokenReplace(string rootPath, string rootFolder, ModuleDefinition moduleDefinition)
|
||||||
|
{
|
||||||
|
var tokenReplace = _serviceProvider.GetService<ITokenReplace>();
|
||||||
|
tokenReplace.AddSource(() =>
|
||||||
|
{
|
||||||
|
return new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
{ "RootPath", rootPath },
|
||||||
|
{ "RootFolder", rootFolder },
|
||||||
|
{ "Owner", moduleDefinition.Owner },
|
||||||
|
{ "Module", moduleDefinition.Name },
|
||||||
|
{ "Description", moduleDefinition.Description },
|
||||||
|
{ "ServerManagerType", moduleDefinition.ServerManagerType }
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
if (moduleDefinition.Version == "local")
|
||||||
|
{
|
||||||
|
tokenReplace.AddSource(() =>
|
||||||
|
{
|
||||||
|
return new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
{ "FrameworkVersion", Constants.Version },
|
||||||
|
{ "ClientReference", $"<Reference Include=\"Oqtane.Client\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Client.dll</HintPath></Reference>" },
|
||||||
|
{ "ServerReference", $"<Reference Include=\"Oqtane.Server\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Server.dll</HintPath></Reference>" },
|
||||||
|
{ "SharedReference", $"<Reference Include=\"Oqtane.Shared\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Shared.dll</HintPath></Reference>" },
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tokenReplace.AddSource(() =>
|
||||||
|
{
|
||||||
|
return new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
{ "FrameworkVersion", moduleDefinition.Version },
|
||||||
|
{ "ClientReference", $"<PackageReference Include=\"Oqtane.Client\" Version=\"{moduleDefinition.Version}\" />" },
|
||||||
|
{ "ServerReference", $"<PackageReference Include=\"Oqtane.Client\" Version=\"{moduleDefinition.Version}\" />" },
|
||||||
|
{ "SharedReference", $"<PackageReference Include=\"Oqtane.Client\" Version=\"{moduleDefinition.Version}\" />" },
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return tokenReplace;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,8 @@ using System.Text.Json;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection.Metadata;
|
using System.Reflection.Metadata;
|
||||||
using System;
|
using System;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Oqtane.Infrastructure.Interfaces;
|
||||||
|
|
||||||
// ReSharper disable StringIndexOfIsCultureSpecific.1
|
// ReSharper disable StringIndexOfIsCultureSpecific.1
|
||||||
|
|
||||||
|
@ -29,8 +31,9 @@ namespace Oqtane.Controllers
|
||||||
private readonly ISyncManager _syncManager;
|
private readonly ISyncManager _syncManager;
|
||||||
private readonly ILogManager _logger;
|
private readonly ILogManager _logger;
|
||||||
private readonly Alias _alias;
|
private readonly Alias _alias;
|
||||||
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
public ThemeController(IThemeRepository themes, IInstallationManager installationManager, IWebHostEnvironment environment, ITenantManager tenantManager, ISyncManager syncManager, ILogManager logger)
|
public ThemeController(IThemeRepository themes, IInstallationManager installationManager, IWebHostEnvironment environment, ITenantManager tenantManager, ISyncManager syncManager, ILogManager logger, IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
_themes = themes;
|
_themes = themes;
|
||||||
_installationManager = installationManager;
|
_installationManager = installationManager;
|
||||||
|
@ -39,6 +42,7 @@ namespace Oqtane.Controllers
|
||||||
_syncManager = syncManager;
|
_syncManager = syncManager;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_alias = tenantManager.GetAlias();
|
_alias = tenantManager.GetAlias();
|
||||||
|
_serviceProvider = serviceProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: api/<controller>
|
// GET: api/<controller>
|
||||||
|
@ -208,54 +212,80 @@ namespace Oqtane.Controllers
|
||||||
|
|
||||||
private void ProcessTemplatesRecursively(DirectoryInfo current, string rootPath, string rootFolder, string templatePath, Theme theme)
|
private void ProcessTemplatesRecursively(DirectoryInfo current, string rootPath, string rootFolder, string templatePath, Theme theme)
|
||||||
{
|
{
|
||||||
|
var tokenReplace = InitializeTokenReplace(rootPath, rootFolder, theme);
|
||||||
|
|
||||||
// process folder
|
// process folder
|
||||||
string folderPath = Utilities.PathCombine(rootPath, current.FullName.Replace(templatePath, ""));
|
var folderPath = Utilities.PathCombine(rootPath, current.FullName.Replace(templatePath, ""));
|
||||||
folderPath = folderPath.Replace("[Owner]", theme.Owner);
|
folderPath = tokenReplace.ReplaceTokens(folderPath);
|
||||||
folderPath = folderPath.Replace("[Theme]", theme.Name);
|
|
||||||
if (!Directory.Exists(folderPath))
|
if (!Directory.Exists(folderPath))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(folderPath);
|
Directory.CreateDirectory(folderPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
FileInfo[] files = current.GetFiles("*.*");
|
tokenReplace.AddSource("Folder", folderPath);
|
||||||
|
var files = current.GetFiles("*.*");
|
||||||
if (files != null)
|
if (files != null)
|
||||||
{
|
{
|
||||||
foreach (FileInfo file in files)
|
foreach (FileInfo file in files)
|
||||||
{
|
{
|
||||||
// process file
|
// process file
|
||||||
string filePath = Path.Combine(folderPath, file.Name);
|
var filePath = Path.Combine(folderPath, file.Name);
|
||||||
filePath = filePath.Replace("[Owner]", theme.Owner);
|
filePath = tokenReplace.ReplaceTokens(filePath);
|
||||||
filePath = filePath.Replace("[Theme]", theme.Name);
|
tokenReplace.AddSource("File", Path.GetFileName(filePath));
|
||||||
|
|
||||||
string text = System.IO.File.ReadAllText(file.FullName);
|
var text = System.IO.File.ReadAllText(file.FullName);
|
||||||
text = text.Replace("[Owner]", theme.Owner);
|
text = tokenReplace.ReplaceTokens(text);
|
||||||
text = text.Replace("[Theme]", theme.Name);
|
|
||||||
text = text.Replace("[RootPath]", rootPath);
|
|
||||||
text = text.Replace("[RootFolder]", rootFolder);
|
|
||||||
text = text.Replace("[Folder]", folderPath);
|
|
||||||
text = text.Replace("[File]", Path.GetFileName(filePath));
|
|
||||||
if (theme.Version == "local")
|
|
||||||
{
|
|
||||||
text = text.Replace("[FrameworkVersion]", Constants.Version);
|
|
||||||
text = text.Replace("[ClientReference]", $"<Reference Include=\"Oqtane.Client\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Client.dll</HintPath></Reference>");
|
|
||||||
text = text.Replace("[SharedReference]", $"<Reference Include=\"Oqtane.Shared\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Shared.dll</HintPath></Reference>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text = text.Replace("[FrameworkVersion]", theme.Version);
|
|
||||||
text = text.Replace("[ClientReference]", "<PackageReference Include=\"Oqtane.Client\" Version=\"" + theme.Version + "\" />");
|
|
||||||
text = text.Replace("[SharedReference]", "<PackageReference Include=\"Oqtane.Shared\" Version=\"" + theme.Version + "\" />");
|
|
||||||
}
|
|
||||||
System.IO.File.WriteAllText(filePath, text);
|
System.IO.File.WriteAllText(filePath, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
DirectoryInfo[] folders = current.GetDirectories();
|
var folders = current.GetDirectories();
|
||||||
|
|
||||||
foreach (DirectoryInfo folder in folders.Reverse())
|
foreach (DirectoryInfo folder in folders.Reverse())
|
||||||
{
|
{
|
||||||
ProcessTemplatesRecursively(folder, rootPath, rootFolder, templatePath, theme);
|
ProcessTemplatesRecursively(folder, rootPath, rootFolder, templatePath, theme);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ITokenReplace InitializeTokenReplace(string rootPath, string rootFolder, Theme theme)
|
||||||
|
{
|
||||||
|
var tokenReplace = _serviceProvider.GetService<ITokenReplace>();
|
||||||
|
tokenReplace.AddSource(() =>
|
||||||
|
{
|
||||||
|
return new Dictionary<string, object>
|
||||||
|
{
|
||||||
|
{ "RootPath", rootPath },
|
||||||
|
{ "RootFolder", rootFolder },
|
||||||
|
{ "Owner", theme.Owner },
|
||||||
|
{ "Theme", theme.Name }
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
if (theme.Version == "local")
|
||||||
|
{
|
||||||
|
tokenReplace.AddSource(() =>
|
||||||
|
{
|
||||||
|
return new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
{ "FrameworkVersion", Constants.Version },
|
||||||
|
{ "ClientReference", $"<Reference Include=\"Oqtane.Client\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Client.dll</HintPath></Reference>" },
|
||||||
|
{ "SharedReference", $"<Reference Include=\"Oqtane.Shared\"><HintPath>..\\..\\{rootFolder}\\Oqtane.Server\\bin\\Debug\\net8.0\\Oqtane.Shared.dll</HintPath></Reference>" },
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tokenReplace.AddSource(() =>
|
||||||
|
{
|
||||||
|
return new Dictionary<string, object>()
|
||||||
|
{
|
||||||
|
{ "FrameworkVersion", theme.Version },
|
||||||
|
{ "ClientReference", $"<PackageReference Include=\"Oqtane.Client\" Version=\"{theme.Version}\" />" },
|
||||||
|
{ "SharedReference", $"<PackageReference Include=\"Oqtane.Client\" Version=\"{theme.Version}\" />" },
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return tokenReplace;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Oqtane.Infrastructure;
|
using Oqtane.Infrastructure;
|
||||||
|
using Oqtane.Infrastructure.Interfaces;
|
||||||
using Oqtane.Managers;
|
using Oqtane.Managers;
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
using Oqtane.Modules;
|
using Oqtane.Modules;
|
||||||
|
@ -150,6 +151,8 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
// obsolete - replaced by ITenantManager
|
// obsolete - replaced by ITenantManager
|
||||||
services.AddTransient<ITenantResolver, TenantResolver>();
|
services.AddTransient<ITenantResolver, TenantResolver>();
|
||||||
|
|
||||||
|
services.AddTransient<ITokenReplace, TokenReplace>();
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
27
Oqtane.Server/Infrastructure/Interfaces/ITokenReplace.cs
Normal file
27
Oqtane.Server/Infrastructure/Interfaces/ITokenReplace.cs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Oqtane.Interfaces;
|
||||||
|
|
||||||
|
namespace Oqtane.Infrastructure.Interfaces
|
||||||
|
{
|
||||||
|
public interface ITokenReplace
|
||||||
|
{
|
||||||
|
void AddSource(ITokenSource source);
|
||||||
|
|
||||||
|
void AddSource(Func<IDictionary<string, object>> sourceFunc);
|
||||||
|
|
||||||
|
void AddSource(IDictionary<string, object> source);
|
||||||
|
|
||||||
|
void AddSource(string key, object value);
|
||||||
|
|
||||||
|
void AddSource(string name, ITokenSource source);
|
||||||
|
|
||||||
|
void AddSource(string name, Func<IDictionary<string, object>> sourceFunc);
|
||||||
|
|
||||||
|
void AddSource(string name, IDictionary<string, object> source);
|
||||||
|
|
||||||
|
void AddSource(string name, string key, object value);
|
||||||
|
|
||||||
|
string ReplaceTokens(string source);
|
||||||
|
}
|
||||||
|
}
|
158
Oqtane.Server/Infrastructure/TokenReplace.cs
Normal file
158
Oqtane.Server/Infrastructure/TokenReplace.cs
Normal file
|
@ -0,0 +1,158 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Text;
|
||||||
|
using Oqtane.Infrastructure.Interfaces;
|
||||||
|
using Oqtane.Interfaces;
|
||||||
|
using Oqtane.Models;
|
||||||
|
|
||||||
|
namespace Oqtane.Infrastructure
|
||||||
|
{
|
||||||
|
public class TokenReplace : ITokenReplace
|
||||||
|
{
|
||||||
|
public const string GenericName = "generic";
|
||||||
|
|
||||||
|
private const string TokenExpression = "(?:(?<text>\\[\\])|\\[(?:(?<source>[^{}\\]\\[:]+):(?<key>[^\\]\\[\\|]+)|(?<key>[^\\]\\[\\|]+))(?:\\|(?:(?<format>[^\\]\\[]+)\\|(?<empty>[^\\]\\\\[]+))|\\|(?:(?<format>[^\\|\\]\\[]+)))?\\])|(?<text>\\[[^\\]\\[]+\\])|(?<text>\\[{0,1}[^\\]\\[]+\\]{0,1})";
|
||||||
|
|
||||||
|
private Regex TokenizerRegex = new Regex(TokenExpression, RegexOptions.Compiled | RegexOptions.Singleline);
|
||||||
|
private IDictionary<string, IDictionary<string, object>> _tokens;
|
||||||
|
|
||||||
|
private readonly ILogManager _logger;
|
||||||
|
|
||||||
|
public TokenReplace(ILogManager logger)
|
||||||
|
{
|
||||||
|
_tokens = new Dictionary<string, IDictionary<string, object>>();
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSource(ITokenSource source)
|
||||||
|
{
|
||||||
|
this.AddSource(GenericName, source);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSource(Func<IDictionary<string, object>> sourceFunc)
|
||||||
|
{
|
||||||
|
this.AddSource(GenericName, sourceFunc);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSource(IDictionary<string, object> source)
|
||||||
|
{
|
||||||
|
this.AddSource(GenericName, source);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSource(string key, object value)
|
||||||
|
{
|
||||||
|
this.AddSource(GenericName, key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSource(string name, ITokenSource source)
|
||||||
|
{
|
||||||
|
var tokens = source.GetTokens();
|
||||||
|
this.AddSource(name, tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSource(string name, Func<IDictionary<string, object>> sourceFunc)
|
||||||
|
{
|
||||||
|
var tokens = sourceFunc();
|
||||||
|
this.AddSource(name, tokens);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSource(string name, IDictionary<string, object> source)
|
||||||
|
{
|
||||||
|
if(source != null)
|
||||||
|
{
|
||||||
|
foreach (var key in source.Keys)
|
||||||
|
{
|
||||||
|
this.AddSource(name, key, source[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddSource(string name, string key, object value)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(name))
|
||||||
|
{
|
||||||
|
name = GenericName;
|
||||||
|
}
|
||||||
|
|
||||||
|
var source = _tokens.ContainsKey(name.ToLower()) ? _tokens[name.ToLower()] : null;
|
||||||
|
if(source == null)
|
||||||
|
{
|
||||||
|
source = new Dictionary<string, object>();
|
||||||
|
}
|
||||||
|
source[key] = value;
|
||||||
|
|
||||||
|
_tokens[name.ToLower()] = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ReplaceTokens(string source)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(source))
|
||||||
|
{
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = new StringBuilder();
|
||||||
|
foreach (Match match in this.TokenizerRegex.Matches(source))
|
||||||
|
{
|
||||||
|
var key = match.Result("${key}");
|
||||||
|
if (!string.IsNullOrWhiteSpace(key))
|
||||||
|
{
|
||||||
|
var sourceName = match.Result("${source}");
|
||||||
|
if (string.IsNullOrWhiteSpace(sourceName) || sourceName == "[")
|
||||||
|
{
|
||||||
|
sourceName = GenericName;
|
||||||
|
}
|
||||||
|
|
||||||
|
var format = match.Result("${format}");
|
||||||
|
var emptyReplacment = match.Result("${empty}");
|
||||||
|
var value = ReplaceTokenValue(sourceName, key, format);
|
||||||
|
if (string.IsNullOrWhiteSpace(value))
|
||||||
|
{
|
||||||
|
if(!string.IsNullOrWhiteSpace(emptyReplacment))
|
||||||
|
{
|
||||||
|
value = emptyReplacment;
|
||||||
|
}
|
||||||
|
else //keep the original content
|
||||||
|
{
|
||||||
|
value = match.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Append(value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.Append(match.Result("${text}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private string ReplaceTokenValue(string sourceName, string key, string format)
|
||||||
|
{
|
||||||
|
if(!_tokens.ContainsKey(sourceName.ToLower()))
|
||||||
|
{
|
||||||
|
_logger.Log(Shared.LogLevel.Debug, this, Enums.LogFunction.Other, $"MissingSource:{sourceName}");
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tokens = _tokens[sourceName.ToLower()];
|
||||||
|
if(!tokens.ContainsKey(key))
|
||||||
|
{
|
||||||
|
_logger.Log(Shared.LogLevel.Debug, this, Enums.LogFunction.Other, $"MissingKey:{key}");
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var value = tokens[key];
|
||||||
|
if(value == null)
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: need to implement the format.
|
||||||
|
return value.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
Oqtane.Shared/Interfaces/ITokenSource.cs
Normal file
13
Oqtane.Shared/Interfaces/ITokenSource.cs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Oqtane.Interfaces
|
||||||
|
{
|
||||||
|
public interface ITokenSource
|
||||||
|
{
|
||||||
|
IDictionary<string, object> GetTokens();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user