forked from microsoft/microsoft-ui-xaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessTextTemplate.ps1
213 lines (176 loc) · 5.67 KB
/
ProcessTextTemplate.ps1
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
Param(
[Parameter(Mandatory = $true)]
[string]$FilePath,
[Parameter(Mandatory = $true)]
[string]$OutputPath,
[string]$ParameterValuesString
)
if ("$env:SDXROOT".Length -eq 0)
{
Write-Error "This script should only be run in a Razzle context."
}
$textTemplatingDllPath = "$env:SDXROOT\tools\managed\v2.0\Microsoft.VisualStudio.TextTemplating.dll"
[System.Reflection.Assembly]::LoadFrom($textTemplatingDllPath) 2>&1>> $null
Add-Type -Path $textTemplatingDllPath
Add-Type -Language CSharp -ReferencedAssemblies $textTemplatingDllPath @"
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.VisualStudio.TextTemplating;
namespace TextTemplateProcessing
{
public sealed class TransformationHost : ITextTemplatingEngineHost
{
private readonly IList<CompilerError> _errors;
private string _basePath;
private IDictionary<string, string> _parameterValues;
public TransformationHost(string basePath, IDictionary<string, string> parameterValues)
{
_errors = new List<CompilerError>();
_basePath = basePath;
_parameterValues = parameterValues;
}
public ICollection<CompilerError> Errors
{
get
{
return _errors;
}
}
public object GetHostOption(string optionName)
{
throw new NotImplementedException();
}
public bool LoadIncludeText(string requestFileName, out string content, out string location)
{
string resolvedLocation = ResolvePath(requestFileName);
if (!string.IsNullOrEmpty(resolvedLocation))
{
content = File.ReadAllText(resolvedLocation);
location = resolvedLocation;
return true;
}
else
{
content = string.Empty;
location = string.Empty;
return false;
}
}
public void LogErrors(CompilerErrorCollection errors)
{
foreach (CompilerError error in errors)
{
_errors.Add(error);
}
}
public AppDomain ProvideTemplatingAppDomain(string content)
{
return AppDomain.CurrentDomain;
}
public string ResolveAssemblyReference(string assemblyReference)
{
if (System.IO.File.Exists(assemblyReference))
{
return assemblyReference;
}
var assembly = Assembly.Load(assemblyReference);
if (assembly != null)
{
return assembly.Location;
}
return string.Empty;
}
public Type ResolveDirectiveProcessor(string processorName)
{
throw new ArgumentException("Invalid directive processor.", "processorName");
}
public string ResolveParameterValue(string directiveId, string processorName, string parameterName)
{
if (directiveId == null)
{
throw new ArgumentNullException("directiveId");
}
if (processorName == null)
{
throw new ArgumentNullException("processorName");
}
if (parameterName == null)
{
throw new ArgumentNullException("parameterName");
}
return _parameterValues[parameterName] as string;
}
public string ResolvePath(string path)
{
if (File.Exists(path))
{
return path;
}
string pathFromBase = Path.Combine(_basePath, path);
if (File.Exists(pathFromBase))
{
return pathFromBase;
}
return string.Empty;
}
public void SetFileExtension(string extension)
{
}
public void SetOutputEncoding(Encoding encoding, bool fromOutputDirective)
{
}
public IList<string> StandardAssemblyReferences
{
get
{
return new string[]
{
typeof(System.Uri).Assembly.Location
};
}
}
public IList<string> StandardImports
{
get
{
return new string[0];
}
}
public string TemplateFile
{
get
{
return string.Empty;
}
}
}
}
"@
[System.Collections.Generic.Dictionary[string, string]]$parameterValues = @{}
$ParameterValuesString.Split(',') | ForEach-Object {
$components = $_.Split('=')
$parameterValues.Add($components[0], $components[1])
}
$engine = New-Object -TypeName Microsoft.VisualStudio.TextTemplating.Engine
$transformationHost = New-Object -TypeName TextTemplateProcessing.TransformationHost -ArgumentList (Split-Path -Path $FilePath),$parameterValues
$transformedText = $engine.ProcessTemplate([System.IO.File]::ReadAllText($FilePath), $transformationHost)
# If we individually write out every error, then every single one of them will be annotated with the line number
# from this script. In order to avoid that hindrance to readability, we'll collect all of the errors (if any)
# into a single string, and then write that out as an error.
$errors = ""
$transformationHost.Errors | ForEach-Object {
$errors = "$errors$_$([Environment]::NewLine)"
}
if ($errors.Length -gt 0)
{
Write-Error $errors
exit 1
}
else
{
[System.IO.File]::WriteAllText($OutputPath, $transformedText)
}