From ccc89803a1a1529376c496e277bd07ff6ecd4be5 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Mon, 6 May 2019 12:12:28 +0300 Subject: [PATCH] Added Add module command --- .../Volo/Abp/Cli/Commands/AddCommand.cs | 55 +++++++ .../Volo/Abp/Cli/Commands/AddCommandHelper.cs | 27 ++++ .../Volo/Abp/Cli/Commands/CommandSelector.cs | 10 +- .../{MainHelpCommand.cs => HelpCommand.cs} | 2 +- .../Abp/Cli/Commands/NewProjectCommand.cs | 8 +- .../Abp/ProjectModification/AddModuleArgs.cs | 26 ++++ .../Abp/ProjectModification/ModuleAdder.cs | 137 ++++++++++++++++++ .../ProjectModification/ProjectModuleAdder.cs | 13 ++ .../SolutionModuleAdder.cs | 13 ++ .../Volo/Abp/Modularity/IAbpModule.cs | 4 +- 10 files changed, 284 insertions(+), 11 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddCommand.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddCommandHelper.cs rename framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/{MainHelpCommand.cs => HelpCommand.cs} (87%) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/AddModuleArgs.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ModuleAdder.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ProjectModuleAdder.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/SolutionModuleAdder.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddCommand.cs new file mode 100644 index 0000000000..e5034725ba --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddCommand.cs @@ -0,0 +1,55 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using System.Threading.Tasks; +using Volo.Abp.Cli.Args; +using Volo.Abp.DependencyInjection; +using Volo.Abp.ProjectModification; + +namespace Volo.Abp.Cli.Commands +{ + public class AddCommand : IConsoleCommand, ITransientDependency + { + public ILogger Logger { get; set; } + + protected ModuleAdder ModuleAdder { get; } + + public AddCommand(ModuleAdder moduleAdder) + { + ModuleAdder = moduleAdder; + Logger = NullLogger.Instance; + } + + public async Task ExecuteAsync(CommandLineArgs commandLineArgs) + { + if (commandLineArgs.Target == null) + { + Logger.LogWarning("Module name is missing."); + AddCommandHelper.WriteUsage(Logger); + return; + } + + await ModuleAdder.AddModuleAsync( + new AddModuleArgs( + commandLineArgs.Target, + commandLineArgs.Options.GetOrNull(Options.Solution.Short, Options.Solution.Long), + commandLineArgs.Options.GetOrNull(Options.Project.Short, Options.Project.Long) + ) + ); + } + + public static class Options + { + public static class Solution + { + public const string Short = "s"; + public const string Long = "solution"; + } + + public static class Project + { + public const string Short = "p"; + public const string Long = "project"; + } + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddCommandHelper.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddCommandHelper.cs new file mode 100644 index 0000000000..0704ecdbf3 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddCommandHelper.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.Logging; + +namespace Volo.Abp.Cli.Commands +{ + public static class AddCommandHelper + { + public static void WriteUsage(ILogger logger) + { + logger.LogWarning(""); + logger.LogWarning("'add' command is used to add a module to a solution or to a project."); + logger.LogWarning("Use in a solution folder to add a multi-package module to a solution."); + logger.LogWarning("Use in a project folder to add a single-package module to a project."); + logger.LogWarning(""); + logger.LogWarning("Usage:"); + logger.LogWarning(" abp add [-s|--solution] [-p|--project]"); + logger.LogWarning(""); + logger.LogWarning("Options:"); + logger.LogWarning(" -s|--solution Specify solution file explicitly."); + logger.LogWarning(" -p|--project Specify project file explicitly."); + logger.LogWarning(""); + logger.LogWarning("Examples:"); + logger.LogWarning(" abp add Volo.Abp.TenantManagement Adds tenant management module packages to the solution."); + logger.LogWarning(" abp add Volo.Abp.FluentValidation Adds a single module package to the project."); + logger.LogWarning(""); + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CommandSelector.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CommandSelector.cs index 32268540a7..9bcc5ed859 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CommandSelector.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CommandSelector.cs @@ -9,14 +9,18 @@ namespace Volo.Abp.Cli.Commands public Type Select(CommandLineArgs commandLineArgs) { //TODO: Create options to define commands - //TODO: Get from dependency injection instead of new? if (commandLineArgs.Command == "new") { - return typeof(NewProjectCommand); + return typeof(NewCommand); } - return typeof(MainHelpCommand); + if (commandLineArgs.Command == "add") + { + return typeof(AddCommand); + } + + return typeof(HelpCommand); } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/MainHelpCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/HelpCommand.cs similarity index 87% rename from framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/MainHelpCommand.cs rename to framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/HelpCommand.cs index 96f7132934..fdad2a445e 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/MainHelpCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/HelpCommand.cs @@ -5,7 +5,7 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.Commands { - public class MainHelpCommand : IConsoleCommand, ITransientDependency + public class HelpCommand : IConsoleCommand, ITransientDependency { public Task ExecuteAsync(CommandLineArgs commandLineArgs) { diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewProjectCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewProjectCommand.cs index 3f274d69cb..c4ab4f6216 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewProjectCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/NewProjectCommand.cs @@ -10,17 +10,17 @@ using Volo.Abp.ProjectBuilding.Building; namespace Volo.Abp.Cli.Commands { - public class NewProjectCommand : IConsoleCommand, ITransientDependency + public class NewCommand : IConsoleCommand, ITransientDependency { - public ILogger Logger { get; set; } + public ILogger Logger { get; set; } protected ProjectBuilder ProjectBuilder { get; } - public NewProjectCommand(ProjectBuilder projectBuilder) + public NewCommand(ProjectBuilder projectBuilder) { ProjectBuilder = projectBuilder; - Logger = NullLogger.Instance; + Logger = NullLogger.Instance; } public async Task ExecuteAsync(CommandLineArgs commandLineArgs) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/AddModuleArgs.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/AddModuleArgs.cs new file mode 100644 index 0000000000..eba87227f3 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/AddModuleArgs.cs @@ -0,0 +1,26 @@ +using JetBrains.Annotations; + +namespace Volo.Abp.ProjectModification +{ + public class AddModuleArgs + { + [NotNull] + public string ModuleName { get; set; } + + [CanBeNull] + public string SolutionFile { get; set; } + + [CanBeNull] + public string ProjectFile { get; set; } + + public AddModuleArgs( + [NotNull] string moduleName, + [CanBeNull] string solutionFile = null, + [CanBeNull] string projectFile = null) + { + ModuleName = Check.NotNullOrWhiteSpace(moduleName, nameof(moduleName)); + SolutionFile = solutionFile; + ProjectFile = projectFile; + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ModuleAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ModuleAdder.cs new file mode 100644 index 0000000000..6b5017f882 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ModuleAdder.cs @@ -0,0 +1,137 @@ +using JetBrains.Annotations; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp.Cli.Commands; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.ProjectModification +{ + public class ModuleAdder : ITransientDependency + { + public ILogger Logger { get; set; } + + protected ProjectModuleAdder ProjectModuleAdder { get; } + protected SolutionModuleAdder SolutionModuleAdder { get; } + + public ModuleAdder(ProjectModuleAdder projectModuleAdder, SolutionModuleAdder solutionModuleAdder) + { + ProjectModuleAdder = projectModuleAdder; + SolutionModuleAdder = solutionModuleAdder; + + Logger = NullLogger.Instance; + } + + public async Task AddModuleAsync(AddModuleArgs args) + { + ValidateArgs(args); + NormalizeArgs(args); + + if (args.ProjectFile.IsNullOrEmpty() && args.SolutionFile.IsNullOrEmpty()) + { + if (!TryToFillProjectOrSolutionName(args)) + { + Logger.LogWarning("Could not find any project (.csproj) or solution (.sln) file in the current directory!"); + AddCommandHelper.WriteUsage(Logger); + return; + } + } + + if (!args.ProjectFile.IsNullOrEmpty()) + { + await ProjectModuleAdder.AddAsync(args.ProjectFile, args.ModuleName); + } + + if (!args.SolutionFile.IsNullOrEmpty()) + { + await SolutionModuleAdder.AddAsync(args.SolutionFile, args.ModuleName); + } + } + + private bool TryToFillProjectOrSolutionName(AddModuleArgs args) + { + var projectFiles = FindFiles(Directory.GetCurrentDirectory(), ".csproj"); + if (projectFiles.Count == 1) + { + args.ProjectFile = projectFiles.First(); + return true; + } + + if (projectFiles.Count > 1) + { + Logger.LogWarning( + "There are multiple project (.csproj) files in the current directory. Please specify one of the files below:"); + foreach (var projectFile in projectFiles) + { + Logger.LogWarning("* " + projectFile); + } + + Logger.LogWarning("Example:"); + Logger.LogWarning($"abp add {args.ModuleName} -p {projectFiles[0]}"); + return false; + } + + var solutionFiles = FindFiles(Directory.GetCurrentDirectory(), ".sln"); + if (solutionFiles.Count == 1) + { + args.SolutionFile = solutionFiles.First(); + return true; + } + + if (solutionFiles.Count > 1) + { + Logger.LogWarning( + "There are multiple solution (.sln) files in the current directory. Please specify one of the files below:"); + foreach (var solutionFile in solutionFiles) + { + Logger.LogWarning("* " + solutionFile); + } + + Logger.LogWarning("Example:"); + Logger.LogWarning($"abp add {args.ModuleName} -s {solutionFiles[0]}"); + } + + return false; + } + + private void ValidateArgs(AddModuleArgs args) + { + if (!args.ProjectFile.IsNullOrEmpty() && + !args.SolutionFile.IsNullOrEmpty()) + { + throw new ArgumentException("Can not specify a solution name and project name together."); + } + } + + private void NormalizeArgs(AddModuleArgs args) + { + args.ProjectFile = NormalizePath(args.ProjectFile); + args.SolutionFile = NormalizePath(args.SolutionFile); + } + + private static string NormalizePath([CanBeNull] string path) + { + if (string.IsNullOrEmpty(path)) + { + return path; + } + + if (Path.IsPathRooted(path)) + { + return path; + } + + return Path.Combine(Directory.GetCurrentDirectory(), path); + } + + + private List FindFiles(string directory, string fileExtension) + { + throw new NotImplementedException(); + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ProjectModuleAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ProjectModuleAdder.cs new file mode 100644 index 0000000000..1960da10a5 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ProjectModuleAdder.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.ProjectModification +{ + public class ProjectModuleAdder : ITransientDependency + { + public async Task AddAsync(string projectFile, string moduleName) + { + + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/SolutionModuleAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/SolutionModuleAdder.cs new file mode 100644 index 0000000000..c78099210c --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/SolutionModuleAdder.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.ProjectModification +{ + public class SolutionModuleAdder : ITransientDependency + { + public async Task AddAsync(string solutionFile, string moduleName) + { + + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/IAbpModule.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/IAbpModule.cs index b58fdc8eed..32c8980c54 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/IAbpModule.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Modularity/IAbpModule.cs @@ -1,6 +1,4 @@ -using JetBrains.Annotations; -using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.DependencyInjection; +using Volo.Abp.DependencyInjection; namespace Volo.Abp.Modularity {