Skip to content

Commit

Permalink
Merge pull request #3834 from zyhfish/task/token-replace-service
Browse files Browse the repository at this point in the history
Fix #3833: introduce token replace class.
  • Loading branch information
sbwalker authored Feb 21, 2024
2 parents 85e289f + 77ce311 commit 9974095
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 60 deletions.
90 changes: 59 additions & 31 deletions Oqtane.Server/Controllers/ModuleDefinitionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Text.Json;
using System.Net;
using Oqtane.Modules;
using Oqtane.Infrastructure.Interfaces;

namespace Oqtane.Controllers
{
Expand Down Expand Up @@ -319,58 +320,85 @@ public List<Template> GetTemplates()

private void ProcessTemplatesRecursively(DirectoryInfo current, string rootPath, string rootFolder, string templatePath, ModuleDefinition moduleDefinition)
{
var tokenReplace = InitializeTokenReplace(rootPath, rootFolder, moduleDefinition);

// process folder
string folderPath = Utilities.PathCombine(rootPath, current.FullName.Replace(templatePath, ""));
folderPath = folderPath.Replace("[Owner]", moduleDefinition.Owner);
folderPath = folderPath.Replace("[Module]", moduleDefinition.Name);
var folderPath = Utilities.PathCombine(rootPath, current.FullName.Replace(templatePath, ""));
folderPath = tokenReplace.ReplaceTokens(folderPath);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}

FileInfo[] files = current.GetFiles("*.*");
tokenReplace.AddSource("Folder", folderPath);
var files = current.GetFiles("*.*");
if (files != null)
{
foreach (FileInfo file in files)
{
// process file
string filePath = Path.Combine(folderPath, file.Name);
filePath = filePath.Replace("[Owner]", moduleDefinition.Owner);
filePath = filePath.Replace("[Module]", moduleDefinition.Name);
var filePath = Path.Combine(folderPath, file.Name);
filePath = tokenReplace.ReplaceTokens(filePath);
tokenReplace.AddSource("File", Path.GetFileName(filePath));

string text = System.IO.File.ReadAllText(file.FullName);
text = text.Replace("[Owner]", moduleDefinition.Owner);
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 + "\" />");
}
var text = System.IO.File.ReadAllText(file.FullName);
text = tokenReplace.ReplaceTokens(text);
System.IO.File.WriteAllText(filePath, text);
}

DirectoryInfo[] folders = current.GetDirectories();
var folders = current.GetDirectories();

foreach (DirectoryInfo folder in folders.Reverse())
{
ProcessTemplatesRecursively(folder, rootPath, rootFolder, templatePath, moduleDefinition);
}
}
}

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;
}
}
}
88 changes: 59 additions & 29 deletions Oqtane.Server/Controllers/ThemeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using System.Net;
using System.Reflection.Metadata;
using System;
using Microsoft.Extensions.DependencyInjection;
using Oqtane.Infrastructure.Interfaces;

// ReSharper disable StringIndexOfIsCultureSpecific.1

Expand All @@ -29,8 +31,9 @@ public class ThemeController : Controller
private readonly ISyncManager _syncManager;
private readonly ILogManager _logger;
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;
_installationManager = installationManager;
Expand All @@ -39,6 +42,7 @@ public ThemeController(IThemeRepository themes, IInstallationManager installatio
_syncManager = syncManager;
_logger = logger;
_alias = tenantManager.GetAlias();
_serviceProvider = serviceProvider;
}

// GET: api/<controller>
Expand Down Expand Up @@ -208,54 +212,80 @@ public Theme Post([FromBody] Theme theme)

private void ProcessTemplatesRecursively(DirectoryInfo current, string rootPath, string rootFolder, string templatePath, Theme theme)
{
var tokenReplace = InitializeTokenReplace(rootPath, rootFolder, theme);

// process folder
string folderPath = Utilities.PathCombine(rootPath, current.FullName.Replace(templatePath, ""));
folderPath = folderPath.Replace("[Owner]", theme.Owner);
folderPath = folderPath.Replace("[Theme]", theme.Name);
var folderPath = Utilities.PathCombine(rootPath, current.FullName.Replace(templatePath, ""));
folderPath = tokenReplace.ReplaceTokens(folderPath);
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}

FileInfo[] files = current.GetFiles("*.*");
tokenReplace.AddSource("Folder", folderPath);
var files = current.GetFiles("*.*");
if (files != null)
{
foreach (FileInfo file in files)
{
// process file
string filePath = Path.Combine(folderPath, file.Name);
filePath = filePath.Replace("[Owner]", theme.Owner);
filePath = filePath.Replace("[Theme]", theme.Name);
var filePath = Path.Combine(folderPath, file.Name);
filePath = tokenReplace.ReplaceTokens(filePath);
tokenReplace.AddSource("File", Path.GetFileName(filePath));

string text = System.IO.File.ReadAllText(file.FullName);
text = text.Replace("[Owner]", theme.Owner);
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 + "\" />");
}
var text = System.IO.File.ReadAllText(file.FullName);
text = tokenReplace.ReplaceTokens(text);
System.IO.File.WriteAllText(filePath, text);
}

DirectoryInfo[] folders = current.GetDirectories();

var folders = current.GetDirectories();
foreach (DirectoryInfo folder in folders.Reverse())
{
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;
}
}
}
3 changes: 3 additions & 0 deletions Oqtane.Server/Extensions/OqtaneServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Oqtane.Infrastructure;
using Oqtane.Infrastructure.Interfaces;
using Oqtane.Managers;
using Oqtane.Models;
using Oqtane.Modules;
Expand Down Expand Up @@ -150,6 +151,8 @@ internal static IServiceCollection AddOqtaneTransientServices(this IServiceColle
// obsolete - replaced by ITenantManager
services.AddTransient<ITenantResolver, TenantResolver>();

services.AddTransient<ITokenReplace, TokenReplace>();

return services;
}

Expand Down
27 changes: 27 additions & 0 deletions Oqtane.Server/Infrastructure/Interfaces/ITokenReplace.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 9974095

Please sign in to comment.