简体   繁体   English

如何以适当的方式将nuget包添加到package.config?

[英]How to add a nuget package to the package.config the proper way?

For my C# project I am maintaining a packages.config which includes all the dependencies my project requires. 对于我的C#项目,我正在维护一个packages.config ,其中包括项目所需的所有依赖项。 Over the time I have been copy-pasting the entries manually using a regular text editor, checking in the version number I found on the NuGet website and so on. 一段时间以来,我一直在使用常规文本编辑器手动复制粘贴条目,检查在NuGet网站上找到的版本号,依此类推。 To restore the package upon a checkout, I use nuget -o nuget-packages install packages.config which worked good so far (I am using Xamarin Studio on non-windows systems, so no VS available). 要在签出时恢复软件包,我使用nuget -o nuget-packages install packages.config ,到目前为止效果很好(我在非Windows系统上使用Xamarin Studio,因此没有VS可用)。

I was just realizing that messing with the packages.config in a text editor couldn't be the intended way. 我只是意识到,在文本编辑器中弄乱packages.config并不是预期的方式。 I know from npm and bower that a npm install --save-dev <pkg-xy> will to the job and write the package version back to the config. 我从npm和bower那里知道npm install --save-dev <pkg-xy>将会完成工作,并将软件包版本写回到配置中。 Is there an equivalent of this in NuGet.exe? 在NuGet.exe中有与此等效的功能吗?

From the command line this not supported. 从命令行不支持。 You will have to edit the packages.config file manually, or write a utility to do this work for you, or extend NuGet.exe with this feature. 您将必须手动编辑packages.config文件,或者编写实用程序来为您完成此工作,或者使用此功能扩展NuGet.exe。

There is no equivalent in NuGet to NPM's npm install -save NuGet中没有等效于NPM的npm install -save

Currently the only way to have the packages.config file automatically updated when you want to install a new NuGet package to your project is to use an IDE, such as Visual Studio or Xamarin Studio, and actually install the package. 当前,当您要将新的NuGet软件包安装到项目中时,自动更新packages.config文件的唯一方法是使用IDE(例如Visual Studio或Xamarin Studio)并实际安装该软件包。 Using NuGet.exe from the command line does not have an option to add/remove entries in to/from the packages.config file when installing a new package. 从命令行使用NuGet.exe时,没有选择在安装新软件包时向/packages.config文件中添加条目或从中删除条目的选项。

NuGet.exe does have an update command which will update the package to the latest version. NuGet.exe确实具有更新命令,该命令会将软件包更新为最新版本。 Whilst this would update your packages.config file it also updates your project file by adding any assembly references that the NuGet package needs. 尽管这将更新您的packages.config文件,但也会通过添加NuGet软件包所需的任何程序集引用来更新您的项目文件。

You really shouldn't be editing packages.config . 您确实不应该编辑packages.config Package Restore doesn't do what you think it does. 程序包还原无法执行您认为的操作。 It simply downloads any missing packages that are listed in packages.config . 它仅下载packages.config中列出的所有缺少的软件包

You might think this is what you want, but Package Restore does NOT add references to your project. 你可能会认为这是你想要的,但包还原添加引用到您的项目。 It also doesn't do any of the other things the package creator had intended like running an install.ps1 script. 它也不会执行软件包创建者打算执行的其他任何操作,例如运行install.ps1脚本。

When installing a package, NuGet handles all of this, so your project files have added references, content, etc. This and the packages.config file is what you would commit to source control. 安装软件包时,NuGet会处理所有这些问题,因此您的项目文件已添加了引用,内容等。这就是您将提交给源代码管理的packages.config文件。 You can leave out the actual packages folder, so you don't have to commit large binary files. 您可以忽略实际的packages文件夹,因此不必提交大型二进制文件。

When you open the solution and build, NuGet will see that the packages are missing and will download them as if you had checked them in. The actual "install" was already done (and committed). 当您打开解决方案并进行构建时,NuGet将看到软件包丢失,并像您已签入它们一样下载它们。实际的“安装”已经完成(并提交)。 That is all that Package Restore does: no more, no less. 这就是Package Restore的全部功能:不多也不少。

If you are using Xamarin Studio, you can install NuGet by following the instructions here: 如果您使用的是Xamarin Studio,则可以按照以下说明安装NuGet:

https://github.com/mrward/monodevelop-nuget-addin https://github.com/mrward/monodevelop-nuget-addin

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

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