How to ship the stylecop.json and custom.ruleset files with a NuGet package in VS2017

末鹿安然 提交于 2021-02-08 05:22:23

问题


At the moment we are switching from VS2015 to VS2017. One of our upgrade steps is to switch from stylecop to the new Stylecop.Analyzer package. The new Stylecop is using 2 files. The stylecop.json and the Stylecop.ruleset.

The target: I want to provide the stylecop files as a custom nuget package. But I dont know how to create the needed .csproj entries.

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
...
        <CodeAnalysisRuleSet>packages\My.StyleCop.1.0.0-pre15\RuleSet\My.StyleCop.ruleset</CodeAnalysisRuleSet>
    </PropertyGroup>    
...
    <ItemGroup>
        <AdditionalFiles Include="packages\My.StyleCop.1.0.0-pre15\Config\stylecop.json">
            <Link>stylecop.json</Link> 
        </AdditionalFiles>  
    </ItemGroup>
</Project>

In the past, there was the possibility to use a install.ps1 script to do this stuff. But with NuGet 3. (or 4.) the install scripts are obsolete and will be ignored.

I already tried to use My.StyleCop.targets:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <AdditionalFiles Include="packages\My.StyleCop.1.0.0-pre17\Config\stylecop.json">
          <Link>stylecop.json</Link> 
        </AdditionalFiles>    
    </ItemGroup>
</Project>

But here I have some issues, too. Since NuGet 3. (or 4.) there is no solution wide package folder and I dont know any variable or placeholder I can use here to get a absolute or relative path to my package.


回答1:


You can add .props or .targets files to the build folder in your packages and they will be imported to the projects.

On the .props file, you can use the MSBuildThisFileDirectory MSBuild variable that represents the folder where that file is located.




回答2:


Thanks to Paulo. How I did it:

This is the structure of my NuGet package.

The solution is quiet easy. You need to create to files. A .props and a .targets file named like the NuGet package and place them in the build folder of your package.

In these MSBuild files you can use the $(MSBuildThisFileDirectory) variable to get the path of your NuGet package.

MSBuildThisFileDirectory  = C:\Users\UserName\.nuget\packages\sig.stylecop\1.0.0-pre23\build\

My SIG.StyleCop.props file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)\..\RuleSet\SIG.combiLink.ruleset</CodeAnalysisRuleSet>
    </PropertyGroup>
</Project>

My SIG.StyleCop.targets file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <AdditionalFiles Include="$(MSBuildThisFileDirectory)\..\Config\stylecop.json">
            <Link>stylecop.json</Link> 
        </AdditionalFiles>    
    </ItemGroup>    
</Project>

Cause of the structure of my package i need to navigate (..) into the Config and into the RuleSet folder.




回答3:


The variable $(MSBuildThisFileDirectory) already includes the backslash at the end. It is important to omit the backslash when you reference the ruleset and the stylecop.json file:

<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)..\RuleSet\SIG.combiLink.ruleset</CodeAnalysisRuleSet>

<AdditionalFiles Include="$(MSBuildThisFileDirectory)..\Config\stylecop.json">

With the double backslash I experienced two strange problems in Visual Studio 2017:

  • Unit tests rebuild the code each time I start them, even without any code change

  • The IDE shows many StyleCop errors in the Error List window and shows red marks in the scroll bar even for rules that are explicitly disabled in the rule set.



来源:https://stackoverflow.com/questions/48870718/how-to-ship-the-stylecop-json-and-custom-ruleset-files-with-a-nuget-package-in-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!