如何在 VS2017 中使用 NuGet 包传送 stylecop.json 和 custom.ruleset 文件 [英] How to ship the stylecop.json and custom.ruleset files with a NuGet package in VS2017

查看:53
本文介绍了如何在 VS2017 中使用 NuGet 包传送 stylecop.json 和 custom.ruleset 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我们正在从 VS2015 切换到 VS2017.我们的升级步骤之一是从 stylecop 切换到新的 Stylecop.Analyzer 包.新的 Stylecop 使用 2 个文件.

解决方案很简单.您需要创建文件.一个 .props 和一个 .targets 文件命名为 NuGet 包,并将它们放在包的 build 文件夹中.

在这些 MSBuild 文件中,您可以使用 $(MSBuildThisFileDirectory) 变量来获取 NuGet 包的路径.

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

我的 SIG.StyleCop.props 文件:

<?xml version="1.0" encoding="utf-8"?><项目工具版本="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><属性组><CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)\..\RuleSet\SIG.combiLink.ruleset</CodeAnalysisRuleSet></PropertyGroup></项目>

我的 SIG.StyleCop.targets 文件:

<项目工具版本="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><项目组><AdditionalFiles Include="$(MSBuildThisFileDirectory)\..\Config\stylecop.json"><Link>stylecop.json</Link></附加文件></项目组></项目>

由于我的包的结构,我需要导航 (..) 到 Config 和 RuleSet 文件夹.

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.

解决方案

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.

这篇关于如何在 VS2017 中使用 NuGet 包传送 stylecop.json 和 custom.ruleset 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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