diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs index 535b68ef59..ba85c48aeb 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs @@ -22,6 +22,7 @@ using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus.Local; using Volo.Abp.Json; +using System.Text.RegularExpressions; namespace Volo.Abp.Cli.ProjectModification; @@ -114,12 +115,11 @@ public class SolutionModuleAdder : ITransientDependency var projectFiles = ProjectFinder.GetProjectFiles(solutionFile); await AddNugetAndNpmReferences(module, projectFiles, !(newTemplate || newProTemplate)); - + var modulesFolderInSolution = Path.Combine(Path.GetDirectoryName(solutionFile), "modules"); if (withSourceCode || newTemplate || newProTemplate) { - await PublishEventAsync(5, $"Downloading source code of {moduleName}"); await DownloadSourceCodesToSolutionFolder(module, modulesFolderInSolution, version, newTemplate, newProTemplate); @@ -147,6 +147,8 @@ public class SolutionModuleAdder : ITransientDependency else { await AddAngularPackages(solutionFile, module); + + await TryConfigureModuleConfigurationsForAngular(solutionFile, module); } await RunBundleForBlazorAsync(projectFiles, module); @@ -167,10 +169,83 @@ public class SolutionModuleAdder : ITransientDependency return module; } + private async Task TryConfigureModuleConfigurationsForAngular(string solutionFilePath, ModuleWithMastersInfo module) + { + var angularPath = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(solutionFilePath)), "angular"); + + if (!Directory.Exists(angularPath)) + { + return; + } + + var angularPackages = module.NpmPackages? + .Where(p => p.ApplicationType.HasFlag(NpmApplicationType.Angular)) + .ToList(); + + if (!angularPackages.Any()) + { + return; + } + + await PublishEventAsync(6, "Configuring angular projects..."); + + var moduleName = module.Name.Split('.').Last(); + + ConfigureAngularPackagesForAppModuleFile(angularPath, angularPackages, moduleName); + + ConfigureAngularPackagesForAppRoutingModuleFile(angularPath, angularPackages, moduleName); + } + + private void ConfigureAngularPackagesForAppModuleFile(string angularPath, List angularPackages, string moduleName) + { + var appModulePath = Path.Combine(angularPath, "src", "app", "app.module.ts"); + if (!File.Exists(appModulePath)) + { + return; + } + + var appModuleFileContent = File.ReadAllText(appModulePath); + + foreach (var angularPackage in angularPackages) + { + var moduleNameAsConfigPath = angularPackage.Name.EnsureStartsWith('@').EnsureEndsWith('/') + "config"; + + appModuleFileContent = "import { " + moduleName + "ConfigModule } from '" + moduleNameAsConfigPath + "';" + Environment.NewLine + appModuleFileContent; + appModuleFileContent = Regex.Replace(appModuleFileContent, "imports\\s*:\\s*\\[", + "imports: [" + Environment.NewLine + + " " + moduleName + "ConfigModule.forRoot(),"); + } + + File.WriteAllText(appModulePath, appModuleFileContent); + } + + private void ConfigureAngularPackagesForAppRoutingModuleFile(string angularPath, List angularPackages, string moduleName) + { + var appRoutingModulePath = Path.Combine(angularPath, "src", "app", "app-routing.module.ts"); + if (!File.Exists(appRoutingModulePath)) + { + return; + } + + var appRoutingModuleFileContent = File.ReadAllText(appRoutingModulePath); + + foreach (var angularPackage in angularPackages) + { + appRoutingModuleFileContent = Regex.Replace(appRoutingModuleFileContent, "Routes\\s*=\\s*\\[", + "Routes = [" + Environment.NewLine + + " " + "{" + Environment.NewLine + + " " + "path: '" + moduleName.ToLower() + "'," + Environment.NewLine + + " " + "loadChildren: () => " + $"import('{angularPackage.Name.EnsureStartsWith('@')}').then(m => m.{moduleName}Module.forLazy())," + Environment.NewLine + + " " + "},"); + } + + File.WriteAllText(appRoutingModulePath, appRoutingModuleFileContent); + } + private async Task SetLeptonXAbpVersionsAsync(string solutionFile, string combine) { var abpVersion = SolutionPackageVersionFinder.FindByCsprojVersion(solutionFile); - + var projects = Directory.GetFiles(Path.GetDirectoryName(solutionFile)!, "*.csproj", SearchOption.AllDirectories); foreach (var project in projects) @@ -183,7 +258,8 @@ public class SolutionModuleAdder : ITransientDependency private async Task PublishEventAsync(int currentStep, string message) { - await LocalEventBus.PublishAsync(new ModuleInstallingProgressEvent { + await LocalEventBus.PublishAsync(new ModuleInstallingProgressEvent + { CurrentStep = currentStep, Message = message }, false); @@ -562,7 +638,7 @@ public class SolutionModuleAdder : ITransientDependency if (webPackagesWillBeAddedToBlazorServerProject) { - if ( nugetTarget == NuGetPackageTarget.Web) + if (nugetTarget == NuGetPackageTarget.Web) { nugetTarget = NuGetPackageTarget.BlazorServer; } @@ -636,7 +712,7 @@ public class SolutionModuleAdder : ITransientDependency } var dbMigrationsProject = projectFiles.FirstOrDefault(p => p.EndsWith(".DbMigrations.csproj")) - ?? projectFiles.FirstOrDefault(p => p.EndsWith(".EntityFrameworkCore.csproj")) ; + ?? projectFiles.FirstOrDefault(p => p.EndsWith(".EntityFrameworkCore.csproj")); if (dbMigrationsProject == null) {