From 8d7b4901270533c3c45b0ce23565e137341b92f5 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 15 May 2019 15:42:49 +0300 Subject: [PATCH 1/6] CLI can configure Entity Framework after installing a module Resolves https://github.com/abpframework/abp/issues/1122 --- .../Volo/Abp/Cli/Commands/AddModuleCommand.cs | 8 ++- .../DbContextFileBuilderConfigureAdder.cs | 53 +++++++++++++++++++ .../EfCoreMigrationAdder.cs | 48 +++++++++++++++++ .../Abp/Cli/ProjectModification/ModuleInfo.cs | 2 + .../SolutionModuleAdder.cs | 48 ++++++++++++++++- 5 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs index 5b07f7f364..f48e2f4542 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs @@ -30,9 +30,13 @@ namespace Volo.Abp.Cli.Commands throw new CliUsageException("Module name is missing!" + Environment.NewLine + Environment.NewLine + GetUsageInfo()); } + var skipDbMigrations = Convert.ToBoolean( + commandLineArgs.Options.GetOrNull(Options.Solution.SkipDbMigrations) ?? "false"); + await SolutionModuleAdder.AddAsync( GetSolutionFile(commandLineArgs), - commandLineArgs.Target + commandLineArgs.Target, + skipDbMigrations ); } @@ -93,6 +97,7 @@ namespace Volo.Abp.Cli.Commands sb.AppendLine("Examples:"); sb.AppendLine(" abp add-module Volo.Blogging Adds the module to the current soluton."); sb.AppendLine(" abp add-module Volo.Blogging -s Acme.BookStore Adds the module to the given soluton."); + sb.AppendLine(" abp add-module Volo.Blogging -s Acme.BookStore --SkipDbMigrations false Adds the module to the given soluton but doesn't add-migration."); sb.AppendLine(""); return sb.ToString(); @@ -104,6 +109,7 @@ namespace Volo.Abp.Cli.Commands { public const string Short = "s"; public const string Long = "solution"; + public const string SkipDbMigrations = "skip-db-migrations"; } } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs new file mode 100644 index 0000000000..8f4d48adfa --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ProjectModification +{ + public class DbContextFileBuilderConfigureAdder : ITransientDependency + { + public void Add(string path, string moduleConfiguration) + { + var file = File.ReadAllText(path); + + var indexToInsert = FindIndexToInsert(file); + var stringToAdd = GetLineToAdd(moduleConfiguration); + + stringToAdd = " " + stringToAdd + Environment.NewLine; + + file = file.Insert(indexToInsert, stringToAdd); + + File.WriteAllText(path, file); + } + + protected int FindIndexToInsert(string file) + { + var indexOfMethodDeclaration = file.IndexOf("OnModelCreating(", StringComparison.Ordinal); + var indexOfOpeningBracket = indexOfMethodDeclaration + file.Substring(indexOfMethodDeclaration).IndexOf('{'); + + var stack = 1; + var index = indexOfOpeningBracket; + + while (stack > 0 || index < file.Length) + { + index++; + if (file[index] == '{') + { + stack++; + } + else if (file[index] == '}') + { + stack--; + } + } + return index; + } + + protected string GetLineToAdd(string moduleConfiguration) + { + return "builder.Configure" + moduleConfiguration + "();"; + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs new file mode 100644 index 0000000000..f22e6107b4 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ProjectModification +{ + public class EfCoreMigrationAdder : ITransientDependency + { + public void AddMigration(string csprojFile, string module, bool updateDatabase = true) + { + var moduleName = ParseModuleName(module); + var migrationName = "Added_" + moduleName + "_Module" + GetUniquePostFix(); + + var process = Process.Start("CMD.exe", "/C cd \"" + csprojFile + "\" & dotnet ef migrations add " + migrationName); + process.WaitForExit(); + + if (updateDatabase) + { + UpdateDatabase(csprojFile); + } + } + + protected void UpdateDatabase(string csprojFile) + { + var process = Process.Start("CMD.exe", "/C cd \"" + csprojFile + "\" & dotnet ef database update"); + process.WaitForExit(); + } + + protected virtual string ParseModuleName(string fullModuleName) + { + var words = fullModuleName?.Split('.'); + + if (words == null || words.Length <= 1) + { + return ""; + } + + return words[words.Length - 1]; + } + + protected virtual string GetUniquePostFix() + { + return "_" + new Random().Next(1,99999); + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleInfo.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleInfo.cs index 08c70eeede..d5e7a4e15a 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleInfo.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleInfo.cs @@ -8,6 +8,8 @@ namespace Volo.Abp.Cli.ProjectModification public string DisplayName { get; set; } + public string EfCoreConfigureMethodName { get; set; } + public List NugetPackages { get; set; } public List NpmPackages { get; set; } 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 917a7bf1a0..6048338b10 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 @@ -19,22 +19,32 @@ namespace Volo.Abp.Cli.ProjectModification protected IJsonSerializer JsonSerializer { get; } protected ProjectNugetPackageAdder ProjectNugetPackageAdder { get; } + protected DbContextFileBuilderConfigureAdder DbContextFileBuilderConfigureAdder { get; } + protected EfCoreMigrationAdder EfCoreMigrationAdder { get; } protected ProjectNpmPackageAdder ProjectNpmPackageAdder { get; } + protected DerivedClassFinder DerivedClassFinder { get; } public SolutionModuleAdder( IJsonSerializer jsonSerializer, ProjectNugetPackageAdder projectNugetPackageAdder, + DbContextFileBuilderConfigureAdder dbContextFileBuilderConfigureAdder, + EfCoreMigrationAdder efCoreMigrationAdder, + DerivedClassFinder derivedClassFinder, ProjectNpmPackageAdder projectNpmPackageAdder) { + EfCoreMigrationAdder = efCoreMigrationAdder; + DerivedClassFinder = derivedClassFinder; JsonSerializer = jsonSerializer; ProjectNugetPackageAdder = projectNugetPackageAdder; + DbContextFileBuilderConfigureAdder = dbContextFileBuilderConfigureAdder; ProjectNpmPackageAdder = projectNpmPackageAdder; Logger = NullLogger.Instance; } public virtual async Task AddAsync( [NotNull] string solutionFile, - [NotNull] string moduleName) + [NotNull] string moduleName, + bool skipDbMigrations = false) { Check.NotNull(solutionFile, nameof(solutionFile)); Check.NotNull(moduleName, nameof(moduleName)); @@ -75,8 +85,42 @@ namespace Volo.Abp.Cli.ProjectModification Logger.LogDebug("Target project is not available for NPM packages."); } } + + ModifyDbContext(projectFiles, module, skipDbMigrations); + } + + protected void ModifyDbContext(string[] projectFiles, ModuleInfo module, bool skipDbMigrations = false) + { + if (string.IsNullOrWhiteSpace(module.EfCoreConfigureMethodName)) + { + return; + } + + var dbMigrationsProject = projectFiles.FirstOrDefault(p => p.EndsWith(".DbMigrations.csproj")); + + if (dbMigrationsProject == null) + { + Logger.LogDebug("Solution doesn't have a \".DbMigrations\" project."); + return; + } + + var dbContextFile = DerivedClassFinder.Find(dbMigrationsProject, "AbpDbContext").FirstOrDefault(); + + if (dbContextFile == null) + { + Logger.LogDebug($"{dbMigrationsProject} project doesn't have a class that is derived from \"AbpDbContext\"."); + return; + } + + DbContextFileBuilderConfigureAdder.Add(dbContextFile, module.EfCoreConfigureMethodName); + + + if (!skipDbMigrations) + { + EfCoreMigrationAdder.AddMigration(dbMigrationsProject, module.Name); + } } - + protected virtual async Task FindModuleInfoAsync(string moduleName) { using (var client = new HttpClient()) From aaee2ef41fd3c551898bc3415ee2c26bbc49ab85 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 16 May 2019 10:38:21 +0300 Subject: [PATCH 2/6] CLI can configure Entity Framework after installing a module cont. --- .../DbContextFileBuilderConfigureAdder.cs | 45 +++++++++++-- .../ProjectModification/DerivedClassFinder.cs | 55 ++++++++++++++-- .../ModuleClassDependcyAdder.cs | 44 +++---------- .../SolutionModuleAdder.cs | 2 +- .../UsingStatementAdder.cs | 65 +++++++++++++++++++ 5 files changed, 160 insertions(+), 51 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs index 8f4d48adfa..b8af0ed471 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs @@ -1,23 +1,42 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Text; +using System.Linq; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.ProjectModification { public class DbContextFileBuilderConfigureAdder : ITransientDependency { + protected UsingStatementAdder UsingStatementAdder { get; } + public ILogger Logger { get; set; } + + public DbContextFileBuilderConfigureAdder(UsingStatementAdder usingStatementAdder) + { + UsingStatementAdder = usingStatementAdder; + Logger = NullLogger.Instance; + } + public void Add(string path, string moduleConfiguration) { var file = File.ReadAllText(path); - var indexToInsert = FindIndexToInsert(file); + file = UsingStatementAdder.Add(file, GetNamespace(moduleConfiguration)); + var stringToAdd = GetLineToAdd(moduleConfiguration); + if (!file.Contains(stringToAdd)) + { + var indexToInsert = FindIndexToInsert(file); - stringToAdd = " " + stringToAdd + Environment.NewLine; + if (indexToInsert <= 0 || indexToInsert >= file.Length) + { + Logger.LogWarning($"\"OnModelCreating(ModelBuilder builder)\" method couldn't be found in {path}"); + return; + } + file = file.Insert(indexToInsert, " " + stringToAdd + Environment.NewLine + " "); + } - file = file.Insert(indexToInsert, stringToAdd); File.WriteAllText(path, file); } @@ -30,9 +49,15 @@ namespace Volo.Abp.Cli.ProjectModification var stack = 1; var index = indexOfOpeningBracket; - while (stack > 0 || index < file.Length) + while (stack > 0) { index++; + + if (index >= file.Length) + { + break; + } + if (file[index] == '{') { stack++; @@ -42,12 +67,18 @@ namespace Volo.Abp.Cli.ProjectModification stack--; } } + return index; } protected string GetLineToAdd(string moduleConfiguration) { - return "builder.Configure" + moduleConfiguration + "();"; + return "builder." + moduleConfiguration.Split('.').Last() + "();"; + } + + protected string GetNamespace(string moduleConfiguration) + { + return string.Join(".", moduleConfiguration.Split('.').Reverse().Skip(2)); } } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DerivedClassFinder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DerivedClassFinder.cs index bf11e0af65..db39a660ce 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DerivedClassFinder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DerivedClassFinder.cs @@ -1,42 +1,83 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.ProjectModification { public class DerivedClassFinder : ITransientDependency { + public ILogger Logger { get; set; } + + public DerivedClassFinder() + { + Logger = NullLogger.Instance; + } + public virtual List Find(string csprojFilePath, string baseClass) { var moduleFilePaths = new List(); + var csprojFileDirectory = Path.GetDirectoryName(csprojFilePath); + var binFile = Path.Combine(csprojFileDirectory, "bin"); + var objFile = Path.Combine(csprojFileDirectory, "obj"); + - var csFiles = new DirectoryInfo(Path.GetDirectoryName(csprojFilePath)) + var csFiles = new DirectoryInfo(csprojFileDirectory) .GetFiles("*.cs", SearchOption.AllDirectories) + .Where(f => !f.DirectoryName.StartsWith(binFile)) + .Where(f => !f.DirectoryName.StartsWith(objFile)) .Select(f => f.FullName) .ToList(); foreach (var csFile in csFiles) { - if (IsDerivedFromAbpModule(csFile, baseClass)) + try { - moduleFilePaths.Add(csFile); + if (IsDerived(csFile, baseClass)) + { + moduleFilePaths.Add(csFile); + } + } + catch (Exception) + { + Logger.LogDebug($"Couldn't parse {csFile}."); } } return moduleFilePaths; } - protected bool IsDerivedFromAbpModule(string csFile, string baseClass) + protected bool IsDerived(string csFile, string baseClass) { + Logger.LogDebug(csFile); var root = CSharpSyntaxTree.ParseText(File.ReadAllText(csFile)).GetRoot(); var namespaceSyntax = root.DescendantNodes().OfType().First(); var classDeclaration = (namespaceSyntax.DescendantNodes().OfType()).First(); + var baseTypeList = classDeclaration.BaseList?.Types.Select(t => t.ToString()).ToList(); + + if (baseTypeList == null) + { + return false; + } + + foreach (var baseType in baseTypeList) + { + if (baseType.Contains('<') && baseType.Substring(0, baseType.IndexOf('<')) == baseClass) + { + return true; + } + if (baseType == baseClass) + { + return true; + } + } - return classDeclaration.BaseList?.Types - .Any(t => t.ToString().Equals(baseClass)) ?? false; + return false; } } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleClassDependcyAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleClassDependcyAdder.cs index 594c77e5fa..9388ce42fe 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleClassDependcyAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleClassDependcyAdder.cs @@ -7,16 +7,20 @@ namespace Volo.Abp.Cli.ProjectModification { public class ModuleClassDependcyAdder : ITransientDependency { + protected UsingStatementAdder UsingStatementAdder { get; } + + public ModuleClassDependcyAdder(UsingStatementAdder usingStatementAdder) + { + UsingStatementAdder = usingStatementAdder; + } + public virtual void Add(string path, string module) { ParseModuleNameAndNameSpace(module, out var nameSpace, out var moduleName); var file = File.ReadAllText(path); - if (!file.Contains(GetUsingStatement(nameSpace)) ) - { - file = InsertUsingStatement(file, nameSpace); - } + file = UsingStatementAdder.Add(file, nameSpace); if (!file.Contains(moduleName) ) { @@ -34,38 +38,6 @@ namespace Volo.Abp.Cli.ProjectModification return file.Insert(indexOfPublicClassDeclaration, dependsOnAttribute); } - protected virtual string InsertUsingStatement(string file, string nameSpace) - { - var indexOfTheEndOfTheLastUsingStatement = GetIndexOfTheEndOfTheLastUsingStatement(file); - - return file.Insert(indexOfTheEndOfTheLastUsingStatement, Environment.NewLine + GetUsingStatement(nameSpace)); - } - - protected virtual int GetIndexOfTheEndOfTheLastUsingStatement(string file) - { - var indexOfPublicClassDeclaration = GetIndexOfWhereDependsOnWillBeAdded(file); - file = file.Substring(0, indexOfPublicClassDeclaration); - - var indexOfTheStartOfLastUsingStatement = - file.LastIndexOf("using ", StringComparison.Ordinal); - - if (indexOfTheStartOfLastUsingStatement < 0) - { - return 0; - } - - var indexOfFirstSemiColonAfterLastUsingStatement = - file.Substring(indexOfTheStartOfLastUsingStatement).IndexOf(';'); - - if (indexOfFirstSemiColonAfterLastUsingStatement < 0) - { - return 0; - } - - return indexOfTheStartOfLastUsingStatement - + indexOfFirstSemiColonAfterLastUsingStatement + 1; - } - protected virtual int GetIndexOfWhereDependsOnWillBeAdded(string file) { var indexOfPublicClassDeclaration = file.IndexOf("public class", StringComparison.Ordinal); 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 6048338b10..c62beeacc1 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 @@ -97,7 +97,7 @@ namespace Volo.Abp.Cli.ProjectModification } var dbMigrationsProject = projectFiles.FirstOrDefault(p => p.EndsWith(".DbMigrations.csproj")); - + if (dbMigrationsProject == null) { Logger.LogDebug("Solution doesn't have a \".DbMigrations\" project."); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs new file mode 100644 index 0000000000..fd6a95fb10 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ProjectModification +{ + public class UsingStatementAdder : ITransientDependency + { + public string Add(string fileContent, string nameSpace) + { + if (fileContent.Contains(nameSpace)) + { + return fileContent; + } + + var index = GetIndexOfTheEndOfTheLastUsingStatement(fileContent); + + if (index < 0 || index >= fileContent.Length) + { + index = 0; + } + + var usingStatement = GetUsingStatement(nameSpace); + + return fileContent.Insert(index, usingStatement); + } + + protected string GetUsingStatement(string nameSpace) + { + return Environment.NewLine + "using " + nameSpace + ";"; + } + + protected virtual int GetIndexOfTheEndOfTheLastUsingStatement(string fileContent) + { + var indexOfNamespaceDeclaration = fileContent.IndexOf("namespace", StringComparison.Ordinal); + + if (indexOfNamespaceDeclaration < 0) + { + return 0; + } + + fileContent = fileContent.Substring(0, indexOfNamespaceDeclaration); + + var indexOfTheStartOfLastUsingStatement = + fileContent.LastIndexOf("using ", StringComparison.Ordinal); + + if (indexOfTheStartOfLastUsingStatement < 0) + { + return 0; + } + + var indexOfFirstSemiColonAfterLastUsingStatement = + fileContent.Substring(indexOfTheStartOfLastUsingStatement).IndexOf(';'); + + if (indexOfFirstSemiColonAfterLastUsingStatement < 0) + { + return 0; + } + + return indexOfTheStartOfLastUsingStatement + + indexOfFirstSemiColonAfterLastUsingStatement + 1; + } + } +} From 469eafed00bfe91b7cbfbd78683669fdba9e8a03 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 16 May 2019 10:39:07 +0300 Subject: [PATCH 3/6] removed redundant code --- .../Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs | 2 -- .../Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs | 2 -- 2 files changed, 4 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs index f22e6107b4..f0ce18a537 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Diagnostics; -using System.Text; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.ProjectModification diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs index fd6a95fb10..aed44cc512 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.ProjectModification From e21d6e44d2d717c3c02d8bfe0f3163495023fae5 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 16 May 2019 11:05:23 +0300 Subject: [PATCH 4/6] Update DbContextFileBuilderConfigureAdder.cs --- .../ProjectModification/DbContextFileBuilderConfigureAdder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs index b8af0ed471..078a8db9b7 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs @@ -78,7 +78,7 @@ namespace Volo.Abp.Cli.ProjectModification protected string GetNamespace(string moduleConfiguration) { - return string.Join(".", moduleConfiguration.Split('.').Reverse().Skip(2)); + return string.Join(".", moduleConfiguration.Split('.').Reverse().Skip(2).Reverse()); } } } From 72cb2dc4188fc2d9a511d422e264953648555cc2 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 16 May 2019 11:10:53 +0300 Subject: [PATCH 5/6] Update EfCoreMigrationAdder.cs --- .../Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs index f0ce18a537..d3178adcb3 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.IO; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.ProjectModification @@ -11,7 +12,7 @@ namespace Volo.Abp.Cli.ProjectModification var moduleName = ParseModuleName(module); var migrationName = "Added_" + moduleName + "_Module" + GetUniquePostFix(); - var process = Process.Start("CMD.exe", "/C cd \"" + csprojFile + "\" & dotnet ef migrations add " + migrationName); + var process = Process.Start("CMD.exe", "/C cd \"" + Path.GetDirectoryName(csprojFile) + "\" & dotnet ef migrations add " + migrationName); process.WaitForExit(); if (updateDatabase) @@ -22,7 +23,7 @@ namespace Volo.Abp.Cli.ProjectModification protected void UpdateDatabase(string csprojFile) { - var process = Process.Start("CMD.exe", "/C cd \"" + csprojFile + "\" & dotnet ef database update"); + var process = Process.Start("CMD.exe", "/C cd \"" + Path.GetDirectoryName(csprojFile) + "\" & dotnet ef database update"); process.WaitForExit(); } From bc891edb18e6a059207bd4d3a1b32446e31b7058 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Thu, 16 May 2019 14:29:58 +0300 Subject: [PATCH 6/6] refactor --- .../Volo/Abp/Cli/Commands/AddModuleCommand.cs | 11 ++++++++--- .../DbContextFileBuilderConfigureAdder.cs | 8 ++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs index f48e2f4542..7925b82aa6 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddModuleCommand.cs @@ -31,7 +31,7 @@ namespace Volo.Abp.Cli.Commands } var skipDbMigrations = Convert.ToBoolean( - commandLineArgs.Options.GetOrNull(Options.Solution.SkipDbMigrations) ?? "false"); + commandLineArgs.Options.GetOrNull(Options.DbMigrations.Skip) ?? "false"); await SolutionModuleAdder.AddAsync( GetSolutionFile(commandLineArgs), @@ -93,11 +93,12 @@ namespace Volo.Abp.Cli.Commands sb.AppendLine(""); sb.AppendLine("Options:"); sb.AppendLine(" -s|--solution Specify the solution file explicitly."); + sb.AppendLine(" --skip-db-migrations Specify if a new migration will be added or not."); sb.AppendLine(""); sb.AppendLine("Examples:"); sb.AppendLine(" abp add-module Volo.Blogging Adds the module to the current soluton."); sb.AppendLine(" abp add-module Volo.Blogging -s Acme.BookStore Adds the module to the given soluton."); - sb.AppendLine(" abp add-module Volo.Blogging -s Acme.BookStore --SkipDbMigrations false Adds the module to the given soluton but doesn't add-migration."); + sb.AppendLine(" abp add-module Volo.Blogging -s Acme.BookStore --skip-db-migrations false Adds the module to the given soluton but doesn't add-migration."); sb.AppendLine(""); return sb.ToString(); @@ -109,7 +110,11 @@ namespace Volo.Abp.Cli.Commands { public const string Short = "s"; public const string Long = "solution"; - public const string SkipDbMigrations = "skip-db-migrations"; + } + + public static class DbMigrations + { + public const string Skip = "skip-db-migrations"; } } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs index 078a8db9b7..0211a42243 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Linq; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; @@ -9,9 +8,10 @@ namespace Volo.Abp.Cli.ProjectModification { public class DbContextFileBuilderConfigureAdder : ITransientDependency { - protected UsingStatementAdder UsingStatementAdder { get; } public ILogger Logger { get; set; } + protected UsingStatementAdder UsingStatementAdder { get; } + public DbContextFileBuilderConfigureAdder(UsingStatementAdder usingStatementAdder) { UsingStatementAdder = usingStatementAdder; @@ -73,12 +73,12 @@ namespace Volo.Abp.Cli.ProjectModification protected string GetLineToAdd(string moduleConfiguration) { - return "builder." + moduleConfiguration.Split('.').Last() + "();"; + return "builder." + moduleConfiguration.Split(':')[1] + "();"; } protected string GetNamespace(string moduleConfiguration) { - return string.Join(".", moduleConfiguration.Split('.').Reverse().Skip(2).Reverse()); + return string.Join(".", moduleConfiguration.Split(':')[0]); } } }