Search code examples
.netvisual-studionuget-packagevsixnuget-server

configure visual studio with nuget package, VSIX automatically


I have to configure nugets amd VSIX in my visual studio. We are right now doing it manually by adding the package source under Tools -> Options -> Enviroments / Tools -> Options -> Nuget Package manager.

I need to populate the feed/URL for nuget and VSIX automatically when we open Tools -> Options in Visual studio so that the user should just select the respective feed and install it, to remove the manual overhead of adding the url for installing nugets/VSIX.

Thanks.


Solution

  • Nuget gallery can be programmatically configured for the required nuget feed. We have done it by writing a custom control. In our system, under 'AppData' folder, there is a file called Nuget.config.

    In this file all the Nuget fields are listed and as a result the user can see those Nuget feeds in Visual Studio -> Tools -> Options -> Enviroments / Tools -> Options -> Nuget Package manager.

    To add your Nuget Feed, you just need to modify the Nuget.Config file and add your feed to it. Below is the Code:

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