使用 nuget 包配置 visual studio,VSIX 自动
configure visual studio with nuget package, VSIX automatically
我必须在 visual studio 中配置 nugets amd VSIX。我们现在通过在 Tools -> Options -> Enviroments / Tools -> Options -> Nuget Package manager 下添加包源来手动完成。
我需要在 Visual studio 中打开工具 -> 选项时自动为 nuget 和 VSIX 填充 feed/URL,这样用户应该 select 相应的提要并安装它, 以消除为安装 nugets/VSIX.
添加 url 的手动开销
谢谢。
可以为所需的 nuget 提要以编程方式配置 Nuget 库。我们通过编写自定义控件来完成它。在我们的系统中,在 'AppData' 文件夹下,有一个名为 Nuget.config 的文件。
在此文件中列出了所有 Nuget 字段,因此用户可以在 Visual Studio -> 工具 -> 选项 -> 环境/工具 -> 选项 -> Nuget 包管理器中看到这些 Nuget 提要.
要添加您的 Nuget 提要,您只需修改 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 命令 Register-PackageSource 例如
Register-PackageSource -Name "My package source" -Location "C:\MyNugetPackages" -ProviderName "NuGet"
这会将包源也添加到 Visual studio 设置中。
运行 来自 C# 的 powershell 命令看起来 here or C++ here。
我必须在 visual studio 中配置 nugets amd VSIX。我们现在通过在 Tools -> Options -> Enviroments / Tools -> Options -> Nuget Package manager 下添加包源来手动完成。
我需要在 Visual studio 中打开工具 -> 选项时自动为 nuget 和 VSIX 填充 feed/URL,这样用户应该 select 相应的提要并安装它, 以消除为安装 nugets/VSIX.
添加 url 的手动开销谢谢。
可以为所需的 nuget 提要以编程方式配置 Nuget 库。我们通过编写自定义控件来完成它。在我们的系统中,在 'AppData' 文件夹下,有一个名为 Nuget.config 的文件。
在此文件中列出了所有 Nuget 字段,因此用户可以在 Visual Studio -> 工具 -> 选项 -> 环境/工具 -> 选项 -> Nuget 包管理器中看到这些 Nuget 提要.
要添加您的 Nuget 提要,您只需修改 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 命令 Register-PackageSource 例如
Register-PackageSource -Name "My package source" -Location "C:\MyNugetPackages" -ProviderName "NuGet"
这会将包源也添加到 Visual studio 设置中。
运行 来自 C# 的 powershell 命令看起来 here or C++ here。