VS拓展开发

前言

部署了一个私域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)
    {

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值