From 4512d0185c869394cf47f96648024e768f25ccc9 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 17 Jun 2019 09:22:15 +0300 Subject: [PATCH] Update NpmPackagesUpdater.cs --- .../ProjectModification/NpmPackagesUpdater.cs | 59 ++++++++----------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs index cc0e26939b..ecab9c2c94 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs @@ -37,38 +37,26 @@ namespace Volo.Abp.Cli.ProjectModification foreach (var file in fileList) { - ProcessFile(file); - } - } - - protected virtual void ProcessFile(string file) - { - UpdatePackagesInFile(file, out var isFileModified); + UpdatePackagesInFile(file); - if (isFileModified) - { RunYarnAndGulp(file); } } - - protected virtual void UpdatePackagesInFile(string file, out bool isAnyPackageUpdated) + protected virtual void UpdatePackagesInFile(string file) { - isAnyPackageUpdated = false; var fileContent = File.ReadAllText(file); + var packageJson = JObject.Parse(fileContent); + var abpPackages = GetAbpPackagesFromPackageJson(packageJson); - if (!fileContent.Contains("\"@abp/") && !fileContent.Contains("\"@volo/")) + if (!abpPackages.Any()) { return; } - var packageJson = JObject.Parse(fileContent); - - var abpPackages = GetAbpPackagesFromPackageJson(packageJson); - foreach (var abpPackage in abpPackages) { - UpdatePackage(file, abpPackage, out isAnyPackageUpdated); + UpdatePackage(file, abpPackage); } var modifiedFileContent = packageJson.ToString(Formatting.Indented); @@ -76,29 +64,33 @@ namespace Volo.Abp.Cli.ProjectModification File.WriteAllText(file, modifiedFileContent); } - protected virtual void UpdatePackage(string file, JProperty package, out bool isPackageUpdated) + protected virtual void UpdatePackage(string file, JProperty package) { - isPackageUpdated = false; - string version; + var version = GetLatestVersion(package); - if (_fileVersionStorage.ContainsKey(package.Name)) - { - version = _fileVersionStorage[package.Name]; - } - else + var versionWithPrefix = $"^{version}"; + + if (versionWithPrefix == (string)package.Value) { - version = _latestNpmPackageVersionFinder.Find(package.Name); - _fileVersionStorage[package.Name] = version; + return; } - if (version == (string) package.Value) + package.Value.Replace(versionWithPrefix); + + Logger.LogInformation($"Updated {package.Name} to {version} in {file.Replace(Directory.GetCurrentDirectory(), "")}."); + } + + private string GetLatestVersion(JProperty package) + { + if (_fileVersionStorage.ContainsKey(package.Name)) { - return; + return _fileVersionStorage[package.Name]; } - package.Value.Replace($"^{version}"); - isPackageUpdated = true; - Logger.LogInformation($"Updated {package.Name} to {version} in {file.Replace(Directory.GetCurrentDirectory(),"")}."); + var version = _latestNpmPackageVersionFinder.Find(package.Name); + _fileVersionStorage[package.Name] = version; + + return version; } protected virtual List GetAbpPackagesFromPackageJson(JObject fileObject) @@ -108,6 +100,7 @@ namespace Volo.Abp.Cli.ProjectModification var abpPackages = properties.Where(p => p.Name.StartsWith("@abp/") || p.Name.StartsWith("@volo/")).ToList(); return abpPackages; } + protected virtual void RunYarnAndGulp(string file) { var fileDirectory = Path.GetDirectoryName(file).EnsureEndsWith(Path.DirectorySeparatorChar);