Split add-package and add-module commands.

pull/1466/head^2
Halil ibrahim Kalkan 7 years ago
parent 203308a8e8
commit be2bb4ba27

@ -22,6 +22,7 @@ namespace Volo.Abp.Cli
{
options.Commands["help"] = typeof(HelpCommand);
options.Commands["new"] = typeof(NewCommand);
options.Commands["add-package"] = typeof(AddPackageCommand);
options.Commands["add-module"] = typeof(AddModuleCommand);
options.Commands["login"] = typeof(LoginCommand);
options.Commands["logout"] = typeof(LogoutCommand);

@ -16,7 +16,7 @@ namespace Volo.Abp.Cli.Args
var argumentList = args.ToList();
//Get command
//Command
var command = argumentList[0];
argumentList.RemoveAt(0);
@ -26,7 +26,7 @@ namespace Volo.Abp.Cli.Args
return new CommandLineArgs(command);
}
//Get target
//Target
var target = argumentList[0];
if (target.StartsWith("-"))
@ -43,7 +43,7 @@ namespace Volo.Abp.Cli.Args
return new CommandLineArgs(command, target);
}
//Get options
//Options
var commandLineArgs = new CommandLineArgs(command, target);

@ -1,28 +1,22 @@
using System;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Commands;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ProjectBuilding;
namespace Volo.Abp.Cli
{
public class CliService : ITransientDependency
{
//public static string Version => typeof(AbpCliCoreModule).Assembly
// .GetFileVersion()
// .RemovePostFix(".0");
public static string Version => "0.17.0.0";
public ILogger<CliService> Logger { get; set; }
protected ICommandLineArgumentParser CommandLineArgumentParser { get; }
protected ICommandSelector CommandSelector { get; }
public IHybridServiceScopeFactory ServiceScopeFactory { get; }
protected IHybridServiceScopeFactory ServiceScopeFactory { get; }
public CliService(
ICommandLineArgumentParser commandLineArgumentParser,
@ -38,8 +32,8 @@ namespace Volo.Abp.Cli
public async Task RunAsync(string[] args)
{
Logger.LogInformation("ABP CLI (abp.io)");
Logger.LogInformation("Version: " + Version);
Logger.LogInformation($"ABP CLI, version {VersionHelper.Version}.");
Logger.LogInformation("https://abp.io");
var commandLineArgs = CommandLineArgumentParser.Parse(args);
var commandType = CommandSelector.Select(commandLineArgs);
@ -47,7 +41,19 @@ namespace Volo.Abp.Cli
using (var scope = ServiceScopeFactory.CreateScope())
{
var command = (IConsoleCommand)scope.ServiceProvider.GetRequiredService(commandType);
await command.ExecuteAsync(commandLineArgs);
try
{
await command.ExecuteAsync(commandLineArgs);
}
catch (CliUsageException usageException)
{
Logger.LogWarning(usageException.Message);
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}
}
}

@ -0,0 +1,19 @@
using System;
namespace Volo.Abp.Cli
{
public class CliUsageException : Exception
{
public CliUsageException(string message)
: base(message)
{
}
public CliUsageException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}

@ -4,24 +4,6 @@ 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 <module-name> [-s|--solution] [-p|--project]");
logger.LogWarning("");
logger.LogWarning("Options:");
logger.LogWarning(" -s|--solution <solution-file> Specify solution file explicitly.");
logger.LogWarning(" -p|--project <project-file> 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("");
}
}
}

@ -1,7 +1,11 @@
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System.Threading.Tasks;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ProjectModification;
@ -11,11 +15,11 @@ namespace Volo.Abp.Cli.Commands
{
public ILogger<AddModuleCommand> Logger { get; set; }
protected ModuleAdder ModuleAdder { get; }
protected SolutionModuleAdder SolutionModuleAdder { get; }
public AddModuleCommand(ModuleAdder moduleAdder)
public AddModuleCommand(SolutionModuleAdder solutionModuleAdder)
{
ModuleAdder = moduleAdder;
SolutionModuleAdder = solutionModuleAdder;
Logger = NullLogger<AddModuleCommand>.Instance;
}
@ -23,18 +27,75 @@ namespace Volo.Abp.Cli.Commands
{
if (commandLineArgs.Target == null)
{
Logger.LogWarning("Module name is missing.");
AddCommandHelper.WriteUsage(Logger);
return;
throw new CliUsageException("Module name is missing!" + Environment.NewLine + Environment.NewLine + GetUsageInfo());
}
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)
await SolutionModuleAdder.AddAsync(
GetSolutionFile(commandLineArgs),
commandLineArgs.Target
);
}
private string GetSolutionFile(CommandLineArgs commandLineArgs)
{
var providedSolutionFile = PathHelper.NormalizePath(
commandLineArgs.Options.GetOrNull(
AddPackageCommand.Options.Project.Short,
AddPackageCommand.Options.Project.Long
)
);
if (!providedSolutionFile.IsNullOrWhiteSpace())
{
return providedSolutionFile;
}
var foundSolutionFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln");
if (foundSolutionFiles.Length == 1)
{
return foundSolutionFiles[0];
}
if (foundSolutionFiles.Length == 0)
{
throw new CliUsageException("'abp add-module' command should be used inside a folder contaning a .sln file!");
}
//foundSolutionFiles.Length > 1
var sb = new StringBuilder("There are multiple solution (.sln) files in the current directory. Please specify one of the files below:");
foreach (var foundSolutionFile in foundSolutionFiles)
{
sb.AppendLine("* " + foundSolutionFile);
}
sb.AppendLine("Example:");
sb.AppendLine($"abp add-module {commandLineArgs.Target} -p {foundSolutionFiles[0]}");
throw new CliUsageException(sb.ToString());
}
protected virtual string GetUsageInfo()
{
var sb = new StringBuilder();
sb.AppendLine("");
sb.AppendLine("'add-module' command is used to add a multi-package ABP module to a solution.");
sb.AppendLine("It should be used in a folder containing a .sln file.");
sb.AppendLine("");
sb.AppendLine("Usage:");
sb.AppendLine(" abp add-module <module-name> [-s|--solution]");
sb.AppendLine("");
sb.AppendLine("Options:");
sb.AppendLine(" -s|--solution <solution-file> Specify the solution file explicitly.");
sb.AppendLine("");
sb.AppendLine("Examples:");
sb.AppendLine(" abp add Volo.Blogging Adds the module to the current soluton.");
sb.AppendLine(" abp add Volo.Blogging -s Acme.BookStore Adds the module to the given soluton.");
sb.AppendLine("");
return sb.ToString();
}
public static class Options
@ -44,12 +105,6 @@ namespace Volo.Abp.Cli.Commands
public const string Short = "s";
public const string Long = "solution";
}
public static class Project
{
public const string Short = "p";
public const string Long = "project";
}
}
}
}

