前言
部署了一个私域nuget,但是上传组件的操作很麻烦,此文档将通过开发插件的方式,简化将组件上传至nuget过程。
demo
https://gitee.com/chenheze90/L30_VSExpand.git
操作过程
MyCommand1.cs的Execute代码如下:
private async void Execute(object sender, EventArgs e)
{
try
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// 获取当前选中的项目
DTE dte = await ServiceProvider.GetServiceAsync(typeof(DTE)) as DTE;
if (dte == null)
{
return;
}
var selectedProject = dte.SelectedItems.Item(1).Project;
if (selectedProject != null)
{
string projectPath = selectedProject.FullName;
string projectDir = Path.GetDirectoryName(projectPath);
// 生成 DLL
string dllPath = Path.Combine(projectDir, "bin", "Debug", $"{selectedProject.Name}.dll");
// 生成 NuGet 包
string nugetExePath = Path.Combine(projectDir, "bin", "Debug", "nuget.exe");
string nuspecPath = Path.Combine(projectDir, "bin", "Debug", $"{selectedProject.Name}.nuspec");
// 创建 nuspec 文件
string nuspecContent = $@"
<package>
<metadata>
<id>{selectedProject.Name}</id>
<version>1.0.0</version>
<authors>chz</authors>
<owners>chz</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<projectUrl>http://www.baidu.com/</projectUrl>
<description>Description</description>
<releaseNotes>myreleaseNotes.</releaseNotes>
<copyright>$copyright$</copyright>
<tags>Tag:111</tags>
</metadata>
<files>
<file src=""{dllPath}"" target=""lib
etstandard2.0"" />
</files>
</package>";
File.WriteAllText(nuspecPath, nuspecContent); // 执行 nuget pack 命令
ProcessStartInfo nugetPackInfo = new ProcessStartInfo
{
FileName = nugetExePath,
Arguments = $"pack {nuspecPath}",
WorkingDirectory = projectDir,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process nugetPackProcess = new Process { StartInfo = nugetPackInfo })
{
nugetPackProcess.Start();
string output = nugetPackProcess.StandardOutput.ReadToEnd();
nugetPackProcess.WaitForExit();
if (nugetPackProcess.ExitCode == 0)
{
string nupkgPath = Path.Combine(projectDir, $"{selectedProject.Name}.1.0.0.nupkg"); // 上传到私域 Git
string gitUrl = "https://your-private-git-url";
string apiKey = "your-api-key";
ProcessStartInfo dotnetPushInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"nuget push {nupkgPath} --source {gitUrl} --api-key {apiKey}",
WorkingDirectory = projectDir,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (Process dotnetPushProcess = new Process { StartInfo = dotnetPushInfo })
{
dotnetPushProcess.Start();
string pushOutput = dotnetPushProcess.StandardOutput.ReadToEnd();
dotnetPushProcess.WaitForExit();
if (dotnetPushProcess.ExitCode == 0)
{
//MessageBox.Show("上传成功");
}
else
{
//MessageBox.Show($"上传失败: {pushOutput}");
}
}
}
else
{
//MessageBox.Show($"打包失败: {output}");
}
}
}
else
{
//MessageBox.Show("没有选中的项目。");
}
//string message = string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.GetType().FullName);
//string title = "MyCommand1";
Show a message box to prove we were here
//VsShellUtilities.ShowMessageBox(
// this.package,
// message,
// title,
// OLEMSGICON.OLEMSGICON_INFO,
// OLEMSGBUTTON.OLEMSGBUTTON_OK,
// OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
}
catch (Exception ex)
{
}
}