SteMa
SteMa

Reputation: 431

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.

Upvotes: 2

Views: 2864

Answers (3)

f8smiles
f8smiles

Reputation: 11

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.

Upvotes: 1

SteMa
SteMa

Reputation: 431

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.

Upvotes: 1

Paulo Morgado
Paulo Morgado

Reputation: 14856

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.

Upvotes: 2

Related Questions