如何打包 C# 9 源代码生成器并将其上传到 Nuget? [英] How to pack a C# 9 source generator and upload it to the Nuget?

查看:18
本文介绍了如何打包 C# 9 源代码生成器并将其上传到 Nuget?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 C# 9 源代码生成器,你可以在 这里找到它

I made a C# 9 source code generator, you can find it here

当我在另一个解决方案中使用整个项目并将其作为项目引用时,它可以工作,但是当我将它与当前配置一起上传到 Nuget (此处)它不起作用.

When I use the whole project inside another solution and reference it as a project it works but when I upload it with current configs into the Nuget (here) it does not work.

如何正确配置 C# 9 源代码生成器以用作 Nuget 包?我的项目有什么问题?

How to config a C# 9 source generator correctly to work as a Nuget package? What is wrong with my project?

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Version>0.0.2</Version>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
    <PackageLicenseExpression>MIT</PackageLicenseExpression>
    <PackageTags>dotnet</PackageTags>
    <PublishRepositoryUrl>true</PublishRepositoryUrl>
    <GenerateRepositoryUrlAttribute>true</GenerateRepositoryUrlAttribute>
    <PackBuildOutput>true</PackBuildOutput>
    <PackageId>MockableStaticGenerator</PackageId>
    <PackOnBuild>true</PackOnBuild>
    <PackFolder>analyzers\cs</PackFolder>
    <DebugType>embedded</DebugType>
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>
  <PropertyGroup>
    <RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json ;$(RestoreAdditionalProjectSources)</RestoreAdditionalProjectSources>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>
</Project>

推荐答案

如果您解压 nuget 包,您将看到该包存储在 lib 目录中.它必须存储在分析器目录中.

If you unzip the nuget package, you'll see that the package is stored in the lib directory. It has to be stored in the analyzers directory.

一种方法是将以下内容添加到您的 csproj:

One way to do that is to add the following to your csproj:

<ItemGroup>
  <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

如果你是多目标,它应该是:

If you're multitargeting it should be:

<ItemGroup>
  <None Include="$(OutputPath)\netstandard2.0\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>

这将包括作为库和分析器的项目.

This will include your project as both a library and an analyzer.

要将其用作分析器,请添加以下内容:

To use it just as an analyzer, add the following:

<PropertyGroup>
  <IncludeBuildOutput>false</IncludeBuildOutput>
</PropertyGroup>

这篇关于如何打包 C# 9 源代码生成器并将其上传到 Nuget?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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