在使用 MSBuild 构建的库代码中使用代码契约
我已经开始在我的所有新代码中使用代码合同写作,例如在我正在构建的框架库中,以帮助引导 IoC,O/RM 等。我为此框架库编写了一个简单的构建脚本,如下所示:
@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
这不起作用。如果我运行这个程序,我最终会在 build
文件夹中得到的结果是,无论出于何种原因,程序集都没有被 ccrewrite
与 .pdb.original 一起完全重写。
、.rewriting
和 .csproj.FileListAbsolute.txt
文件散布在输出目录中。
有效的方法是首先在 Visual Studio 2010 中构建解决方案,注释掉批处理文件中的第 3 行到第 7 行,然后再次运行它。然后,我最终得到正确重写的程序集,并且没有 .pdb.original
或 .rewriting
文件。
我从中推断出,Visual Studio 2010 以某种方式正确触发了代码契约重写器,因此 Visual Studio 2010 构建生成的程序集被命令行 MSBuild 调用重新使用,所以我的批处理脚本基本上所做的只是将文件复制到 build
目录。换句话说,相当无用。
我已阅读此,但乔恩的问题似乎与我的不同,因为 ccrewrite 显然正在做一些事情,但无论出于何种原因它都没有完成重写。 Integration.build
文件构建了正确的配置(在 .csproj
文件中启用了代码契约),其他一切看起来都正确,只是无法正常工作。
所以,我想知道:如何以 Visual Studio 2010 的方式运行 MSBuild,其中 ccrewrite
执行其应有的操作,并且不会在我的输出目录中乱扔 .rewriting
和 .pdb.original
文件?有谁有一个完美的示例来说明 MSBuild 文件如何进行正确的代码契约重写?
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 what 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案就在剧本里。 Visual Studio 要做的就是运行 MSBuild 任务来调用其他任务。您可以做的一件事是转到“工具”|“选项”|“构建...”并打开日志记录,以便您可以详细查看哪个位正在执行哪些操作来生成工件。
如此复杂且复杂的事情,该如何去做呢?阅读MSBuild 指南,例如< a href="http://msdn.microsoft.com/en-us/magazine/dd419659.aspx" rel="nofollow noreferrer">Hashimi p1 和 第 2 部分。
然后深入研究构建的源代码,例如:
到达那里的最佳方法是打开 .csproj 并查看它包含的内容并通过它(.CSharp.targets 是第一个 - 我引用的那个在下面堆栈)。
(然后等待有人给出实际答案!)
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.:
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!)
我玩了一下 Code Contract 的静态分析,它非常酷。
现在尝试设置 TeamCity 构建...
这是 msbuild 集成 来自微软研究院的信息(参见第 44 页)
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)