diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackagePreviewSwitcher.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackagePreviewSwitcher.cs index 9e65595182..b17b4250f4 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackagePreviewSwitcher.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackagePreviewSwitcher.cs @@ -132,6 +132,7 @@ public class PackagePreviewSwitcher : ITransientDependency var solutionAngularFolder = GetSolutionAngularFolder(solutionFolder); _packageSourceManager.Remove(solutionFolder, "ABP Nightly"); + _packageSourceManager.RemovePackageSourceMapping(solutionFolder, "ABP Nightly"); await _nugetPackagesVersionUpdater.UpdateSolutionAsync( solutionPath, @@ -177,10 +178,13 @@ public class PackagePreviewSwitcher : ITransientDependency foreach (var project in projects) { var folder = Path.GetDirectoryName(project); + var solutionFolder = FindSolutionFolder(project) ?? folder; - _packageSourceManager.Add(FindSolutionFolder(project) ?? folder, "ABP Nightly", + _packageSourceManager.Add(solutionFolder, "ABP Nightly", "https://www.myget.org/F/abp-nightly/api/v3/index.json"); + _packageSourceManager.AddPackageSourceMapping(solutionFolder, "ABP Nightly", "Volo.*"); + await _nugetPackagesVersionUpdater.UpdateSolutionAsync( project, true); @@ -200,6 +204,8 @@ public class PackagePreviewSwitcher : ITransientDependency _packageSourceManager.Add(solutionFolder, "ABP Nightly", "https://www.myget.org/F/abp-nightly/api/v3/index.json"); + + _packageSourceManager.AddPackageSourceMapping(solutionFolder, "ABP Nightly", "Volo.*"); if (solutionPath != null) { diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceManager.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceManager.cs index 90364781d3..cf93c93fa1 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceManager.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceManager.cs @@ -108,6 +108,88 @@ public class PackageSourceManager : ITransientDependency } } + public void AddPackageSourceMapping(string solutionFolder, string sourceKey, string sourceValue) + { + var nugetConfigPath = GetNugetConfigPath(solutionFolder); + if (!File.Exists(nugetConfigPath)) + { + return; + } + + var fileContent = File.ReadAllText(nugetConfigPath); + if (fileContent.Contains($"")) + { + return; + } + + try + { + var doc = new XmlDocument() { PreserveWhitespace = true }; + + doc.Load(GenerateStreamFromString(fileContent)); + + var sourceNodes = doc.SelectNodes("/configuration/packageSourceMapping"); + + var newNode = doc.CreateElement("packageSource"); + + var keyAttr = doc.CreateAttribute("key"); + keyAttr.Value = sourceKey; + newNode.Attributes.Append(keyAttr); + + var packageNode = doc.CreateElement("package"); + + var patternAttr = doc.CreateAttribute("pattern"); + patternAttr.Value = sourceValue; + packageNode.Attributes.Append(patternAttr); + + newNode.AppendChild(packageNode); + + sourceNodes?[0]?.AppendChild(newNode); + + File.WriteAllText(nugetConfigPath, doc.OuterXml); + } + catch + { + Logger.LogWarning($"Adding \"{sourceValue}\" ({sourceKey}) to package source mapping FAILED."); + } + } + + public void RemovePackageSourceMapping(string solutionFolder, string sourceKey) + { + var nugetConfigPath = GetNugetConfigPath(solutionFolder); + if (!File.Exists(nugetConfigPath)) + { + return; + } + + var fileContent = File.ReadAllText(nugetConfigPath); + if (!fileContent.Contains($"")) + { + return; + } + + Logger.LogInformation($"Removing \"{sourceKey}\" from nuget package source mappings..."); + + try + { + var doc = new XmlDocument() { PreserveWhitespace = true }; + + doc.Load(GenerateStreamFromString(fileContent)); + + var nodes = doc.SelectNodes($"/configuration/packageSourceMapping/packageSource[@key='{sourceKey}']"); + if (nodes != null && nodes.Count > 0) + { + nodes[0]!.ParentNode?.RemoveChild(nodes[0]); + } + + File.WriteAllText(nugetConfigPath, doc.OuterXml); + } + catch + { + Logger.LogWarning($"Removing \"{sourceKey}\" from package source mappings FAILED."); + } + } + private static string GetNugetConfigPath(string solutionFolder) { return Path.Combine(solutionFolder, "NuGet.Config");