2015-10-21 63 views
1

我必須在我的視覺工作室配置新手和amd VSIX。我們現在正在通過在工具 - >選項 - >環境/工具 - >選項 - > Nuget包管理器下添加軟件包源手動執行它。配置視覺工作室與Nuget包,VSIX自動

當我們在Visual Studio中打開Tools - > Options時,我需要自動填充nuget和VSIX的feed/URL,以便用戶應該選擇相應的feed並安裝它,以消除手動添加url的開銷用於安裝nugets/VSIX。

謝謝。

回答

1

可以通過編程爲Nuget庫配置所需的nuget feed。我們通過編寫自定義控件來完成它。在我們的系統中,在'AppData'文件夾下,有一個名爲的文件Nuget.config

在這個文件中列出了所有的Nu​​get字段,結果用戶可以在Visual Studio - > Tools - > Options - > Enviroments/Tools - > Options - > Nuget Package manager中看到這些Nuget Feed。

要添加您的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; 
    }