【问题标题】:configure visual studio with nuget package, VSIX automatically使用 nuget 包、VSIX 自动配置 Visual Studio
【发布时间】:2015-10-21 11:45:07
【问题描述】:

我必须在我的 Visual Studio 中配置 nugets amd VSIX。我们现在通过在 Tools -> Options -> Enviroments / Tools -> Options -> Nuget Package manager 下添加包源来手动执行此操作。

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

谢谢。

【问题讨论】:

    标签: .net visual-studio nuget-package vsix nuget-server


    【解决方案1】:

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

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

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

    [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;
        }
    

    【讨论】:

      【解决方案2】:

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

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

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

      要从 C# 运行 powershell 命令,请查看 here 或 C++ here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-08
        • 2014-06-25
        • 1970-01-01
        相关资源
        最近更新 更多