forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectivesParser.cs
More file actions
75 lines (67 loc) · 3.02 KB
/
DirectivesParser.cs
File metadata and controls
75 lines (67 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace ServiceStack.Razor.Compilation
{
internal static class DirectivesParser
{
private const string GlobalDirectivesFileName = "razorgenerator.directives";
public static Dictionary<string, string> ParseDirectives(string baseDirectory, string fullPath)
{
var directives = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string directivesPath;
if (TryFindGlobalDirectivesFile(baseDirectory, fullPath, out directivesPath))
{
ParseGlobalDirectives(directives, directivesPath);
}
ParseFileDirectives(directives, fullPath);
return directives;
}
/// <summary>
/// Attempts to locate the nearest global directive file by
/// </summary>
private static bool TryFindGlobalDirectivesFile(string baseDirectory, string fullPath, out string path)
{
baseDirectory = baseDirectory.TrimEnd(Path.DirectorySeparatorChar);
var directivesDirectory = Path.GetDirectoryName(fullPath).TrimEnd(Path.DirectorySeparatorChar);
while (directivesDirectory != null && directivesDirectory.Length >= baseDirectory.Length)
{
path = Path.Combine(directivesDirectory, GlobalDirectivesFileName);
if (File.Exists(path))
{
return true;
}
directivesDirectory = Path.GetDirectoryName(directivesDirectory).TrimEnd(Path.DirectorySeparatorChar);
}
path = null;
return false;
}
private static void ParseGlobalDirectives(Dictionary<string, string> directives, string directivesPath)
{
var fileContent = File.ReadAllText(directivesPath);
ParseKeyValueDirectives(directives, fileContent);
}
private static void ParseFileDirectives(Dictionary<string, string> directives, string filePath)
{
var inputFileContent = File.ReadAllText(filePath);
int index = inputFileContent.IndexOf("*@", StringComparison.OrdinalIgnoreCase);
if (inputFileContent.TrimStart().StartsWith("@*") && index != -1)
{
string directivesLine = inputFileContent.Substring(0, index).TrimStart('*', '@');
ParseKeyValueDirectives(directives, directivesLine);
}
}
private static void ParseKeyValueDirectives(Dictionary<string, string> directives, string directivesLine)
{
// TODO: Make this better.
var regex = new Regex(@"\b(?<Key>\w+)\s*:\s*(?<Value>[~\\\/\w\.]+)\b", RegexOptions.ExplicitCapture);
foreach (Match item in regex.Matches(directivesLine))
{
var key = item.Groups["Key"].Value;
var value = item.Groups["Value"].Value;
directives.Add(key, value);
}
}
}
}