将代码分析与 Visual Studio DNX 项目 (.xproj) 结合使用 [英] Use code analysis with Visual Studio DNX project (.xproj)

查看:27
本文介绍了将代码分析与 Visual Studio DNX 项目 (.xproj) 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在普通"Visual Studio 项目 (.csproj) 的属性中,可以说Enable Code Analysis on Build(以前称为 FxCop).

Within the properties of an "ordinary" Visual Studio project (.csproj) it's possible to say Enable Code Analysis on Build (formerly known as FxCop).

自从我开始尝试新的 DNX 项目 (.xproj) 以来,我一直在寻找类似的东西.我知道可能没有构建输出,所以旧方法可能并不真正适合于此,但我很确定代码分析/FxCop 规则仍然适用.此外,应该有一些方法可以在新的实际"项目文件 (project.json) 中注册自定义规则集 (.ruleset) 文件.

Since I've started playing around with new DNX projects (.xproj) I'm searching for something similar. I know there may be no build output, so the old approach may not really fit into this, but I'm quite sure the Code Analysis / FxCop rules still apply. Furthermore there should be some way to register a custom rule set (.ruleset) file within the new "actual" project file (project.json).

也许我忽略了基于 Roslyn 之类的更现代的东西?

Maybe I'm overlooking something more modern based on Roslyn or the like?

推荐答案

终于找到了一种解决方法,该方法应该可以解决,直到他们(希望)在下一版本的 .NET Core 和 Visual Studio 中修复它.诀窍是为经典的 .NET Framework 构建执行旧的"FxCop,这是运行老式代码分析所必需的.

Finally found a workaround, which should do it until they'll (hopefully) fix it with the next release of .NET Core and Visual Studio. The trick is to execute the "good old" FxCop for the classic .NET Framework build, which is necessary to run an old fashioned code analysis.

库的 project.json 应该包含如下内容:

The project.json for libraries should contain something like that:

{
  "frameworks": {
    "net46": {
      "buildOptions": {
        "define": [ "CODE_ANALYSIS" ]
      }
    },
    "netstandard1.3": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  },

  "scripts": {
    "postcompile": "../../CodeAnalysis.cmd %compile:TargetFramework% %compile:Configuration% %compile:OutputFile% %compile:CompilerExitCode%"
  }
 }

应用程序"的 project.json 应包含实际运行时:

The project.json for "apps" should include the actual runtime:

{
  "scripts": {
    "postcompile": "../../CodeAnalysis.cmd %compile:TargetFramework% %compile:Configuration% %compile:OutputFile% %compile:CompilerExitCode% %compile:RuntimeOutputDir%"
  }
 }

因此,使用 postcompile 事件可以运行某种批处理脚本来执行经典的 FxCop(需要 Visual Studio!).我目前正在使用包含三个文件的设置:

Thus, using the postcompile event makes it possible to run some kind of batch script to execute the classic FxCop (Visual Studio required!). I'm currently using a setup with three files:

  • 代码分析.cmd
  • 代码分析.规则集
  • CodeAnalysis.xml(字典)

批处理文件支持"当前的 .NET Framework 4.6 版本,如下所示:

The batch file "supports" the current .NET Framework 4.6 versions and looks like this:

@echo off

if not [%4]==[0] (
  goto :eof
)

if not [%2]==[Release] (
  goto :eof
)

if [%1]==[net46] (
  set VERSION=v4.6
) else if [%1]==[net461] (
  set VERSION=v4.6.1
) else if [%1]==[net462] (
  set VERSION=v4.6.2
) else (
  goto :eof
)

if not [%5]==[] (
  set FILE=%5\%~nx3
) else (
  set FILE=%3
)

set PLATFORM=%ProgramFiles(x86)%Reference AssembliesMicrosoftFramework.NETFramework\%VERSION%
set DIRECTORY=%ProgramFiles(x86)%Reference AssembliesMicrosoftFramework.NETFramework\%VERSION%Facades

set FXCOP=%VS140COMNTOOLS:Common7Tools=Team ToolsStatic Analysis ToolsFxCop%FxCopCmd.exe
set RULES=%VS140COMNTOOLS:Common7Tools=Team ToolsStatic Analysis ToolsFxCop%Rules

"%FXCOP%" /platform:"%PLATFORM%" /directory:"%DIRECTORY%" /rule:"-%RULES%" /ruleset:"=%~dp0CodeAnalysis.ruleset" /dictionary:"%~dp0CodeAnalysis.xml" /file:"%FILE%" /ignoregeneratedcode /console /culture:de-DE

它不像普通的内置东西那么方便,但是当使用 Visual Studio 时,FxCop 的错误/警告出现在错误列表中em>(有时需要第二次构建).不过,它们不会导致构建失败(也许还有另一个技巧......).

It's not that handy like the ordinary built-in stuff, but the errors / warnings of FxCop appear within the Error List when using Visual Studio (sometimes a second build is necessary). They don't lead to a failed build though (maybe there's another trick...).

CodeAnalysis.ruleset 示例:

<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="All Rules, except a few ones" ToolsVersion="14.0">
    <IncludeAll Action="Error" />
    <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
        <!-- CLS compliant -->
        <Rule Id="CA1014" Action="None" />
        <!-- COM visibility -->
        <Rule Id="CA1017" Action="None" />
    </Rules>
</RuleSet>

这篇关于将代码分析与 Visual Studio DNX 项目 (.xproj) 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