1

我正在为我的团队在 Visual Studio 2015 Enterprise 中使用的代码分析器设置一个内部 NuGet 包。我通过创建一个新文件夹来保持简单,并使用“nuget spec”命令来创建 nuspec 文件。

我的 nuspec 文件如下:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>Test.CodeAnalysis</id>
    <version>1.0.0.5</version>
    <authors>Tom Atwood</authors>
    <owners>Tom Atwood</owners>
    <licenseUrl>LICENSEURL</licenseUrl>
    <projectUrl>PROJECTURL</projectUrl>
    <iconUrl>ICONURL</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Code analysis analyzers used at my company.</description>
    <releaseNotes>No release notes available.</releaseNotes>
    <copyright>COPYRIGHT MESSAGE HERE</copyright>
    <tags>code analysis</tags>
    <dependencies>
      <dependency id="ClrHeapAllocationAnalyzer" version="[1.0.0.8]" />
      <dependency id="codecracker.CSharp" version="[1.0.0]" />
      <dependency id="Microsoft.CodeAnalysis.FxCopAnalyzers" version="[1.1.0]" />
      <dependency id="RefactoringEssentials" version="[4.2.0]" />
      <dependency id="SonarAnalyzer.CSharp" version="[1.16.0]" />
      <dependency id="StyleCop.Analyzers" version="[1.0.0]" />
    </dependencies>
    <frameworkAssemblies>
      <frameworkAssembly assemblyName="System" targetFramework=".NETFramework4.0" />
      <frameworkAssembly assemblyName="System" targetFramework=".NETFramework4.5" />
      <frameworkAssembly assemblyName="System" targetFramework=".NETFramework4.51" />
      <frameworkAssembly assemblyName="System" targetFramework=".NETFramework4.52" />
      <frameworkAssembly assemblyName="System" targetFramework=".NETFramework4.6" />
      <frameworkAssembly assemblyName="System" targetFramework=".NETFramework4.61" />
      <frameworkAssembly assemblyName="System" targetFramework="native0.0" />
      <frameworkAssembly assemblyName="System.Xml" targetFramework=".NETFramework4.0" />
      <frameworkAssembly assemblyName="System.Xml" targetFramework=".NETFramework4.5" />
      <frameworkAssembly assemblyName="System.Xml" targetFramework=".NETFramework4.51" />
      <frameworkAssembly assemblyName="System.Xml" targetFramework=".NETFramework4.52" />
      <frameworkAssembly assemblyName="System.Xml" targetFramework=".NETFramework4.6" />
      <frameworkAssembly assemblyName="System.Xml" targetFramework=".NETFramework4.61" />
      <frameworkAssembly assemblyName="System.Xml" targetFramework="native0.0" />
    </frameworkAssemblies>
  </metadata>
  <files>
    <file src="CodeAnalysis.props" target="\build\net40\" />
    <file src="CodeAnalysis.props" target="\build\net45\" />
    <file src="CodeAnalysis.props" target="\build\net451\" />
    <file src="CodeAnalysis.props" target="\build\net452\" />
    <file src="CodeAnalysis.props" target="\build\net46\" />
    <file src="CodeAnalysis.props" target="\build\net461\" />
    <file src="CodeAnalysis.ruleset" target = "\content\net40\" />
    <file src="CodeAnalysis.ruleset" target = "\content\net45\" />
    <file src="CodeAnalysis.ruleset" target = "\content\net451\" />
    <file src="CodeAnalysis.ruleset" target = "\content\net452\" />
    <file src="CodeAnalysis.ruleset" target = "\content\net46\" />
    <file src="CodeAnalysis.ruleset" target = "\content\net461\" />
    <file src="stylecop.json" target = "\content\net40\" />
    <file src="stylecop.json" target = "\content\net45\" />
    <file src="stylecop.json" target = "\content\net451\" />
    <file src="stylecop.json" target = "\content\net452\" />
    <file src="stylecop.json" target = "\content\net46\" />
    <file src="stylecop.json" target = "\content\net461\" />
    <file src="install.ps1" target="\tools\net40\" />
    <file src="install.ps1" target="\tools\net45\" />
    <file src="install.ps1" target="\tools\net451\" />
    <file src="install.ps1" target="\tools\net452\" />
    <file src="install.ps1" target="\tools\net46\" />
    <file src="install.ps1" target="\tools\net461\" />
    <file src="uninstall.ps1" target="\tools\net40\" />
    <file src="uninstall.ps1" target="\tools\net45\" />
    <file src="uninstall.ps1" target="\tools\net451\" />
    <file src="uninstall.ps1" target="\tools\net452\" />
    <file src="uninstall.ps1" target="\tools\net46\" />
    <file src="uninstall.ps1" target="\tools\net461\" />
  </files>
</package>

我遇到的问题如下:

  1. 即使 stylecop.json 文件根据需要添加到目标项目中,props 文件也没有正确设置文件。对于 stylecop.json,这意味着在 csproj 文件中具有以下设置,以便 stylecop.json 文件处于活动状态并被识别:

    <ItemGroup>
        <AdditionalFiles include="stylecop.json"
    </ItemGroup>
    

    目前,它不是“AdditionalFiles”,而是设置为“None”。

  2. 即使将规则集文件加载到项目中,项目的属性也不会在构建时启用代码分析,并且为规则集文件而不是导入的文件设置了默认的“Microsoft 托管推荐规则”。

对于以上两项,props 文件设置如下:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <RunCodeAnalysis>true</RunCodeAnalysis>
    <CodeAnalysisRuleSet>CodeAnalysis.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <ItemGroup>
    <AdditionalFiles Include="stylecop.json"/>
    <CodeAnalysisDictionary Include="CodeAnalysis.Dictionary.xml" />
  </ItemGroup>
</Project>

请注意 install.ps1 和 uninstall.ps1 目前处于默认配置,不处理任何任务。

关于如何正确进行此操作的详细信息并不多。是否存在我执行不正确的具体问题?

4

0 回答 0