forked from andrewabest/Conventional
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[andrewabestGH-87] Added convention to enforce a project file sets a …
…property Some examples: - Convention.MustSetPropertyValue("Nullable", "enable") // Ensure nullable types are enabled for the project - Convention.MustSetPropertyValue("IsPackable", "true") // Ensure the project can be packed for NuGet See https://learn.microsoft.com/en-us/visualstudio/msbuild/property-element-msbuild?view=vs-2022#example
- Loading branch information
eddie.stanley
committed
Jan 6, 2024
1 parent
41f311f
commit be5b0b6
Showing
4 changed files
with
114 additions
and
0 deletions.
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
40 changes: 40 additions & 0 deletions
40
...onventional/Conventions/Assemblies/MustSetPropertyValueAssemblyConventionSpecification.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,40 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using System.Xml.XPath; | ||
|
||
namespace Conventional.Conventions.Assemblies | ||
{ | ||
public class MustSetPropertyValueAssemblyConventionSpecification : AssemblyConventionSpecification | ||
{ | ||
private string ExpectedPropertyName { get; } | ||
private string ExpectedPropertyValue { get; } | ||
|
||
public MustSetPropertyValueAssemblyConventionSpecification(string expectedPropertyName, string expectedPropertyValue) | ||
{ | ||
ExpectedPropertyName = expectedPropertyName; | ||
ExpectedPropertyValue = expectedPropertyValue; | ||
} | ||
|
||
protected override ConventionResult IsSatisfiedByLegacyCsprojFormat(string assemblyName, XDocument projectDocument) | ||
{ | ||
return IsSatisfiedBy(assemblyName, projectDocument); | ||
} | ||
|
||
protected override ConventionResult IsSatisfiedBy(string assemblyName, XDocument projectDocument) | ||
{ | ||
// The Project element (and descendants) are namespaced in legacy csproj files, so our XPath ignores the | ||
// namespace by considering the local element name only. Once we no-longer need to support legacy csproj | ||
// files, the XPath can be simplified to /Project/PropertyGroup/{ExpectedPropertyName} | ||
var matchingProperties = projectDocument.XPathSelectElements($"/*[local-name() = 'Project']/*[local-name() = 'PropertyGroup']/*[local-name() = '{ExpectedPropertyName}']") | ||
.Select(propertyElement => propertyElement.Value) | ||
.Where(propertyValue => string.Equals(ExpectedPropertyValue, propertyValue, StringComparison.InvariantCulture)); | ||
|
||
return matchingProperties.Count() == 1 | ||
? ConventionResult.Satisfied(assemblyName) | ||
: ConventionResult.NotSatisfied(assemblyName, string.Format(FailureMessage, assemblyName)); | ||
} | ||
|
||
protected override string FailureMessage => "{0} should have property " + ExpectedPropertyName + " with value " + ExpectedPropertyValue; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/Core/TestSolution/TestSolution.TestProject/SdkClassLibrary1/SdkClassLibrary1.csproj
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