在使用 MSBuild 构建的库代码中使用代码契约

发布于 2024-09-15 17:07:51 字数 1802 浏览 4 评论 0原文

我已经开始在我的所有新代码中使用代码合同写作,例如在我正在构建的框架库中,以帮助引导 IoCO/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2024-09-22 17:07:51

答案就在剧本里。 Visual Studio 要做的就是运行 MSBuild 任务来调用其他任务。您可以做的一件事是转到“工具”|“选项”|“构建...”并打开日志记录,以便您可以详细查看哪个位正在执行哪些操作来生成工件。

如此复杂且复杂的事情,该如何去做呢?阅读MSBuild 指南,例如< a href="http://msdn.microsoft.com/en-us/magazine/dd419659.aspx" rel="nofollow noreferrer">Hashimi p1 和 第 2 部分

然后深入研究构建的源代码,例如:

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

到达那里的最佳方法是打开 .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.:

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!)

风铃鹿 2024-09-22 17:07:51

我玩了一下 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文