|
| 1 | +using System.Diagnostics; |
| 2 | +using System.IO.Compression; |
| 3 | +using System.Text.Json; |
| 4 | +using Common.BasicHelper.Utils.Extensions; |
| 5 | +using Spectre.Console; |
| 6 | + |
| 7 | +AnsiConsole.Write(new FigletText("KitX Publisher").Centered().Color(Color.Blue)); |
| 8 | + |
| 9 | +PathHelper.Instance.AssertInSlnDirectory(out _); |
| 10 | + |
| 11 | +var baseDir = PathHelper.Instance.BaseSlnDir; |
| 12 | + |
| 13 | +var publishDir = $"{baseDir}/KitX Publish".GetFullPath(); |
| 14 | + |
| 15 | +if (Directory.Exists(publishDir)) |
| 16 | + foreach (var dir in new DirectoryInfo(publishDir).GetDirectories()) |
| 17 | + Directory.Delete(dir.FullName, true); |
| 18 | + |
| 19 | +const string pro = "Properties/"; |
| 20 | +const string pub = "PublishProfiles/"; |
| 21 | + |
| 22 | +var path = $"{baseDir}/KitX Clients/KitX Dashboard/KitX Dashboard/".GetFullPath(); |
| 23 | +var abPubPath = $"{path}{pro}{pub}".GetFullPath(); |
| 24 | +var files = Directory.GetFiles(abPubPath, "*.pubxml", SearchOption.AllDirectories); |
| 25 | + |
| 26 | +var logsDir = $"{publishDir}/logs".GetFullPath(); |
| 27 | +var log = $"{logsDir}/log-{DateTime.Now:yyyy-MM-dd-HH-mm-ss}.log".GetFullPath(); |
| 28 | +if (!Directory.Exists(logsDir)) |
| 29 | + Directory.CreateDirectory(logsDir); |
| 30 | + |
| 31 | +var packIgnore = $"{publishDir}/.packignore".GetFullPath(); |
| 32 | +var packIgnoreExists = File.Exists(packIgnore); |
| 33 | + |
| 34 | +var finishedThreads = 0; |
| 35 | +var executingThreadIndex = 0; |
| 36 | + |
| 37 | +const string prompt = "[white]>>>[/]"; |
| 38 | + |
| 39 | +AnsiConsole |
| 40 | + .Progress() |
| 41 | + .Columns( |
| 42 | + new ProgressColumn[] |
| 43 | + { |
| 44 | + new TaskDescriptionColumn(), |
| 45 | + new ProgressBarColumn(), |
| 46 | + new PercentageColumn(), |
| 47 | + new SpinnerColumn(), |
| 48 | + } |
| 49 | + ) |
| 50 | + .Start(ctx => |
| 51 | + { |
| 52 | + var genTask = ctx.AddTask("[green]Publishing[/]"); |
| 53 | + var packTask = ctx.AddTask("[blue]Packing[/]"); |
| 54 | + |
| 55 | + { |
| 56 | + AnsiConsole.Write(new Rule($"[blue]Begin Generating[/]")); |
| 57 | + |
| 58 | + AnsiConsole.MarkupLine($"{prompt} Found {files.Length} profiles"); |
| 59 | + AnsiConsole.Markup( |
| 60 | + $"{prompt} Generating logs at: \"{Path.GetRelativePath(baseDir, log)}\"" |
| 61 | + ); |
| 62 | + AnsiConsole.WriteLine(); |
| 63 | + |
| 64 | + packTask.IsIndeterminate = true; |
| 65 | + |
| 66 | + foreach (var item in files) |
| 67 | + { |
| 68 | + var index = executingThreadIndex++; |
| 69 | + var filename = Path.GetFileName(item); |
| 70 | + const string cmd = "dotnet"; |
| 71 | + var arg = new StringBuilder() |
| 72 | + .Append("publish ") |
| 73 | + .Append($"\"{(path + "/KitX.Dashboard.csproj").GetFullPath()}\" ") |
| 74 | + .Append($"\"/p:PublishProfile={item}\"") |
| 75 | + .ToString(); |
| 76 | + |
| 77 | + AnsiConsole.MarkupLine($"{prompt} 📄 [white]{filename}[/]: [gray]{cmd} {arg}[/]"); |
| 78 | + |
| 79 | + var process = new Process(); |
| 80 | + var psi = new ProcessStartInfo() |
| 81 | + { |
| 82 | + FileName = cmd, |
| 83 | + Arguments = arg, |
| 84 | + UseShellExecute = false, |
| 85 | + CreateNoWindow = true, |
| 86 | + RedirectStandardOutput = true, |
| 87 | + RedirectStandardError = true, |
| 88 | + }; |
| 89 | + process.StartInfo = psi; |
| 90 | + process.Start(); |
| 91 | + |
| 92 | + while (!process.StandardOutput.EndOfStream) |
| 93 | + { |
| 94 | + var line = process.StandardOutput.ReadLine(); |
| 95 | + |
| 96 | + File.AppendAllText(log, Environment.NewLine + line); |
| 97 | + } |
| 98 | + |
| 99 | + process.WaitForExit(); |
| 100 | + |
| 101 | + ++finishedThreads; |
| 102 | + |
| 103 | + genTask.Increment((1.0 / files.Length) * 100); |
| 104 | + |
| 105 | + AnsiConsole.MarkupLine( |
| 106 | + $"{prompt} Finished task_{index}, still {files.Length - finishedThreads} tasks waiting" |
| 107 | + ); |
| 108 | + |
| 109 | + AnsiConsole.Write(new Rule($"[red]Finished task-{index}[/]")); |
| 110 | + } |
| 111 | + |
| 112 | + AnsiConsole.MarkupLine($"{prompt} All tasks done."); |
| 113 | + |
| 114 | + genTask.StopTask(); |
| 115 | + } |
| 116 | + |
| 117 | + { |
| 118 | + AnsiConsole.MarkupLine($"{prompt} Begin packing."); |
| 119 | + |
| 120 | + if (packIgnoreExists) |
| 121 | + AnsiConsole.MarkupLine( |
| 122 | + $"{prompt} `.packignore` not exists, all folder will be packed." |
| 123 | + ); |
| 124 | + |
| 125 | + packTask.IsIndeterminate = false; |
| 126 | + |
| 127 | + var ignoredDirectories = packIgnoreExists |
| 128 | + ? JsonSerializer.Deserialize<List<string>>(File.ReadAllText(packIgnore)) |
| 129 | + : new List<string>(); |
| 130 | + |
| 131 | + var folders = new DirectoryInfo(publishDir).GetDirectories().ToList(); |
| 132 | + |
| 133 | + folders.RemoveAll(f => ignoredDirectories.Contains(f.Name)); |
| 134 | + |
| 135 | + foreach (var folder in folders) |
| 136 | + { |
| 137 | + var name = folder.Name; |
| 138 | + var zipFileName = $"{publishDir}/{name}.zip"; |
| 139 | + |
| 140 | + AnsiConsole.MarkupLine($"{prompt} Packing 📂 [yellow]{name}[/]"); |
| 141 | + |
| 142 | + if (File.Exists(zipFileName)) |
| 143 | + File.Delete(zipFileName); |
| 144 | + |
| 145 | + ZipFile.CreateFromDirectory( |
| 146 | + folder.FullName, |
| 147 | + zipFileName, |
| 148 | + CompressionLevel.SmallestSize, |
| 149 | + true |
| 150 | + ); |
| 151 | + |
| 152 | + AnsiConsole.MarkupLine( |
| 153 | + $" Packed to {Path.GetRelativePath(baseDir, zipFileName)}" |
| 154 | + ); |
| 155 | + |
| 156 | + packTask.Increment((1.0 / folders.Count) * 100); |
| 157 | + } |
| 158 | + |
| 159 | + AnsiConsole.MarkupLine($"{prompt} Packing done."); |
| 160 | + |
| 161 | + packTask.StopTask(); |
| 162 | + } |
| 163 | + }); |
0 commit comments