|
1 | 1 | using System;
|
2 | 2 | using System.IO;
|
3 | 3 | using System.Linq;
|
| 4 | +using System.Net.Http; |
4 | 5 | using System.Text;
|
| 6 | +using System.Text.RegularExpressions; |
| 7 | +using System.Threading.Tasks; |
5 | 8 | using Build;
|
6 | 9 | using Cake.Common;
|
7 | 10 | using Cake.Common.Build;
|
@@ -570,3 +573,82 @@ public override void Run(BuildContext context)
|
570 | 573 | context.GenerateRedirects();
|
571 | 574 | }
|
572 | 575 | }
|
| 576 | + |
| 577 | +[TaskName("UpdateStats")] |
| 578 | +public class UpdateStatsTask : FrostingTask<BuildContext> |
| 579 | +{ |
| 580 | + public class Updater |
| 581 | + { |
| 582 | + public string Prefix { get; } |
| 583 | + public Regex Regex { get; } |
| 584 | + public int Value { get; } |
| 585 | + |
| 586 | + public Updater(string prefix, string regex, int value) |
| 587 | + { |
| 588 | + Prefix = prefix; |
| 589 | + Regex = new Regex(regex); |
| 590 | + Value = value; |
| 591 | + } |
| 592 | + |
| 593 | + public string Apply(string line) |
| 594 | + { |
| 595 | + if (!line.StartsWith(Prefix)) |
| 596 | + return line; |
| 597 | + |
| 598 | + var match = Regex.Match(line); |
| 599 | + if (!match.Success) |
| 600 | + return line; |
| 601 | + |
| 602 | + // Groups[1] refers to the first group (\d+) |
| 603 | + var numberString = match.Groups[1].Value; |
| 604 | + var number = int.Parse(numberString); |
| 605 | + return line.Replace(number.ToString(), Value.ToString()); |
| 606 | + } |
| 607 | + } |
| 608 | + |
| 609 | + private static async Task<int> GetDependentProjectsNumber() |
| 610 | + { |
| 611 | + using var httpClient = new HttpClient(); |
| 612 | + const string url = "https://github.com/dotnet/BenchmarkDotNet/network/dependents"; |
| 613 | + var response = await httpClient.GetAsync(new Uri(url)); |
| 614 | + var dependentsPage = await response.Content.ReadAsStringAsync(); |
| 615 | + var match = new Regex(@"([0-9\,]+)[\n\r\s]+Repositories").Match(dependentsPage); |
| 616 | + var number = int.Parse(match.Groups[1].Value.Replace(",", "")); |
| 617 | + number = number / 100 * 100; |
| 618 | + return number; |
| 619 | + } |
| 620 | + |
| 621 | + public override void Run(BuildContext context) |
| 622 | + { |
| 623 | + var dependentProjectsNumber = GetDependentProjectsNumber().Result; |
| 624 | + var updaters = new Updater[] |
| 625 | + { |
| 626 | + new( |
| 627 | + "The library is adopted by", |
| 628 | + @"\[(\d+)\+ GitHub projects\]", |
| 629 | + dependentProjectsNumber |
| 630 | + ), |
| 631 | + new( |
| 632 | + "BenchmarkDotNet is already adopted by more than ", |
| 633 | + @"\[(\d+)\+\]", |
| 634 | + dependentProjectsNumber |
| 635 | + ), |
| 636 | + }; |
| 637 | + var files = new[] |
| 638 | + { |
| 639 | + context.RootDirectory.CombineWithFilePath("README.md"), |
| 640 | + context.DocsDirectory.CombineWithFilePath("index.md") |
| 641 | + }; |
| 642 | + foreach (var file in files) |
| 643 | + { |
| 644 | + var lines = context.FileReadLines(file); |
| 645 | + for (var i = 0; i < lines.Length; i++) |
| 646 | + { |
| 647 | + foreach (var updater in updaters) |
| 648 | + lines[i] = updater.Apply(lines[i]); |
| 649 | + } |
| 650 | + |
| 651 | + context.FileWriteLines(file, lines); |
| 652 | + } |
| 653 | + } |
| 654 | +} |
0 commit comments