-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Verify projects for required properties #75
Comments
Hi @omidkrad thanks for the comments. I think this is an interesting but I don't see how we can implement something like this in a generic fashion. For example how would we know the available set of value for WarningLevel and other properties. Across all the different project types there are thousands of properties so it's not possible for someone to author a set of possible values list. In addition to this when I have a blog post on how to validate specific setting in msbuild at http://sedodream.com/2009/06/30/elementsofreusablemsbuildscriptsvalidation.aspx but I think that's different from what you are looking for. http://sedodream.com/2009/06/30/elementsofreusablemsbuildscriptsvalidation.aspx |
We don't want to validate if properties have valid values, so really don't care what the property name or value is. The whole point is to make sure properties are set consistently across all projects in a solution as we require. Your article is very close, but the main part that is a little bugging is having to manually add that import in every project. And if some new developer adds a new project to the solution then again someone has to manually check if proper import is added to the project. One way I can think of to avoid that is to use the
where |
Right I was going to suggest CustomBeforeMicrosoftCommonTargets. For those not familiar with it see my blog post at http://sedodream.com/2012/07/28/MSBuildHowToExecuteATargetAfterCoreCompilePart2.aspx. OK so if I understand your proposal, you'd like to have the following.
Did I get that right? If not can you elaborate more on exactly what you are proposing. |
I just read your SO question, now I understand better what you are looking for. Essentially you want to be able to "analyze" a set of projects to ensure certain properties are declared. I think the right approach is to use the MSBuild construction APIs to read those projects. In psbuild I have PowerShell functions that can help. For example Get-MSBuildProject https://github.com/ligershark/psbuild/blob/master/src/psbuild.psm1#L1411 Find-Property https://github.com/ligershark/psbuild/blob/master/src/psbuild.psm1#L1856 and Test-Property https://github.com/ligershark/psbuild/blob/master/src/psbuild.psm1#L1927. |
Exactly. Using MSBuild APIs is the right way to go. I absolutely don't want to get into parsing XML for this. So here's my proposal: Add a new parameter Invoke-MSBuild Solution.sln -requireProperties @{
'TargetFrameworkVersion' = 'v4.5'
'Platform' = 'AnyCPU'
'WarningLevel' = 4
'TreatWarningsAsErrors' = true
'OutputPath' = '$(SolutionDir)bin'
'SignAssembly' = true
'AssemblyName' = '$(ProjectFolderName)'
} During compile, if any of the projects is not explicitly setting these properties then the build should fail with a descriptive error message. This will be very useful. In all teams I've worked with they put together stuff to accomplish this in some way but I never really liked any of the workarounds. |
Thanks for the reply. In your SO question you stated
So I understood that to mean that you wanted to ensure that project files are authored in a consistent manner and having the property values you listed included. And the reason why you wanted to make sure those properties were included in the project file is to ensure that the projects are built in a consistent manner no matter where they are built. That I totally get. Now the specific thing that you are proposing is to have a new parameter on About checking property values, checking values of properties at build time will not ensure that properties are declared in the project. Property values can come from a lot of different places, the project file is just one. In addition to this I don't think having a property name/value pair is good enough. For example perhaps you want to ensure that $proj = Get-MSBuildProject -projectFile .\temp.proj
$tfv = Find-Property -propertyContainer $proj -name TargetFrameworkVersion
if($tfv.Value -ne 'v4.5'){throw ('invalid value for TargetFrameworkVersion [{0}]' -f $tfv.Value)} Thanks for the comments I appreciate your interest in this project. |
Yeah, as long as we can ensure consistency in properties and even more I'm happy with the solution. This also works well with psake which is great. Do you have helper methods to get certain project types from a solution file? Maybe we can create a psake plugin for testing Visual Studio projects/solutions like VSUnitTest that works with PowerShell. I like these verifications done during build than test to get early errors. Basically using helper functions you specify the solution file and project types you're looking in it along with a list of required properties and patterns for their values in each configuration. Checking if a project AssemblyName is the same as project FolderName is a good example. What do you think? |
I'm not sure if there is an OSS parser for .sln files. There may be one in the MSBuild repo but I'm not sure. One way to do this could be to call msbuild.exe with /pp which will produce an MSBuild file for the solution, and then use the psbuild functions to find projects and then check conditions. If you get that figured out, it may be a good addition of a new function, Get-SolutionProjects, to psbuild. About the verification part, psbuild already exposes all the Lego blocks for users to compose those checks. I'm not sure if it would make sense to add a verify function because we'd be limited in what we can check as I discussed above. |
I opened a question on StackOverflow for how to check Visual Studio projects (.csproj files) for consistency in their property settings. Apparently there is no straightforward way to do this and you have to either write custom tasks or manually parse
.csproj
files as XML for testing project properties.I think this is a good feature to be added to
Invoke-MSBuild
. Right now,Invoke-MSBuild
has a-properties
parameter which passes these parameters for build but it does not test projects for having them. For example if you pass-properties @{'WarningLevel'='4'}
it will ignore the currentWarningLevel
property in projects and use the specified value instead. But what if you want the build to fail with proper error message ifWarningLevel
in any of the building projects is not4
? I've seen different team invent their own stuff to implement this kind of verification in their projects.I suggest adding a new parameter to
Invoke-MSBuild
just for this, like-requireProperties
that will cause the build to fail if any of the projects does not comply with the specified property requirements.One thing to notice is, if
-configuration Debug
is specified together with-requireProperties $hash
then required properties will be checked only for the Debug condition. For this we may want to alternatively have the option to pass an MSBuild project with all required properties on different conditions and getInvoke-MSBuild
to verify that every project being built follows that.The text was updated successfully, but these errors were encountered: