Browse Source

switch Preview npm package & Refactor

npm previews are not tested yet
pull/2868/head
Yunus Emre Kalkan 7 years ago
parent
commit
a835d6bca4
  1. 6
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/UpdateCommand.cs
  2. 1
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/MyGetPackageListFinder.cs
  3. 58
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs
  4. 149
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceSwitcher.cs
  5. 18
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs

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

@ -42,13 +42,13 @@ namespace Volo.Abp.Cli.Commands
if (updateNpm || !updateNuget) if (updateNpm || !updateNuget)
{ {
UpdateNpmPackages(directory); await UpdateNpmPackages(directory);
} }
} }
private void UpdateNpmPackages(string directory) private async Task UpdateNpmPackages(string directory)
{ {
_npmPackagesUpdater.Update(directory); await _npmPackagesUpdater.Update(directory);
} }
private async Task UpdateNugetPackages(CommandLineArgs commandLineArgs, string directory) private async Task UpdateNugetPackages(CommandLineArgs commandLineArgs, string directory)

1
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/MyGetPackageListFinder.cs

@ -46,5 +46,6 @@ namespace Volo.Abp.Cli.ProjectModification
return _response; return _response;
} }
} }
} }

58
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -18,18 +19,20 @@ namespace Volo.Abp.Cli.ProjectModification
private readonly PackageJsonFileFinder _packageJsonFileFinder; private readonly PackageJsonFileFinder _packageJsonFileFinder;
private readonly NpmGlobalPackagesChecker _npmGlobalPackagesChecker; private readonly NpmGlobalPackagesChecker _npmGlobalPackagesChecker;
private readonly MyGetPackageListFinder _myGetPackageListFinder;
private readonly Dictionary<string, string> _fileVersionStorage = new Dictionary<string, string>(); private readonly Dictionary<string, string> _fileVersionStorage = new Dictionary<string, string>();
public NpmPackagesUpdater(PackageJsonFileFinder packageJsonFileFinder, NpmGlobalPackagesChecker npmGlobalPackagesChecker) public NpmPackagesUpdater(PackageJsonFileFinder packageJsonFileFinder, NpmGlobalPackagesChecker npmGlobalPackagesChecker, MyGetPackageListFinder myGetPackageListFinder)
{ {
_packageJsonFileFinder = packageJsonFileFinder; _packageJsonFileFinder = packageJsonFileFinder;
_npmGlobalPackagesChecker = npmGlobalPackagesChecker; _npmGlobalPackagesChecker = npmGlobalPackagesChecker;
_myGetPackageListFinder = myGetPackageListFinder;
Logger = NullLogger<NpmPackagesUpdater>.Instance; Logger = NullLogger<NpmPackagesUpdater>.Instance;
} }
public void Update(string rootDirectory) public async Task Update(string rootDirectory, bool includePreviews = false, bool switchToStable = false)
{ {
var fileList = _packageJsonFileFinder.Find(rootDirectory); var fileList = _packageJsonFileFinder.Find(rootDirectory);
@ -42,7 +45,7 @@ namespace Volo.Abp.Cli.ProjectModification
foreach (var file in fileList) foreach (var file in fileList)
{ {
UpdatePackagesInFile(file, out var packagesUpdated); var packagesUpdated = await UpdatePackagesInFile(file, includePreviews, switchToStable);
if (packagesUpdated) if (packagesUpdated)
{ {
@ -64,21 +67,21 @@ namespace Volo.Abp.Cli.ProjectModification
return File.Exists(Path.Combine(fileDirectory, "angular.json")); return File.Exists(Path.Combine(fileDirectory, "angular.json"));
} }
protected virtual void UpdatePackagesInFile(string file, out bool packagesUpdated) protected virtual async Task<bool> UpdatePackagesInFile(string file, bool includePreviews = false, bool switchToStable = false)
{ {
packagesUpdated = false; var packagesUpdated = false;
var fileContent = File.ReadAllText(file); var fileContent = File.ReadAllText(file);
var packageJson = JObject.Parse(fileContent); var packageJson = JObject.Parse(fileContent);
var abpPackages = GetAbpPackagesFromPackageJson(packageJson); var abpPackages = GetAbpPackagesFromPackageJson(packageJson);
if (!abpPackages.Any()) if (!abpPackages.Any())
{ {
return; return packagesUpdated;
} }
foreach (var abpPackage in abpPackages) foreach (var abpPackage in abpPackages)
{ {
TryUpdatePackage(file, abpPackage, out var updated); var updated = await TryUpdatePackage(file, abpPackage, includePreviews, switchToStable);
if (updated) if (updated)
{ {
@ -89,18 +92,23 @@ namespace Volo.Abp.Cli.ProjectModification
var modifiedFileContent = packageJson.ToString(Formatting.Indented); var modifiedFileContent = packageJson.ToString(Formatting.Indented);
File.WriteAllText(file, modifiedFileContent); File.WriteAllText(file, modifiedFileContent);
return packagesUpdated;
} }
protected virtual void TryUpdatePackage(string file, JProperty package, out bool updated) protected virtual async Task<bool> TryUpdatePackage(string file, JProperty package,
bool includePreviews = false, bool switchToStable = false)
{ {
var version = GetLatestVersion(package); var updated = false;
var currentVersion = (string)package.Value;
var version = await GetLatestVersion(package, currentVersion, includePreviews, switchToStable);
var versionWithPrefix = $"^{version}"; var versionWithPrefix = $"^{version}";
if (versionWithPrefix == (string)package.Value) if (versionWithPrefix == currentVersion)
{ {
updated = false; return false;
return;
} }
else else
{ {
@ -110,25 +118,41 @@ namespace Volo.Abp.Cli.ProjectModification
package.Value.Replace(versionWithPrefix); package.Value.Replace(versionWithPrefix);
Logger.LogInformation($"Updated {package.Name} to {version} in {file.Replace(Directory.GetCurrentDirectory(), "")}."); Logger.LogInformation($"Updated {package.Name} to {version} in {file.Replace(Directory.GetCurrentDirectory(), "")}.");
return updated;
} }
protected virtual string GetLatestVersion(JProperty package) protected virtual async Task<string> GetLatestVersion(JProperty package, string currentVersion,
bool includePreviews = false, bool switchToStable = false)
{ {
if (_fileVersionStorage.ContainsKey(package.Name)) if (_fileVersionStorage.ContainsKey(package.Name))
{ {
return _fileVersionStorage[package.Name]; return _fileVersionStorage[package.Name];
} }
var version = CmdHelper.RunCmdAndGetOutput($"npm show {package.Name} version"); string newVersion = currentVersion;
if (includePreviews || (!switchToStable && currentVersion.Contains("-preview")))
{
var mygetPackage = (await _myGetPackageListFinder.GetPackages()).Packages.FirstOrDefault(p => p.Id == package.Name);
if (mygetPackage != null)
{
newVersion = mygetPackage.Versions.Last();
}
}
else
{
newVersion = CmdHelper.RunCmdAndGetOutput($"npm show {package.Name} version");
}
_fileVersionStorage[package.Name] = version; _fileVersionStorage[package.Name] = newVersion;
return version; return newVersion;
} }
protected virtual List<JProperty> GetAbpPackagesFromPackageJson(JObject fileObject) protected virtual List<JProperty> GetAbpPackagesFromPackageJson(JObject fileObject)
{ {
var dependencyList = new [] { "dependencies", "devDependencies", "peerDependencies" }; var dependencyList = new[] { "dependencies", "devDependencies", "peerDependencies" };
var abpPackages = new List<JProperty>(); var abpPackages = new List<JProperty>();
foreach (var dependencyListName in dependencyList) foreach (var dependencyListName in dependencyList)

149
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceSwitcher.cs

@ -1,30 +1,29 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.Cli.Args; using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.NuGet;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.ProjectModification namespace Volo.Abp.Cli.ProjectModification
{ {
public class PackageSourceSwitcher : ITransientDependency public class PackageSourceSwitcher : ITransientDependency
{ {
private readonly MyGetPackageListFinder _myGetPackageListFinder;
private readonly NuGetService _nuGetService;
private readonly PackageSourceAdder _packageSourceAdder; private readonly PackageSourceAdder _packageSourceAdder;
private readonly NpmPackagesUpdater _npmPackagesUpdater;
private readonly VoloNugetPackagesVersionUpdater _nugetPackagesVersionUpdater;
public ILogger<PackageSourceSwitcher> Logger { get; set; } public ILogger<PackageSourceSwitcher> Logger { get; set; }
public PackageSourceSwitcher(MyGetPackageListFinder myGetPackageListFinder, NuGetService nuGetService, PackageSourceAdder packageSourceAdder) public PackageSourceSwitcher(PackageSourceAdder packageSourceAdder,
NpmPackagesUpdater npmPackagesUpdater,
VoloNugetPackagesVersionUpdater nugetPackagesVersionUpdater)
{ {
_myGetPackageListFinder = myGetPackageListFinder;
_nuGetService = nuGetService;
_packageSourceAdder = packageSourceAdder; _packageSourceAdder = packageSourceAdder;
_npmPackagesUpdater = npmPackagesUpdater;
_nugetPackagesVersionUpdater = nugetPackagesVersionUpdater;
Logger = NullLogger<PackageSourceSwitcher>.Instance; Logger = NullLogger<PackageSourceSwitcher>.Instance;
} }
@ -32,113 +31,29 @@ namespace Volo.Abp.Cli.ProjectModification
{ {
_packageSourceAdder.Add("ABP Nightly", "https://www.myget.org/F/abp-nightly/api/v3/index.json"); _packageSourceAdder.Add("ABP Nightly", "https://www.myget.org/F/abp-nightly/api/v3/index.json");
await Switch(commandLineArgs, SwitchTarget.Preview); await _nugetPackagesVersionUpdater.UpdateSolutionAsync(
} GetSolutionPath(commandLineArgs),
true);
public async Task SwitchToStable(CommandLineArgs commandLineArgs)
{
await Switch(commandLineArgs, SwitchTarget.Stable);
}
private async Task Switch(CommandLineArgs commandLineArgs, SwitchTarget target)
{
var solutionPath = GetSolutionPath(commandLineArgs);
var latestStableAbpVersion = target == SwitchTarget.Stable ? (await _nuGetService.GetLatestVersionOrNullAsync("Volo.Abp")).ToString() : null;
Logger.LogInformation($"Packages on solution \"{Path.GetFileName(solutionPath)}\" are being switched to {target}.");
Logger.LogInformation("");
var projectFiles = GetCsprojFiles(solutionPath);
foreach (var projectFile in projectFiles)
{
Logger.LogInformation($"Packages on project \"{Path.GetFileName(projectFile)}\" are being switched to {target}.");
var content = File.ReadAllText(projectFile);
var doc = new XmlDocument() { PreserveWhitespace = true };
doc.Load(GenerateStreamFromString(content));
doc = await SwitchPackagesInDocument(doc, content, target, latestStableAbpVersion);
File.WriteAllText(projectFile, doc.OuterXml);
}
Logger.LogInformation("");
Logger.LogInformation($"Packages on solution \"{Path.GetFileName(solutionPath)}\" are switched to {target}.");
}
private async Task<XmlDocument> SwitchPackagesInDocument(XmlDocument doc, string content, SwitchTarget target, string latestStableAbpVersion = null)
{
Check.NotNull(content, nameof(content));
var nodes = doc.SelectNodes("/Project/ItemGroup/PackageReference[@Include]");
foreach (XmlNode node in nodes)
{
var packageName = node.Attributes["Include"].Value;
if (!packageName.StartsWith("Volo.Abp"))
{
continue;
}
var version = node.Attributes["Version"].Value; await _npmPackagesUpdater.Update(
Path.GetFileName(GetSolutionPath(commandLineArgs)),
XmlNode newNode = null; true);
if (target == SwitchTarget.Stable)
{
newNode = GetNewStableReferenceNode(doc, packageName, version, latestStableAbpVersion);
}
else if (target == SwitchTarget.Preview)
{
var packageList = (await _myGetPackageListFinder.GetPackages()).Packages;
newNode = GetNewPreviewReferenceNode(doc, packageName, version, packageList);
}
node.ParentNode?.ReplaceChild(newNode, node);
}
return doc;
}
private XmlElement GetNewStableReferenceNode(XmlDocument doc, string packageName, string version, string newVersion)
{
var newNode = doc.CreateElement("PackageReference");
var includeAttr = doc.CreateAttribute("Include");
includeAttr.Value = packageName;
newNode.Attributes.Append(includeAttr);
var versionAttr = doc.CreateAttribute("Version");
versionAttr.Value = newVersion;
newNode.Attributes.Append(versionAttr);
return newNode;
} }
private XmlElement GetNewPreviewReferenceNode(XmlDocument doc, string packageName, string version, List<MyGetPackage> packageList) public async Task SwitchToStable(CommandLineArgs commandLineArgs)
{
var newVersion = packageList.FirstOrDefault(p => p.Id == packageName)?.Versions.LastOrDefault() ?? version;
var newNode = doc.CreateElement("PackageReference");
var includeAttr = doc.CreateAttribute("Include");
includeAttr.Value = packageName;
newNode.Attributes.Append(includeAttr);
var versionAttr = doc.CreateAttribute("Version");
versionAttr.Value = newVersion;
newNode.Attributes.Append(versionAttr);
return newNode;
}
private List<string> GetCsprojFiles(string slnPath)
{ {
return Directory.GetFiles(Path.GetDirectoryName(slnPath), "*.csproj", SearchOption.AllDirectories).ToList(); await _nugetPackagesVersionUpdater.UpdateSolutionAsync(
GetSolutionPath(commandLineArgs),
false,
true);
await _npmPackagesUpdater.Update(
Path.GetFileName(GetSolutionPath(commandLineArgs)),
false,
true);
} }
private string GetSolutionPath(CommandLineArgs commandLineArgs) private string GetSolutionPath(CommandLineArgs commandLineArgs)
{ {
var solutionPath = commandLineArgs.Options.GetOrNull(Options.SolutionPath.Short, Options.SolutionPath.Long); var solutionPath = commandLineArgs.Options.GetOrNull(Options.SolutionPath.Short, Options.SolutionPath.Long);
@ -159,16 +74,6 @@ namespace Volo.Abp.Cli.ProjectModification
return solutionPath; return solutionPath;
} }
private static Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
public static class Options public static class Options
{ {
public static class SolutionPath public static class SolutionPath
@ -177,11 +82,5 @@ namespace Volo.Abp.Cli.ProjectModification
public const string Long = "solution-path"; public const string Long = "solution-path";
} }
} }
enum SwitchTarget
{
Preview,
Stable
}
} }
} }

18
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs

@ -24,29 +24,29 @@ namespace Volo.Abp.Cli.ProjectModification
Logger = NullLogger<VoloNugetPackagesVersionUpdater>.Instance; Logger = NullLogger<VoloNugetPackagesVersionUpdater>.Instance;
} }
public async Task UpdateSolutionAsync(string solutionPath, bool includePreviews) public async Task UpdateSolutionAsync(string solutionPath, bool includePreviews = false, bool switchToStable = false)
{ {
var projectPaths = ProjectFinder.GetProjectFiles(solutionPath); var projectPaths = ProjectFinder.GetProjectFiles(solutionPath);
foreach (var filePath in projectPaths) foreach (var filePath in projectPaths)
{ {
await UpdateInternalAsync(filePath, includePreviews); await UpdateInternalAsync(filePath, includePreviews, switchToStable);
} }
} }
public async Task UpdateProjectAsync(string projectPath, bool includePreviews) public async Task UpdateProjectAsync(string projectPath, bool includePreviews = false, bool switchToStable = false)
{ {
await UpdateInternalAsync(projectPath, includePreviews); await UpdateInternalAsync(projectPath, includePreviews, switchToStable);
} }
protected virtual async Task UpdateInternalAsync(string projectPath, bool includePreviews) protected virtual async Task UpdateInternalAsync(string projectPath, bool includePreviews = false, bool switchToStable = false)
{ {
var fileContent = File.ReadAllText(projectPath); var fileContent = File.ReadAllText(projectPath);
File.WriteAllText(projectPath, await UpdateVoloPackagesAsync(fileContent, includePreviews)); File.WriteAllText(projectPath, await UpdateVoloPackagesAsync(fileContent, includePreviews, switchToStable));
} }
private async Task<string> UpdateVoloPackagesAsync(string content, bool includePreviews) private async Task<string> UpdateVoloPackagesAsync(string content, bool includePreviews = false, bool switchToStable = false)
{ {
string packageId = null; string packageId = null;
@ -79,7 +79,7 @@ namespace Volo.Abp.Cli.ProjectModification
Logger.LogDebug("Checking package: \"{0}\" - Current version: {1}", packageId, packageVersion); Logger.LogDebug("Checking package: \"{0}\" - Current version: {1}", packageId, packageVersion);
if (currentVersion.Contains("preview") || includePreviews) if (includePreviews || (currentVersion.Contains("-preview") && !switchToStable))
{ {
var latestVersion = (await _myGetPackageListFinder.GetPackages()).Packages var latestVersion = (await _myGetPackageListFinder.GetPackages()).Packages
.FirstOrDefault(p => p.Id == packageId) .FirstOrDefault(p => p.Id == packageId)
@ -99,7 +99,7 @@ namespace Volo.Abp.Cli.ProjectModification
{ {
var latestVersion = await _nuGetService.GetLatestVersionOrNullAsync(packageId); var latestVersion = await _nuGetService.GetLatestVersionOrNullAsync(packageId);
if (latestVersion != null && packageVersion < latestVersion) if (latestVersion != null && (currentVersion.Contains("-preview") || packageVersion < latestVersion))
{ {
Logger.LogInformation("Updating package \"{0}\" from v{1} to v{2}.", packageId, packageVersion.ToString(), latestVersion.ToString()); Logger.LogInformation("Updating package \"{0}\" from v{1} to v{2}.", packageId, packageVersion.ToString(), latestVersion.ToString());
versionAttribute.Value = latestVersion.ToString(); versionAttribute.Value = latestVersion.ToString();

Loading…
Cancel
Save