129 lines
4.6 KiB
C#
129 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using LDUninstaller.Core.Models;
|
|
|
|
namespace LDUninstaller.Core.Services
|
|
{
|
|
public class WingetUpgradeItem
|
|
{
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Id { get; set; } = string.Empty;
|
|
public string Version { get; set; } = string.Empty;
|
|
public string AvailableVersion { get; set; } = string.Empty;
|
|
}
|
|
|
|
public static class WingetService
|
|
{
|
|
public static bool IsWingetAvailable()
|
|
{
|
|
try
|
|
{
|
|
var psi = new ProcessStartInfo("winget", "--version")
|
|
{
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
};
|
|
using var proc = Process.Start(psi);
|
|
if (proc == null) return false;
|
|
proc.WaitForExit(3000);
|
|
return proc.ExitCode == 0;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static async Task<List<WingetUpgradeItem>> GetAvailableUpgradesAsync()
|
|
{
|
|
var results = new List<WingetUpgradeItem>();
|
|
if (!IsWingetAvailable()) return results;
|
|
|
|
await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var psi = new ProcessStartInfo("winget", "upgrade --include-unknown")
|
|
{
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
StandardOutputEncoding = System.Text.Encoding.UTF8
|
|
};
|
|
|
|
using var proc = Process.Start(psi);
|
|
if (proc == null) return;
|
|
|
|
string output = proc.StandardOutput.ReadToEnd();
|
|
proc.WaitForExit(15000);
|
|
|
|
// Parse table output: Name | Id | Version | Available | Source
|
|
using var reader = new StringReader(output);
|
|
string? line;
|
|
bool inTable = false;
|
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
{
|
|
if (line.StartsWith("---") || line.Contains("------"))
|
|
{
|
|
inTable = true;
|
|
continue;
|
|
}
|
|
|
|
if (!inTable || string.IsNullOrWhiteSpace(line)) continue;
|
|
|
|
// Match columns separated by 2+ spaces
|
|
var parts = Regex.Split(line.Trim(), @"\s{2,}");
|
|
if (parts.Length >= 4)
|
|
{
|
|
results.Add(new WingetUpgradeItem
|
|
{
|
|
Name = parts[0],
|
|
Id = parts[1],
|
|
Version = parts[2],
|
|
AvailableVersion = parts[3]
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
});
|
|
|
|
return results;
|
|
}
|
|
|
|
public static string GenerateWingetExportScript(IEnumerable<ApplicationUninstallerEntry> apps)
|
|
{
|
|
var sb = new System.Text.StringBuilder();
|
|
sb.AppendLine("@echo off");
|
|
sb.AppendLine("chcp 65001 >nul");
|
|
sb.AppendLine("echo === LD Uninstaller - Toplu Yazilim Kurulum Scripti ===");
|
|
sb.AppendLine("echo Bu script bilgisayarinizda kayitli yazilimlari Winget ile otomatik kurar.");
|
|
sb.AppendLine("echo.");
|
|
|
|
foreach (var app in apps)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(app.DisplayName) || app.SystemComponent || app.IsOrphaned)
|
|
continue;
|
|
|
|
// Escape quotes
|
|
string safeName = app.DisplayName.Replace("\"", "").Trim();
|
|
sb.AppendLine($"echo [{safeName}] kuruluyor...");
|
|
sb.AppendLine($"winget install --name \"{safeName}\" -e --accept-package-agreements --accept-source-agreements");
|
|
sb.AppendLine();
|
|
}
|
|
|
|
sb.AppendLine("echo Tum kurulum komutlari tamamlandi!");
|
|
sb.AppendLine("pause");
|
|
return sb.ToString();
|
|
}
|
|
}
|
|
}
|