@namespace Oqtane.Modules.Admin.ModuleCreator @inherits ModuleBase @inject NavigationManager NavigationManager @inject IModuleDefinitionService ModuleDefinitionService @inject IModuleService ModuleService @inject ISystemService SystemService @inject ISettingService SettingService @inject IStringLocalizer Localizer @using System.Text.RegularExpressions @using System.IO; @if (string.IsNullOrEmpty(_moduledefinitionname) && _systeminfo != null && _templates != null) { @if (!string.IsNullOrEmpty(_location)) { }
} else { } @code { private string _moduledefinitionname = string.Empty; private string _owner = string.Empty; private string _module = string.Empty; private string _description = string.Empty; private string _template = "-"; private string _reference = Constants.Version; private string _location = string.Empty; private Dictionary _systeminfo; private List _templates; public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Host; protected override async Task OnInitializedAsync() { try { _moduledefinitionname = SettingService.GetSetting(ModuleState.Settings, "ModuleDefinitionName", ""); _systeminfo = await SystemService.GetSystemInfoAsync(); _templates = await ModuleDefinitionService.GetModuleDefinitionTemplatesAsync(); if (string.IsNullOrEmpty(_moduledefinitionname)) { AddModuleMessage(Localizer["Please Note That The Module Creator Is Only Intended To Be Used In A Development Environment"], MessageType.Info); } else { AddModuleMessage(Localizer["Once You Have Compiled The Module And Restarted The Application You Can Activate The Module Below"], MessageType.Info); } } catch (Exception ex) { await logger.LogError(ex, "Error Loading Module Creator"); } } private async Task CreateModule() { try { if (IsValid(_owner) && IsValid(_module) && _owner != _module && _template != "-") { var moduleDefinition = new ModuleDefinition { Owner = _owner, Name = _module, Description = _description, Template = _template, Version = _reference }; moduleDefinition = await ModuleDefinitionService.CreateModuleDefinitionAsync(moduleDefinition); var settings = ModuleState.Settings; SettingService.SetSetting(settings, "ModuleDefinitionName", moduleDefinition.ModuleDefinitionName); await SettingService.UpdateModuleSettingsAsync(settings, ModuleState.ModuleId); GetLocation(); AddModuleMessage(Localizer["The Source Code For Your Module Has Been Created At The Location Specified Below And Must Be Compiled In Order To Make It Functional. Once It Has Been Compiled You Must Restart Your Application To Apply These Changes.", NavigateUrl("admin/system")], MessageType.Success); } else { AddModuleMessage(Localizer["You Must Provide A Valid Owner Name And Module Name ( ie. No Punctuation Or Spaces And The Values Cannot Be The Same ) And Choose A Template"], MessageType.Warning); } } catch (Exception ex) { await logger.LogError(ex, "Error Creating Module"); } } private async Task ActivateModule() { try { if (!string.IsNullOrEmpty(_moduledefinitionname)) { Module module = await ModuleService.GetModuleAsync(ModuleState.ModuleId); module.ModuleDefinitionName = _moduledefinitionname; await ModuleService.UpdateModuleAsync(module); ClearModuleMessage(); NavigationManager.NavigateTo(NavigateUrl()); } } catch (Exception ex) { await logger.LogError(ex, "Error Activating Module"); } } private bool IsValid(string name) { // must contain letters, underscores and digits and first character must be letter or underscore return !string.IsNullOrEmpty(name) && Regex.IsMatch(name, "^[A-Za-z_][A-Za-z0-9_]*$"); } private void TemplateChanged(ChangeEventArgs e) { _template = (string)e.Value; GetLocation(); } private void GetLocation() { _location = string.Empty; if (_template != "-" && _systeminfo != null && _systeminfo.ContainsKey("serverpath")) { string[] path = _systeminfo["serverpath"].Split(Path.DirectorySeparatorChar); _location = string.Join(Path.DirectorySeparatorChar, path, 0, path.Length - 2) + Path.DirectorySeparatorChar + _owner + "." + _module; } StateHasChanged(); } }