简体   繁体   English

具有相关项目的本地开发nuget

[英]Locally developed nuget with dependent projects

Let's say I have project A and project B . 假设我有项目A和项目B。 Project A depends on project B . 项目A取决于项目B。 So A would normally have a direct reference to B 's DLL. 因此, A通常会直接引用B的DLL。

Then I decided to publish B as a nuget package. 然后,我决定将B发布为nuget包。 Now, A has a reference to B via nuget instead of a local DLL. 现在, A通过nuget而不是本地DLL引用了B。

The downside to this arrangement is that if I update B , I need to upload a new nuget and wait for it to be available in order to use it from A . 这种安排的缺点是,如果我更新B ,则需要上载新的nuget并等待它可用,以便从A使用它。

I see that I can point to a local nuget package when updating A 's reference to B so that does help a bit. 我看到在更新AB的引用时可以指向本地nuget包,这确实有所帮助。 However, if I make a change to B , I do still have to go through the steps of generating a new package and updating A 's reference to the package before A will see the change. 但是,如果我对B进行更改,则在A看到更改之前,我仍然必须经历生成新包并更新A对包的引用的步骤。 With the pre-nuget arrangement, I simply built B and A would see the changes. 通过预先的安排,我简单地构建了B,A可以看到更改。

An alternative is to remove A 's reference to B 's nuget package and revert to pointing to the local DLL while doing local development. 另一种选择是删除AB的nuget包的引用,并在进行本地开发时恢复为指向本地DLL。 However, if A is published to github, then the reference has to be reverted to a nuget reference before pushing to github. 但是,如果将A发布到github,则在推送到github之前,该引用必须还原为nuget引用。

What's the best practice in a situation like this? 在这种情况下,最佳做法是什么? Surely many folks are dealing with this sort of thing with github and nuget being widely used. 当然,有很多人正在使用github和nuget来处理这种事情。


UPDATE UPDATE

This topic came up for discussion on the C# subreddit and some interesting approaches were pointed out. 这个话题引起了有关C#subreddit的讨论 ,并指出了一些有趣的方法。

Azure devops - original comment - thanks to B0dona Azure开发人员- 原始评论-感谢B0dona

What we do is use azure devops ( https://azure.microsoft.com/en-us/services/devops/ ) to build and push our nuget packages to our own repository (nuget.org is slow) automatically after a new commit has been pushed. 我们要做的是在新提交后使用azure devops( https://azure.microsoft.com/zh-cn/services/devops/ )自动构建并将nuget软件包推送到我们自己的存储库中(nuget.org较慢)。已被推。

You set it up once, push project B drink some coffee and enjoy the joy that is updating packages. 设置一次,推动项目B喝点咖啡,享受更新软件包的乐趣。

git submodules - original comment - thanks to alkrun git子模块- 原始评论-感谢alkrun

You mention GitHub so I'll propose something a bit different: 您提到了GitHub,所以我会提出一些不同的建议:

In my opinion, if work on project A is likely to cause changes in project B, A referencing B as a git submodule is much easier than dealing with nuget. 我认为,如果在项目A上进行的工作很可能导致项目B发生变化,则将B引用为git子模块的A比处理nuget容易得多。 Git Submodules aren't without their headaches, but this is what they were designed for. Git子模块并非没有头痛,但这就是它们的设计目标。 Some of the benefits: 一些好处:

1) The biggest is that if you say "If I need to change B then I'll just make the change and push to get a new package built then test it out in A" it's not a very fluid model to work with and it's asking developers to push untested code into B. Then that 1-3 minute turn around for CI build/package turns into 3 x 1-3 minute turnarounds and it just feels horrible when I've tried it in the past. 1)最大的问题是,如果您说“如果我需要更改B,那么我将进行更改并推动构建新的程序包,然后在A中对其进行测试”,这不是一个非常流畅的模型,它可以要求开发人员将未经测试的代码推送到B中。然后,CI构建/打包的1-3分钟转换时间变成了3 x 1-3分钟的转换时间,当我过去尝试过它时,这简直太可怕了。 It's a massive productivity killer. 这是一个巨大的生产力杀手。

2) Any other options around changing csproj files, they can work, but they're very error prone. 2)关于更改csproj文件的任何其他选项,它们都可以使用,但是它们很容易出错。 Developers aren't always great about reviewing all changes before they commit them, you're going to have developers forgetting and checking in the change to a project reference and it's going to cause build failures. 开发人员在提交变更之前并不总是很擅长审查所有变更,您将让开发人员忘记并检查对项目参考的变更,这将导致构建失败。

