microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
packages/cadl-vs/src/VariableResolver.cs
24lines · modecode
| 1 | using System.Collections.Generic; |
| 2 | using System.Text.RegularExpressions; |
| 3 | |
| 4 | namespace Microsoft.Cadl.VisualStudio |
| 5 | { |
| 6 | internal static class VariableResolver |
| 7 | { |
| 8 | private const string VARIABLE_REGEXP = @"\$\{(.*?)\}"; |
| 9 | |
| 10 | public static string ResolveVariables(string value, IDictionary<string, string> variables) |
| 11 | { |
| 12 | return Regex.Replace(value, VARIABLE_REGEXP, (match) => |
| 13 | { |
| 14 | var group = match.Groups[1]; |
| 15 | if (group != null && variables.TryGetValue(group.Value, out var variable)) |
| 16 | { |
| 17 | return variable; |
| 18 | } |
| 19 | return match.Value; |
| 20 | }); |
| 21 | } |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | |