diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNpmPackageAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNpmPackageAdder.cs index a0ee3b463a..3c283319ac 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNpmPackageAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ProjectNpmPackageAdder.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using System.Xml; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Newtonsoft.Json.Linq; using Volo.Abp.Cli.Args; using Volo.Abp.Cli.Commands; using Volo.Abp.Cli.Commands.Services; @@ -42,7 +43,8 @@ namespace Volo.Abp.Cli.ProjectModification Logger = NullLogger.Instance; } - public async Task AddAngularPackageAsync(string directory, string npmPackageName, string version = null, bool withSourceCode = false) + public async Task AddAngularPackageAsync(string directory, string npmPackageName, string version = null, + bool withSourceCode = false) { await AddAngularPackageAsync( directory, @@ -52,7 +54,8 @@ namespace Volo.Abp.Cli.ProjectModification ); } - public async Task AddAngularPackageAsync(string directory, NpmPackageInfo npmPackage, string version = null, bool withSourceCode = false) + public async Task AddAngularPackageAsync(string directory, NpmPackageInfo npmPackage, string version = null, + bool withSourceCode = false) { var packageJsonFilePath = Path.Combine(directory, "package.json"); if (!File.Exists(packageJsonFilePath)) @@ -85,9 +88,11 @@ namespace Volo.Abp.Cli.ProjectModification } } - protected virtual async Task DownloadAngularSourceCode(string angularDirectory, NpmPackageInfo package, string version = null) + protected virtual async Task DownloadAngularSourceCode(string angularDirectory, NpmPackageInfo package, + string version = null) { - var targetFolder = Path.Combine(angularDirectory, "projects", package.Name.RemovePreFix("@").Replace("/","-")); + var targetFolder = Path.Combine(angularDirectory, "projects", + package.Name.RemovePreFix("@").Replace("/", "-")); if (Directory.Exists(targetFolder)) { @@ -101,7 +106,8 @@ namespace Volo.Abp.Cli.ProjectModification ); } - public Task AddMvcPackageAsync(string directory, NpmPackageInfo npmPackage, string version = null, bool skipGulpCommand = false) + public Task AddMvcPackageAsync(string directory, NpmPackageInfo npmPackage, string version = null, + bool skipGulpCommand = false) { var packageJsonFilePath = Path.Combine(directory, "package.json"); if (!File.Exists(packageJsonFilePath) || @@ -113,6 +119,11 @@ namespace Volo.Abp.Cli.ProjectModification Logger.LogInformation($"Installing '{npmPackage.Name}' package to the project '{packageJsonFilePath}'..."); + if (version == null) + { + version = DetectAbpVersionOrNull(Path.Combine(directory, "package.json")); + } + var versionPostfix = version != null ? $"@{version}" : string.Empty; using (DirectoryHelper.ChangeCurrentDirectory(directory)) @@ -132,9 +143,45 @@ namespace Volo.Abp.Cli.ProjectModification return Task.CompletedTask; } - private async Task FindNpmPackageInfoAsync(string packageName) + private string DetectAbpVersionOrNull(string packageJsonFile) { + if (string.IsNullOrEmpty(packageJsonFile) || + !File.Exists(packageJsonFile)) + { + return null; + } + + try + { + var packageJsonFileContent = File.ReadAllText(packageJsonFile); + var packageJsonObject = JObject.Parse(packageJsonFileContent); + var dependenciesObject = (JObject) packageJsonObject["dependencies"]; + + if (dependenciesObject == null) + { + return null; + } + + var packages = dependenciesObject.Children(); + foreach (var package in packages) + { + if (package.Name.StartsWith("@abp/") || package.Name.StartsWith("@volo/")) + { + return package.Value.ToString(); + } + } + } + catch (Exception ex) + { + Logger.LogWarning("Cannot detect ABP package version. " + ex.Message); + } + + return null; + } + + private async Task FindNpmPackageInfoAsync(string packageName) + { var url = $"{CliUrls.WwwAbpIo}api/app/npmPackage/byName/?name=" + packageName; var client = _cliHttpClientFactory.CreateClient(); @@ -155,4 +202,4 @@ namespace Volo.Abp.Cli.ProjectModification } } } -} +} \ No newline at end of file