-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
.vssbe
1655 lines (1655 loc) · 76.7 KB
/
.vssbe
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"Header": {
"_": [
" This file for vsSolutionBuildEvent ",
" https://github.com/3F/vsSolutionBuildEvent "
],
"Compatibility": "0.12.4"
},
"PreBuild": [
{
"Enabled": true,
"Name": "Prepare",
"Caption": "Prepare data and directories",
"Mode": {
"$type": "net.r_eg.vsSBE.Events.ModeScript, vsSolutionBuildEvent",
"Type": "Script",
"Command__": [
"#[$(dbin = \"bin/Release/\")]",
"#[$(dobj = \"obj/\")]",
"",
"~ Inputs",
"",
"$(fCore = '$(dobj)core')",
"$(fCoreMin = '$(dobj)gnt.min.core')",
"$(fPacked = '$(dobj)gnt.bat')",
"$(fEmbd = 'embd.bat')",
"$(fPackMin = '$(dobj)gnt.minified.bat')",
"",
"#[($(packmaxline) == *Undefined*){ $(packmaxline = '1900') }]",
"",
"~ Output structure",
"",
"$(odir = \"$(dbin)raw/\")",
"",
"#[IO delete.directory(\"$(dbin)\", true)]",
"#[IO copy.directory(\"\", \"$(dbin)\", true)]",
"#[IO copy.directory(\"\", \"$(dobj)\", true)]"
]
}
},
{
"Enabled": true,
"Name": "ActCs",
"SupportMSBuild": false,
"Mode": {
"$type": "net.r_eg.vsSBE.Events.ModeScript, vsSolutionBuildEvent",
"Type": "Script",
"Command__": [
"",
"initialize without msbuild processing",
"#[var cslogic = #[IO get(\"csharp.cs\")]]"
]
}
},
{
"Enabled": true,
"Name": "ActCore",
"Mode": {
"$type": "net.r_eg.vsSBE.Events.ModeScript, vsSolutionBuildEvent",
"Type": "Script",
"Command__": [
"#[$(revDeltaBase = \"2017/08/12\")]",
"#[$(revDeltaMin = $([System.Math]::Pow(10, 3)))]",
"#[$(revDeltaMax = 65534)]",
"",
"#[var pVer = #[File get(\".version\")]]",
"",
"#[\" ",
" Calculate revision",
"\"]",
"#[var tBase = $([System.DateTime]::Parse('$(revDeltaBase)').ToBinary())]",
"#[var tNow = $([System.DateTime]::UtcNow.Ticks)]",
"#[var revBuild = #[$(",
" [System.TimeSpan]::FromTicks('$(",
" [MSBuild]::Subtract($(tNow), $(tBase))",
" )')",
" .TotalMinutes.ToString('0'))]]",
" ",
"#[var revBuild = #[$(",
" [MSBuild]::Add(",
" $(revDeltaMin), ",
" $([MSBuild]::Modulo(",
" $(revBuild), ",
" $([MSBuild]::Subtract(",
" $(revDeltaMax), $(revDeltaMin)",
" ))",
" ))",
" )",
" )]",
"]",
"",
"#[$(pVerPrintFile = \"$(pVer).$(revBuild)\"))]",
"",
"#[\" ",
" Checking of the git folder +tool & define sha1, branch name, etc.",
"\"]",
"#[var isGit = #[IO cmd(\"git rev-parse 2>&1\")]]",
"#[( $(isGit) == \"\" )",
"{",
" #[var bSha1 = #[IO sout(\"git\", \"rev-parse --short HEAD\")]]",
" ",
" #[$(pVerPrintFile = \"$(pVerPrintFile)+$(bSha1)\")]",
"}",
"else {",
" #[$(bSha1 = '-')]",
"}]",
"",
"#[( $(reltype) == \"PublicRelease\" ) {",
" #[$(pVerPrintApp = \"$(pVer).$(revBuild)+$(bSha1)\")]",
"}",
"else {",
" #[$(pVerPrintApp = $(pVer))]",
"}]",
"",
"#[\" obj \"]",
"",
"#[IO copy.file(\"csinit.targets\", \"$(dobj)core\", true)]",
"#[IO replace.Regex(\"$(dobj)core\", \"<GetNuTool>.*?</GetNuTool>\", \"<GetNuTool>$(pVerPrintApp)</GetNuTool>\")]",
"",
"$ -> $$ to prevent double evaluation",
"#[IO replace",
"(",
" \"$(dobj)core\",",
" \"return true;\",",
" \"$(cslogic.Replace('$(', '$$('))\"",
")]"
]
}
},
{
"Enabled": true,
"Name": "CompressorMinified",
"Caption": ".compressor at /minified ",
"SupportSBEScripts": false,
"Mode": {
"$type": "net.r_eg.vsSBE.Events.ModeCSharp, vsSolutionBuildEvent",
"Type": "CSharp",
"References": [
"System.dll",
"System.Core.dll"
],
"SmartReferences": true,
"GenerateInMemory": true,
"TreatWarningsAsErrors": false,
"WarningLevel": 4,
"FilesMode": false,
"CachingBytecode": false,
"Command__": [
"/*",
"* Copyright (c) 2015 Denis Kuzmin <[email protected]> github/3F",
"*",
"* Licensed under the GetNuTool license",
"* https://github.com/3F/GetNuTool",
"*/",
"",
"using System;",
"using System.Collections.Generic;",
"using System.IO;",
"using System.Linq;",
"using System.Text;",
"using System.Text.RegularExpressions;",
"using ICommand = net.r_eg.vsSBE.Actions.ICommand;",
"using ISolutionEvent = net.r_eg.vsSBE.Events.ISolutionEvent;",
"",
"namespace vsSolutionBuildEvent",
"{",
" public class CSharpMode",
" {",
" public static int Init(ICommand cmd, ISolutionEvent evt)",
" {",
" const string _CORE = @\"$(fCore)\";",
" const string _OUTPUT = @\"$(fCoreMin)\";",
"",
" Func<char, string> quotes = (symbol) => string.Format",
" (@\"",
" (?<!\\\\)",
" (",
" {0}(?:",
" (?:",
" [^{0}\\\\\\n]",
" |",
" \\\\\\\\",
" |",
" \\\\{0}?",
" )*",
" ){0}",
" )\",",
" symbol",
" );",
"",
" Func<char[], int, Func<int, bool>, string[]> gencomb = (_dict, _size, _rule0) =>",
" {",
" var combination = new char[_size];",
" var set = new List<string>((int)Math.Pow(_dict.Length, _size));",
"",
" int pos = 0;",
" Action generator = null;",
" generator = () =>",
" {",
" for(int i = 0, lim = _size - 1; i < _dict.Length; ++i)",
" {",
" if(pos == 0 && !_rule0(i))",
" {",
" continue;",
" }",
"",
" if(pos < lim)",
" {",
" combination[pos] = _dict[i];",
" ++pos;",
" generator();",
" --pos;",
" }",
" else",
" {",
" combination[pos] = _dict[i];",
" set.Add(new string(combination.ToArray()));",
" }",
" }",
" };",
"",
" generator();",
" return set.ToArray();",
" };",
"",
" var variables = new Dictionary<string, string>();",
"",
" var cdict = new[]",
" {",
" 'd', 'e', 'f', 'g', 'h', 'i', 'j',",
" 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',",
" 'u', 'v', 'w', 'x', 'y', 'z',",
" 'D', 'E', 'F', 'G', 'H', 'I', 'J',",
" 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',",
" 'U', 'V', 'W', 'X', 'Y', 'Z',",
" 'a', 'b', 'c', 'A', 'B', 'C',",
" '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',",
" '_'",
" };",
"",
" Func<int, bool> rule = (i) => { return char.IsLetter(cdict[i]) || cdict[i] == '_'; };",
" var vdict1 = gencomb(cdict, 1, rule);",
" var vdict2 = gencomb(cdict, 2, rule);",
"",
" var vdict = new string[vdict1.Length + vdict2.Length];",
" vdict1.CopyTo(vdict, 0);",
" vdict2.CopyTo(vdict, vdict1.Length);",
"",
" // to skip processing for:",
" var exvar = new string[] { \"true\", \"false\" };",
"",
" // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/",
" var rsvwords = new[] { \"do\", \"in\", \"is\", \"as\", \"if\", \"by\", \"on\" };",
"",
" using(var reader = new StreamReader(_CORE, Encoding.UTF8, true))",
" {",
" string content = reader.ReadToEnd();",
"",
" // protect strings",
"",
" var strings = new Dictionary<uint, string>();",
" uint ident = 0;",
" content = Regex.Replace",
" (",
" content,",
" string.Format(@\"({0}|{1})\", quotes('\"'), quotes('\\'')),",
" (m) =>",
" {",
" ++ident;",
" strings[ident] = m.Groups[1].Value;",
" return string.Format(\"!s{0}!\", ident);",
" },",
" RegexOptions.IgnorePatternWhitespace",
" );",
"",
" // code",
"",
" content = Regex.Replace(content, @\"<!\\[CDATA\\[.+?\\]\\]>\", (m) =>",
" {",
" string data = m.Value;",
"",
" // comments",
"",
" data = Regex.Replace(data, @\"\\s*\\/\\*.+?\\*\\/\\s*\", \"\", RegexOptions.Singleline);",
" data = Regex.Replace(data, @\"\\s*\\/\\/.+?$\", \"\", RegexOptions.Multiline);",
"",
" // shorten variables",
" // TODO: sort by popularity of use with 1 byte priority",
"",
" variables.Clear();",
" uint uniqVars = 0;",
"",
" Func<string, string> allocvar = (vname) =>",
" {",
" if(uniqVars + 1 > vdict.Length) throw new OverflowException(\"vdict does not contain data for new vars\");",
" do",
" {",
" variables[vname] = vdict[uniqVars++];",
" }",
" while(rsvwords.Contains(variables[vname]));",
"",
" return variables[vname];",
" };",
"",
" Func<Match, string, string, string, string> shname = (_m, l, vname, r) =>",
" {",
" if(exvar.Contains(vname) /*TODO: L-66 || variables.ContainsValue(vname)*/) return _m.Value;",
" if(variables.ContainsKey(vname)) return l + variables[vname] + r;",
"",
" return l + allocvar(vname) + r;",
" };",
"",
" Func<string, string> shnameLArgs = (vname) =>",
" {",
" if(variables.ContainsKey(vname)) return variables[vname];",
" return allocvar(vname);",
" };",
"",
" data = Regex.Replace(data, @\"([\\s;},])String([\\s.(]+)\", \"$1string$2\");",
" data = Regex.Replace(data, @\"[sS]tring\\s*\\.\\s*Empty\", \"\\\"\\\"\");",
" data = Regex.Replace(data, @\"\\s*const\\s+[sS]tring\\s+\", \"var \");",
" data = Regex.Replace(data, @\"([;(){}]\\s*)\\w+(?:\\s*\\[\\])?\\s+(\\w+)\\s*(in|=)\\s*\", \" $1 var $2 $3 \");",
" data = Regex.Replace(data, @\"\\s*([{}()=+\\-\\[\\]*?!@,;.])\\s*\", \"$1\");",
"",
" data = Regex.Replace",
" (",
" data,",
" @\"(?:",
" (?'type'",
" \\w+\\s*<[\\w_<,\\s>\\[\\]]+>",
" |",
" \\w+[\\(\\s]*",
" )",
" \\s+",
" (?'name'\\w+)",
" \\s*",
" (?'lim'",
" (?:[+\\-*\\/]?=|\\s+in\\s+|[,)]|\\s*;)",
" )",
" |",
" (?'fl'[(\\s])*(?'lmbd'[^.()=]+?)(?'fr'[)\\s]*)=>",
" )",
" \",",
" (_m) =>",
" {",
" var vname = _m.Groups[\"name\"];",
" if(vname.Success)",
" {",
" return shname(_m, _m.Groups[\"type\"].Value + \" \", vname.Value, _m.Groups[\"lim\"].Value);",
" }",
"",
" var lmbd = _m.Groups[\"lmbd\"];",
" if(lmbd.Success)",
" {",
" var sb = new StringBuilder();",
" foreach(string arg in lmbd.Value.Split(new[] { ',' }))",
" {",
" sb.Append(shnameLArgs(arg));",
" sb.Append(',');",
" }",
"",
" sb.Remove(sb.Length - 1, 1);",
" string fargs = sb.ToString();",
"",
" string fl = _m.Groups[\"fl\"].Value;",
" string fr = _m.Groups[\"fr\"].Value;",
"",
" if(!lmbd.Value.Contains(',') && fr == \")\") fl = fr = \"\";",
"",
" return string.Format(\"{0}{1}{2}=>\", fl, fargs, fr);",
" }",
"",
" return _m.Value;",
" },",
" RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase",
" );",
"",
" // using of variables",
" data = Regex.Replace",
" (",
" data,",
" @\"(?'def'^|[\\s}]else\\s|\\s+in\\s+|(?<==)>|[!={};:\\[(),?+\\-|&])",
" \\s*",
" (?'name'\\w+)",
" (?'exc'[<>]*)?",
" \",",
" (Match _m) =>",
" {",
" var def = _m.Groups[\"def\"].Value;",
" var vname = _m.Groups[\"name\"].Value;",
" var exc = _m.Groups[\"exc\"].Value;",
"",
" if(exc.IndexOfAny(new[] { '<', '>' }) != -1) return _m.Value;",
"",
" if(!Regex.IsMatch(vname, \"^[a-zA-Z_]\")) return _m.Value;",
"",
" if(!variables.ContainsKey(vname))",
" {",
" return def + vname + exc;",
" }",
" return def + variables[vname] + exc;",
" },",
" RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Multiline",
" );",
"",
" // predefined",
"",
" data = data.Replace(\"StringComparison.OrdinalIgnoreCase\", \"(StringComparison)5\");",
" data = data.Replace(\"StringComparison.Ordinal\", \"(StringComparison)4\");",
" data = data.Replace(\"SearchOption.AllDirectories\", \"(SearchOption)1\");",
" data = data.Replace(\"FileMode.Open\", \"(FileMode)3\");",
" data = data.Replace(\"FileAccess.Read\", \"(FileAccess)1\");",
" data = data.Replace(\"UriKind.Relative\", \"(UriKind)2\");",
" data = data.Replace(\"FileMode.Create\", \"(FileMode)2\");",
" data = data.Replace(\"CompressionOption.Maximum\", \"(CompressionOption)1\");",
" data = data.Replace(\"StringSplitOptions.RemoveEmptyEntries\", \"(StringSplitOptions)1\");",
"",
" // 0",
" data = data.Replace(\"TargetMode.Internal\", \"0\");",
" data = data.Replace(\"SearchOption.TopDirectoryOnly\", \"0\");",
"",
" return data;",
" },",
" RegexOptions.Singleline);",
"",
" // common rules",
"",
" content = content.Replace(\"\\r\", \"\").Replace(\"\\n\", \"\");",
" content = Regex.Replace(content, @\"\\s{2,}\", \" \");",
" content = Regex.Replace(content, @\"\\s*([=,()\\[\\];:.&|{}\\/<>]+)\\s*\", \"$1\");",
"",
" /* XML */",
"",
" Action<Dictionary<string, string>, int> writeMap = (vars, part) =>",
" {",
" using(var wmap = new StreamWriter(string.Format(\"{0}.{1}.map\", _OUTPUT, part), false, new UTF8Encoding(false)))",
" {",
" string map = string.Empty;",
" foreach(var v in vars) map += string.Format(\"{0}={1}{2}\", v.Value, v.Key, Environment.NewLine);",
" wmap.Write(map);",
" }",
" };",
" writeMap(variables, 0);",
"",
" // XML rules",
"",
" content = Regex.Replace(content, @\"<!--.+?-->\", \"\");",
" content = Regex.Replace(content, @\">\\s+<\", \"><\");",
" content = Regex.Replace(content, @\"\\s+\\/>\", \"/>\");",
"",
" // recover strings",
"",
" content = Regex.Replace(content, @\"!s(\\d+)!\", (m) => {",
" return strings[uint.Parse(m.Groups[1].Value)];",
" });",
"",
" // xml Tasknames",
"",
" variables.Clear();",
" uint uniqt = 0;",
" content = Regex.Replace(content, @\"TaskName\\s*=\\s*\"\"(?'name'\\S+)\"\"\", (_m) =>",
" {",
" var tname = _m.Groups[\"name\"].Value;",
"",
" variables[tname] = vdict[uniqt++];",
"",
" return \"TaskName=\\\"\" + variables[tname] + \"\\\"\";",
" });",
"",
" content = Regex.Replace(content, @\"(?'l'<\\/?)(?'name'[a-z0-9A-Z\\-_]+)\", (_m) =>",
" {",
" var tname = _m.Groups[\"name\"].Value;",
"",
" if(variables.ContainsKey(tname))",
" {",
" return _m.Groups[\"l\"].Value + variables[tname];",
" }",
"",
" return _m.Value;",
" });",
"",
" // XML rules - Post filter",
"",
" content = Regex.Replace(content, @\"\\s+ParameterType\\s*=\\s*\"\"System.String\"\"\\s*\", \" \");",
" content = Regex.Replace(content, @\"\\s+Required\\s*=\\s*\"\"\\S+\"\"\\s*\", \" \");",
" content = Regex.Replace(content, @\"\\s+(\\/)?>\", \"$1>\");",
" content = Regex.Replace(content, @\"'\\s*==\\s*'\", \"'=='\");",
"",
" using(var writer = new StreamWriter(_OUTPUT, false, new UTF8Encoding(false)))",
" writer.Write(content);",
"",
" writeMap(variables, 1);",
" Console.WriteLine(\"{0} -> {1}\", _CORE, _OUTPUT);",
" }",
"",
" return 0;",
" }",
" }",
"}"
]
}
},
{
"Enabled": true,
"Name": "Packer",
"Caption": ".packer at /embedded",
"SupportSBEScripts": false,
"Mode": {
"$type": "net.r_eg.vsSBE.Events.ModeCSharp, vsSolutionBuildEvent",
"Type": "CSharp",
"References": [
"System.dll",
"System.Core.dll"
],
"SmartReferences": true,
"GenerateInMemory": true,
"TreatWarningsAsErrors": false,
"WarningLevel": 4,
"FilesMode": false,
"CachingBytecode": false,
"Command__": [
"/*",
"* Copyright (c) 2015 Denis Kuzmin <[email protected]> github/3F",
"*",
"* Licensed under the GetNuTool license",
"* https://github.com/3F/GetNuTool",
"*/",
"",
"using System;",
"using System.Collections.Generic;",
"using System.IO;",
"using System.Linq;",
"using System.Text;",
"using System.Text.RegularExpressions;",
"using ICommand = net.r_eg.vsSBE.Actions.ICommand;",
"using ISolutionEvent = net.r_eg.vsSBE.Events.ISolutionEvent;",
"",
"namespace vsSolutionBuildEvent",
"{",
" public class CSharpMode",
" {",
" public static int Init(ICommand cmd, ISolutionEvent evt)",
" {",
" const string _CORE = @\"$(fCoreMin)\";",
" const string _OUTPUT = @\"$(fPacked)\";",
" const string _EMBDTPL = @\"$(fEmbd)\";",
" const string _P_VER = @\"$(pVer)\";",
"",
" Func<char, string> quotes = (symbol) =>",
" {",
" return string.Format",
" (@\"",
" (?<!\\\\)",
" (",
" {0}(?:",
" (?:",
" [^{0}\\\\]",
" |",
" \\\\\\\\",
" |",
" \\\\{0}?",
" )*",
" ){0}",
" )\",",
" symbol",
" );",
" };",
"",
" Func<string, Dictionary<int, int>> scalc = (content) =>",
" {",
" var data = new Dictionary<int, int>(); // \"(start) -> \"(length)",
"",
" var matches = Regex.Matches(content, quotes('\"'), RegexOptions.IgnorePatternWhitespace);",
" foreach(Match m in matches) {",
" data[m.Index] = m.Length;",
" }",
" return data;",
" };",
"",
" Func<int, Dictionary<int, int>, bool> isInsideString = (idx, strings) =>",
" {",
" foreach(var s in strings)",
" {",
" if(idx > s.Key && idx < (s.Key + s.Value)) {",
" return true;",
" }",
" }",
" return false;",
" };",
"",
" Func<char[], int, Func<int, bool>, string[]> gencomb = (_dict, _size, _rule0) =>",
" {",
" var combination = new char[_size];",
" var set = new List<string>((int)Math.Pow(_dict.Length, _size));",
"",
" int pos = 0;",
" Action generator = null;",
" generator = () =>",
" {",
" for(int i = 0, lim = _size - 1; i < _dict.Length; ++i)",
" {",
" if(pos == 0 && !_rule0(i)) {",
" continue;",
" }",
"",
" if(pos < lim) {",
" combination[pos] = _dict[i];",
" ++pos;",
" generator();",
" --pos;",
" }",
" else {",
" combination[pos] = _dict[i];",
" set.Add(new String(combination.ToArray()));",
" }",
" }",
" };",
"",
" generator();",
" return set.ToArray();",
" };",
"",
" var variables = new Dictionary<string, string>();",
"",
" var cdict = new[]",
" {",
" '-', '[', ']', ';', '.', ',', ':', '+', '{', '}', '_',",
" 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',",
" 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',",
" 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',",
" '4', '5', '6', '7', '8', '9',",
" '$', '#', '@', '`', '?',",
" // '!',",
" };",
"",
" var vdict = gencomb(cdict, 1, (i) => { return !char.IsDigit(cdict[i]); });",
"",
" // to skip processing for:",
" var exword = new[] { \"\" };",
" int skipFirst = 110;",
"",
" const string rlexemes = @\"(?:",
" (?'wrd'",
" Console\\s*\\.\\s*WriteLine\\(\\s*",
" | Console\\s*\\.\\s*Error\\s*\\.\\s*WriteLine\\(\\s*",
" | string\\.IsNullOrEmpty\\(\\s*",
" | Microsoft\\.Build\\.Tasks\\.",
" | Console\\.Write\\(",
" | \\.Trim\\(\\)",
" | ServicePointManager\\.SecurityProtocol",
" | packages\\.config",
" )",
" |",
" (?'ld'%%?0[aA])?",
" (?'cls'[a-z0-9A-Z_\\-]+)",
" )\";",
"",
" using(var reader = new StreamReader(_CORE, System.Text.Encoding.UTF8, true))",
" {",
" var content = reader.ReadToEnd();",
"",
" content = content.Replace(\"%\", \"%%\");",
" ",
" // Protecting data outside strings only:",
" content = Regex.Replace(content, @\"[<|>&]\", (m) => isInsideString(m.Index, scalc(content)) ? m.Value : \"^\" + m.Value);",
"",
" // lexemes via vars",
"",
" var lstat = new Dictionary<string, int>();",
" foreach(Match m in Regex.Matches(content, rlexemes, RegexOptions.IgnorePatternWhitespace))",
" {",
" var wrd = m.Groups[\"wrd\"];",
" var cls = m.Groups[\"cls\"];",
"",
" var vname = wrd.Success ? wrd.Value : cls.Value;",
"",
" if(vname.Length < 4) { // minimal is 3: %a%",
" continue;",
" }",
"",
" if(exword.Contains(vname)) {",
" continue;",
" }",
"",
" if(!lstat.ContainsKey(vname)) {",
" lstat[vname] = -1 * (vname.Length + 7); //for init: set a= + length + &",
" }",
" lstat[vname] += (vname.Length - 3); //for use: % + a + %",
" }",
" ",
" uint uniqVars = 0;",
" variables.Clear();",
" var prio = lstat.Where(x => x.Value > 0).OrderByDescending(x => x.Value).Take(vdict.Length);",
" var defv = \"\";",
"",
" content = Regex.Replace(content, rlexemes, (m) => ",
" {",
" if(m.Index < skipFirst) {",
" return m.Value;",
" }",
"",
" var wrd = m.Groups[\"wrd\"];",
" var cls = m.Groups[\"cls\"];",
" var ld = m.Groups[\"ld\"].Value;",
"",
" var vname = wrd.Success ? wrd.Value : cls.Value;",
"",
" if(prio.All(x => x.Key != vname)) {",
" return m.Value;",
" }",
"",
" if(variables.ContainsKey(vname)) {",
" return ld + variables[vname];",
" }",
"",
" if(uniqVars + 1 > vdict.Length) {",
" throw new OverflowException(\"vdict does not contain data for new vars\");",
" }",
"",
" var nv = vdict[uniqVars++];",
"",
" variables[vname] = string.Format(\"%{0}%\", nv);",
"",
" defv += string.Format(\"set {0}={1}&\", nv, vname);",
" return ld + variables[vname];",
" },",
" RegexOptions.IgnorePatternWhitespace);",
"",
" using(var wmap = new StreamWriter(_OUTPUT + \".p.map\", false, new UTF8Encoding(false)))",
" {",
" string map = string.Empty;",
" foreach(var v in variables) map += string.Format(\"{0}={1}{2}\", v.Value, v.Key, Environment.NewLine);",
" wmap.Write(map);",
" }",
" ",
" // We'll work without double quotes, so we should define all pairs of \"...\" per line",
"",
" var strings = scalc(content);",
"",
" // Now, we should split long strings",
"",
" var lines = new List<string>();",
" ",
" // Protecting from some symbols in zero position via moving to the end of the previous line",
" Action<int, int> safeAdd = (idx, l) =>",
" {",
" int lofs = idx;",
" while( (content[lofs] == ' ' || content[lofs] == ')' || content[lofs] == '=')",
" ||",
" //TODO: lexemes via vars %a%, %ab%, ... + escaped %%data",
" (content[lofs] == '%' || content[Math.Min(lofs + 1, content.Length - 1)] == '%') ",
" )",
" {",
" ++lofs;",
" }",
"",
" int ofsdelta = lofs - idx;",
" if(ofsdelta > 0)",
" {",
" lines[lines.Count - 1] += content.Substring(idx, ofsdelta);",
" lines.Add(content.Substring(lofs, l - ofsdelta));",
" return;",
" }",
"",
" lines.Add(content.Substring(idx, l));",
" };",
"",
" for(int i = 0, len = $(packmaxline); i < content.Length; i += len)",
" {",
" int rlen = Math.Min(content.Length - i, len);",
" int rpos = i + rlen;",
"",
" bool added = false;",
" foreach(var ifq in strings)",
" {",
" // Protect strings that we saw above",
" if(rpos >= ifq.Key && rpos <= ifq.Key + ifq.Value)",
" {",
" int repos = ((ifq.Key + ifq.Value) - rpos);",
"",
" safeAdd(i, rlen + repos);",
" i += repos;",
"",
" added = true;",
" break;",
" }",
" }",
"",
" if(added) {",
" continue;",
" }",
"",
" // Protecting from escaped chars as ^ + char above",
"",
" int esc = rpos - 1; // the end of the previous line that should be checked on `^` char",
" if(content.Length > esc && content[esc] == '^') {",
" rlen += 1;",
" safeAdd(i, rlen);",
" i += 1;",
" continue;",
" }",
" ",
" safeAdd(i, rlen);",
" }",
"",
" // Now we can define, how it should be written into external file",
"",
" const string corevar = \"tgnt\";",
" content = string.Empty;",
"",
" foreach(var line in lines)",
" {",
" //content += $\"<nul set /P ={line}>> %{corevar}%{Environment.NewLine}\";",
" string ldata = string.Format(\"<nul set/P={0}>>%{1}%\\r\\n\", line, corevar);",
"",
" if(ldata.Length >= 2047) {",
" Console.WriteLine(\"[Warn] Length of line {0}\", ldata.Length);",
" }",
" else if(ldata.Length >= 8191) {",
" Console.Error.WriteLine(\"[Error] Length of line {0}\", ldata.Length);",
" }",
" ",
" content += ldata;",
" }",
"",
" // definition of lexemes",
"",
" content = defv.Substring(0, defv.Length - 1) + \"\\r\\n\" + content;",
"",
" // Finally, format script to work with gnt.core",
"",
" using(var tplreader = new StreamReader(_EMBDTPL, System.Text.Encoding.UTF8, true))",
" {",
" content = tplreader.ReadToEnd()",
" .Replace(\"$gnt.core.logic$\", content)",
" .Replace(\"$tpl.corevar$\", corevar)",
" .Replace(\"$core.version$\", _P_VER);",
" }",
"",
" // ",
"",
" using(var writer = new StreamWriter(_OUTPUT, false, new UTF8Encoding(false))) { // do not use BOM ! it failed with initial signature",
" writer.Write(content);",
" }",
" Console.WriteLine(\"{0} -> {1}\", _CORE, _OUTPUT);",
" }",
" ",
" return 0;",
" }",
" }",
"}"
]
}
},
{
"Enabled": true,
"Name": "CompressorEmbd",
"Caption": ".compressor at /embedded",
"SupportSBEScripts": false,
"Mode": {
"$type": "net.r_eg.vsSBE.Events.ModeCSharp, vsSolutionBuildEvent",
"Type": "CSharp",
"References": [
"System.dll",
"System.Core.dll"
],
"SmartReferences": true,
"GenerateInMemory": true,
"TreatWarningsAsErrors": false,
"WarningLevel": 4,
"FilesMode": false,
"CachingBytecode": false,
"Command__": [
"/*",
"* Copyright (c) 2015 Denis Kuzmin <[email protected]> github/3F",
"*",
"* Licensed under the GetNuTool license",
"* https://github.com/3F/GetNuTool",
"*",
"* Based on compressor from hMSBuild project",
"* https://github.com/3F/hMSBuild",
"*/",
"",
"using System;",
"using System.Collections.Generic;",
"using System.IO;",
"using System.Linq;",
"using System.Text;",
"using System.Text.RegularExpressions;",
"using ICommand = net.r_eg.vsSBE.Actions.ICommand;",
"using ISolutionEvent = net.r_eg.vsSBE.Events.ISolutionEvent;",
"",
"namespace vsSolutionBuildEvent",
"{",
" public class CSharpMode",
" {",
" public static int Init(ICommand cmd, ISolutionEvent evt)",
" {",
" const string _CORE = @\"$(fPacked)\";",
" const string _OUTPUT = @\"$(fPackMin)\";",
"",
" Func<char[], int, Func<int, bool>, string[]> gencomb = (_dict, _size, _rule0) =>",
" {",
" var combination = new char[_size];",
" var set = new List<string>((int)Math.Pow(_dict.Length, _size));",
"",
" int pos = 0;",
" Action generator = null;",
" generator = () =>",
" {",
" for(int i = 0, lim = _size - 1; i < _dict.Length; ++i)",
" {",
" if(pos == 0 && !_rule0(i)) continue;",
"",
" if(pos < lim)",
" {",
" combination[pos] = _dict[i];",
" ++pos;",
" generator();",
" --pos;",
" }",
" else",
" {",
" combination[pos] = _dict[i];",
" set.Add(new String(combination.ToArray()));",
" }",
" }",
" };",
"",
" generator();",
" return set.ToArray();",
" };",
"",
" Func<string, string> review = (input) =>",
" {",
" /* predefined parser behaviour */",
" return Regex.Replace(input, @\"\\s*::&:\\s*&?\", \"\\r\\n\");",
" };",
"",
" var variables = new Dictionary<string, string>();",
" UTF8Encoding utf8noId = new UTF8Encoding(false);",
"",
" var cdict = new[]",
" {",
" 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',",
" 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',",
" 'y', 'z', 'a', 'b', 'c', 'd', '0', '1', '2', '3',",
" '4', '5', '6', '7', '8', '9',",
" // '_'",
" };",
"",
" string[] vdict = gencomb(cdict, 2, (i) => { return char.IsLetter(cdict[i]) || cdict[i] == '_'; });",
"",
" // to skip processing for:",
" var exvar = new[] { \"__p_call\", \"__p_msb\", \"msb.gnt.cmd\" };",
"",
" const string VNAME = \"[a-z_][a-z_0-9]+\";",
" using(var reader = new StreamReader(_CORE, Encoding.UTF8, true))",
" {",
" string content = reader.ReadToEnd();",
"",
" /* set /a ERROR_ codes */",
"",
" var errorsCodes = new Dictionary<string, string>();",
" content = Regex.Replace",
" (",
" content,",
" @\"set\\s+\\/a\\s+(?'k'ERROR_[^= ]+)\\s*=\\s*(?'v'\\d+)\",",
" (m) =>",
" {",
" errorsCodes[m.Groups[\"k\"].Value] = m.Groups[\"v\"].Value;",
" return string.Empty;",
" }",
" );",
"",
" foreach(var err in errorsCodes)",
" {",
" content = Regex.Replace(",
" content,",
" string.Format(\"({1}{0}{1}|{2}{0}{2})\", err.Key, \"%\", \"!\"),",
" err.Value",
" );",
" }",
"",
"",
" /* Shorten variables & labels */",
"",
" uint uniqVars = 0;",
" content = Regex.Replace",
" (",
" content,",
" @\"(?'def' ",
" set\\s+?",
" (?:\\/\\S\\s+?)?",
" (?:\"\"\\s*?)?",
" )",
" (?'name'\" + VNAME + @\")",
" (?'lim'\\s?\\S?=)\", // aq+=1,.. TODO: aq=aq+1 ; aq=1+aq ...",
" (m) =>",
" {",