繁体   English   中英

使用nuget程序包自动配置VSIX的Visual Studio

[英]configure visual studio with nuget package, VSIX automatically

我必须在我的视觉工作室中配置nugets和VSIX。 现在,我们通过在“工具”->“选项”->“环境/工具”->“选项”->“ Nuget软件包管理器”下添加软件包源来手动执行此操作。

当我们在Visual Studio中打开“工具”->“选项”时,我需要自动填充nuget和VSIX的提要/ URL,以便用户应仅选择相应的提要并进行安装,以消除添加用于安装nugets /的url的手动开销VSIX。

谢谢。

可以以编程方式为所需的nuget提要配置Nuget画廊。 我们通过编写一个自定义控件来做到这一点。 在我们的系统中,“ AppData”文件夹下有一个名为Nuget.config的文件。

在此文件中,列出了所有Nuget字段,因此用户可以在Visual Studio->工具->选项->环境/工具->选项-> Nuget软件包管理器中看到那些Nuget提要。

要添加您的Nuget Feed,您只需要修改Nuget.Config文件并将其添加到其中即可。 下面是代码:

[CustomAction]        
    public static ActionResult ConfigAdeptNuGetFeed(Session session)
    {
        session.Log("*** Begin ConfigAdeptNuGetFeed ***");

        var result = ActionResult.Failure;
        if (File.Exists(XDocPath))
        {
            session.Log("Nuget.config was found in the %appdata% folder.");
            try
            {
                var xDoc = new XmlDocument();
                xDoc.Load(XDocPath);
                var baseNode = xDoc.DocumentElement;

                if (baseNode != null)
                {
                    session.Log($"{baseNode.Name} has {baseNode.ChildNodes.Count} child elements.");
                    session.Log($"XML before install: {baseNode.OuterXml}");
                    if (baseNode.ChildNodes.Count >= 2)
                    {

                        //Checks for the AdeptNugetfeed.    
                        var node =
                            baseNode.SelectSingleNode($"//add[@value='{AdeptInstaller.AdeptNuGetFeedPath}']");

                        //AdeptFeed not found, adding it.
                        if (node == null)
                        {
                            session.Log("Adept Feed not present. Adding it now.");
                            var packageNode = xDoc.GetElementsByTagName("packageSources")[0];
                            var newElement = xDoc.CreateElement("add");
                            var xAttribute = xDoc.CreateAttribute("key");
                            xAttribute.Value = "AdeptNugetFeed";
                            var xAttributeVal = xDoc.CreateAttribute("value");
                            xAttributeVal.Value = AdeptInstaller.AdeptNuGetFeedPath;
                            newElement.Attributes.Append(xAttribute);
                            newElement.Attributes.Append(xAttributeVal);
                            packageNode.AppendChild(newElement);

                        }

                        else
                        {
                            session.Log("Adept feed is already present. Nothing to do.");
                        }
                    }
                    else
                    {
                        session.Log($"{baseNode.Name} is empty.");
                        baseNode.InnerXml = AdeptInstaller.NuGetFullConfig;

                    }
                    xDoc.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Nuget", "Nuget.config"));
                    session.Log($"XML after install: {baseNode.OuterXml}");
                }
                else
                {
                    session.Log("NuGet.config file is invalid... abbending.");
                    throw new XmlException("XML issue with the reading of the nuget.config file.");
                }
                result = ActionResult.Success;
            }

            catch (Exception exc)
            {
                session.Log(exc.ToString());
                result = ActionResult.Failure;
            }
        }

        else
        {
            Console.WriteLine(AdeptInstaller.Nuget_config_file_not_found);
            result = ActionResult.Failure;
        }
        return result;
    }

更好的方法是运行powershell comand Register-PackageSource例如

Register-PackageSource -Name "My package source" -Location "C:\MyNugetPackages" -ProviderName "NuGet"

这会将软件包源也添加到Visual Studio设置中。

若要从C#PowerShell的COMAND看这里或C ++ 在这里

暂无
暂无

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

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