Search code examples
msbuildvisual-studio-2017nugetstylecop

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


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.


Solution

  • Thanks to Paulo. How I did it:

    This is the structure of my NuGet package.

    enter image description here

    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.