@ -0,0 +1,110 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ProjectModification;
namespace Volo.Abp.Cli.Commands
{
public class AddPackageCommand : IConsoleCommand, ITransientDependency
{
public ILogger<AddPackageCommand> Logger { get; set; }
protected ProjectNugetPackageAdder ProjectNugetPackageAdder { get; }
public AddPackageCommand(ProjectNugetPackageAdder projectNugetPackageAdder)
{
ProjectNugetPackageAdder = projectNugetPackageAdder;
Logger = NullLogger<AddPackageCommand>.Instance;
}
public virtual async Task ExecuteAsync(CommandLineArgs commandLineArgs)
{
if (commandLineArgs.Target == null)
{
throw new CliUsageException("Package name is missing!" + Environment.NewLine + Environment.NewLine + GetUsageInfo());
}
await ProjectNugetPackageAdder.AddAsync(
GetProjectFile(commandLineArgs),
commandLineArgs.Target
);
}
protected virtual string GetProjectFile(CommandLineArgs commandLineArgs)
{
var providedProjectFile = PathHelper.NormalizePath(
commandLineArgs.Options.GetOrNull(
Options.Project.Short,
Options.Project.Long
)
);
if (!providedProjectFile.IsNullOrWhiteSpace())
{
return providedProjectFile;
}
var foundProjectFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.csproj");
if (foundProjectFiles.Length == 1)
{
return foundProjectFiles[0];
}
if (foundProjectFiles.Length == 0)
{
throw new CliUsageException("'abp add-package' command should be used inside a folder contaning a .csproj file!");
}
//foundProjectFiles.Length > 1
var sb = new StringBuilder("There are multiple project (.csproj) files in the current directory. Please specify one of the files below:");
foreach (var foundProjectFile in foundProjectFiles)
{
sb.AppendLine("* " + foundProjectFile);
}
sb.AppendLine("Example:");
sb.AppendLine($"abp add-package {commandLineArgs.Target} -p {foundProjectFiles[0]}");
throw new CliUsageException(sb.ToString());
}
protected virtual string GetUsageInfo()
{
var sb = new StringBuilder();
sb.AppendLine("");
sb.AppendLine("'add-package' command is used to add an ABP package to a project.");
sb.AppendLine("It should be used in a folder containing a .csproj file.");
sb.AppendLine("");
sb.AppendLine("Usage:");
sb.AppendLine(" abp add-package <package-name> [-p|--project]");
sb.AppendLine("");
sb.AppendLine("Options:");
sb.AppendLine(" -p|--project <project-file> Specify the project file explicitly.");
sb.AppendLine("");
sb.AppendLine("Examples:");
sb.AppendLine(" abp add Volo.Abp.FluentValidation Adds the package to the current project.");
sb.AppendLine(" abp add Volo.Abp.FluentValidation -p Acme.BookStore.Application Adds the package to the given project.");
sb.AppendLine("");
return sb.ToString();
}
public static class Options
{
public static class Project
{
public const string Short = "p";
public const string Long = "project";
}
}
}
}

