-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathatoms-gen.tt
462 lines (350 loc) · 12.9 KB
/
atoms-gen.tt
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".t.js" #>
<#@ assembly name="Microsoft.CSharp" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Data" #>
<#@ assembly name="System.Data.DataSetExtensions" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xaml" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Data" #>
<#@ import namespace="System.Xml.Linq" #>
<#@ import namespace="System.Xaml" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
/*
The MIT License (MIT)
Copyright (c) 2014 Akash Kava
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
<#
currentDirectory= Path.GetDirectoryName(Host.TemplateFile);
string templateDirectory = currentDirectory + "\\src";
var debug = true;
#>
<#= System.IO.File.ReadAllText(templateDirectory + "\\AtomPrototype.js") #>
<#= System.IO.File.ReadAllText(templateDirectory + "\\jquery.maskedinput.js") #>
(function(window){
//var $old = window.$;
//
//var $ = function(o){
//if(typeof HTMLElement === "object" ? o instanceof HTMLElement : o
//&& typeof o === "object"
//&& o !== null
//&& o.nodeType === 1
//&& typeof o.nodeName==="string"){
//o.__$ = o.__$ || $old(o);
//return o.__$;
//}
//return $old(o);
//};
//
//$.prototype = $old;
var $ = window.$;
var document = window.document;
var Templates = { jsonML: {} };
var WebAtoms = {};
window.WebAtoms = WebAtoms;
window.Templates = Templates;
var jsonML = Templates.jsonML;
<#
PreCompiler = new DynamicCompiler(currentDirectory + "\\bin");
var templates = WriteTemplates(templateDirectory).ToList();
foreach(var t in templates){
WriteLine(t.Template);
}
#>
WebAtoms.PageSetup = WebAtoms.PageSetup || {};
(function(window,WebAtoms){
<# foreach(var t in templates){
WriteLine("/* {0} */",t.Name);
WriteLine(t.Script);
}#>
}).call(WebAtoms.PageSetup,window,WebAtoms);
<#
/* load all files.. of type .js...*/
JSCodeList codeList = new JSCodeList();
ParseDirectory(templateDirectory,codeList, false);
/* codeList.Sort(codeList.Comparer);*/
foreach(JSCode code in codeList){
code.AssignParentReferences(codeList);
}
JSCodeList orderedList = new JSCodeList();
foreach(JSCode code in codeList.OrderBy(x=>x.Index)){
orderedList.Add(code, codeList);
}
foreach(JSCode code in orderedList){
int i = 0;
foreach(string line in code.Code){
if(debug){
if(!string.IsNullOrWhiteSpace(line))
Write("/*Line {0} - '{1}' */",i, code.FileName);
if(line.StartsWith("/// <reference"))
WriteLine("");
else
WriteLine(line);
i++;
}else{
string l = line.Trim();
if(string.IsNullOrWhiteSpace(l))
continue;
if(l.StartsWith("//"))
continue;
WriteLine(l);
}
}
}
#>
})(window);
<#
string txt = this.GenerationEnvironment.ToString();
System.IO.File.WriteAllText(currentDirectory + "\\web-atoms.js" , txt);
//System.IO.File.WriteAllText(currentDirectory + "\\atoms.js" , txt);
//Action[] actions= new Action[]{
// ()=> Minify(currentDirectory, txt,"code", "atoms.min.js"),
// ()=> Minify(currentDirectory, txt,"map", "atoms.min.map")
//};
//System.Threading.Tasks.Parallel.ForEach(actions, a=> a());
#><#+
public string currentDirectory {get;set;}
private void Minify(string currentDirectory,string code, string mode, string fileName){
var form = new System.Collections.Specialized.NameValueCollection();
form["code"] = code;
form["name"] = "atoms";
form["mode"] = mode;
using(var client= new System.Net.WebClient()){
client.Headers.Add(System.Net.HttpRequestHeader.ContentType,"application/x-www-form-urlencoded");
var data = client.UploadValues("http://uglify-js-api.azurewebsites.net/minify", "POST", form);
File.WriteAllBytes(currentDirectory + "\\" + fileName, data);
}
}
public DynamicCompiler PreCompiler { get; set;}
private IEnumerable<TemplateResult> WriteTemplates(string dir){
foreach(string file in System.IO.Directory.EnumerateFiles(dir,"*.html")){
string name = System.IO.Path.GetFileNameWithoutExtension(file);
string[] tokens = name.Split('.');
string templateType = tokens[1];
name = tokens[0];
string text = string.Join("\n",System.IO.File.ReadAllLines(file).Select(x=>x.Trim()).Where(x=> !string.IsNullOrWhiteSpace(x) ));
if(string.IsNullOrWhiteSpace(text)){
continue;
}
yield return WriteTemplate("WebAtoms." + name + "." + templateType, text);
}
foreach(string child in System.IO.Directory.EnumerateDirectories(dir)){
foreach(var t in WriteTemplates(child))
{
yield return t;
}
}
}
private TemplateResult WriteTemplate(string key, string text){
try{
var result = PreCompiler.Compile(text);
return new TemplateResult{
Script = result.Script ,
Name = key,
Template = "jsonML[\"" + key + "\"] = \r\n" + result.JsonMLDocument + "\r\n" + ";"
};
}catch(Exception ex){
throw new Exception("failed to parse " + key , ex);
}
}
public void ParseDirectory(string parent, List<JSCode> codeList , bool parseFiles = true){
if(parseFiles){
foreach(string file in System.IO.Directory.EnumerateFiles(parent,"*.js")){
JSCode code = new JSCode();
code.FileName = System.IO.Path.GetFileName(file);
code.FilePath = file;
code.Code = System.IO.File.ReadAllLines(file);
code.LoadReferences();
codeList.Add(code);
if(code.FileName.StartsWith("ZZZZ"))
code.Index = int.MaxValue;
else
code.Index = int.MaxValue - 100;
}
}
foreach(string dir in System.IO.Directory.EnumerateDirectories(parent,"*.*", System.IO.SearchOption.AllDirectories)){
ParseDirectory(dir,codeList);
}
}
public class GrowingDictionary<T>
where T:class
{
private Dictionary<string,T> values = new Dictionary<string,T>();
public T this[string key]{
get{
T obj = default(T);
if(values.TryGetValue(key,out obj))
return obj;
obj = Activator.CreateInstance<T>();
values[key] = obj;
return obj;
}
}
public IEnumerable<KeyValuePair<string,T>> Values{
get{
return values;
}
}
}
public class JSCodeList : List<JSCode> {
public void Add(JSCode code, JSCodeList all){
// make sure we have all existing base classes first...
foreach(string item in code.References){
JSCode parent = all.FirstOrDefault(x=>x.FileName == item);
if(parent!=null){
Add(parent,all);
}
}
if(!this.Any(x=>x.FileName == code.FileName))
this.Add(code);
}
public IComparer<JSCode> Comparer {
get {
return new JSCodeListSort(this);
}
}
public class JSCodeListSort : IComparer<JSCode> {
private JSCodeList list;
public JSCodeListSort(JSCodeList list)
{
this.list = list;
}
#region public int Compare(JSCode x, JSCode y)
public int Compare(JSCode x, JSCode y)
{
if(x.FileName.StartsWith("ZZZZZ"))
return +1;
if(y.FileName.StartsWith("ZZZZZ"))
return -1;
int n = 0;
n = x.IsParent(list,y);
if (n>0)
return -n;
n = y.IsParent(list,x);
if(n>0)
return n;
return 0;
}
#endregion
}
}
public class JSCode
{
public string FilePath { get; set; }
public string FileName { get; set; }
public string FileNameSmall { get; set; }
public string[] Code { get; set; }
public string[] References { get; set; }
public int Index { get; set; }
public void AssignParentReferences(JSCodeList list){
foreach(var item in References){
JSCode parent = list.FirstOrDefault(x=>x.FileName == item);
if(parent!=null){
parent.Index = this.Index -1;
parent.AssignParentReferences(list);
}
}
}
public int IsParent(JSCodeList list, JSCode other)
{
if (other.References.Any(x => x.Equals(this.FileName)))
return 1;
foreach (var item in other.References)
{
JSCode parentOther = list.FirstOrDefault(x => x.FileName.Equals(item));
if (parentOther != null) {
int n = this.IsParent(list, parentOther);
if(n>0)
return n+1;
}
}
return 0;
}
public void LoadReferences(){
List<string> referenceList = new List<string>();
foreach(string line in this.Code.Where(x=>x.StartsWith("/// <reference "))){
string r = line.Substring(4);
XDocument doc = XDocument.Parse(r);
string path = doc.Root.Attribute(XName.Get("path")).Value;
path = System.IO.Path.GetFileName(path);
referenceList.Add(path);
}
this.References = referenceList.ToArray();
}
}
public class DynamicCompiler {
public object DObject { get; set; }
public DynamicCompiler(string path)
{
string typeName = "NeuroSpeech.AtomsPreCompiler.HtmlCompiler";
string[] dependencies = new string[] {
"HtmlAgilityPack.dll",
"NeuroSpeech.AtomsPreCompiler.dll"
};
System.Reflection.Assembly a = null;
foreach (var item in dependencies)
{
if (!TryLoad(item, out a, path))
throw new System.IO.FileLoadException("Unable to load " + item);
}
var t = a.GetExportedTypes().FirstOrDefault(x => x.Namespace + "." + x.Name == typeName);
DObject = Activator.CreateInstance(t);
DObject.GetType().GetProperty("Prefix").SetValue(DObject,"t");
}
public Result Compile(string html)
{
object r = DObject.GetType().GetMethod("Compile").Invoke(DObject, new object[] { html });
Type t = r.GetType();
var result = new Result { };
result.Document = (string)t.GetProperty("Document").GetValue(r);
result.Script = (string)t.GetProperty("Script").GetValue(r);
result.JsonMLDocument = (string)t.GetProperty("JsonMLDocument").GetValue(r);
return result;
}
public class Result {
public string Document { get; set; }
public string Script { get; set; }
public string JsonMLDocument {get; set;}
}
private bool TryLoad(string item, out System.Reflection.Assembly a, string root = null)
{
foreach (var f in System.IO.Directory.EnumerateFiles(root,"*.dll")) {
if (f.EndsWith(item, StringComparison.OrdinalIgnoreCase)) {
a = System.Reflection.Assembly.LoadFrom(f);
return true;
}
}
foreach (var d in System.IO.Directory.EnumerateDirectories(root))
{
if (TryLoad(item, out a, d)) {
return true;
}
}
a = null;
return false;
}
}
public class TemplateResult{
public string Name { get; set; }
public string Script { get; set; }
public string Template { get; set; }
}
#>