Skip to content

Commit b7a79ca

Browse files
committed
Added test and fixed issue when converting a project with an XML namespace.
See issue #1
1 parent 21a773d commit b7a79ca

3 files changed

Lines changed: 106 additions & 10 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,11 @@ Has this extension helped you at all?
5050
If so, please rate and share it.
5151

5252
Thank you! :)
53+
54+
# Change Log
55+
56+
## v1.0.1101.15 (November 1<sup>st</sup>, 2024)
57+
- Fixed [#1](https://github.com/icnocop/PackageReferenceVersionToAttribute/issues/1) - Fixed issue when converting a project with an XML namespace.
58+
59+
## v1.0.1026.3 (October 26<sup>th</sup>, 2024)
60+
- First release

src/PackageReferenceVersionToAttributeExtension/Commands/ConvertPackageReferenceVersionElementsToAttributesCommand.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,42 @@ private async Task ConvertPackageReferenceVersionElementsToAttributesAsync(IEnum
7979
continue;
8080
}
8181

82-
await VS.StatusBar.ShowMessageAsync($"Converting PackageReference Version child elements to attributes in {Path.GetFileName(projectPath)}...");
82+
string message = $"Converting PackageReference Version child elements to attributes in the project file \"{projectPath}\"...";
83+
await this.loggingService.LogInfoAsync(message);
84+
await VS.StatusBar.ShowMessageAsync(message);
85+
86+
var document = XDocument.Load(projectPath, LoadOptions.PreserveWhitespace);
87+
88+
// Find all PackageReference elements with a <Version> child element
89+
// Use the XML namespace if one is set
90+
XNamespace ns = document.Root.GetDefaultNamespace();
91+
92+
if (!string.IsNullOrWhiteSpace(ns.NamespaceName))
93+
{
94+
await this.loggingService.LogInfoAsync($"Found XML namespace \"{ns.NamespaceName}\".");
95+
}
96+
97+
// Query for PackageReference elements
98+
var packageReferences = document.Descendants(ns != null ? ns + "PackageReference" : "PackageReference")
99+
.Where(pr => pr.Element(ns != null ? ns + "Version" : "Version") != null)
100+
.ToList();
101+
if (!packageReferences.Any())
102+
{
103+
await this.loggingService.LogInfoAsync($"No PackageReference Version child elements found in the project file \"{projectPath}\".");
104+
continue;
105+
}
106+
107+
bool modified = false;
83108

84109
// backup project file
85110
await this.fileSystemService.BackupFileAsync(projectPath);
86111

87112
// check out files from source control
88113
await this.projectService.CheckOutFileFromSourceControlAsync(projectPath);
89114

90-
var document = XDocument.Load(projectPath, LoadOptions.PreserveWhitespace);
91-
bool modified = false;
92-
93-
// Find all PackageReference elements with a <Version> child element
94-
var packageReferences = document.Descendants("PackageReference")
95-
.Where(pr => pr.Element("Version") != null)
96-
.ToList();
97-
98115
foreach (var packageReference in packageReferences)
99116
{
100-
var versionElement = packageReference.Element("Version");
117+
var versionElement = packageReference.Element(ns != null ? ns + "Version" : "Version");
101118
if (versionElement != null)
102119
{
103120
// Move the Version element content to an attribute

src/PackageReferenceVersionToAttributeExtensionTests/ConvertPackageReferenceVersionElementsToAttributesCommandTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,77 @@ public async Task ExecuteAsync_WithOneSelectedProjectWithConditionalElements_Suc
298298
project.Contents);
299299
}
300300

301+
/// <summary>
302+
/// Verifies that the <c>ExecuteAsync</c> method completes successfully
303+
/// when one project is selected in the solution,
304+
/// which contains an XML namespace.
305+
/// </summary>
306+
/// <returns>A task representing the asynchronous test operation.</returns>
307+
[TestMethod]
308+
public async Task ExecuteAsync_WithOneSelectedProjectWithAnXmlNamespace_SucceedsAsync()
309+
{
310+
// Arrange
311+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
312+
313+
using MockProject project = new(this.loggerFactory)
314+
{
315+
Name = "ProjectA",
316+
Contents = """
317+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" Sdk="Microsoft.NET.Sdk.Uwp">
318+
319+
<PropertyGroup>
320+
<TargetFramework>uap10.0</TargetFramework>
321+
<RootNamespace>ExampleUWPProject</RootNamespace>
322+
<AssemblyName>ExampleUWPProject</AssemblyName>
323+
</PropertyGroup>
324+
325+
<ItemGroup>
326+
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
327+
<Version>6.2.10</Version>
328+
</PackageReference>
329+
<PackageReference Include="Newtonsoft.Json">
330+
<Version>13.0.1</Version>
331+
</PackageReference>
332+
<PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls">
333+
<Version>6.1.2</Version>
334+
</PackageReference>
335+
<PackageReference Include="Microsoft.Extensions.Logging">
336+
<Version>5.0.0</Version>
337+
</PackageReference>
338+
</ItemGroup>
339+
340+
</Project>
341+
""",
342+
};
343+
this.mockVisualStudio.AddProjects(project);
344+
this.mockVisualStudio.AddSelections(project);
345+
346+
// Act
347+
this.command.Invoke(null);
348+
349+
// Assert
350+
Assert.AreEqual(
351+
"""
352+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" Sdk="Microsoft.NET.Sdk.Uwp">
353+
354+
<PropertyGroup>
355+
<TargetFramework>uap10.0</TargetFramework>
356+
<RootNamespace>ExampleUWPProject</RootNamespace>
357+
<AssemblyName>ExampleUWPProject</AssemblyName>
358+
</PropertyGroup>
359+
360+
<ItemGroup>
361+
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.10" />
362+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
363+
<PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls" Version="6.1.2" />
364+
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
365+
</ItemGroup>
366+
367+
</Project>
368+
""",
369+
project.Contents);
370+
}
371+
301372
/// <summary>
302373
/// Verifies that the <c>ExecuteAsync</c> method completes successfully
303374
/// when two projects are selected in the solution.

0 commit comments

Comments
 (0)