@ -0,0 +1,23 @@
using System.IO;
using JetBrains.Annotations;
namespace Volo.Abp.Cli.Utils
{
public static class PathHelper
{
public static string NormalizePath([CanBeNull] string path)
{
if (string.IsNullOrEmpty(path))
{
return path;
}
if (Path.IsPathRooted(path))
{
return path;
}
return Path.Combine(Directory.GetCurrentDirectory(), path);
}
}
}

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Volo.Abp.Cli.Utils
{
public static class VersionHelper
{
//public static string Version => typeof(AbpCliCoreModule).Assembly
// .GetFileVersion()
// .RemovePostFix(".0");
public static string Version => "0.17.0.0";
}
}

@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Cli;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
using Volo.Abp.ProjectBuilding.Building;
@ -31,7 +32,7 @@ namespace Volo.Abp.ProjectBuilding
}
}
var version = CliService.Version;
var version = VersionHelper.Version;
var templateFile = await TemplateStore.GetAsync(args.TemplateName, version);

@ -1,26 +0,0 @@
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;
}
}
}

@ -1,137 +0,0 @@
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<ModuleAdder> Logger { get; set; }
protected ProjectNugetPackageAdder ProjectNugetPackageAdder { get; }
protected SolutionModuleAdder SolutionModuleAdder { get; }
public ModuleAdder(ProjectNugetPackageAdder projectNugetPackageAdder, SolutionModuleAdder solutionModuleAdder)
{
ProjectNugetPackageAdder = projectNugetPackageAdder;
SolutionModuleAdder = solutionModuleAdder;
Logger = NullLogger<ModuleAdder>.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 ProjectNugetPackageAdder.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.Length == 1)
{
args.ProjectFile = projectFiles.First();
return true;
}
if (projectFiles.Length > 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.Length == 1)
{
args.SolutionFile = solutionFiles.First();
return true;
}
if (solutionFiles.Length > 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 string[] FindFiles(string directory, string fileExtension)
{
return Directory.GetFiles(directory, "*" + fileExtension);
}
}
}
Loading…
Cancel
Save