diff --git a/.cheese/publishers/general.cs b/.cheese/publishers/general.cs new file mode 100644 index 0000000..2da864b --- /dev/null +++ b/.cheese/publishers/general.cs @@ -0,0 +1,185 @@ +using System.Diagnostics; +using System.IO.Compression; +using Cheese.Options; +using Cheese.Utils.Cheese; +using Common.BasicHelper.Utils.Extensions; + +namespace Cheese.Utils.Publisher; + +public class Publisher +{ + private static Publisher? _instance; + + public static Publisher Instance => _instance ??= new(); + + internal static List AvailableColors = + [ + 1, + 2, + 3, + 5, + 9, + 10, + 11, + 13 + ]; + + public void Execute(PublishOptions options) + { + Console.WriteLine("Running Cheese Publisher"); + + if (PathHelper.Instance.BaseSlnDir is null) + { + Console.WriteLine("! You're not in KitX repo."); + return; + } + + var baseDir = PathHelper.Instance.BaseSlnDir; + + var publishDir = $"{baseDir}/KitX Publish".GetFullPath(); + + if (Directory.Exists(publishDir) && !options.SkipGenerating) + foreach (var dir in new DirectoryInfo(publishDir).GetDirectories()) + Directory.Delete(dir.FullName, true); + + var path = $"{baseDir}/KitX Clients/KitX Dashboard/KitX Dashboard/".GetFullPath(); + const string pro = "Properties/"; + const string pub = "PublishProfiles/"; + var abPubPath = $"{path}{pro}{pub}".GetFullPath(); + var files = Directory.GetFiles( + abPubPath, + "*.pubxml", + SearchOption.AllDirectories + ); + + var finishedThreads = 0; + var executingThreadIndex = 0; + + var updateFinishedThreadsLock = new object(); + var singleThreadUpdateLock = new object(); + + var random = new Random(); + + var threadOutputColors = new Dictionary(); + var usedColorsCount = 0; + var defaultColor = Console.ForegroundColor; + + var tasks = new List(); + + foreach (var item in files) + { + var index = executingThreadIndex++; + var color = GetRandomColor(); + threadOutputColors.Add(index, color); + var filename = Path.GetFileName(item); + + tasks.Add(() => + { + const string cmd = "dotnet"; + var arg = $"publish \"{(path + "/KitX.Dashboard.csproj").GetFullPath()}\" \"/p:PublishProfile={item}\""; + lock (singleThreadUpdateLock) + { + Print( + $""" + >>> On task_{index}: + Task file: {filename} + Executing: {cmd} {arg} + Output: + + """ + ); + } + var process = new Process(); + var psi = new ProcessStartInfo() + { + FileName = cmd, + Arguments = arg, + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true + }; + process.StartInfo = psi; + process.Start(); + + while (!process.StandardOutput.EndOfStream) + { + var line = process.StandardOutput.ReadLine(); + Console.WriteLine($" {line}"); + } + + process.WaitForExit(); + + lock (updateFinishedThreadsLock) + { + ++finishedThreads; + Print($">>> Finished task_{index}, still {files.Length - finishedThreads} tasks running."); + } + }); + + Print($">>> New task: task_{index}\t-> {filename}"); + + continue; + + void Print(string msg) + { + Console.ForegroundColor = threadOutputColors[index]; + Console.WriteLine(msg); + Console.ForegroundColor = defaultColor; + } + } + + if (!options.SkipGenerating) + foreach (var task in tasks) + task.Invoke(); + + if (!options.SkipGenerating) + while (finishedThreads != files.Length) + { + } + + Console.WriteLine($">>> All tasks done."); + + if (options.SkipPacking) return; + + Console.WriteLine(">>> Begin packing."); + + var folders = new DirectoryInfo(publishDir).GetDirectories(); + + foreach (var folder in folders) + { + var name = folder.Name; + var zipFileName = $"{publishDir}/{name}.zip"; + + Console.WriteLine($">>> Packing {name}"); + + if (File.Exists(zipFileName)) + File.Delete(zipFileName); + + ZipFile.CreateFromDirectory( + folder.FullName, + zipFileName, + CompressionLevel.SmallestSize, + true + ); + } + + Console.WriteLine(">>> Packing done."); + + return; + + ConsoleColor GetRandomColor() + { + var cc = AvailableColors[GetRandomIndex(AvailableColors.Count)]; + if (usedColorsCount < AvailableColors.Count) + { + while (threadOutputColors.Values.ToList().Contains((ConsoleColor)cc)) + cc = AvailableColors[GetRandomIndex(AvailableColors.Count)]; + } + ++usedColorsCount; + return (ConsoleColor)cc; + } + + int GetRandomIndex(int max) => random.Next(0, max); + } +} diff --git a/KitX Clients/KitX Dashboard b/KitX Clients/KitX Dashboard index 48bf463..8f3ce1e 160000 --- a/KitX Clients/KitX Dashboard +++ b/KitX Clients/KitX Dashboard @@ -1 +1 @@ -Subproject commit 48bf463db28e702efc131678a4e2c8cd1725323d +Subproject commit 8f3ce1ee2f11f756179717bd4b3a456f13f66788 diff --git a/KitX Clients/KitX Installer b/KitX Clients/KitX Installer index 5b06184..a9de442 160000 --- a/KitX Clients/KitX Installer +++ b/KitX Clients/KitX Installer @@ -1 +1 @@ -Subproject commit 5b06184ea538a12b129d37b9eeec7bbb418d263c +Subproject commit a9de44296ca6fe528c440aae822e4e2f74bcd877 diff --git a/KitX Clients/KitX Mobile b/KitX Clients/KitX Mobile index ad26d7b..181bef4 160000 --- a/KitX Clients/KitX Mobile +++ b/KitX Clients/KitX Mobile @@ -1 +1 @@ -Subproject commit ad26d7bfd07463e4ef09c0cf3764c68dc01f9bae +Subproject commit 181bef4e754499a67e2280c97edba3b7f5f50d1f diff --git a/KitX Clients/KitX Website b/KitX Clients/KitX Website index 4b62aed..33f5440 160000 --- a/KitX Clients/KitX Website +++ b/KitX Clients/KitX Website @@ -1 +1 @@ -Subproject commit 4b62aed1aa9c9899ce7bc2320949e5f7aa706069 +Subproject commit 33f5440c6c9e657b4c68408d006b56d358d3aae1 diff --git a/KitX SDK b/KitX SDK index 7e9c2fd..67feebe 160000 --- a/KitX SDK +++ b/KitX SDK @@ -1 +1 @@ -Subproject commit 7e9c2fddaf9d924f91b38465e9d3ac5971a8eeba +Subproject commit 67feebed01e8fa11bd98bc4face88a88c6da7f5b diff --git a/KitX Standard b/KitX Standard index 22d8067..e6a7860 160000 --- a/KitX Standard +++ b/KitX Standard @@ -1 +1 @@ -Subproject commit 22d80670d28fbef17a3319d21e01ebb0935d0580 +Subproject commit e6a78609b07733e42c932f081d2b01ae6df5571e