Forge
csharpc88715b7
1using System.Text.RegularExpressions;
2
3namespace AIGuiders.DotnetTools.TomlCheck;
4
5internal static partial class SchemaDirective
6{
7 // Taplo-style: #:schema relative/or/absolute/path.json
8 [GeneratedRegex(@"^\s*#\s*:\s*schema\s+(\S+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
9 private static partial Regex SchemaLineRegex();
10
11 /// <summary>Reads the first 32 lines for a #:schema directive.</summary>
12 public static string? TryReadSchemaPath(string tomlPath)
13 {
14 using var reader = File.OpenText(tomlPath);
15 for (var i = 0; i < 32 && !reader.EndOfStream; i++)
16 {
17 var line = reader.ReadLine();
18 if (line is null)
19 break;
20
21 var match = SchemaLineRegex().Match(line);
22 if (match.Success)
23 return match.Groups[1].Value.Trim().Trim('"', '\'');
24 }
25
26 return null;
27 }
28
29 public static string ResolveSchemaPath(string tomlPath, string schemaRef)
30 {
31 if (Path.IsPathRooted(schemaRef))
32 return Path.GetFullPath(schemaRef);
33
34 var tomlDir = Path.GetDirectoryName(Path.GetFullPath(tomlPath))
35 ?? Environment.CurrentDirectory;
36 return Path.GetFullPath(Path.Combine(tomlDir, schemaRef));
37 }
38}
39
View only · write via MCP/CIDE