|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Net; |
| 6 | +using CsQuery; |
| 7 | +using ShellProgressBar; |
| 8 | + |
| 9 | +namespace CodeGeneration.LowLevelClient |
| 10 | +{ |
| 11 | + public class RestSpecDownloader |
| 12 | + { |
| 13 | + public static RestSpecDownloader Download(string branch) => new RestSpecDownloader(branch); |
| 14 | + |
| 15 | + private static readonly Dictionary<string, string> OnlineSpecifications = new Dictionary<string, string> |
| 16 | + { |
| 17 | + { "Core", "https://github.com/elastic/elasticsearch/tree/{version}/rest-api-spec/src/main/resources/rest-api-spec/api" }, |
| 18 | + { "DeleteByQuery", "https://github.com/elastic/elasticsearch/tree/{version}/plugins/delete-by-query/src/test/resources/rest-api-spec/api" }, |
| 19 | + }; |
| 20 | + private class Specification |
| 21 | + { |
| 22 | + public string FolderOnDisk { get; set; } |
| 23 | + public string GithubListingUrl { get; set; } |
| 24 | + public string Branch { get; set; } |
| 25 | + public string GithubDownloadUrl(string file) => this.GithubListingUrl.Replace("github.com", "raw.githubusercontent.com").Replace("tree/", "") + "/" + file; |
| 26 | + } |
| 27 | + |
| 28 | + private static readonly ProgressBarOptions MainProgressBarOptions = new ProgressBarOptions { BackgroundColor = ConsoleColor.DarkGray }; |
| 29 | + private static readonly ProgressBarOptions SubProgressBarOptions = new ProgressBarOptions |
| 30 | + { |
| 31 | + ForeGroundColor = ConsoleColor.Cyan, |
| 32 | + ForeGroundColorDone = ConsoleColor.DarkGreen, |
| 33 | + ProgressCharacter = '─', |
| 34 | + BackgroundColor = ConsoleColor.DarkGray, |
| 35 | + }; |
| 36 | + |
| 37 | + private RestSpecDownloader(string branch) |
| 38 | + { |
| 39 | + var specifications = |
| 40 | + (from kv in OnlineSpecifications |
| 41 | + let url = kv.Value.Replace("{version}", branch) |
| 42 | + select new Specification { FolderOnDisk = kv.Key, Branch = branch, GithubListingUrl = url }).ToList(); |
| 43 | + |
| 44 | + |
| 45 | + using (var pbar = new ProgressBar(specifications.Count, "Downloading specifications", MainProgressBarOptions)) |
| 46 | + { |
| 47 | + foreach (var spec in specifications) |
| 48 | + { |
| 49 | + pbar.UpdateMessage($"Downloading rest-api-spec to {spec.FolderOnDisk} for branch {branch}"); |
| 50 | + DownloadJsonDefinitions(spec, pbar); |
| 51 | + pbar.Tick($"Downloaded rest-api-spec to {spec.FolderOnDisk} for branch {branch}"); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private void DownloadJsonDefinitions(Specification spec, IProgressBar pbar) |
| 57 | + { |
| 58 | + using (var client = new WebClient()) |
| 59 | + { |
| 60 | + var html = client.DownloadString(spec.GithubListingUrl); |
| 61 | + FindJsonFilesOnListing(spec, html, pbar); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void FindJsonFilesOnListing(Specification spec, string html, IProgressBar pbar) |
| 66 | + { |
| 67 | + if (!Directory.Exists(CodeConfiguration.RestSpecificationFolder)) Directory.CreateDirectory(CodeConfiguration.RestSpecificationFolder); |
| 68 | + |
| 69 | + var dom = CQ.Create(html); |
| 70 | + |
| 71 | + WriteToEndpointsFolder(spec.FolderOnDisk, "root.html", html); |
| 72 | + |
| 73 | + var endpoints = dom[".js-navigation-open"] |
| 74 | + .Select(s => s.InnerText) |
| 75 | + .Where(s => !string.IsNullOrEmpty(s) && s.EndsWith(".json")) |
| 76 | + .ToList(); |
| 77 | + |
| 78 | + using (var subBar = pbar.Spawn(endpoints.Count, "fetching individual json files", SubProgressBarOptions)) |
| 79 | + endpoints.ForEach(s => WriteEndpointFile(spec, s, subBar)); |
| 80 | + } |
| 81 | + |
| 82 | + private void WriteEndpointFile(Specification spec, string s, IProgressBar pbar) |
| 83 | + { |
| 84 | + var rawFile = spec.GithubDownloadUrl(s); |
| 85 | + using (var client = new WebClient()) |
| 86 | + { |
| 87 | + var fileName = rawFile.Split('/').Last(); |
| 88 | + var json = client.DownloadString(rawFile); |
| 89 | + WriteToEndpointsFolder(spec.FolderOnDisk, fileName, json); |
| 90 | + pbar.Tick($"Downloading {fileName}"); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void WriteToEndpointsFolder(string folder, string filename, string contents) |
| 95 | + { |
| 96 | + var f = Path.Combine(CodeConfiguration.RestSpecificationFolder, folder); |
| 97 | + if (!Directory.Exists(f)) Directory.CreateDirectory(f); |
| 98 | + File.WriteAllText(f + "\\" + filename, contents); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments