From a857e3f31e816b6eda9d11d9abe8fe4e6b9650f9 Mon Sep 17 00:00:00 2001 From: sbwalker Date: Mon, 28 Aug 2023 09:03:05 -0400 Subject: [PATCH] abstract namespace specification to template.json so that module and theme templates can use their own naming conventions --- .../Admin/ModuleDefinitions/Create.razor | 17 ++++++++++++----- .../Modules/Admin/Themes/Create.razor | 13 ++++++++++--- .../Controllers/ModuleDefinitionController.cs | 19 ++++++++++++++----- Oqtane.Server/Controllers/ThemeController.cs | 13 +++++++++++-- .../Modules/Templates/External/template.json | 3 ++- .../Themes/Templates/External/template.json | 3 ++- Oqtane.Shared/Models/Template.cs | 5 +++++ 7 files changed, 56 insertions(+), 17 deletions(-) diff --git a/Oqtane.Client/Modules/Admin/ModuleDefinitions/Create.razor b/Oqtane.Client/Modules/Admin/ModuleDefinitions/Create.razor index baada16b..40a9b1e2 100644 --- a/Oqtane.Client/Modules/Admin/ModuleDefinitions/Create.razor +++ b/Oqtane.Client/Modules/Admin/ModuleDefinitions/Create.razor @@ -115,7 +115,8 @@ { if (IsValid(_owner) && IsValid(_module) && _owner != _module && _template != "-") { - var moduleDefinition = new ModuleDefinition { Owner = _owner, Name = _module, Description = _description, Template = _template, Version = _reference }; + var template = _templates.FirstOrDefault(item => item.Name == _template); + var moduleDefinition = new ModuleDefinition { Owner = _owner, Name = _module, Description = _description, Template = _template, Version = _reference, ModuleDefinitionName = template.Namespace }; moduleDefinition = await ModuleDefinitionService.CreateModuleDefinitionAsync(moduleDefinition); GetLocation(); AddModuleMessage(string.Format(Localizer["Success.Module.Create"], NavigateUrl("admin/system")), MessageType.Success); @@ -139,7 +140,7 @@ private bool IsValid(string name) { // must contain letters, underscores and digits and first character must be letter or underscore - return !string.IsNullOrEmpty(name) && name.ToLower() != "module" && !name.ToLower().Contains("oqtane") && Regex.IsMatch(name, "^[A-Za-z_][A-Za-z0-9_]*$"); + return !string.IsNullOrEmpty(name) && name.ToLower() != "module" && !name.ToLower().Contains("oqtane") && Regex.IsMatch(name, "^[A-Za-z_][A-Za-z0-9_]*$"); } private void TemplateChanged(ChangeEventArgs e) @@ -151,7 +152,7 @@ var template = _templates.FirstOrDefault(item => item.Name == _template); _minversion = template.Version; } - GetLocation(); + GetLocation(); } private void GetLocation() @@ -160,8 +161,14 @@ if (_owner != "" && _module != "" && _template != "-") { var template = _templates.FirstOrDefault(item => item.Name == _template); - _location = template.Location + _owner + ".Module." + _module; - + if (!string.IsNullOrEmpty(template.Namespace)) + { + _location = template.Location + template.Namespace.Replace("[Owner]", _owner).Replace("[Module]", _module); + } + else + { + _location = template.Location + _owner + ".Module." + _module; + } } StateHasChanged(); } diff --git a/Oqtane.Client/Modules/Admin/Themes/Create.razor b/Oqtane.Client/Modules/Admin/Themes/Create.razor index b318026d..04fa0049 100644 --- a/Oqtane.Client/Modules/Admin/Themes/Create.razor +++ b/Oqtane.Client/Modules/Admin/Themes/Create.razor @@ -102,7 +102,8 @@ { if (IsValid(_owner) && IsValid(_theme) && _owner != _theme && _template != "-") { - var theme = new Theme { Owner = _owner, Name = _theme, Template = _template, Version = _reference }; + var template = _templates.FirstOrDefault(item => item.Name == _template); + var theme = new Theme { Owner = _owner, Name = _theme, Template = _template, Version = _reference, ThemeName = template.Namespace }; theme = await ThemeService.CreateThemeAsync(theme); GetLocation(); AddModuleMessage(string.Format(Localizer["Success.Theme.Create"], NavigateUrl("admin/system")), MessageType.Success); @@ -142,8 +143,14 @@ if (_owner != "" && _theme != "" && _template != "-") { var template = _templates.FirstOrDefault(item => item.Name == _template); - _location = template.Location + _owner + ".Theme." + _theme; - + if (!string.IsNullOrEmpty(template.Namespace)) + { + _location = template.Location + template.Namespace.Replace("[Owner]", _owner).Replace("[Theme]", _theme); + } + else + { + _location = template.Location + _owner + ".Theme." + _theme; + } } StateHasChanged(); } diff --git a/Oqtane.Server/Controllers/ModuleDefinitionController.cs b/Oqtane.Server/Controllers/ModuleDefinitionController.cs index 95380bef..314ec0e3 100644 --- a/Oqtane.Server/Controllers/ModuleDefinitionController.cs +++ b/Oqtane.Server/Controllers/ModuleDefinitionController.cs @@ -127,17 +127,26 @@ namespace Oqtane.Controllers DirectoryInfo rootFolder = Directory.GetParent(_environment.ContentRootPath); string templatePath = Utilities.PathCombine(_environment.WebRootPath, "Modules", "Templates", moduleDefinition.Template, Path.DirectorySeparatorChar.ToString()); + if (!string.IsNullOrEmpty(moduleDefinition.ModuleDefinitionName)) + { + moduleDefinition.ModuleDefinitionName = moduleDefinition.ModuleDefinitionName.Replace("[Owner]", moduleDefinition.Owner).Replace("[Module]", moduleDefinition.Name); + } + else + { + moduleDefinition.ModuleDefinitionName = moduleDefinition.Owner + ".Module." + moduleDefinition.Name; + } + if (moduleDefinition.Template.ToLower().Contains("internal")) { rootPath = Utilities.PathCombine(rootFolder.FullName, Path.DirectorySeparatorChar.ToString()); - moduleDefinition.ModuleDefinitionName = moduleDefinition.Owner + ".Module." + moduleDefinition.Name + ", Oqtane.Client"; - moduleDefinition.ServerManagerType = moduleDefinition.Owner + ".Module." + moduleDefinition.Name + ".Manager." + moduleDefinition.Name + "Manager, Oqtane.Server"; + moduleDefinition.ModuleDefinitionName = moduleDefinition.ModuleDefinitionName + ", Oqtane.Client"; + moduleDefinition.ServerManagerType = moduleDefinition.ModuleDefinitionName + ".Manager." + moduleDefinition.Name + "Manager, Oqtane.Server"; } else { rootPath = Utilities.PathCombine(rootFolder.Parent.FullName, moduleDefinition.Owner + ".Module." + moduleDefinition.Name, Path.DirectorySeparatorChar.ToString()); - moduleDefinition.ModuleDefinitionName = moduleDefinition.Owner + ".Module." + moduleDefinition.Name + ", " + moduleDefinition.Owner + ".Module." + moduleDefinition.Name + ".Client.Oqtane"; - moduleDefinition.ServerManagerType = moduleDefinition.Owner + ".Module." + moduleDefinition.Name + ".Manager." + moduleDefinition.Name + "Manager, " + moduleDefinition.Owner + ".Module." + moduleDefinition.Name + ".Server.Oqtane"; + moduleDefinition.ModuleDefinitionName = moduleDefinition.ModuleDefinitionName + ", " + moduleDefinition.ModuleDefinitionName + ".Client.Oqtane"; + moduleDefinition.ServerManagerType = moduleDefinition.ModuleDefinitionName + ".Manager." + moduleDefinition.Name + "Manager, " + moduleDefinition.ModuleDefinitionName + ".Server.Oqtane"; } ProcessTemplatesRecursively(new DirectoryInfo(templatePath), rootPath, rootFolder.Name, templatePath, moduleDefinition); @@ -301,7 +310,7 @@ namespace Oqtane.Controllers } else { - templates.Add(new Template { Name = name, Title = name, Type = "External", Version = "", Location = Utilities.PathCombine(root.Parent.ToString(), Path.DirectorySeparatorChar.ToString()) }); + templates.Add(new Template { Name = name, Title = name, Type = "External", Version = "", Namespace = "", Location = Utilities.PathCombine(root.Parent.ToString(), Path.DirectorySeparatorChar.ToString()) }); } } } diff --git a/Oqtane.Server/Controllers/ThemeController.cs b/Oqtane.Server/Controllers/ThemeController.cs index 5135ef22..cec332fe 100644 --- a/Oqtane.Server/Controllers/ThemeController.cs +++ b/Oqtane.Server/Controllers/ThemeController.cs @@ -173,15 +173,24 @@ namespace Oqtane.Controllers DirectoryInfo rootFolder = Directory.GetParent(_environment.ContentRootPath); string templatePath = Utilities.PathCombine(_environment.WebRootPath, "Themes", "Templates", theme.Template, Path.DirectorySeparatorChar.ToString()); + if (!string.IsNullOrEmpty(theme.ThemeName)) + { + theme.ThemeName = theme.ThemeName.Replace("[Owner]", theme.Owner).Replace("[Theme]", theme.Name); + } + else + { + theme.ThemeName = theme.Owner + ".Theme." + theme.Name; + } + if (theme.Template.ToLower().Contains("internal")) { rootPath = Utilities.PathCombine(rootFolder.FullName, Path.DirectorySeparatorChar.ToString()); - theme.ThemeName = theme.Owner + ".Theme." + theme.Name + ", Oqtane.Client"; + theme.ThemeName = theme.ThemeName + ", Oqtane.Client"; } else { rootPath = Utilities.PathCombine(rootFolder.Parent.FullName, theme.Owner + ".Theme." + theme.Name, Path.DirectorySeparatorChar.ToString()); - theme.ThemeName = theme.Owner + ".Theme." + theme.Name + ", " + theme.Owner + ".Theme." + theme.Name + ".Client.Oqtane"; + theme.ThemeName = theme.ThemeName + ", " + theme.ThemeName + ".Client.Oqtane"; } ProcessTemplatesRecursively(new DirectoryInfo(templatePath), rootPath, rootFolder.Name, templatePath, theme); diff --git a/Oqtane.Server/wwwroot/Modules/Templates/External/template.json b/Oqtane.Server/wwwroot/Modules/Templates/External/template.json index 777da289..493c825e 100644 --- a/Oqtane.Server/wwwroot/Modules/Templates/External/template.json +++ b/Oqtane.Server/wwwroot/Modules/Templates/External/template.json @@ -1,5 +1,6 @@ { "Title": "Default Module Template", "Type": "External", - "Version": "4.0.2" + "Version": "4.0.3", + "Namespace": "[Owner].Module.[Module]" } diff --git a/Oqtane.Server/wwwroot/Themes/Templates/External/template.json b/Oqtane.Server/wwwroot/Themes/Templates/External/template.json index 04ee5175..7c3b80bb 100644 --- a/Oqtane.Server/wwwroot/Themes/Templates/External/template.json +++ b/Oqtane.Server/wwwroot/Themes/Templates/External/template.json @@ -1,5 +1,6 @@ { "Title": "Default Theme Template", "Type": "External", - "Version": "4.0.0" + "Version": "4.0.3", + "Namespace": "[Owner].Theme.[Theme]" } diff --git a/Oqtane.Shared/Models/Template.cs b/Oqtane.Shared/Models/Template.cs index 12859fff..9d667ec2 100644 --- a/Oqtane.Shared/Models/Template.cs +++ b/Oqtane.Shared/Models/Template.cs @@ -25,6 +25,11 @@ namespace Oqtane.Models /// public string Version { get; set; } + /// + /// namespace (uses tokens) + /// + public string Namespace { get; set; } + /// /// location where template will be created (dynamically set) ///