1 Star 0 Fork 0

陈源华/NugetPushTool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Form1.cs 9.08 KB
一键复制 编辑 原始数据 按行查看 历史
chenqi2 提交于 2024-05-09 16:31 . init
using DevExpress.DataAccess.Native.Web;
using DevExpress.Map.Native;
using DevExpress.Mvvm.Native;
using DevExpress.XtraEditors;
using NuGet.Common;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Xml;
using System.Xml.Linq;
namespace NugetPush
{
public partial class Form1 : XtraForm
{
public Form1()
{
InitializeComponent();
}
private void edtInitDir_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
{
FolderBrowserDialog browserDialog = new FolderBrowserDialog();
if (browserDialog.ShowDialog() == DialogResult.OK)
{
edtInitDir.Text = browserDialog.SelectedPath;
}
}
private void simpleButton2_Click(object sender, EventArgs e)
{
DirectoryInfo directoryInfo = new DirectoryInfo(edtInitDir.Text);
FileInfo[] fileInfos = directoryInfo.GetFiles(edtFilter.Text + ".nupkg", SearchOption.AllDirectories);
checkFiles.DataSource = fileInfos;
checkFiles.DisplayMember = "Name";
checkFiles.ValueMember = "FullName";
checkFiles.CheckAll();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
List<string> list = new List<string>();
var objects = checkFiles.CheckedItems;
foreach (FileInfo obj in objects)
{
list.Add($"dotnet nuget push {obj.FullName} -k {edtApiKey.Text.Trim()} -s {edtSource.Text.Trim()} --skip-duplicate");
}
ExecCmds(list);
}
private async Task ExecCmds(IEnumerable<string> cmds)
{
Process p = new Process();
//设置要启动的应用程序
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell启动
p.StartInfo.UseShellExecute = false;
// 接受来自调用程序的输入信息
p.StartInfo.RedirectStandardInput = true;
//输出信息
p.StartInfo.RedirectStandardOutput = true;
// 输出错误
p.StartInfo.RedirectStandardError = true;
//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
p.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
foreach (string cmd in cmds)
{
await ExecCmd(p, cmd);
}
}
private async Task ExecCmd(Process p, string cmd)
{
//启动程序
p.Start();
try
{
p.StandardInput.WriteLine($"{cmd}&exit");
while (!p.StandardOutput.EndOfStream)
{
string outdata = await p.StandardOutput.ReadLineAsync();
if (outdata == null)
break;
if (!string.IsNullOrEmpty(outdata))
{
memoEdit1.BeginInvoke(() => memoEdit1.AppendLine(outdata));
}
}
}
catch (Exception ex)
{
memoEdit1.BeginInvoke(() => memoEdit1.AppendLine(ex.Message));
}
//向cmd窗口发送输入信息
p.StandardInput.AutoFlush = true;
//等待程序执行完退出进程
p.WaitForExit();
p.Close();
}
List<PackageData> Datas = new List<PackageData>();
private async void simpleButton3_Click(object sender, EventArgs e)
{
simpleButton3.Enabled = false;
try
{
await QueryPackages(textEdit1.Text.Trim());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
simpleButton3.Enabled = true;
}
private async Task QueryPackages(string id)
{
//SourceRepository repository = Repository.Factory.GetCoreV2(new NuGet.Configuration.PackageSource(edtSource.Text));
//// "https://api.nuget.org/v3/index.json");
//PackageSearchResource resource = await repository.GetResourceAsync<PackageSearchResource>();
//SearchFilter searchFilter = new SearchFilter(includePrerelease: true, SearchFilterType.IsLatestVersion);
//ILogger logger = NullLogger.Instance;
//CancellationToken cancellationToken = CancellationToken.None;
//IEnumerable<IPackageSearchMetadata> results = await resource.SearchAsync(
// id,
// searchFilter,
// skip: 0,
// take: 1000,
// logger,
// cancellationToken);
//Datas.Clear();
//foreach (IPackageSearchMetadata result in results)
//{
// Datas.Add(new PackageData() { Id = result.Identity.Id, Version = result.Identity.Version.ToString(), Published = result.Published.ToString() });
// Console.WriteLine($"Found package {result.Identity.Id} {result.Identity.Version}");
//}
HttpClient httpClient = new HttpClient();
XNamespace m = @"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";
XNamespace d = @"http://schemas.microsoft.com/ado/2007/08/dataservices";
var response = await httpClient.GetAsync($"{edtSource.Text.Trim()}/Search()?$filter=IsAbsoluteLatestVersion&searchTerm='{id}'&$skip=0&$top=20");
if (response != null && response.IsSuccessStatusCode)
{
string str = await response.Content.ReadAsStringAsync();
var document = XDocument.Parse(str);
Datas.Clear();
foreach (XElement element1 in document.Descendants().Where(a => a.Name.LocalName.Equals("entry")))
{
var idEl = from el in element1.Descendants(m + "properties").First().Descendants().Where(a=> a.Name.NamespaceName.Equals(d.NamespaceName) && a.Name.LocalName .Equals("Id"))
select el;
var node = idEl.FirstOrDefault();
if (node == null) continue;
var packResponse = await httpClient.GetAsync($"{edtSource.Text.Trim()}/Packages?$filter=Id eq '{node.Value}'");
if (packResponse != null && packResponse.IsSuccessStatusCode)
{
string str2 = await packResponse.Content.ReadAsStringAsync();
var document2 = XDocument.Parse(str2);
foreach (XElement elementPack in document2.Descendants().Where(a => a.Name.LocalName.Equals("entry")))
{
PackageData data = new PackageData();
foreach (XElement element2 in elementPack.Descendants(m + "properties"))
{
foreach (XElement element3 in element2.Descendants().Where(a => a.Name.NamespaceName.Equals(d.NamespaceName)))
{
switch (element3.Name.LocalName)
{
case "Id": data.Id = element3.Value; break;
case "Version": data.Version = element3.Value; break;
case "Published": data.Published = element3.Value; break;
default: break;
}
}
}
Datas.Add(data);
}
}
}
}
gridControl1.DataSource = Datas;
gridControl1.RefreshDataSource();
}
class PackageData
{
public string Id { get; set; }
public string Version { get; set; }
public string Published { get; set; }
}
private async void simpleButton4_Click(object sender, EventArgs e)
{
var rowIds = gridView1.GetSelectedRows();
if (rowIds.Length < 1)
{
MessageBox.Show("没有选择任何数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (MessageBox.Show("确认删除选择的包吗?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
List<string> list = new List<string>();
foreach (var id in rowIds)
{
var data = gridView1.GetRow(id) as PackageData;
if (data != null)
list.Add($"dotnet nuget delete {data.Id} {data.Version} -k {edtApiKey.Text.Trim()} -s {edtSource.Text.Trim()} --non-interactive");
}
await ExecCmds(list);
}
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chen-yuanhua/nuget-push-tool.git
git@gitee.com:chen-yuanhua/nuget-push-tool.git
chen-yuanhua
nuget-push-tool
NugetPushTool
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385