开发者

Using Code Contracts in library code built with MSBuild

开发者 https://www.devze.com 2023-01-13 07:43 出处:网络
I\'ve started using Code Contracts in all new code I\'m writing, such as in a framework library I\'m building to help bootstrap IoC, O/RM, etc., in an ASP.NET MVC application. I\'ve written a simple b

I've started using Code Contracts in all new code I'm writing, such as in a framework library I'm building to help bootstrap IoC, O/RM, etc., in an ASP.NET MVC application. I've written a simple build script for this framework library that looks like the following:

@echo off
echo.
echo Cleaning build output (removing 'obj' and 'bin' folders)...
for /f "tokens=*" %%G in ('dir /b /ad /s bin') do rmdir /s /q "%%G"
for /f "tokens=*" %%G in ('dir /b /ad /s obj') do rmdir /s /q "%%G"
rmdir /s /q build
echo.
echo Starting the build...
call "%VS100COMNTOOLS%\vsvars32.bat"
msbuild Integration.build /target:Build
echo.
echo Done!
pause

This doesn't work. What I end up with in my build folder if I run this is, for whatever reason, assemblies that aren't fully rewritten by ccrewrite alongside .pdb.original, .rewritten and .csproj.FileListAbsolute.txt files that litter the output directory.

What does work is first building the solution in Visual Studio 2010, commenting out line 3 through 7 in the batch file and running it again. I then end up with properly rewritten assemblies and no .pdb.original nor .rewritten files.

What I've deduced from this is that Visual Studio 2010 somehow triggers the Code Contract rewriter properly so the resulting assemblies from the Visual Studio 2010 build is re-used by the command-line MSBuild call, so wh开发者_运维百科at my batch script basically does is just copying files to the build directory. Rather useless, in other words.

I've read this, but Jon's problem seems different from mine since ccrewrite is obviously doing something, but it's just not completing the rewriting for whatever reason. The Integration.build file builds the correct configuration (that has Code Contracts enabled in the .csproj files) and everything else looks right, it just doesn't work properly.

So, I'm wondering: How do I run MSBuild the way Visual Studio 2010 is where ccrewrite does what it's supposed to and doesn't litter my output directory with .rewritten and .pdb.original files? Does anyone have a perfect example of how an MSBuild file doing proper Code Contracts rewriting looks like?


The answer is in the script. All Visual Studio is ever going to do is run MSBuild tasks that will invoke others. One thing you can do is go to Tools|Options|Build... and turn on logging so you can see in detail which bit is doing what to generate the artifacts.

How would one do such a complex and involved thing? Read a guide to MSBuild such as Hashimi p1 and Part 2.

Then dig into the source for the build in e.g.:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets

The best way to get there is to open your .csproj and look what it includes and go via that (the .CSharp.targets is the first one - the one I cited comes further down the stack).

(That and wait for someone to pop in with an actual answer!)


I've played a little with Code Contract's static analysis and it is pretty cool.
Now trying to set up TeamCity build ...

Here is msbuild integration info from Microsoft Research (see page 44)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号