3) Using B as a submodule for A doesn't prevent you from having automated builds on B that produce nuget packages, and maybe other projects which are less likely to change B could/should refer to those 3)将B用作A的子模块不会阻止您在B上自动生成生成nuget程序包,并且也许其他不太可能更改B的项目可以/应该引用那些

4) At some point in the development of A, if it matures and becomes less likely to cause changes in B, then you can switch A->B to a nuget package reference also 4)在A的开发过程中,如果它成熟并不太可能引起B的变化,那么您也可以将A-> B切换为nuget包引用

Another option, I remember reading an article years ago where someone had created a tool that generated an msbuild xproj file that would replace package references with project references. 我还记得几年前读过一篇文章,其中有人创建了一个工具,该工具生成了一个msbuild xproj文件,该文件将用项目引用替换程序包引用。 They had it set up where the xproj file was on the .gitignore and if the file didn't exist, the nuget package reference was used. 他们设置了xproj文件在.gitignore上的位置,如果该文件不存在,则使用nuget包引用。 In this way, a developer would run a command to switch over to project references, make the changes they need, commit them, push changes, then update the nuget reference. 这样,开发人员将运行命令以切换到项目引用,进行所需的更改,提交更改,推送更改,然后更新nuget引用。 This seemed fairly clean to me, but I can't find the article and it was a bit more complicated than I'm making it sound. 这对我来说似乎很干净,但是我找不到这篇文章,而且比说的听起来要复杂得多。

So I'd still go with the git submodule route. 所以我仍然会继续使用git子模块路由。 There are some quirks with git submodules but they have gotten a lot easier to deal with in the last couple years and it feels like the quirks are easier to explain than the other options. 有一些带有git子模块的怪癖,但是在最近几年中,它们变得更容易处理了,觉得怪癖比其他选项容易解释。 Having used this for a few projects now, it's very intuitive. 现在已经在一些项目中使用了它,这非常直观。 Understand what a Git Submodule in a detached head state is and how to avoid it, make sure to use the -b option when adding a submodule, and find a good tool that can handle submodules well. 了解处于分离头部状态的Git子模块是什么以及如何避免它,确保在添加子模块时使用-b选项,并找到可以很好地处理子模块的好工具。 For what it's worth, VS Code is the most intuitive interface I've found for working with submodules. 就其价值而言,VS Code是我发现用于处理子模块的最直观的界面。 Open A in VS Code then switch to the version control tab and you'll see A and any submodules listed up top along with the branches they're tracking. 在VS Code中打开A,然后切换到版本控制标签,您会看到A和所有子模块以及它们正在跟踪的分支列在顶部。 Commit changes to the submodule, then to the parent and you're good to go. 将更改提交给子模块,然后提交给父模块,您就可以开始了。

If you are using Visual Studio 2017, you can install NuGet Reference Switcher for Visual Studio 2017 extension. 如果您使用的是Visual Studio 2017,则可以为Visual Studio 2017扩展安装NuGet参考切换器 Here is a guide how to use it: https://github.com/RicoSuter/NuGetReferenceSwitcher/wiki/Guide 这是如何使用它的指南: https : //github.com/RicoSuter/NuGetReferenceSwitcher/wiki/Guide

The Node community demonstrates a more robust approach. Node社区展示了一种更强大的方法。 The npm link command which is available out of the box (see also this blog post ), creates symbolic link from distributed package directory to package source directory. 开箱即用的npm link命令(另请参阅此博客文章 )创建从分布式软件包目录到软件包源目录的符号链接。 In this way: 通过这种方式:

  • package consumers are transparently redirected to package source project 包使用者透明地重定向到包源项目
  • changes in package sources are automatically reflected on the consumer side 包裹来源的变化会自动反映在消费者方面

The npm link approach has also these advantages over reference switching: 与参考切换相比, npm link方法还具有以下优点:

  • no changes are made to consumer's source code -- which can accidentally be committed. 不会对消费者的源代码进行任何更改-可能会意外提交。
  • works cross-platform, doesn't need a specific IDE 跨平台工作,不需要特定的IDE
  • can be scripted and shared with the team 可以编写脚本并与团队共享

This feature is obviously required in the NuGet community, but for some reason, NuGet still lacks it. NuGet社区显然需要此功能,但是由于某些原因,NuGet仍然缺少此功能。 There is a feature request https://github.com/NuGet/Home/issues/1821 , which indicates they have no plans of adding it. 有一个功能请求https://github.com/NuGet/Home/issues/1821 ,表明他们没有计划添加它。

Meanwhile I created a tool, which works similarly to npm link for NuGet packages, you may want to give it a try: https://www.nuget.org/packages/NuLink 同时,我创建了一个工具,该工具的工作方式类似于NuGet软件包的npm链接 ,您可以尝试一下: https : //www.nuget.org/packages/NuLink

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

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