PkgInst
91 строка · 3.1 Кб
1using System.Collections.Concurrent;
2using System.Diagnostics;
3using System.Reflection;
4
5namespace PkgInst.Benchmark;
6
7public class PackageHelper
8{
9private readonly string _basePath;
10private readonly string _appPath;
11
12public PackageHelper(string basePath)
13{
14_basePath = basePath;
15_appPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location) ?? throw new DirectoryNotFoundException();
16}
17
18public IEnumerable<Package> GetPackages()
19{
20var list = new List<Package>();
21
22foreach (var packageId in new DirectoryInfo(_basePath).GetDirectories().Select(x => x.Name))
23{
24var packageInfo = GetPackageInfo(packageId);
25var fileInfo = new FileInfo(Path.Combine(_basePath, packageId, "installer.exe"));
26
27string name = packageInfo.Item1;
28string url = $"/download?id={packageId}&name={name}";
29long size = fileInfo.Length;
30DateTime dateTimeCreated = fileInfo.LastWriteTime;
31string parameters = packageInfo.Item2;
32
33list.Add(new Package(packageId, name, url, size, dateTimeCreated, parameters));
34}
35
36return list;
37}
38
39public IEnumerable<Package> GetPackagesInParallel()
40{
41var list = new ConcurrentBag<Package>();
42
43Parallel.ForEach(new DirectoryInfo(_basePath).GetDirectories().Select(x => x.Name), packageId =>
44{
45var packageInfo = GetPackageInfo(packageId);
46var fileInfo = new FileInfo(Path.Combine(_basePath, packageId, "installer.exe"));
47
48string name = packageInfo.Item1;
49string url = $"/download?id={packageId}&name={name}";
50long size = fileInfo.Length;
51DateTime dateTimeCreated = fileInfo.LastWriteTime;
52string parameters = packageInfo.Item2;
53
54list.Add(new Package(packageId, name, url, size, dateTimeCreated, parameters));
55});
56
57return list;
58}
59
60public (string, string) GetPackageInfo(string packageId)
61{
62string tempPath = ExtractPackage(packageId, Path.Combine("pkg_1", "executable_package.kpd"));
63
64string[] lines = File.ReadAllLines(Path.Combine(tempPath, "executable_package.kpd"));
65string name = lines.FirstOrDefault(x => x.StartsWith("LocalizedName="))!.Split('=')[1];
66string parameters = string.Join('=', lines.FirstOrDefault(x => x.StartsWith("Params="))!.Split('=').Skip(1));
67
68Directory.Delete(tempPath, true);
69
70return (string.IsNullOrWhiteSpace(name) ? "installer.exe" : name, parameters);
71}
72
73public string ExtractPackage(string packageId, string nameToExtract)
74{
75string packagePath = Path.Combine(_basePath, packageId, "installer.exe");
76string guid = Guid.NewGuid().ToString();
77string tempPath = Path.Combine(_appPath, "temp", guid);
78
79var processInfo = new ProcessStartInfo
80{
81FileName = "7za",
82Arguments = $"e \"{packagePath}\" -aoa -o\"{tempPath}\" \"{nameToExtract}\"",
83WindowStyle = ProcessWindowStyle.Hidden
84};
85
86var process = Process.Start(processInfo);
87process?.WaitForExit();
88
89return tempPath;
90}
91}
92