|
|
|
@ -1,10 +1,14 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.IO; |
|
|
|
using System.Linq; |
|
|
|
using System.Text; |
|
|
|
using System.Threading; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Microsoft.Extensions.Logging.Abstractions; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using Newtonsoft.Json.Linq; |
|
|
|
using Volo.Abp.Cli.Commands; |
|
|
|
using Volo.Abp.Cli.Utils; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
|
|
@ -12,6 +16,8 @@ namespace Volo.Abp.Cli.ProjectModification |
|
|
|
{ |
|
|
|
public class NpmPackagesUpdater : ITransientDependency |
|
|
|
{ |
|
|
|
public ILogger<NpmPackagesUpdater> Logger { get; set; } |
|
|
|
|
|
|
|
private readonly LatestNpmPackageVersionFinder _latestNpmPackageVersionFinder; |
|
|
|
private readonly PackageJsonFileFinder _packageJsonFileFinder; |
|
|
|
|
|
|
|
@ -21,6 +27,8 @@ namespace Volo.Abp.Cli.ProjectModification |
|
|
|
{ |
|
|
|
_latestNpmPackageVersionFinder = latestNpmPackageVersionFinder; |
|
|
|
_packageJsonFileFinder = packageJsonFileFinder; |
|
|
|
|
|
|
|
Logger = NullLogger<NpmPackagesUpdater>.Instance; |
|
|
|
} |
|
|
|
|
|
|
|
public void Update(string rootDirectory) |
|
|
|
@ -35,20 +43,22 @@ namespace Volo.Abp.Cli.ProjectModification |
|
|
|
|
|
|
|
protected virtual void ProcessFile(string file) |
|
|
|
{ |
|
|
|
ModifyFile(file, out var isFileModified); |
|
|
|
UpdatePackagesInFile(file, out var isFileModified); |
|
|
|
|
|
|
|
if (isFileModified) |
|
|
|
{ |
|
|
|
var fileDirectory = Path.GetDirectoryName(file).EnsureEndsWith(Path.DirectorySeparatorChar); |
|
|
|
Logger.LogInformation($"Running Yarn on {fileDirectory}"); |
|
|
|
CmdHelper.RunCmd($"cd {fileDirectory} && yarn"); |
|
|
|
Thread.Sleep(500); |
|
|
|
Logger.LogInformation($"Running Gulp on {fileDirectory}"); |
|
|
|
CmdHelper.RunCmd($"cd {fileDirectory} && gulp"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual void ModifyFile(string file, out bool modified) |
|
|
|
protected virtual void UpdatePackagesInFile(string file, out bool isAnyPackageUpdated) |
|
|
|
{ |
|
|
|
modified = false; |
|
|
|
isAnyPackageUpdated = false; |
|
|
|
var fileContent = File.ReadAllText(file); |
|
|
|
|
|
|
|
if (!fileContent.Contains("\"@abp/") && !fileContent.Contains("\"@volo/")) |
|
|
|
@ -56,38 +66,51 @@ namespace Volo.Abp.Cli.ProjectModification |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
var fileObject = JObject.Parse(fileContent); |
|
|
|
var packageJson = JObject.Parse(fileContent); |
|
|
|
|
|
|
|
var dependencies = (JObject)fileObject["dependencies"]; |
|
|
|
var abpPackages = GetAbpPackagesFromPackageJson(packageJson); |
|
|
|
|
|
|
|
var properties = dependencies.Properties(); |
|
|
|
|
|
|
|
foreach (var property in properties) |
|
|
|
foreach (var abpPackage in abpPackages) |
|
|
|
{ |
|
|
|
if (property.Name.StartsWith("@abp/") || property.Name.StartsWith("@volo/")) |
|
|
|
{ |
|
|
|
var version = ""; |
|
|
|
|
|
|
|
if (_fileVersionStorage.ContainsKey(property.Name)) |
|
|
|
{ |
|
|
|
version = _fileVersionStorage[property.Name]; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
_fileVersionStorage[property.Name] = version; |
|
|
|
version = _latestNpmPackageVersionFinder.Find(property.Name); |
|
|
|
} |
|
|
|
|
|
|
|
property.Value.Replace($"^{version}"); |
|
|
|
modified = true; |
|
|
|
Console.WriteLine($"Updated {property.Name} to {version} in {file}."); |
|
|
|
} |
|
|
|
UpdatePackage(file, abpPackage, out isAnyPackageUpdated); |
|
|
|
} |
|
|
|
|
|
|
|
var modifiedFileContent = fileObject.ToString(Formatting.Indented); |
|
|
|
var modifiedFileContent = packageJson.ToString(Formatting.Indented); |
|
|
|
|
|
|
|
File.WriteAllText(file, modifiedFileContent); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual void UpdatePackage(string file, JProperty package, out bool isPackageUpdated) |
|
|
|
{ |
|
|
|
isPackageUpdated = false; |
|
|
|
string version; |
|
|
|
|
|
|
|
if (_fileVersionStorage.ContainsKey(package.Name)) |
|
|
|
{ |
|
|
|
version = _fileVersionStorage[package.Name]; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
version = _latestNpmPackageVersionFinder.Find(package.Name); |
|
|
|
_fileVersionStorage[package.Name] = version; |
|
|
|
} |
|
|
|
|
|
|
|
if (version == (string) package.Value) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
package.Value.Replace($"^{version}"); |
|
|
|
isPackageUpdated = true; |
|
|
|
Logger.LogInformation($"Updated {package.Name} to {version} in {file.Replace(Directory.GetCurrentDirectory(),"")}."); |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual List<JProperty> GetAbpPackagesFromPackageJson(JObject fileObject) |
|
|
|
{ |
|
|
|
var dependencies = (JObject)fileObject["dependencies"]; |
|
|
|
var properties = dependencies.Properties().ToList(); |
|
|
|
var abpPackages = properties.Where(p => p.Name.StartsWith("@abp/") || p.Name.StartsWith("@volo/")).ToList(); |
|
|
|
return abpPackages; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|