From de67b01444603ef31ad161fcc7ace1709f50f4ad Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 2 Aug 2022 11:16:56 +0300 Subject: [PATCH] Cli: Make Cli compatible with LeptonX packages --- .../SolutionPackageVersionFinder.cs | 110 +++++------------- .../VoloNugetPackagesVersionUpdater.cs | 16 ++- 2 files changed, 45 insertions(+), 81 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionPackageVersionFinder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionPackageVersionFinder.cs index f33d7f8ff1..060a8cb8f2 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionPackageVersionFinder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionPackageVersionFinder.cs @@ -1,5 +1,6 @@ -using System.IO; -using System.Text.RegularExpressions; +using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Xml; using NuGet.Versioning; using Volo.Abp.Cli.Utils; @@ -9,13 +10,13 @@ namespace Volo.Abp.Cli.ProjectModification; public class SolutionPackageVersionFinder : ITransientDependency { - public string Find(string solutionFile, string packagePrefix = "Volo.Abp") + public string Find(string solutionFile, string packagePrefix = "Volo.Abp", string excludedKeywords = "LeptonX") { var projectFilesUnderSrc = GetProjectFilesOfSolution(solutionFile); foreach (var projectFile in projectFilesUnderSrc) { var content = File.ReadAllText(projectFile); - if (TryParseVersionFromCsprojViaXmlDocument(content, out var s, packagePrefix)) + if (TryParseVersionFromCsprojViaXmlDocument(content, out var s, packagePrefix, excludedKeywords)) { return s; } @@ -24,14 +25,35 @@ public class SolutionPackageVersionFinder : ITransientDependency return null; } - private static bool TryParseVersionFromCsprojViaXmlDocument(string content, out string version, string packagePrefix) + private static bool TryParseVersionFromCsprojViaXmlDocument(string content, out string version, string packagePrefix, string excludedKeywords) { var doc = new XmlDocument() { PreserveWhitespace = true }; using (var stream = StreamHelper.GenerateStreamFromString(content)) { doc.Load(stream); var nodes = doc.SelectNodes($"/Project/ItemGroup/PackageReference[starts-with(@Include, '{packagePrefix}')]"); - var value = nodes?[0]?.Attributes?["Version"]?.Value; + + var targetNodes = new List(); + + foreach (XmlNode node in nodes!) + { + var packageId = node!.Attributes["Include"]?.Value; + + if (excludedKeywords.Split(',').Any(ek => packageId!.Contains(ek))) + { + continue; + } + + targetNodes.Add(node); + } + + if (!targetNodes.Any()) + { + version = null; + return false; + } + + var value = targetNodes.First().Attributes?["Version"]?.Value; if (value == null) { version = null; @@ -43,40 +65,17 @@ public class SolutionPackageVersionFinder : ITransientDependency } } - public static bool TryParseVersionFromCsprojFile(string csprojContent, out string version, string packagePrefix = "Volo.Abp") + public static bool TryParseVersionFromCsprojFile(string csprojContent, out string version, string packagePrefix = "Volo.Abp", string excludedKeywords = "LeptonX") { - try - { - var matches = Regex.Matches(csprojContent, - @"PackageReference\s*Include\s*=\s*\""" + packagePrefix + @"(.*?)\""\s*Version\s*=\s*\""(.*?)\""", - RegexOptions.IgnoreCase | - RegexOptions.IgnorePatternWhitespace | - RegexOptions.Singleline | RegexOptions.Multiline); - - foreach (Match match in matches) - { - if (match.Groups.Count > 2) - { - version = match.Groups[2].Value; - return true; - } - } - } - catch - { - //ignored - } - - version = null; - return false; + return TryParseVersionFromCsprojViaXmlDocument(csprojContent, out version, packagePrefix, excludedKeywords); } - public static bool TryParseSemanticVersionFromCsprojFile(string csprojContent, out SemanticVersion version, string packagePrefix = "Volo.Abp") + public static bool TryParseSemanticVersionFromCsprojFile(string csprojContent, out SemanticVersion version, string packagePrefix = "Volo.Abp", string excludedKeywords = "LeptonX") { try { - if (TryParseVersionFromCsprojFile(csprojContent, out var versionText, packagePrefix)) + if (TryParseVersionFromCsprojViaXmlDocument(csprojContent, out var versionText, packagePrefix, excludedKeywords)) { return SemanticVersion.TryParse(versionText, out version); } @@ -90,51 +89,6 @@ public class SolutionPackageVersionFinder : ITransientDependency return false; } - public static bool TryFind(string solutionFile, out string version, string packagePrefix = "Volo.Abp") - { - var projectFiles = GetProjectFilesOfSolution(solutionFile); - foreach (var projectFile in projectFiles) - { - var csprojContent = File.ReadAllText(projectFile); - if (TryParseVersionFromCsprojFile(csprojContent, out var parsedVersion, packagePrefix)) - { - version = parsedVersion; - return true; - } - } - - version = null; - return false; - } - - public static bool TryFindSemanticVersion(string solutionFile, out SemanticVersion version, string packagePrefix = "Volo.Abp") - { - var projectFiles = GetProjectFilesOfSolution(solutionFile); - foreach (var projectFile in projectFiles) - { - var csprojContent = File.ReadAllText(projectFile); - if (TryParseSemanticVersionFromCsprojFile(csprojContent, out var parsedVersion, packagePrefix)) - { - version = parsedVersion; - return true; - } - } - - version = null; - return false; - } - - //public static bool TryFindSemanticVersion(string solutionFile, out SemanticVersion version) - //{ - // if (TryFind(solutionFile, out var versionText)) - // { - // return SemanticVersion.TryParse(versionText, out version); - // } - - // version = null; - // return false; - //} - private static string[] GetProjectFilesOfSolution(string solutionFile) { var solutionDirectory = Path.GetDirectoryName(solutionFile); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs index 98ad57f4b4..578d010214 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/VoloNugetPackagesVersionUpdater.cs @@ -183,11 +183,18 @@ public class VoloNugetPackagesVersionUpdater : ITransientDependency var versionAttribute = package.Attributes["Version"]; var currentVersion = versionAttribute.Value; var currentSemanticVersion = SemanticVersion.Parse(currentVersion); + + var isLeptonXPackage = packageId.Contains("LeptonX"); Logger.LogDebug("Checking package: \"{0}\" - Current version: {1}", packageId, currentSemanticVersion); if (!specifiedVersion.IsNullOrWhiteSpace()) { + if (isLeptonXPackage) + { + continue; + } + if (await SpecifiedVersionExists(specifiedVersion, packageId)) { var specifiedSemanticVersion = SemanticVersion.Parse(specifiedVersion); @@ -210,7 +217,8 @@ public class VoloNugetPackagesVersionUpdater : ITransientDependency { if ((includeNightlyPreviews || (currentVersion.Contains("-preview") && !switchToStable)) && !includeReleaseCandidates) { - var latestVersion = latestMyGetVersion ?? await GetLatestVersionFromMyGet(packageId); + var latestVersion = latestMyGetVersion == null || isLeptonXPackage ? + await GetLatestVersionFromMyGet(packageId) : latestMyGetVersion; if (currentVersion != latestVersion) { @@ -227,11 +235,13 @@ public class VoloNugetPackagesVersionUpdater : ITransientDependency SemanticVersion latestVersion; if (currentSemanticVersion.IsPrerelease && !switchToStable) { - latestVersion = latestNugetReleaseCandidateVersion ?? await _nuGetService.GetLatestVersionOrNullAsync(packageId, includeReleaseCandidates: true); + latestVersion = latestNugetReleaseCandidateVersion == null || isLeptonXPackage ? + await _nuGetService.GetLatestVersionOrNullAsync(packageId, includeReleaseCandidates: true) : latestNugetReleaseCandidateVersion; } else { - latestVersion = latestNugetVersion ?? await _nuGetService.GetLatestVersionOrNullAsync(packageId, includeReleaseCandidates: includeReleaseCandidates); + latestVersion = latestNugetVersion == null || isLeptonXPackage ? + await _nuGetService.GetLatestVersionOrNullAsync(packageId, includeReleaseCandidates: includeReleaseCandidates) : latestNugetVersion; } if (latestVersion != null && (currentSemanticVersion < latestVersion || (currentSemanticVersion.IsPrerelease && switchToStable)))