Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli: Make Cli compatible with LeptonX packages #13520

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand All @@ -24,14 +25,35 @@ public string Find(string solutionFile, string packagePrefix = "Volo.Abp")
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<XmlNode>();

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;
Expand All @@ -43,40 +65,17 @@ private static bool TryParseVersionFromCsprojViaXmlDocument(string content, out
}
}

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);
}
Expand All @@ -90,51 +89,6 @@ public static bool TryParseSemanticVersionFromCsprojFile(string csprojContent, o
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,18 @@ private async Task<string> UpdateVoloPackagesAsync(string content,
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);
Expand All @@ -210,7 +217,8 @@ private async Task<string> UpdateVoloPackagesAsync(string content,
{
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)
{
Expand All @@ -227,11 +235,13 @@ private async Task<string> UpdateVoloPackagesAsync(string content,
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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public void Find_Abp_Version()
"<ProjectReference Include=\"..\\Blazoor.EfCore07062034.Domain.Shared\\Blazoor.EfCore07062034.Domain.Shared.csproj\" />" +
"</ItemGroup>" +
"<ItemGroup>" +
"< PackageReference Include = \"Volo.Abp.Emailing\" Version = \"4.4.0-rc.1\" />" +
"<PackageReference Include= \"Volo.Abp.PermissionManagement.Domain.Identity\" Version= \"4.4.0-rc.1\" />" +
"<PackageReference Include=\"Volo.Abp.Emailing\" Version=\"4.4.0-rc.1\" />" +
"<PackageReference Include=\"Volo.Abp.PermissionManagement.Domain.Identity\" Version=\"4.4.0-rc.1\" />" +
"<PackageReference Include=\"Volo.Abp.IdentityServer.Domain\" Version=\"4.4.0-rc.1\" />" +
"<PackageReference Include=\"Volo.Abp.PermissionManagement.Domain.IdentityServer\" Version=\"4.4.0-rc.1\" />" +
"<PackageReference Include=\"Volo.Abp.BackgroundJobs.Domain\" Version=\"4.4.0-rc.1\" />" +
Expand Down Expand Up @@ -54,7 +54,7 @@ public void Find_Abp_Semantic_Version()
"<ProjectReference Include=\"..\\Blazoor.EfCore07062034.Domain.Shared\\Blazoor.EfCore07062034.Domain.Shared.csproj\" />" +
"</ItemGroup>" +
"<ItemGroup>" +
"< PackageReference Include = \"Volo.Abp.Emailing\" Version= \"12.8.3-beta.1\" />" +
"<PackageReference Include=\"Volo.Abp.Emailing\" Version=\"12.8.3-beta.1\" />" +
"</ItemGroup>" +
"</Project>";

Expand Down