Microsoft 代码合同和 CI 构建服务器
我们正在迁移到 .NET 4,并且对实现新的“按合同设计”功能非常感兴趣。
据我们所知,代码合同引擎需要安装代码合同插件
和 VS Ultimate 或 Premium(用于静态检查)。
这是我的问题:
- 我可以使用代码契约重写吗 无需在 CI 构建服务器(TeamCity)上安装 VS?
- 有没有 msbuild 任务执行合同检查?
- 您是否在 CI 构建中使用代码契约验证?
We are migrating to .NET 4 and very interested in implementing new Design By Contract capabilities.
As we know Code Contract engine requires installation of Code Contract addin
and VS Ultimate or Premium (for static checking).
Here is my questions:
- Can I use code contract rewriting
without installing VS on CI build Server (TeamCity)? - Is there any
msbuild tasks to execute Contract checking? - Do you use Code Contract's validation with CI builds?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。在构建服务器上安装 CodeContracts。 (如果它拒绝在没有 Visual Studio 的计算机上安装,只需将下面列出的文件及其依赖项复制到构建服务器上。)安装后,您将发现 CodeContract 工具安装在 %programfiles%\Microsoft\Contracts\ 中垃圾桶。在该目录中,有 4 个您可能感兴趣的可执行文件:
ccrewrite.exe - 二进制重写器。这应该在编译后执行。它将您的合约转换为运行时检查或您指定的任何内容。
ccrefgen.exe - 这可以与您的程序集一起生成合同引用程序集。如果您要传送供其他方使用的 dll,这非常有用。
cccheck.exe - 静态检查器。在构建服务器上,您可以在包含契约的程序集上运行此工具,当遇到潜在问题时,它会发出警告和消息。
ccdocgen.exe - 这会根据代码中的合同生成 XML 文档。如果您要发送带有合同的 dll 供其他方使用,或者您只需要代码的内部文档,则可能需要使用此选项。
是的。 CodeContracts 附带了 2 个 MSBuild 任务:在同一 CodeContracts 安装目录中,查看 MSBuild\[框架版本] 文件夹。在该目录中,有 2 个文件可以帮助您:Microsoft.CodeContracts.targets 和 Microsoft.CodeContractAnalysis.targets。
根据 CodeContracts 文档,
如您所见,可以并支持通过 MSBuild 目标将工具集成到 CI 构建中。
假设您的意思是使用警告/消息进行静态检查,我个人已经这样做过,但还没有在大型项目中这样做过。
我希望这有帮助!
向 Jon Skeet 的 C# In Depth 一书致敬,其中介绍了命令行工具。
Yes. Install CodeContracts on the build server. (If it refuses to install on a machine without Visual Studio, just copy the files listed below, and their dependencies, onto the build server.) Once installed, you'll find the CodeContract tools installed in %programfiles%\Microsoft\Contracts\Bin. In that directory, there are 4 executables you'll be interested in:
ccrewrite.exe - The binary rewriter. This should be executed after compilation. It turns your contracts into runtime checks or whatever you specify you want them turned into.
ccrefgen.exe - This can generate contract reference assemblies alongside your assemblies. This is useful if you're shipping dlls to be consumed by other parties.
cccheck.exe - The static checker. On the build server, you'd run this tool over your assemblies containing contracts, and it will spit out warnings and messages as it encounters potential problems.
ccdocgen.exe - This generates XML documentation from the contracts in your code. You might want to use this if you're shipping dlls with contracts for consumption by other parties, or if you just need internal docs on your code.
Yes. There are 2 MSBuild tasks shipping with CodeContracts: in the same CodeContracts installation directory, check out the MSBuild\[framework version] folder. In that directory, there are 2 files that should help you out: Microsoft.CodeContracts.targets and Microsoft.CodeContractAnalysis.targets.
According to the CodeContracts documentation,
As you can see, it is possible and supported to integrate the tools into CI builds via the MSBuild targets.
Assuming you mean static checking with warnings/messages, I've done this personally, but haven't done this on a big project.
I hope this helps!
Hat tip to Jon Skeet's C# In Depth book for explanation of the command line tools.