-
Notifications
You must be signed in to change notification settings - Fork 954
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
force set
Condition="false"
on Microsoft.WebApplication.targets
The file $(VSToolsPath)\WebApplications\Microsoft.WebApplications.targets can never be resolved in the updater as it runs in the Linux Docker container, so an always false condition is added to that import to make the minimal change necessary to allow the NuGet updater to successfully run. After completion, the condition attribute is restored.
- Loading branch information
Showing
5 changed files
with
287 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...lpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/WebApplicationTargetsConditionPatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
using Microsoft.Language.Xml; | ||
|
||
namespace NuGetUpdater.Core.Updater | ||
{ | ||
internal class WebApplicationTargetsConditionPatcher : IDisposable | ||
{ | ||
private string? _capturedCondition; | ||
private readonly XmlFilePreAndPostProcessor _processor; | ||
|
||
public WebApplicationTargetsConditionPatcher(string projectFilePath) | ||
{ | ||
_processor = new XmlFilePreAndPostProcessor( | ||
getContent: () => File.ReadAllText(projectFilePath), | ||
setContent: s => File.WriteAllText(projectFilePath, s), | ||
nodeFinder: doc => doc.Descendants() | ||
.FirstOrDefault(e => e.Name == "Import" && e.GetAttributeValue("Project") == @"$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets") | ||
as XmlNodeSyntax, | ||
preProcessor: n => | ||
{ | ||
var element = (IXmlElementSyntax)n; | ||
_capturedCondition = element.GetAttributeValue("Condition"); | ||
return (XmlNodeSyntax)element.RemoveAttributeByName("Condition").WithAttribute("Condition", "false"); | ||
}, | ||
postProcessor: n => | ||
{ | ||
var element = (IXmlElementSyntax)n; | ||
var newElement = element.RemoveAttributeByName("Condition"); | ||
if (_capturedCondition is not null) | ||
{ | ||
newElement = newElement.WithAttribute("Condition", _capturedCondition); | ||
} | ||
return (XmlNodeSyntax)newElement; | ||
} | ||
); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_processor.Dispose(); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/XmlFilePreAndPostProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
|
||
using Microsoft.Language.Xml; | ||
|
||
namespace NuGetUpdater.Core.Updater | ||
{ | ||
internal class XmlFilePreAndPostProcessor : IDisposable | ||
{ | ||
public Func<string> GetContent { get; } | ||
public Action<string> SetContent { get; } | ||
public Func<XmlDocumentSyntax, XmlNodeSyntax?> NodeFinder { get; } | ||
public Func<XmlNodeSyntax, XmlNodeSyntax> PreProcessor { get; } | ||
public Func<XmlNodeSyntax, XmlNodeSyntax> PostProcessor { get; } | ||
|
||
public XmlFilePreAndPostProcessor(Func<string> getContent, Action<string> setContent, Func<XmlDocumentSyntax, XmlNodeSyntax?> nodeFinder, Func<XmlNodeSyntax, XmlNodeSyntax> preProcessor, Func<XmlNodeSyntax, XmlNodeSyntax> postProcessor) | ||
{ | ||
GetContent = getContent; | ||
SetContent = setContent; | ||
NodeFinder = nodeFinder; | ||
PreProcessor = preProcessor; | ||
PostProcessor = postProcessor; | ||
PreProcess(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
PostProcess(); | ||
} | ||
|
||
private void PreProcess() => RunProcessor(PreProcessor); | ||
|
||
private void PostProcess() => RunProcessor(PostProcessor); | ||
|
||
private void RunProcessor(Func<XmlNodeSyntax, XmlNodeSyntax> processor) | ||
{ | ||
var content = GetContent(); | ||
var xml = Parser.ParseText(content); | ||
if (xml is null) | ||
{ | ||
return; | ||
} | ||
|
||
var node = NodeFinder(xml); | ||
if (node is null) | ||
{ | ||
return; | ||
} | ||
|
||
var replacementElement = processor(node); | ||
var replacementXml = xml.ReplaceNode(node, replacementElement); | ||
var replacementString = replacementXml.ToFullString(); | ||
SetContent(replacementString); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters