简体   繁体   English

随 nuget package 一起安装的源生成器可配置设置文件

[英]Source generator configurable settings file installed with nuget package

I've created a source generator that reads in some information from a config file, allowing the consumer to change settings that alter the output of the generator.我创建了一个源生成器,它从配置文件中读取一些信息,允许使用者更改更改生成器的 output 的设置。 That file is added to the consuming project by adding the following to the.csproj file:通过将以下内容添加到 .csproj 文件中,该文件将添加到使用项目中:

<ItemGroup>
      <AdditionalFiles Include="generator.config" />
</ItemGroup>

I'd like for coworkers to be able to install my nuget package and have the generator.config file as well as the above ItemGroup automatically added to their projects.我希望同事能够安装我的 nuget package 并将 generator.config 文件以及上述 ItemGroup 自动添加到他们的项目中。

I thought this would be rather simple, but I've spent days trying to achieve this with no luck so far and I'm ready to pull my hair out.我认为这会很简单,但是到目前为止,我已经花了几天时间试图实现这一目标,但没有运气,我已经准备好拔掉头发了。 I've tried:我试过了:

  • Including generator.config in the content and contentFiles directories of the package.在 package 的 content 和 contentFiles 目录下包含 generator.config。 This did get the file into the consuming project, but it wasn't editable, which made it useless a config file.这确实使文件进入了消费项目,但它不可编辑,这使得它成为无用的配置文件。
  • Various permutations of.props and.targets files in my nuget package.我的 nuget package 中的 .props 和 .targets 文件的各种排列。 I managed to get generator.config copied to the output directory after building, but not to the project directory我设法在构建后将 generator.config 复制到 output 目录,但没有复制到项目目录
  • I found some posts talking about about running a powershell script to copy files on install but that seemed like that's a deprecated way to go about this我发现一些帖子谈论运行 powershell 脚本以在安装时复制文件,但这似乎是 go 不推荐使用的方式

I think I may be experiencing some additional difficulty due to my consuming project targeting .NET 6, while the source generator has to target netstandard2.0.我想我可能会遇到一些额外的困难,因为我的消费项目针对 .NET 6,而源生成器必须针对 netstandard2.0。 I only have a passing familiarity with Nuget/MSBuild from looking at.csproj files so I suspect I may be missing something fairly obvious, but any help/suggestions would be appreciated.我只是通过查看 .csproj 文件对 Nuget/MSBuild 有了初步的了解,所以我怀疑我可能遗漏了一些相当明显的东西,但任何帮助/建议都将不胜感激。

I managed to get this working.我设法让这个工作。 For anyone who may be having a similar issue:对于可能遇到类似问题的任何人:

I mostly worked off this answer from Mr Qian .我主要从钱先生那里得到这个答案 I first tried implementing almost exactly as in that post:我首先尝试几乎完全按照该帖子中的方式实现:

I placed my generator.config file in the project root added the following to the generator project file:我将 generator.config 文件放在项目根目录中,将以下内容添加到生成器项目文件中:

<ItemGroup>
    <None Include="generator.config" Pack="true" PackagePath="lib"></None>
    <None Include="build\myGenerator.props" Pack="true" PackagePath="build"></None>
</ItemGroup>

Then I created a "build" directory off my project root, and created myGenerator.props like this as suggested in the linked answer:然后我在我的项目根目录下创建了一个“build”目录,并按照链接答案中的建议创建了 myGenerator.props:

<Project>
    <ItemGroup>
        <AdditionalFiles Include="generator.config" />
    </ItemGroup>
    <Target Name="CopyGeneratorConfig" BeforeTargets="Build" Condition="!Exists('$(MSBuildProjectDirectory)\generator.config')">
        <ItemGroup>
            <MyEnvConfig Include="$(MSBuildThisFileDirectory)..\lib\generator.config"/>
        </ItemGroup>
        <Copy
           SourceFiles="@(MyEnvConfig)"
           DestinationFolder="$(MSBuildProjectDirectory)"
         />
    </Target>
</Project>

But this didn't work for me at all.但这对我根本不起作用。 Nothing seemed to be happening.似乎什么都没有发生。

I made some progress by separating out the <Target> into its own myGenerator.targets file.通过将<Target>分离到它自己的 myGenerator.targets 文件中,我取得了一些进展。 After doing so, I could see my <AdditionalFiles> under Imports in the consuming project immediately after installing the nuget package.这样做之后,我可以在安装 nuget package 后立即在消费项目的 Imports 下看到我的<AdditionalFiles> But the file copy still didn't work.但是文件副本仍然不起作用。 It didn't seem that my Copy target was running at all.我的 Copy 目标似乎根本没有运行。

After much hand wringing, teeth gnashing, hair pulling, and furious googling, I found a solution: change the value of BeforeTargets on the copy target from "Build" to "BeforeBuild".经过一番绞尽脑汁、咬牙切齿、拔头发和疯狂的谷歌搜索后,我找到了一个解决方案:将复制目标上的BeforeTargets值从“Build”更改为“BeforeBuild”。 Once I made that change, it worked exactly as I wanted.一旦我做了那个改变,它就完全按照我的意愿工作了。 (Well, almost exactly... it still requires a build, and I ideally wanted the copy to happen upon installation of the nuget package.) (嗯,几乎完全一样......它仍然需要构建,我理想地希望在安装 nuget package 时发生副本。)

So my working set-up uses the change to the project file mentioned above, the following.props file (note the name before.props and.targets must match your project name):所以我的工作设置使用了对上面提到的项目文件的更改,以下 .props 文件(注意之前的名称 .props 和 .targets 必须与您的项目名称匹配):

<Project>
    <ItemGroup>
        <AdditionalFiles Include="generator.config" />
    </ItemGroup>
</Project>

And the following.targets file:以及以下.targets 文件:

<Project>
    <Target Name="CopyGeneratorConfig" BeforeTargets="BeforeBuild" Condition="!Exists('$(MSBuildProjectDirectory)\generator.config')">
        <ItemGroup>
            <MyEnvConfig Include="$(MSBuildThisFileDirectory)..\lib\generator.config"/>
        </ItemGroup>
        <Copy
           SourceFiles="@(MyEnvConfig)"
           DestinationFolder="$(MSBuildProjectDirectory)"
         />
    </Target>
</Project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM