diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs index df95839767..56fed5f7e4 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs @@ -15,18 +15,33 @@ namespace Volo.Abp.Cli.Commands { public ILogger Logger { get; set; } - private readonly VoloPackagesVersionUpdater _packagesVersionUpdater; + private readonly VoloNugetPackagesVersionUpdater _nugetPackagesVersionUpdater; + private readonly NpmPackagesUpdater _npmPackagesUpdater; - public UpdateCommand(VoloPackagesVersionUpdater packagesVersionUpdater) + public UpdateCommand(VoloNugetPackagesVersionUpdater nugetPackagesVersionUpdater, NpmPackagesUpdater npmPackagesUpdater) { - _packagesVersionUpdater = packagesVersionUpdater; + _nugetPackagesVersionUpdater = nugetPackagesVersionUpdater; + _npmPackagesUpdater = npmPackagesUpdater; Logger = NullLogger.Instance; } public async Task ExecuteAsync(CommandLineArgs commandLineArgs) { - var includePreviews = commandLineArgs.Options.GetOrNull(Options.IncludePreviews.Short, Options.IncludePreviews.Long) != null; + UpdateNugetPackages(commandLineArgs); + UpdateNpmPackages(); + return; + } + + private void UpdateNpmPackages() + { + _npmPackagesUpdater.Update(Directory.GetCurrentDirectory()); + } + + private void UpdateNugetPackages(CommandLineArgs commandLineArgs) + { + var includePreviews = + commandLineArgs.Options.GetOrNull(Options.IncludePreviews.Short, Options.IncludePreviews.Long) != null; var solution = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln").FirstOrDefault(); @@ -34,7 +49,7 @@ namespace Volo.Abp.Cli.Commands { var solutionName = Path.GetFileName(solution).RemovePostFix(".sln"); - _packagesVersionUpdater.UpdateSolution(solution, includePreviews); + _nugetPackagesVersionUpdater.UpdateSolution(solution, includePreviews); Logger.LogInformation($"Volo packages are updated in {solutionName} solution."); return; @@ -46,13 +61,14 @@ namespace Volo.Abp.Cli.Commands { var projectName = Path.GetFileName(project).RemovePostFix(".csproj"); - _packagesVersionUpdater.UpdateProject(project, includePreviews); + _nugetPackagesVersionUpdater.UpdateProject(project, includePreviews); Logger.LogInformation($"Volo packages are updated in {projectName} project."); return; } - throw new CliUsageException("No solution or project found in this directory." + Environment.NewLine + Environment.NewLine + GetUsageInfo()); + throw new CliUsageException("No solution or project found in this directory." + Environment.NewLine + + Environment.NewLine + GetUsageInfo()); } public Task GetUsageInfo() diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LatestNpmPackageVersionFinder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LatestNpmPackageVersionFinder.cs new file mode 100644 index 0000000000..5f7887170a --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LatestNpmPackageVersionFinder.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Text; +using Volo.Abp.Cli.Utils; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ProjectModification +{ + public class LatestNpmPackageVersionFinder : ITransientDependency + { + public string Find(string packageName) + { + var output = ""; + + using (var process = new Process()) + { + process.StartInfo = new ProcessStartInfo(CmdHelper.GetFileName()) + { + Arguments = CmdHelper.GetArguments($"npm show {packageName} version"), + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true + }; + process.Start(); + + using (var stdOut = process.StandardOutput) + { + using (var stdErr = process.StandardError) + { + output = stdOut.ReadToEnd(); + output += stdErr.ReadToEnd(); + } + } + } + + return output.Trim(); + } + } +} 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 new file mode 100644 index 0000000000..ad4827aba7 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Volo.Abp.Cli.Utils; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ProjectModification +{ + public class NpmPackagesUpdater : ITransientDependency + { + private readonly LatestNpmPackageVersionFinder _latestNpmPackageVersionFinder; + private readonly PackageJsonFileFinder _packageJsonFileFinder; + + private readonly Dictionary _fileVersionStorage = new Dictionary(); + + public NpmPackagesUpdater(LatestNpmPackageVersionFinder latestNpmPackageVersionFinder, PackageJsonFileFinder packageJsonFileFinder) + { + _latestNpmPackageVersionFinder = latestNpmPackageVersionFinder; + _packageJsonFileFinder = packageJsonFileFinder; + } + + public void Update(string rootDirectory) + { + var fileList = _packageJsonFileFinder.Find(rootDirectory); + + foreach (var file in fileList) + { + ProcessFile(file); + } + } + + protected virtual void ProcessFile(string file) + { + ModifyFile(file, out var isFileModified); + + if (isFileModified) + { + var fileDirectory = Path.GetDirectoryName(file).EnsureEndsWith(Path.DirectorySeparatorChar); + CmdHelper.RunCmd($"cd {fileDirectory} && yarn"); + Thread.Sleep(500); + CmdHelper.RunCmd($"cd {fileDirectory} && gulp"); + } + } + + protected virtual void ModifyFile(string file, out bool modified) + { + modified = false; + var fileContent = File.ReadAllText(file); + + if (!fileContent.Contains("\"@abp/") && !fileContent.Contains("\"@volo/")) + { + return; + } + + var fileObject = JObject.Parse(fileContent); + + var dependencies = (JObject)fileObject["dependencies"]; + + var properties = dependencies.Properties(); + + foreach (var property in properties) + { + 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}."); + } + } + + var modifiedFileContent = fileObject.ToString(Formatting.Indented); + + File.WriteAllText(file, modifiedFileContent); + } + + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageJsonFileFinder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageJsonFileFinder.cs new file mode 100644 index 0000000000..cf0c2335d4 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageJsonFileFinder.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ProjectModification +{ + public class PackageJsonFileFinder : ITransientDependency + { + public List Find(string rootDirectory) + { + var slash = Path.DirectorySeparatorChar; + + return + Directory.GetFiles(rootDirectory.EnsureEndsWith(Path.DirectorySeparatorChar), "*package.json", SearchOption.AllDirectories) + .Where(f => + !f.Contains(slash + "node_modules" + slash) && + !f.Contains(slash + "Release" + slash) && + !f.Contains(slash + "Debug" + slash) && + IsWithProjectFile(f) + ).ToList(); + } + + protected virtual bool IsWithProjectFile(string path) + { + var dir = Path.GetDirectoryName(path); + + return Directory.GetFiles(dir, "*.csproj", searchOption: SearchOption.TopDirectoryOnly).Length > 0; + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloPackagesVersionUpdater.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs similarity index 95% rename from framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloPackagesVersionUpdater.cs rename to framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs index 8e0be8f1a1..545f5b0b05 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloPackagesVersionUpdater.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs @@ -6,11 +6,11 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.ProjectModification { - public class VoloPackagesVersionUpdater : ITransientDependency + public class VoloNugetPackagesVersionUpdater : ITransientDependency { private readonly NuGetService _nuGetService; - public VoloPackagesVersionUpdater(NuGetService nuGetService) + public VoloNugetPackagesVersionUpdater(NuGetService nuGetService) { _nuGetService = nuGetService; }