-
Notifications
You must be signed in to change notification settings - Fork 368
/
ConditionalConfig.cs
139 lines (126 loc) · 6.38 KB
/
ConditionalConfig.cs
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections.Generic;
using Microsoft.TemplateEngine.Abstractions.Mount;
using Microsoft.TemplateEngine.Core;
using Microsoft.TemplateEngine.Core.Contracts;
using Microsoft.TemplateEngine.Core.Operations;
using Microsoft.TemplateEngine.Utils;
using Newtonsoft.Json.Linq;
namespace Microsoft.TemplateEngine.Orchestrator.RunnableProjects.Config
{
public class ConditionalConfig : IOperationConfig
{
public string Key => Conditional.OperationName;
public Guid Id => new Guid("3E8BCBF0-D631-45BA-A12D-FBF1DE03AA38");
public IEnumerable<IOperationProvider> ConfigureFromJObject(JObject rawConfiguration, IDirectory templateRoot)
{
string commentStyle = rawConfiguration.ToString("style");
IEnumerable<IOperationProvider> operations = null;
if (string.IsNullOrEmpty(commentStyle) || string.Equals(commentStyle, "custom", StringComparison.OrdinalIgnoreCase))
{
operations = ConditionalCustomConfig.ConfigureFromJObject(rawConfiguration);
}
else if (string.Equals(commentStyle, "line", StringComparison.OrdinalIgnoreCase))
{
operations = ConditionalLineCommentConfig.ConfigureFromJObject(rawConfiguration);
}
else if (string.Equals(commentStyle, "block", StringComparison.OrdinalIgnoreCase))
{
operations = ConditionalBlockCommentConfig.ConfigureFromJObject(rawConfiguration);
}
else
{
throw new TemplateAuthoringException($"Template authoring error. Invalid comment style [{commentStyle}].", "style");
}
foreach (IOperationProvider op in operations)
{
yield return op;
}
}
public static IReadOnlyList<IOperationProvider> ConditionalSetup(ConditionalType style, string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
{
List<IOperationProvider> setup;
switch (style)
{
case ConditionalType.MSBuild:
setup = new List<IOperationProvider>();
setup.AddRange(MSBuildConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, "msbuild-conditional"));
setup.AddRange(ConditionalBlockCommentConfig.GenerateConditionalSetup("<!--", "-->"));
break;
case ConditionalType.Xml:
setup = ConditionalBlockCommentConfig.GenerateConditionalSetup("<!--", "-->");
break;
case ConditionalType.Razor:
setup = ConditionalBlockCommentConfig.GenerateConditionalSetup("@*", "*@");
break;
case ConditionalType.CLineComments:
setup = ConditionalLineCommentConfig.GenerateConditionalSetup("//");
break;
case ConditionalType.CNoComments:
setup = CStyleNoCommentsConditionalSetup(evaluatorType, wholeLine, trimWhiteSpace, id);
break;
case ConditionalType.CBlockComments:
setup = ConditionalBlockCommentConfig.GenerateConditionalSetup("/*", "*/");
break;
case ConditionalType.HashSignLineComment:
// Most line comment conditional tags use: <comment symbol><keyword prefix><keyword>
// But for this one, the '#' comment symbol is all that's needed, so it uses an empty keyword prefix.
// So we end up with regular conditionals suchs as '#if', '#else'
// and actionables such as '##if'
ConditionalKeywords keywords = new ConditionalKeywords()
{
KeywordPrefix = string.Empty
};
setup = ConditionalLineCommentConfig.GenerateConditionalSetup("#", keywords, new ConditionalOperationOptions());
break;
case ConditionalType.RemLineComment:
setup = ConditionalLineCommentConfig.GenerateConditionalSetup("rem ");
break;
case ConditionalType.HamlLineComment:
setup = ConditionalLineCommentConfig.GenerateConditionalSetup("-#");
break;
case ConditionalType.JsxBlockComment:
setup = ConditionalBlockCommentConfig.GenerateConditionalSetup("{/*", "*/}");
break;
default:
throw new Exception($"Unrecognized conditional type {style}");
}
return setup;
}
// Nice to have: Generalize this type of setup similarly to Line, Block, & Custom
public static List<IOperationProvider> MSBuildConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
{
ConditionEvaluator evaluator = EvaluatorSelector.Select(evaluatorType);
IOperationProvider conditional = new InlineMarkupConditional(
new MarkupTokens("<".TokenConfig(), "</".TokenConfig(), ">".TokenConfig(), "/>".TokenConfig(), "Condition=\"".TokenConfig(), "\"".TokenConfig()),
wholeLine,
trimWhiteSpace,
evaluator,
"$({0})",
id,
true
);
return new List<IOperationProvider>()
{
conditional
};
}
// Nice to have: Generalize this type of setup similarly to Line, Block, & Custom
public static List<IOperationProvider> CStyleNoCommentsConditionalSetup(string evaluatorType, bool wholeLine, bool trimWhiteSpace, string id)
{
ConditionalTokens tokens = new ConditionalTokens
{
IfTokens = new[] { "#if".TokenConfig() },
ElseTokens = new[] { "#else".TokenConfig() },
ElseIfTokens = new[] { "#elseif".TokenConfig() },
EndIfTokens = new[] { "#endif".TokenConfig() }
};
ConditionEvaluator evaluator = EvaluatorSelector.Select(evaluatorType);
IOperationProvider conditional = new Conditional(tokens, wholeLine, trimWhiteSpace, evaluator, id, true);
return new List<IOperationProvider>()
{
conditional
};
}
}
}