|
| 1 | +// Licensed to Elasticsearch B.V under one or more agreements. |
| 2 | +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
| 3 | +// See the LICENSE file in the project root for more information |
| 4 | + |
| 5 | +using System.Collections.Concurrent; |
| 6 | +using System.Diagnostics; |
| 7 | +using ConsoleAppFramework; |
| 8 | +using Documentation.Assembler; |
| 9 | +using Documentation.Assembler.Cli; |
| 10 | +using Elastic.Markdown.IO; |
| 11 | +using ProcNet; |
| 12 | +using ProcNet.Std; |
| 13 | + |
| 14 | +var configFile = Path.Combine(Paths.Root.FullName, "src/docs-assembler/conf.yml"); |
| 15 | +var config = AssemblyConfiguration.Deserialize(File.ReadAllText(configFile)); |
| 16 | + |
| 17 | +var app = ConsoleApp.Create(); |
| 18 | +app.UseFilter<StopwatchFilter>(); |
| 19 | +app.UseFilter<CatchExceptionFilter>(); |
| 20 | + |
| 21 | +// would love to use libgit2 so there is no git dependency but |
| 22 | +// libgit2 is magnitudes slower to clone repositories https://github.com/libgit2/libgit2/issues/4674 |
| 23 | +app.Add("clone-all", async Task (CancellationToken ctx) => |
| 24 | +{ |
| 25 | + Console.WriteLine(config.Repositories.Count); |
| 26 | + var dict = new ConcurrentDictionary<string, Stopwatch>(); |
| 27 | + await Parallel.ForEachAsync(config.Repositories, new ParallelOptions |
| 28 | + { |
| 29 | + CancellationToken = ctx, |
| 30 | + MaxDegreeOfParallelism = Environment.ProcessorCount / 4 |
| 31 | + }, async (kv, c) => |
| 32 | + { |
| 33 | + await Task.Run(() => |
| 34 | + { |
| 35 | + var name = kv.Key; |
| 36 | + var repository = kv.Value; |
| 37 | + var checkoutFolder = Path.Combine(Paths.Root.FullName, $".artifacts/assembly/{name}"); |
| 38 | + |
| 39 | + var sw = Stopwatch.StartNew(); |
| 40 | + dict.AddOrUpdate(name, sw, (_, _) => sw); |
| 41 | + Console.WriteLine($"Checkout: {name}\t{repository}\t{checkoutFolder}"); |
| 42 | + var branch = repository.Branch ?? "main"; |
| 43 | + var args = new StartArguments( |
| 44 | + "git", "clone", repository.Origin, checkoutFolder, "--depth", "1" |
| 45 | + , "--single-branch", "--branch", branch |
| 46 | + ); |
| 47 | + Proc.StartRedirected(args, new ConsoleLineHandler(name)); |
| 48 | + sw.Stop(); |
| 49 | + }, c); |
| 50 | + }).ConfigureAwait(false); |
| 51 | + |
| 52 | + foreach (var kv in dict.OrderBy(kv => kv.Value.Elapsed)) |
| 53 | + Console.WriteLine($"-> {kv.Key}\ttook: {kv.Value.Elapsed}"); |
| 54 | +}); |
| 55 | +app.Add("list", async Task (CancellationToken ctx) => |
| 56 | +{ |
| 57 | + |
| 58 | + var assemblyPath = Path.Combine(Paths.Root.FullName, $".artifacts/assembly"); |
| 59 | + var dir = new DirectoryInfo(assemblyPath); |
| 60 | + var dictionary = new Dictionary<string, string>(); |
| 61 | + foreach (var d in dir.GetDirectories()) |
| 62 | + { |
| 63 | + var checkoutFolder = Path.Combine(assemblyPath, d.Name); |
| 64 | + |
| 65 | + var capture = Proc.Start( |
| 66 | + new StartArguments("git", "rev-parse", "--abbrev-ref", "HEAD") |
| 67 | + { |
| 68 | + WorkingDirectory = checkoutFolder |
| 69 | + } |
| 70 | + ); |
| 71 | + dictionary.Add(d.Name, capture.ConsoleOut.FirstOrDefault()?.Line ?? "unknown"); |
| 72 | + } |
| 73 | + foreach (var kv in dictionary.OrderBy(kv => kv.Value)) |
| 74 | + Console.WriteLine($"-> {kv.Key}\tbranch: {kv.Value}"); |
| 75 | + |
| 76 | + await Task.CompletedTask; |
| 77 | +}); |
| 78 | + |
| 79 | +await app.RunAsync(args); |
| 80 | + |
| 81 | +namespace Documentation.Assembler |
| 82 | +{ |
| 83 | + public class ConsoleLineHandler(string prefix) : IConsoleLineHandler |
| 84 | + { |
| 85 | + public void Handle(LineOut lineOut) => lineOut.CharsOrString( |
| 86 | + r => Console.Write(prefix + ": " + r), |
| 87 | + l => Console.WriteLine(prefix + ": " + l)); |
| 88 | + |
| 89 | + public void Handle(Exception e) { } |
| 90 | + } |
| 91 | +} |
0 commit comments