Browse Source

Add Npm Packages Updater to Cli

resolves https://github.com/abpframework/abp/issues/1287
pull/1296/head
Yunus Emre Kalkan 7 years ago
parent
commit
b2ef749bd6
  1. 30
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs
  2. 41
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/LatestNpmPackageVersionFinder.cs
  3. 93
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs
  4. 33
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageJsonFileFinder.cs
  5. 4
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs

30
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs

@ -15,18 +15,33 @@ namespace Volo.Abp.Cli.Commands
{
public ILogger<UpdateCommand> 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<UpdateCommand>.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<string> GetUsageInfo()

41
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();
}
}
}

93
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<string, string> _fileVersionStorage = new Dictionary<string, string>();
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);
}
}
}

33
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<string> 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;
}
}
}

4
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloPackagesVersionUpdater.cs → 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;
}
Loading…
Cancel
Save