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 5, 2024
1 parent
ee0b163
commit 0db29cc
Showing
4 changed files
with
110 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
37 changes: 37 additions & 0 deletions
37
...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,37 @@ | ||
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) | ||
{ | ||
var matchingProperties = projectDocument.XPathSelectElements($"/Project/PropertyGroup/{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