mirror of https://github.com/abpframework/abp.git
12 changed files with 264 additions and 215 deletions
@ -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) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -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,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…
Reference in new issue