|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using DevProxy.Abstractions; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.RegularExpressions; |
| 8 | + |
| 9 | +namespace DevProxy.CommandHandlers; |
| 10 | + |
| 11 | +public class VisualStudioCodeSnippet |
| 12 | +{ |
| 13 | + public string? Prefix { get; set; } |
| 14 | + public string[]? Body { get; set; } |
| 15 | + public string? Description { get; set; } |
| 16 | +} |
| 17 | + |
| 18 | +public static class ConfigNewCommandHandler |
| 19 | +{ |
| 20 | + private static readonly string snippetsFileUrl = "https://aka.ms/devproxy/snippets"; |
| 21 | + private static readonly string configFileSnippetName = "ConfigFile"; |
| 22 | + |
| 23 | + public static async Task CreateConfigFileAsync(string name, ILogger logger) |
| 24 | + { |
| 25 | + try |
| 26 | + { |
| 27 | + var snippets = await DownloadSnippetsAsync(logger); |
| 28 | + if (snippets is null) |
| 29 | + { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + if (!snippets.TryGetValue(configFileSnippetName, out var snippet)) |
| 34 | + { |
| 35 | + logger.LogError("Snippet {snippetName} not found", configFileSnippetName); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if (snippet.Body is null || snippet.Body.Length == 0) |
| 40 | + { |
| 41 | + logger.LogError("Snippet {snippetName} is empty", configFileSnippetName); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + var snippetBody = GetSnippetBody(snippet.Body); |
| 46 | + var targetFileName = GetTargetFileName(name, logger); |
| 47 | + File.WriteAllText(targetFileName, snippetBody); |
| 48 | + logger.LogInformation("Config file created at {targetFileName}", targetFileName); |
| 49 | + } |
| 50 | + catch (Exception ex) |
| 51 | + { |
| 52 | + logger.LogError(ex, "Error downloading config"); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static string? GetSnippetBody(string[] bodyLines) |
| 57 | + { |
| 58 | + var body = string.Join("\n", bodyLines); |
| 59 | + // unescape $ |
| 60 | + body = body.Replace("\\$", "$"); |
| 61 | + // remove snippet $n markers |
| 62 | + body = Regex.Replace(body, @"\$[0-9]+", ""); |
| 63 | + return body; |
| 64 | + } |
| 65 | + |
| 66 | + private static async Task<Dictionary<string, VisualStudioCodeSnippet>?> DownloadSnippetsAsync(ILogger logger) |
| 67 | + { |
| 68 | + logger.LogDebug("Downloading snippets from {snippetsFileUrl}...", snippetsFileUrl); |
| 69 | + using var client = new HttpClient(); |
| 70 | + var response = await client.GetAsync(snippetsFileUrl); |
| 71 | + if (response.IsSuccessStatusCode) |
| 72 | + { |
| 73 | + var content = await response.Content.ReadAsStringAsync(); |
| 74 | + return JsonSerializer.Deserialize<Dictionary<string, VisualStudioCodeSnippet>>(content, ProxyUtils.JsonSerializerOptions); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + logger.LogError("Failed to download snippets. Status code: {statusCode}", response.StatusCode); |
| 79 | + return null; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static string GetTargetFileName(string name, ILogger logger) |
| 84 | + { |
| 85 | + var originalNameWithoutExtension = Path.GetFileNameWithoutExtension(name); |
| 86 | + var originalExtension = Path.GetExtension(name); |
| 87 | + var counter = 1; |
| 88 | + |
| 89 | + while (true) |
| 90 | + { |
| 91 | + if (!File.Exists(name)) |
| 92 | + { |
| 93 | + return name; |
| 94 | + } |
| 95 | + |
| 96 | + var newName = $"{originalNameWithoutExtension}-{++counter}{originalExtension}"; |
| 97 | + logger.LogDebug("File {name} already exists. Trying {newName}...", name, newName); |
| 98 | + name = newName; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments