mirror of https://github.com/abpframework/abp.git
10 changed files with 284 additions and 11 deletions
@ -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<AddCommand> Logger { get; set; } |
|||
|
|||
protected ModuleAdder ModuleAdder { get; } |
|||
|
|||
public AddCommand(ModuleAdder moduleAdder) |
|||
{ |
|||
ModuleAdder = moduleAdder; |
|||
Logger = NullLogger<AddCommand>.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"; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -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 <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(""); |
|||
} |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
} |
|||
@ -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<ModuleAdder> Logger { get; set; } |
|||
|
|||
protected ProjectModuleAdder ProjectModuleAdder { get; } |
|||
protected SolutionModuleAdder SolutionModuleAdder { get; } |
|||
|
|||
public ModuleAdder(ProjectModuleAdder projectModuleAdder, SolutionModuleAdder solutionModuleAdder) |
|||
{ |
|||
ProjectModuleAdder = projectModuleAdder; |
|||
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 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<string> FindFiles(string directory, string fileExtension) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
} |
|||
} |
|||
@ -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) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
@ -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) |
|||
{ |
|||
|
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue