简体   繁体   English

NuGet:在项目中明确列出 package 依赖项作为 package 引用,以便可以卸载它们

[英]NuGet: explicitly list package dependencies as package references in project so they can be uninstalled

I'm creating a NuGet package (let's call it Lib ) that also has analyzers in a separate package ( Lib.Analyzers ).我正在创建一个 NuGet package (我们称之为Lib ),它在单独的 package ( Lib.Analyzers )中也有分析器

By default I want Lib.Analyzers to be installed to the the project when Lib is installed.默认情况下,我希望在安装Lib时将Lib.Analyzers安装到项目中。 I can achieve this by listing Lib.Analyzers in the dependencies of Lib (in the .nuspec file).我可以通过在Lib的依赖项(在.nuspec文件中)中列出Lib.Analyzers来实现这一点。

But I also want users to be able to uninstall Lib.Analyzers again when they don't want them.但我也希望用户能够在不需要时再次卸载Lib.Analyzers

AFAIK package dependencies are not listed as individual packages in the package manager and the following wouldn't work in the package manager console: AFAIK package 依赖项未在 package 管理器中列为单独的包,以下在 package 管理器控制台中不起作用:

PM> Install-Package Lib
...
PM> Uninstall-Package Lib.Analyzers
Uninstall-Package : Package 'Lib.Analyzers' to be uninstalled could not be found in project 'ClassLibrary1'

Is there a way to achieve this?有没有办法做到这一点? I couldn't find anything in the docs .我在文档中找不到任何东西。

Rationale基本原理

I want to minimize friction for new users, ie they should get the help Lib.Analyzers provides without having to remember to install the analyzers every time they install Lib .我想尽量减少新用户的摩擦,即他们应该得到Lib.Analyzers提供的帮助,而不必记住每次安装Lib时都要安装分析器。

But I also want Lib.Analyzers to not be forced onto experienced users that might not want them anymore.但我也希望Lib.Analyzers不会被强加给可能不再需要它们的有经验的用户。

Workaround解决方法

My current workaround would be to rename Lib to Lib.Core , remove its dependency to Lib.Analyzers and create a third meta-package named Lib that bundles the two, arriving at the following dependency graph:我目前的解决方法是将Lib重命名为Lib.Core ,删除其对Lib.Analyzers的依赖关系,并创建第三个名为Lib的元包,将两者捆绑在一起,得到以下依赖关系图:

Lib
|- Lib.Core
'- Lib.Analyzers

New users would then always install Lib and experienced ones could install only Lib.Core instead.新用户总是会安装Lib ,而有经验的用户只能安装Lib.Core

However, Lib is actually a rather small library so this solution feels a little "over-engineered".然而, Lib实际上是一个相当小的库,所以这个解决方案感觉有点“过度设计”。

There is a GitHub Issue about optional dependencies here , which is closed.这里有一个关于可选依赖项的 GitHub 问题,已关闭。

So unfortunately it is not supported currently, and does not seems to be in the future.所以不幸的是,目前不支持它,而且似乎将来也不支持。

rohit21agrawal gave the following close reason: rohit21agrawal给出了以下接近的理由:

Couple of reasons - we are closing bugs that have not been active for over a year, and two, this doesn't fit in with the plans we have for NuGet in the near future.有几个原因 - 我们正在关闭一年多没有活跃的错误,第二,这不符合我们在不久的将来为 NuGet 制定的计划。

You can open a new issue if you have a strong case for this feature and we would consider it.如果您对此功能有充分的理由,您可以打开一个新问题,我们会考虑它。

Maybe your issue is a "strong case" .也许您的问题是“强有力的案例”

That being said, your workaround seems pretty good.话虽如此,您的解决方法似乎还不错。 More a documentation/introduction problem.更多文档/介绍问题。


There is also an option to suppress specific analyzers , if that is the root of your question.如果这是您问题的根源,还有一个选项可以抑制特定分析器

灯泡菜单

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

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