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

Adding PackageSourceMapping while adding new sources #21049

Merged
merged 3 commits into from
Oct 11, 2024
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
Expand Up @@ -193,7 +193,7 @@ private async Task SwitchProjectsToNightlyPreview(List<string> projects)
var folder = Path.GetDirectoryName(project);

_packageSourceManager.Add(FindSolutionFolder(project) ?? folder, "ABP Nightly",
"https://www.myget.org/F/abp-nightly/api/v3/index.json");
"https://www.myget.org/F/abp-nightly/api/v3/index.json", "Volo.*");

await _nugetPackagesVersionUpdater.UpdateSolutionAsync(
project,
Expand All @@ -213,7 +213,8 @@ private async Task SwitchSolutionsToNightlyPreview(List<string> solutionPaths)
var solutionAngularFolder = GetSolutionAngularFolder(solutionFolder);

_packageSourceManager.Add(solutionFolder, "ABP Nightly",
"https://www.myget.org/F/abp-nightly/api/v3/index.json");
"https://www.myget.org/F/abp-nightly/api/v3/index.json",
"Volo.*");

if (solutionPath != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Xml;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
Expand All @@ -16,7 +17,7 @@ public PackageSourceManager()
Logger = NullLogger<PackageSourceManager>.Instance;
}

public void Add(string solutionFolder, string sourceKey, string sourceValue)
public void Add(string solutionFolder, string sourceKey, string sourceValue, params string[] packageMappingPatterns)
{
var nugetConfigPath = GetNugetConfigPath(solutionFolder);

Expand Down Expand Up @@ -61,6 +62,11 @@ public void Add(string solutionFolder, string sourceKey, string sourceValue)

sourceNodes?[0]?.AppendChild(newNode);

if (packageMappingPatterns.Any())
{
ConfigurePackageSourceMappings(doc, sourceKey, packageMappingPatterns);
}

File.WriteAllText(nugetConfigPath, doc.OuterXml);
}
catch
Expand All @@ -69,6 +75,31 @@ public void Add(string solutionFolder, string sourceKey, string sourceValue)
}
}

protected virtual void ConfigurePackageSourceMappings(XmlDocument doc, string sourceKey, string[] packageMappingPatterns)
{
var packageMappingNodes = doc.SelectNodes("/configuration/packageSourceMapping");

if (packageMappingNodes == null)
{
// If there is no packageSourceMapping node, leave it as it is.
Logger.LogWarning($"<packageSourceMapping> node not found in 'NuGet.Config' file. Skipping adding patterns for '{sourceKey}' source.");
return;
}
var packageSourceNode = doc.CreateElement("packageSource");
var sourceAttr = doc.CreateAttribute("key");
sourceAttr.Value = sourceKey;
packageSourceNode.Attributes.Append(sourceAttr);
packageMappingNodes[0]?.AppendChild(packageSourceNode);
foreach (var pattern in packageMappingPatterns)
{
var packageNode = doc.CreateElement("package");
var patternAttr = doc.CreateAttribute("pattern");
patternAttr.Value = pattern;
packageNode.Attributes.Append(patternAttr);
packageSourceNode.AppendChild(packageNode);
}
}

public void Remove(string solutionFolder, string sourceKey)
{
var nugetConfigPath = GetNugetConfigPath(solutionFolder);
Expand Down
Loading