简体   繁体   English

如何使用nuget.core将nuget包添加到c#项目文件

[英]How to add nuget packages to c# project files with nuget.core

I am trying to upgrade a bunch of old c# projects to consume nuget packages that I have made from old assembly dependencies. 我试图升级一堆旧的c#项目,以使用我从旧程序集依赖项制作的nuget包。 I want to write a C# program to update all the csproj files to reference the package. 我想编写一个C#程序来更新所有csproj文件以引用该程序包。

I have the code using nuget.core making a dictionary of dependent assemblies to a nuget package. 我有使用nuget.core的代码,将依赖程序集的字典制作成nuget包。 I can iterate over the references in my project file and find the needed nuget package from my repository. 我可以遍历项目文件中的引用,并从存储库中找到所需的nuget包。

var localRepo = PackageRepositoryFactory.Default.CreateRepository(nugetCachePath);
var packages = localRepo.GetPackages();
foreach (var package in packages)
{
   foreach (var assemblyReference in package.AssemblyReferences)
   {
        assemblyToPackage.Add(Path.GetFileNameWithoutExtension(assemblyReference.Name).ToLower(),package);
   }               
}

I think I need to use Nuget.Core ProjectManager to add the reference to my project file (csproj). 我想我需要使用Nuget.Core ProjectManager将引用添加到我的项目文件(csproj)。

<ItemGroup>
    <PackageReference Incluse="KB.MyOldAssemblyPackage">
       <Version>1.0.0</Version>
    </PackageReference>
</ItemGroup>

What I can't figure out is how to get an instance of ProjectManager or any examples on how to use it to add the nuget reference. 我不知道是如何获得ProjectManager的实例或有关如何使用它添加nuget引用的任何示例。 I know I could simply inject the xml with an xdocument but since I made the effort to use nuget.core I was hoping to find a solution using it. 我知道我可以简单地用xdocument注入xml,但是由于我努力使用nuget.core,所以我希望找到一个使用它的解决方案。

Any help out there? 有什么帮助吗?

Short answer. 简短的答案。 You can't. 你不能

The NuGet.Core dll is NuGet 2.x, while PackageReference was introduced in 4.x 4.x has a more modular approach, and there isn't one package you can use to achieve your goal. NuGet.Core dll是NuGet 2.x,而PackageReference是在4.x 4.x中引入的,它具有一种更加模块化的方法,没有一个可用于实现目标的软件包。

If you have Visual Studio 2017, you can use the latest 15.7 release (to be released in 2 weeks or just use the preview) to migrate your packages to PackageReference. 如果您拥有Visual Studio 2017,则可以使用最新的15.7版本(将在2周内发布或仅使用预览)将包迁移到PackageReference。 That's a built in tool that should handle most of your scenarios if compatible. 这是一个内置工具,如果兼容,它应该可以处理大多数情况。

If you're really stuck with having to this programmatically the following code should be a great start. 如果您真的不得不通过编程来解决这个问题,那么下面的代码应该是一个很好的开始。

I don't understand what ways to manage packages you use now, but if it's packages.config , then I think better migrate to PackageReference . 我不知道现在使用什么方法来管理软件包,但是如果是packages.config ,那么我认为最好迁移到PackageReference For it's may use NuGet PackageReference Upgrader that convert your dependencies from packages.config to PackageReference format. 因为它可能使用NuGet PackageReference升级程序 ,将您的依赖项从packages.config转换为PackageReference格式。

After that you can add/remove our packages with NuGet Package Manager UI extension that built-in in Visual Studio 2017 . 之后,您可以使用Visual Studio 2017中内置的NuGet软件包管理器UI扩展来添加/删除我们的软件包。

UPD2 . UPD2 Visual Studio 17 Preview 3 has tool for migrate from packages.config to PackageReference . Visual Studio 17 Preview 3 已经用于迁移从工具packages.configPackageReference


Also MSBuild 15.1 + has built-in NuGet targets such as restore and pack that you can combining with custom build targets. 此外, MSBuild 15.1 + 还具有内置的NuGet目标,例如还原打包 ,您可以将其与自定义构建目标结合使用。 It's make easier build process. 它使构建过程更容易。


If you still want to write program to added references to .csproj you can create extension for Visual Studio . 如果仍要编写程序以添加对.csproj引用,则可以为Visual Studio创建扩展名。


UPD1 . UPD1 Get IEnumerable<Project> from GlobalProjectCollection and parse libraries names from the Include attributes: GlobalProjectCollection获取IEnumerable<Project>并从Include属性解析库名称:

GetReferences - GetReferences

public static IEnumerable<string> GetReferences(IEnumerable<Project> project)
{
    return project.Select(p => p.GetItems("Reference").Select(i => i.EvaluatedInclude);
}

After that check availability this library in your NuGet repository with help your code and if exists, then remove current reference and create new: 在检查可用性之后,使用帮助您的代码在NuGet存储库中的该库,如果存在,则删除当前引用并创建新的库:

CreateReference - CreateReference

using Microsoft.Build.Evaluation;

using System.Collections.Generic;

namespace NuGetReference
{
    public static class Reference
    {
        public static void CreateReference(string projectName, string packageName, string packageVersion)
        {
            Project project = ProjectCollection.GlobalProjectCollection.LoadProject(projectName);

            project.AddItemFast("PackageReference", packageName, new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("Version", packageVersion) });

            project.Save();
        }
    }
}

Migrate from packages.config to PackageReference | 从packages.config迁移到PackageReference | Project Class | 项目类别 GlobalProjectCollection | GlobalProjectCollection | AddItemFast AddItemFast

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

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