diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddPackageCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddPackageCommand.cs index 96740fa418..b2d407c00f 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddPackageCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/AddPackageCommand.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -39,6 +40,7 @@ namespace Volo.Abp.Cli.Commands var addSourceCodeToSolutionFile = withSourceCode && commandLineArgs.Options.ContainsKey("add-to-solution-file"); await ProjectNugetPackageAdder.AddAsync( + GetSolutionFile(commandLineArgs), GetProjectFile(commandLineArgs), commandLineArgs.Target, version, @@ -94,30 +96,24 @@ namespace Volo.Abp.Cli.Commands 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 + return Directory.GetFiles(Directory.GetCurrentDirectory(), "*.csproj").FirstOrDefault(); + } - var sb = new StringBuilder("There are multiple project (.csproj) files in the current directory. Please specify one of the files below:"); + protected virtual string GetSolutionFile(CommandLineArgs commandLineArgs) + { + var providedSolutionFile = PathHelper.NormalizePath( + commandLineArgs.Options.GetOrNull( + Options.Solution.Short, + Options.Solution.Long + ) + ); - foreach (var foundProjectFile in foundProjectFiles) + if (!providedSolutionFile.IsNullOrWhiteSpace()) { - sb.AppendLine("* " + foundProjectFile); + return providedSolutionFile; } - sb.AppendLine("Example:"); - sb.AppendLine($"abp add-package {commandLineArgs.Target} -p {foundProjectFiles[0]}"); - - throw new CliUsageException(sb.ToString()); + return Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln").FirstOrDefault(); } public static class Options @@ -128,6 +124,12 @@ namespace Volo.Abp.Cli.Commands public const string Long = "project"; } + public static class Solution + { + public const string Short = "s"; + public const string Long = "solution"; + } + public static class Version { public const string Short = "v"; diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs index 8665a80bc3..8b08775c93 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNugetPackageAdder.cs @@ -58,6 +58,7 @@ namespace Volo.Abp.Cli.ProjectModification } public async Task AddAsync( + string solutionFile, string projectFile, string packageName, string version = null, @@ -66,6 +67,7 @@ namespace Volo.Abp.Cli.ProjectModification bool addSourceCodeToSolutionFile = false) { await AddAsync( + solutionFile, projectFile, await FindNugetPackageInfoAsync(packageName), version, @@ -76,6 +78,7 @@ namespace Volo.Abp.Cli.ProjectModification } public async Task AddAsync( + string solutionFile, string projectFile, NugetPackageInfo package, string version = null, @@ -83,6 +86,23 @@ namespace Volo.Abp.Cli.ProjectModification bool withSourceCode = false, bool addSourceCodeToSolutionFile = false) { + if (projectFile == null) + { + if (solutionFile == null) + { + throw new CliUsageException("Couldn't find any project/solution."); + } + + projectFile = GetProjectFile(solutionFile, package); + + if (projectFile == null) + { + throw new CliUsageException("Couldn't find any project/solution."); + } + } + + solutionFile ??= FindSolutionFile(projectFile); + if (version == null) { version = GetAbpVersionOrNull(projectFile); @@ -92,17 +112,30 @@ namespace Volo.Abp.Cli.ProjectModification if (withSourceCode) { - await AddSourceCode(projectFile, package, version); - await ConvertPackageReferenceToProjectReference(projectFile, package); + await AddSourceCode(projectFile, solutionFile, package, version); + await ConvertPackageReferenceToProjectReference(projectFile, solutionFile, package); if (addSourceCodeToSolutionFile) { - await SolutionFileModifier.AddPackageToSolutionFileAsync(package, FindSolutionFile(projectFile)); + await SolutionFileModifier.AddPackageToSolutionFileAsync(package, solutionFile); } } } - private async Task ConvertPackageReferenceToProjectReference(string projectFile, NugetPackageInfo package) + private string GetProjectFile(string solutionFile, NugetPackageInfo package) + { + var projectFiles = Directory.GetFiles(Path.GetDirectoryName(solutionFile), "*.csproj", SearchOption.AllDirectories); + var isSolutionTiered = IsSolutionTiered(projectFiles); + + var projectFile = ProjectFinder.FindNuGetTargetProjectFile( + projectFiles, + isSolutionTiered && package.TieredTarget != NuGetPackageTarget.Undefined + ? package.TieredTarget + : package.Target); + return projectFile; + } + + protected virtual async Task ConvertPackageReferenceToProjectReference(string projectFile,string solutionFile, NugetPackageInfo package) { var content = File.ReadAllText(projectFile); var doc = new XmlDocument() {PreserveWhitespace = true}; @@ -117,7 +150,7 @@ namespace Volo.Abp.Cli.ProjectModification return; } - var downloadedProjectPath = FindRelativeFolderToDownloadPackage(projectFile, package); + var downloadedProjectPath = FindRelativeFolderToDownloadPackage(projectFile, solutionFile, package); var oldNodeIncludeValue = nodes[0]?.Attributes?["Include"]?.Value; if (package.Name == oldNodeIncludeValue) @@ -135,9 +168,9 @@ namespace Volo.Abp.Cli.ProjectModification File.WriteAllText(projectFile, doc.OuterXml); } - private async Task AddSourceCode(string projectFile, NugetPackageInfo package, string version = null) + protected virtual async Task AddSourceCode(string projectFile, string solutionFile, NugetPackageInfo package, string version = null) { - var targetFolder = FindFolderToDownloadPackage(projectFile, package); + var targetFolder = FindFolderToDownloadPackage(solutionFile, package); if (Directory.Exists(targetFolder)) { @@ -147,19 +180,19 @@ namespace Volo.Abp.Cli.ProjectModification await DownloadSourceCode(targetFolder, package, version); } - private string FindFolderToDownloadPackage(string projectFile, NugetPackageInfo package) + protected virtual string FindFolderToDownloadPackage(string solutionFile, NugetPackageInfo package) { - return Path.Combine(FindSolutionFolder(projectFile), "packages", package.Name); + return Path.Combine(Path.GetDirectoryName(solutionFile), "packages", package.Name); } - private string FindRelativeFolderToDownloadPackage(string projectFile, NugetPackageInfo package) + protected virtual string FindRelativeFolderToDownloadPackage(string projectFile, string solutionFile, NugetPackageInfo package) { - var folder = Path.Combine(FindSolutionFolder(projectFile), "packages", package.Name); + var folder = Path.Combine(Path.GetDirectoryName(solutionFile), "packages", package.Name); return new Uri(projectFile).MakeRelativeUri(new Uri(folder)).ToString().Replace("/", "\\"); } - private async Task DownloadSourceCode(string targetFolder, NugetPackageInfo package, string version = null) + protected virtual async Task DownloadSourceCode(string targetFolder, NugetPackageInfo package, string version = null) { await SourceCodeDownloadService.DownloadPackageAsync( package.Name, @@ -168,14 +201,14 @@ namespace Volo.Abp.Cli.ProjectModification ); } - private string FindSolutionFile(string projectFile) + protected virtual string FindSolutionFile(string projectFile) { var folder = FindSolutionFolder(projectFile); return Directory.GetFiles(folder, "*.sln", SearchOption.TopDirectoryOnly).FirstOrDefault(); } - private string FindSolutionFolder(string projectFile) + protected virtual string FindSolutionFolder(string projectFile) { var targetFolder = Path.GetDirectoryName(projectFile); @@ -199,7 +232,7 @@ namespace Volo.Abp.Cli.ProjectModification return targetFolder; } - private async Task AddAsPackageReference(string projectFile, NugetPackageInfo package, string version, + protected virtual async Task AddAsPackageReference(string projectFile, NugetPackageInfo package, string version, bool useDotnetCliToInstall) { var projectFileContent = File.ReadAllText(projectFile); @@ -250,7 +283,7 @@ namespace Volo.Abp.Cli.ProjectModification Logger.LogInformation("Successfully installed."); } - private Task AddUsingDotnetCli(NugetPackageInfo package, string version = null) + protected virtual Task AddUsingDotnetCli(NugetPackageInfo package, string version = null) { var versionOption = version == null ? "" : $" -v {version}"; @@ -259,7 +292,7 @@ namespace Volo.Abp.Cli.ProjectModification return Task.CompletedTask; } - private Task AddToCsprojManuallyAsync(string projectFile, NugetPackageInfo package, string version = null) + protected virtual Task AddToCsprojManuallyAsync(string projectFile, NugetPackageInfo package, string version = null) { var projectFileContent = File.ReadAllText(projectFile); var doc = new XmlDocument() {PreserveWhitespace = true}; @@ -301,7 +334,7 @@ namespace Volo.Abp.Cli.ProjectModification return Task.CompletedTask; } - private string GetAbpVersionOrNull(string projectFile) + protected virtual string GetAbpVersionOrNull(string projectFile) { var projectFileContent = File.ReadAllText(projectFile); @@ -345,5 +378,13 @@ namespace Volo.Abp.Cli.ProjectModification await BundleCommand.ExecuteAsync(args); } + + protected virtual bool IsSolutionTiered(string[] projectFiles) + { + return projectFiles.Select(ProjectFileNameHelper.GetAssemblyNameFromProjectPath) + .Any(p => p.EndsWith(".HttpApi.Host")) + && projectFiles.Select(ProjectFileNameHelper.GetAssemblyNameFromProjectPath) + .Any(p => p.EndsWith(".IdentityServer")); + } } } 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 caf47e0a87..56f0bc5003 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 @@ -438,7 +438,7 @@ namespace Volo.Abp.Cli.ProjectModification continue; } - await ProjectNugetPackageAdder.AddAsync(targetProjectFile, nugetPackage, null, useDotnetCliToInstall); + await ProjectNugetPackageAdder.AddAsync(null, targetProjectFile, nugetPackage, null, useDotnetCliToInstall); } var mvcNpmPackages = module.NpmPackages?.Where(p => p.ApplicationType.HasFlag(NpmApplicationType.Mvc))