From 5741d1d16f90e2e8af5e11b2953f95edb1a46351 Mon Sep 17 00:00:00 2001 From: Waldek Mastykarz Date: Fri, 11 Sep 2026 08:39:43 +0200 Subject: [PATCH] Use local templates for config new Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../skills/upgrade-devproxy-version/SKILL.md | 2 + .../scripts/upgrade-version.sh | 9 ++- DevProxy/Commands/ConfigCommand.cs | 81 ++----------------- DevProxy/DevProxy.csproj | 3 + DevProxy/config-templates/devproxyrc.json | 10 +++ DevProxy/config-templates/devproxyrc.yaml | 8 ++ 6 files changed, 39 insertions(+), 74 deletions(-) create mode 100644 DevProxy/config-templates/devproxyrc.json create mode 100644 DevProxy/config-templates/devproxyrc.yaml diff --git a/.github/skills/upgrade-devproxy-version/SKILL.md b/.github/skills/upgrade-devproxy-version/SKILL.md index b34da8be..0bb4e781 100644 --- a/.github/skills/upgrade-devproxy-version/SKILL.md +++ b/.github/skills/upgrade-devproxy-version/SKILL.md @@ -56,6 +56,8 @@ Update `schemas/v/` to `schemas/v/` in: - `DevProxy/config/microsoft-graph.json` - all `$schema` URLs - `DevProxy/config/microsoft-graph-rate-limiting.json` - `$schema` URL - `DevProxy/config/spo-csom-types.json` - `$schema` URL +- `DevProxy/config-templates/devproxyrc.json` - `$schema` URL +- `DevProxy/config-templates/devproxyrc.yaml` - `$schema` URL - `DevProxy.Plugins/Mocking/MockResponsePlugin.cs` - hardcoded schema URL **Category C: Installer Files** (format: `X.Y.Z` or `X.Y.Z-beta.N`) diff --git a/.github/skills/upgrade-devproxy-version/scripts/upgrade-version.sh b/.github/skills/upgrade-devproxy-version/scripts/upgrade-version.sh index f199ec4d..bc438bf0 100755 --- a/.github/skills/upgrade-devproxy-version/scripts/upgrade-version.sh +++ b/.github/skills/upgrade-devproxy-version/scripts/upgrade-version.sh @@ -56,13 +56,20 @@ done echo "" echo "Step 3: Updating schema URLs..." # JSON files -for json in DevProxy/devproxyrc.json DevProxy/devproxy-errors.json DevProxy/config/*.json; do +for json in DevProxy/devproxyrc.json DevProxy/devproxy-errors.json DevProxy/config/*.json DevProxy/config-templates/*.json; do if [[ -f "$WORKSPACE_ROOT/$json" ]]; then sed -i '' "s|schemas/v$CURRENT_VERSION/|schemas/v$NEW_VERSION/|g" "$WORKSPACE_ROOT/$json" echo " Updated: $json" fi done +for yaml in DevProxy/config-templates/*.yaml; do + if [[ -f "$WORKSPACE_ROOT/$yaml" ]]; then + sed -i '' "s|schemas/v$CURRENT_VERSION/|schemas/v$NEW_VERSION/|g" "$WORKSPACE_ROOT/$yaml" + echo " Updated: $yaml" + fi +done + # CS files with hardcoded schema URLs MOCK_PLUGIN="$WORKSPACE_ROOT/DevProxy.Plugins/Mocking/MockResponsePlugin.cs" if [[ -f "$MOCK_PLUGIN" ]]; then diff --git a/DevProxy/Commands/ConfigCommand.cs b/DevProxy/Commands/ConfigCommand.cs index 5ead2b1e..5d794f8c 100644 --- a/DevProxy/Commands/ConfigCommand.cs +++ b/DevProxy/Commands/ConfigCommand.cs @@ -36,13 +36,6 @@ sealed class GitHubTreeItem public string Type { get; set; } = string.Empty; } -sealed class VisualStudioCodeSnippet -{ - public string? Prefix { get; set; } - public string[]? Body { get; set; } - public string? Description { get; set; } -} - sealed class ConfigCommand : Command { private enum ConfigFileFormat @@ -54,8 +47,8 @@ private enum ConfigFileFormat private readonly ILogger _logger; private readonly IProxyConfiguration _proxyConfiguration; private readonly HttpClient _httpClient; - private readonly string snippetsBaseUrl = $"https://aka.ms/devproxy/snippets/v{ProxyUtils.NormalizeVersion(ProxyUtils.ProductVersion)}"; - private readonly string configFileSnippetName = "ConfigFile"; + private const string ConfigTemplatesFolder = "config-templates"; + public ConfigCommand( HttpClient httpClient, IProxyConfiguration proxyConfiguration, @@ -447,35 +440,13 @@ private async Task CreateConfigFileAsync(string name, ConfigFileFormat? format, try { var selectedFormat = format ?? GetConfigFileFormatFromFileName(name); - - var snippets = await DownloadSnippetsAsync(selectedFormat); - if (snippets is null) - { - return; - } - - if (!snippets.TryGetValue(configFileSnippetName, out var snippet)) - { - if (outputFormat == OutputFormat.Text) - { - _logger.LogError("Snippet {SnippetName} not found", configFileSnippetName); - } - return; - } - - if (snippet.Body is null || snippet.Body.Length == 0) - { - if (outputFormat == OutputFormat.Text) - { - _logger.LogError("Snippet {SnippetName} is empty", configFileSnippetName); - } - return; - } - - var snippetBody = GetSnippetBody(snippet.Body); + var templateFileName = selectedFormat == ConfigFileFormat.Yaml ? "devproxyrc.yaml" : "devproxyrc.json"; + var templateFilePath = Path.Combine(AppContext.BaseDirectory, ConfigTemplatesFolder, templateFileName); + _logger.LogDebug("Reading config template from {TemplateFilePath}...", templateFilePath); + var template = await File.ReadAllTextAsync(templateFilePath); var targetFileName = GetTargetFileName(name); - await File.WriteAllTextAsync(targetFileName, snippetBody); + await File.WriteAllTextAsync(targetFileName, template); if (outputFormat == OutputFormat.Json) { @@ -494,7 +465,7 @@ private async Task CreateConfigFileAsync(string name, ConfigFileFormat? format, { if (outputFormat == OutputFormat.Text) { - _logger.LogError(ex, "Error downloading config"); + _logger.LogError(ex, "Error creating config"); } } } @@ -508,32 +479,6 @@ private static ConfigFileFormat GetConfigFileFormatFromFileName(string name) return extension is ".yaml" or ".yml" ? ConfigFileFormat.Yaml : ConfigFileFormat.Json; } - private async Task?> DownloadSnippetsAsync(ConfigFileFormat format) - { - var formatSuffix = format == ConfigFileFormat.Yaml ? "yaml" : "json"; - var snippetsFileUrl = $"{snippetsBaseUrl}/{formatSuffix}"; - _logger.LogDebug("Downloading snippets from {SnippetsFileUrl}...", snippetsFileUrl); - var response = await _httpClient.GetAsync(new Uri(snippetsFileUrl)); - if (response.IsSuccessStatusCode) - { - var content = await response.Content.ReadAsStringAsync(); - try - { - return JsonSerializer.Deserialize>(content, ProxyUtils.JsonSerializerOptions); - } - catch (Exception ex) - { - _logger.LogError(ex, "Failed to parse snippets from {Url}", snippetsFileUrl); - return null; - } - } - else - { - _logger.LogError("Failed to download snippets. Status code: {StatusCode}", response.StatusCode); - return null; - } - } - private string GetTargetFileName(string name) { var originalNameWithoutExtension = Path.GetFileNameWithoutExtension(name); @@ -566,16 +511,6 @@ private static string GetTargetFolderPath(string dataFolder, string configId) return newFolder; } - private static string? GetSnippetBody(string[] bodyLines) - { - var body = string.Join("\n", bodyLines); - // unescape $ - body = body.Replace("\\$", "$", StringComparison.OrdinalIgnoreCase); - // remove snippet $n markers - body = Regex.Replace(body, @"\$[0-9]+", ""); - return body; - } - private static string? ResolveConfigFile(string? configFilePath) { if (!string.IsNullOrEmpty(configFilePath)) diff --git a/DevProxy/DevProxy.csproj b/DevProxy/DevProxy.csproj index d418f5b3..2a108da7 100644 --- a/DevProxy/DevProxy.csproj +++ b/DevProxy/DevProxy.csproj @@ -89,6 +89,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/DevProxy/config-templates/devproxyrc.json b/DevProxy/config-templates/devproxyrc.json new file mode 100644 index 00000000..6abf02b4 --- /dev/null +++ b/DevProxy/config-templates/devproxyrc.json @@ -0,0 +1,10 @@ +{ + "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.2.0/rc.schema.json", + "plugins": [ + ], + "urlsToWatch": [ + ], + "logLevel": "information", + "newVersionNotification": "stable", + "showSkipMessages": true +} diff --git a/DevProxy/config-templates/devproxyrc.yaml b/DevProxy/config-templates/devproxyrc.yaml new file mode 100644 index 00000000..01a66c87 --- /dev/null +++ b/DevProxy/config-templates/devproxyrc.yaml @@ -0,0 +1,8 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.2.0/rc.schema.json +plugins: + - +urlsToWatch: + - +logLevel: information +newVersionNotification: stable +showSkipMessages: true