-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainClass.cs
2572 lines (2272 loc) · 115 KB
/
MainClass.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
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
// MainClass.cs
//
// Copyright 2010 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Resources;
using System.Runtime.Serialization;
using System.Security;
using System.Text;
using System.Xml;
namespace Microsoft.Ajax.Utilities
{
/// <summary>
/// Application entry point
/// </summary>
public partial class MainClass
{
#region common fields
// default resource object name if not specified
private const string c_defaultResourceObjectName = "Strings";
/// <summary>
/// This field is initially false, and it set to true if any errors were
/// found parsing the javascript. The return value for the application
/// will be set to non-zero if this flag is true.
/// Use the -W argument to limit the severity of errors caught by this flag.
/// </summary>
private bool m_errorsFound;// = false;
/// <summary>
/// Set to true when header is written
/// </summary>
private bool m_headerWritten;
/// <summary>
/// list of preprocessor "defines" specified on the command-line
/// </summary>
private List<string> m_defines;
#endregion
#region common settings
// whether to clobber existing output files
private bool m_clobber; // = false
// Whether to allow embedded asp.net blocks.
private bool m_allowAspNet; // = false
/// <summary>
/// Bitfield for turning off individual AST modifications if so desired
/// </summary>
private long m_killSwitch;// = 0;
// simply echo the input code, not the crunched code
private bool m_echoInput;// = false;
// encoding to use on input
private string m_encodingInputName;// = null;
// encoding to use for output file
private string m_encodingOutputName;// = null;
// "pretty" indent size
private int m_indentSize = 4;
/// <summary>
/// File name of the source file or directory (if in recursive mode)
/// </summary>
private string[] m_inputFiles;// = null;
/// <summary>
/// Input type: JS or CSS
/// </summary>
private InputType m_inputType = InputType.Unknown;
/// <summary>
/// Output mode
/// </summary>
private ConsoleOutputMode m_outputMode = ConsoleOutputMode.Console;
/// <summary>
/// Whether or not we are outputting the crunched code to one or more files (false) or to stdout (true)
/// </summary>
private bool m_outputToStandardOut;// = false;
// output "pretty" instead of crunched
private bool m_prettyPrint;// = false;
/// <summary>
/// Optional file name of the destination file. Must be blank for in-place processing.
/// If not in-place, a blank destination output to STDOUT
/// </summary>
private string m_outputFile = string.Empty;
/// <summary>
/// Optional resource file name. This file should contain a single global variable
/// assignment using an object literal. The properties on that object are localizable
/// values that get replaced in the input files with the actual values.
/// </summary>
private string m_resourceFile = string.Empty;
/// <summary>
/// Optional name for the global resource object to be created from a .resx or .resources
/// file specified by the path in m_resourceFile
/// </summary>
private string m_resourceObjectName = string.Empty;
/// <summary>
/// This field is false by default. If it is set to true by an optional
/// command-line parameter, then the crunched stream will always be terminated
/// with a semi-colon.
/// </summary>
private bool m_terminateWithSemicolon;// = false;
// default warning level ignores warnings
private int m_warningLevel;// = 0;
/// <summary>
/// Optionally specify an XML file that indicates the input and output file(s)
/// instead of specifying a single output and the input file(s) on the command line.
/// </summary>
private string m_xmlInputFile;// = null;
#endregion
#region startup code
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static int Main(string[] args)
{
int retVal;
try
{
MainClass app = new MainClass(args);
retVal = app.Run();
}
catch (UsageException e)
{
Usage(e);
retVal = 1;
}
return retVal;
}
#endregion
#region Constructor
private MainClass(string[] args)
{
if (args != null && args.Length > 0)
{
// process the arguments
ProcessArgs(args);
}
else
{
// no args -- output the usage
throw new UsageException(ConsoleOutputMode.Console);
}
}
#endregion
#region ProcessArgs method
private void ProcessArgs(string[] args)
{
List<string> inputFiles = new List<string>();
bool levelSpecified = false;
bool renamingSpecified = false;
for (int ndx = 0; ndx < args.Length; ++ndx)
{
// parameter switch
string thisArg = args[ndx];
if (thisArg.Length > 1
&& (thisArg.StartsWith("-", StringComparison.Ordinal) // this is a normal hyphen (minus character)
|| thisArg.StartsWith("–", StringComparison.Ordinal) // this character is what Word will convert a hyphen to
|| thisArg.StartsWith("/", StringComparison.Ordinal)))
{
// general switch syntax is -switch:param
string[] parts = thisArg.Substring(1).Split(':');
string switchPart = parts[0].ToUpper(CultureInfo.InvariantCulture);
string paramPart = (parts.Length == 1 ? null : parts[1].ToUpper(CultureInfo.InvariantCulture));
// switch off the switch part
switch (switchPart)
{
case "ANALYZE":
case "A": // <-- old-style
// ignore any arguments
m_analyze = true;
break;
case "ASPNET":
BooleanSwitch(paramPart, switchPart, false, out m_allowAspNet);
break;
case "CC":
BooleanSwitch(paramPart, switchPart, true, out m_ignoreConditionalCompilation);
// actually, the flag is the opposite of the member -- turn CC ON and we DON'T
// want to ignore them; turn CC OFF and we DO want to ignore them
m_ignoreConditionalCompilation = !m_ignoreConditionalCompilation;
JavaScriptOnly();
break;
case "CLOBBER":
// just putting the clobber switch on the command line without any arguments
// is the same as putting -clobber:true and perfectly valid.
BooleanSwitch(paramPart, switchPart, true, out m_clobber);
break;
case "COLORS":
// two options: hex or names
if (paramPart == "HEX")
{
m_colorNames = CssColor.Hex;
}
else if (paramPart == "STRICT")
{
m_colorNames = CssColor.Strict;
}
else if (paramPart == "MAJOR")
{
m_colorNames = CssColor.Major;
}
else if (paramPart == null)
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
CssOnly();
break;
case "COMMENTS":
// four options for css: none, all, important, or hacks
// two options for js: none, important
// (default is important)
if (paramPart == "NONE")
{
m_cssComments = CssComment.None;
m_preserveImportantComments = false;
}
else if (paramPart == "ALL")
{
m_cssComments = CssComment.All;
CssOnly();
}
else if (paramPart == "IMPORTANT")
{
m_cssComments = CssComment.Important;
m_preserveImportantComments = true;
}
else if (paramPart == "HACKS")
{
m_cssComments = CssComment.Hacks;
CssOnly();
}
else if (paramPart == null)
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
break;
case "CSS":
// if we've already declared JS, then error
CssOnly();
break;
case "DEBUG":
// see if the param part is a comma-delimited list
if (paramPart.IndexOf(',') >= 0)
{
// we have a comma-separated list.
// the first item is the flag (if any), and the rest (if any) are the "debug" lookup names
// use parts[1] rather than paramParts because paramParts has been forced to upper-case
var items = parts[1].Split(',');
// use the first value as the debug boolean switch
BooleanSwitch(items[0], switchPart, true, out m_stripDebugStatements);
// and the rest as names.
// if we haven't created the list yet, do it now
if (m_debugLookups == null)
{
m_debugLookups = new List<string>();
}
// start with index 1, since index 0 was the flag
for (var item = 1; item < items.Length; ++item)
{
// get the identifier that was specified
var identifier = items[item];
// a blank identifier is okay -- we just ignore it
if (!string.IsNullOrEmpty(identifier))
{
// but if it's not blank, it better be a valid JavaScript identifier or member chain
if (identifier.IndexOf('.') > 0)
{
// it's a member chain -- check that each part is a valid JS identifier
var names = identifier.Split('.');
foreach (var name in names)
{
if (!JSScanner.IsValidIdentifier(name))
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", name, switchPart);
}
}
}
else
{
// no dot -- just an identifier
if (!JSScanner.IsValidIdentifier(identifier))
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", identifier, switchPart);
}
}
// don't add duplicates
if (!m_debugLookups.Contains(identifier))
{
m_debugLookups.Add(identifier);
}
}
}
}
else
{
// no commas -- just use the entire param part as the boolean value.
// just putting the debug switch on the command line without any arguments
// is the same as putting -debug:true and perfectly valid.
BooleanSwitch(paramPart, switchPart, true, out m_stripDebugStatements);
}
// actually the inverse - a TRUE on the -debug switch means we DON'T want to
// strip debug statements, and a FALSE means we DO want to strip them
m_stripDebugStatements = !m_stripDebugStatements;
// this is a JS-only switch
JavaScriptOnly();
break;
case "DEFINE":
// the parts can be a comma-separate list of identifiers
if (string.IsNullOrEmpty(paramPart))
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
// use paramPart because it has been forced to upper-case and these identifiers are
// supposed to be case-insensitive
foreach (string upperCaseName in paramPart.Split(','))
{
// better be a valid JavaScript identifier
if (!JSScanner.IsValidIdentifier(upperCaseName))
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", upperCaseName, switchPart);
}
// if we haven't created the list yet, do it now
if (m_defines == null)
{
m_defines = new List<string>();
}
// don't add duplicates
if (!m_defines.Contains(upperCaseName))
{
m_defines.Add(upperCaseName);
}
}
break;
case "ECHO":
case "I": // <-- old style
// ignore any arguments
m_echoInput = true;
// -pretty and -echo are not compatible
if (m_prettyPrint)
{
throw new UsageException(m_outputMode, "PrettyAndEchoArgs");
}
break;
case "ENC":
// the encoding is the next argument
if (ndx >= args.Length - 1)
{
// must be followed by an encoding
throw new UsageException(m_outputMode, "EncodingArgMustHaveEncoding", switchPart);
}
string encoding = args[++ndx];
// whether this is an in or an out encoding
if (paramPart == "IN")
{
// save the name -- we'll create the encoding later because we may
// override it on a file-by-file basis in an XML file
m_encodingInputName = encoding;
}
else if (paramPart == "OUT")
{
// just save the name -- we'll create the encoding later because we need
// to know whether we are JS or CSS to pick the right encoding fallback
m_encodingOutputName = encoding;
}
else if (paramPart == null)
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
break;
case "EVALS":
// three options: ignore, make immediate scope safe, or make all scopes safe
if (paramPart == "IGNORE")
{
m_evalTreatment = EvalTreatment.Ignore;
}
else if (paramPart == "IMMEDIATE")
{
m_evalTreatment = EvalTreatment.MakeImmediateSafe;
}
else if (paramPart == "SAFEALL")
{
m_evalTreatment = EvalTreatment.MakeAllSafe;
}
else if (paramPart == null)
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
// this is a JS-only switch
JavaScriptOnly();
break;
case "EXPR":
// two options: minify (default) or raw
if (paramPart == "MINIFY")
{
m_minifyExpressions = true;
}
else if (paramPart == "RAW")
{
m_minifyExpressions = false;
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
CssOnly();
break;
case "FNAMES":
// three options:
// LOCK -> keep all NFE names, don't allow renaming of function names
// KEEP -> keep all NFE names, but allow function names to be renamed
// ONLYREF -> remove unref'd NFE names, allow function named to be renamed (DEFAULT)
if (paramPart == "LOCK")
{
// don't remove function expression names
m_removeFunctionExpressionNames = false;
// and preserve the names (don't allow renaming)
m_preserveFunctionNames = true;
}
else if (paramPart == "KEEP")
{
// don't remove function expression names
m_removeFunctionExpressionNames = false;
// but it's okay to rename them
m_preserveFunctionNames = false;
}
else if (paramPart == "ONLYREF")
{
// remove function expression names if they aren't referenced
m_removeFunctionExpressionNames = true;
// and rename them if we so desire
m_preserveFunctionNames = false;
}
else if (paramPart == null)
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
// this is a JS-only switch
JavaScriptOnly();
break;
case "GLOBAL":
case "G": // <-- old style
// the parts can be a comma-separate list of identifiers
if (string.IsNullOrEmpty(paramPart))
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
// use parts[1] rather than paramParts because paramParts has been forced to upper-case
foreach (string global in parts[1].Split(','))
{
// better be a valid JavaScript identifier
if (!JSScanner.IsValidIdentifier(global))
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", global, switchPart);
}
// if we haven't created the list yet, do it now
if (m_globals == null)
{
m_globals = new List<string>();
}
// don't add duplicates
if (!m_globals.Contains(global))
{
m_globals.Add(global);
}
}
// this is a JS-only switch
JavaScriptOnly();
break;
case "HELP":
case "?":
// just show usage
throw new UsageException(m_outputMode);
case "INLINE":
// set safe for inline to the same boolean.
// if no param part, will return false (indicating the default)
// if invalid param part, will throw error
if (!BooleanSwitch(paramPart, switchPart, true, out m_safeForInline))
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
// this is a JS-only switch
JavaScriptOnly();
break;
case "JS":
// if we've already declared CSS, then error
JavaScriptOnly();
break;
case "KILL":
// optional integer switch argument
if (paramPart == null)
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
// get the numeric portion
if (paramPart.StartsWith("0X", StringComparison.OrdinalIgnoreCase))
{
// it's hex -- convert the number after the "0x"
if (!long.TryParse(paramPart.Substring(2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out m_killSwitch))
{
throw new UsageException(m_outputMode, "InvalidKillSwitchArg", paramPart);
}
}
else if (!long.TryParse(paramPart, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out m_killSwitch))
{
throw new UsageException(m_outputMode, "InvalidKillSwitchArg", paramPart);
}
break;
case "LITERALS":
// two options: keep or combine
if (paramPart == "KEEP")
{
m_combineDuplicateLiterals = false;
}
else if (paramPart == "COMBINE")
{
m_combineDuplicateLiterals = true;
}
else if (paramPart == "EVAL")
{
m_evalLiteralExpressions = true;
}
else if (paramPart == "NOEVAL")
{
m_evalLiteralExpressions = false;
}
else if (paramPart == null)
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
// this is a JS-only switch
JavaScriptOnly();
break;
case "MAC":
// optional boolean switch
// no arg is valid scenario (default is true)
BooleanSwitch(paramPart, switchPart, true, out m_macSafariQuirks);
// this is a JS-only switch
JavaScriptOnly();
break;
case "NEW":
// two options: keep and collapse
if (paramPart == "KEEP")
{
m_collapseToLiteral = false;
}
else if (paramPart == "COLLAPSE")
{
m_collapseToLiteral = true;
}
else if (paramPart == null)
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
// this is a JS-only switch
JavaScriptOnly();
break;
case "NFE": // <-- deprecate; use FNAMES option instead
if (paramPart == "KEEPALL")
{
m_removeFunctionExpressionNames = false;
}
else if (paramPart == "ONLYREF")
{
m_removeFunctionExpressionNames = true;
}
else if (paramPart == null)
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
// this is a JS-only switch
JavaScriptOnly();
break;
case "NORENAME":
// the parts can be a comma-separate list of identifiers
if (string.IsNullOrEmpty(paramPart))
{
throw new UsageException(m_outputMode, "SwitchRequiresArg", switchPart);
}
// use parts[1] rather than paramParts because paramParts has been forced to upper-case
foreach (string ident in parts[1].Split(','))
{
// better be a valid JavaScript identifier
if (!JSScanner.IsValidIdentifier(ident))
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", ident, switchPart);
}
// if we haven't created the list yet, do it now
if (m_noAutoRename == null)
{
m_noAutoRename = new List<string>();
}
// don't add duplicates
if (!m_noAutoRename.Contains(ident))
{
m_noAutoRename.Add(ident);
}
}
// this is a JS-only switch
JavaScriptOnly();
break;
case "OUT":
case "O": // <-- old style
// next argument is the output path
// cannot have two out arguments
if (!string.IsNullOrEmpty(m_outputFile))
{
throw new UsageException(m_outputMode, "MultipleOutputArg");
}
else if (ndx >= args.Length - 1)
{
throw new UsageException(m_outputMode, "OutputArgNeedsPath");
}
m_outputFile = args[++ndx];
break;
case "PPONLY":
// just putting the pponly switch on the command line without any arguments
// is the same as putting -pponly:true and perfectly valid.
BooleanSwitch(paramPart, switchPart, true, out m_preprocessOnly);
// this is a JS-only switch
JavaScriptOnly();
break;
case "PRETTY":
case "P": // <-- old style
m_prettyPrint = true;
// pretty-print and echo-input not compatible
if (m_echoInput)
{
throw new UsageException(m_outputMode, "PrettyAndEchoArgs");
}
// if renaming hasn't been specified yet, turn it off for prety-print
if (!renamingSpecified)
{
m_localRenaming = LocalRenaming.KeepAll;
}
// optional integer switch argument
if (paramPart != null)
{
// get the numeric portion
try
{
// must be an integer value
int indentSize = int.Parse(paramPart, CultureInfo.InvariantCulture);
if (indentSize >= 0)
{
m_indentSize = indentSize;
}
else
{
throw new UsageException(m_outputMode, "InvalidTabSizeArg", paramPart);
}
}
catch (FormatException e)
{
Debug.WriteLine(e.ToString());
throw new UsageException(m_outputMode, "InvalidTabSizeArg", paramPart);
}
}
break;
case "RENAME":
if (paramPart == null)
{
// there are no other parts after -rename
// the next argument should be a filename from which we can pull the
// variable renaming information
if (ndx >= args.Length - 1)
{
// must be followed by an encoding
throw new UsageException(m_outputMode, "RenameArgMissingParameterOrFilePath", switchPart);
}
// the renaming file is specified as the NEXT argument
string renameFilePath = args[++ndx];
// and it needs to exist
EnsureInputFileExists(renameFilePath);
// process the renaming file
ProcessRenamingFile(renameFilePath);
}
else if (paramPart.IndexOf('=') > 0)
{
// there is at least one equal sign -- treat this as a set of JS identifier
// pairs. split on commas -- multiple pairs can be specified
var paramPairs = parts[1].Split(',');
foreach (var paramPair in paramPairs)
{
// split on the equal sign -- each pair needs to have an equal sige
var pairParts = paramPair.Split('=');
if (pairParts.Length == 2)
{
// there is an equal sign. The first part is the source name and the
// second part is the new name to which to rename those entities.
string fromIdentifier = pairParts[0];
string toIdentifier = pairParts[1];
// make sure both parts are valid JS identifiers
var fromIsValid = JSScanner.IsValidIdentifier(fromIdentifier);
var toIsValid = JSScanner.IsValidIdentifier(toIdentifier);
if (fromIsValid && toIsValid)
{
// create the map if it hasn't been created yet.
if (m_renameMap == null)
{
m_renameMap = new Dictionary<string, string>();
}
m_renameMap.Add(fromIdentifier, toIdentifier);
}
else if (fromIsValid)
{
// the toIdentifier is invalid!
throw new UsageException(m_outputMode, "InvalidRenameToIdentifier", toIdentifier);
}
else if (toIsValid)
{
// the fromIdentifier is invalid!
throw new UsageException(m_outputMode, "InvalidRenameFromIdentifier", fromIdentifier);
}
else
{
// NEITHER of the rename parts are valid identifiers! BOTH are required to
// be valid JS identifiers
throw new UsageException(m_outputMode, "InvalidRenameIdentifiers", fromIdentifier, toIdentifier);
}
}
else
{
// either zero or more than one equal sign. Invalid.
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
}
}
else
{
// no equal sign; just a plain option
// three options: all, localization, none
if (paramPart == "ALL")
{
m_localRenaming = LocalRenaming.CrunchAll;
// automatic renaming strategy has been specified by this option
renamingSpecified = true;
}
else if (paramPart == "LOCALIZATION")
{
m_localRenaming = LocalRenaming.KeepLocalizationVars;
// automatic renaming strategy has been specified by this option
renamingSpecified = true;
}
else if (paramPart == "NONE")
{
m_localRenaming = LocalRenaming.KeepAll;
// automatic renaming strategy has been specified by this option
renamingSpecified = true;
}
else if (paramPart == "NOPROPS")
{
// manual-renaming does not change property names
m_renameProperties = false;
}
else
{
throw new UsageException(m_outputMode, "InvalidSwitchArg", paramPart, switchPart);
}
}
// this is a JS-only switch
JavaScriptOnly();
break;
case "REORDER":
// default is true
BooleanSwitch(paramPart, switchPart, true, out m_reorderScopeDeclarations);
// this is a JS-only switch
JavaScriptOnly();
break;
case "RES":
case "R": // <-- old style
// -res:id path
// can't specify -R more than once
if (!string.IsNullOrEmpty(m_resourceFile))
{
throw new UsageException(m_outputMode, "MultipleResourceArgs");
}
// must have resource file on next parameter
if (ndx >= args.Length - 1)
{
throw new UsageException(m_outputMode, "ResourceArgNeedsPath");
}
// the resource file name is the NEXT argument
m_resourceFile = args[++ndx];
EnsureInputFileExists(m_resourceFile);
// check the extension to see if the resource file is a supported file type.
switch (Path.GetExtension(m_resourceFile).ToUpper(CultureInfo.InvariantCulture))
{
case ".RESOURCES":
case ".RESX":
if (!string.IsNullOrEmpty(paramPart))
{
// reset paramPart to not be the forced-to-upper version
paramPart = parts[1];
// must be valid JS identifier
if (JSScanner.IsValidIdentifier(paramPart))
{
// save the name portion
m_resourceObjectName = paramPart;
}
else
{
throw new UsageException(m_outputMode, "ResourceArgInvalidName", paramPart);
}
}
else
{
// no name specified -- use Strings as the default
// (not recommended)
m_resourceObjectName = c_defaultResourceObjectName;
}
break;
default:
// not a supported resource file type
throw new UsageException(m_outputMode, "ResourceArgInvalidType");
}
break;
case "SILENT":
case "S": // <-- old style
// ignore any argument part
m_outputMode = ConsoleOutputMode.Silent;
break;
case "TERM":
// optional boolean argument, defaults to true
BooleanSwitch(paramPart, switchPart, true, out m_terminateWithSemicolon);
break;
case "UNUSED":
// two options: keep and remove
if (paramPart == "KEEP")
{
m_removeUnneededCode = false;
}
else if (paramPart == "REMOVE")
{
m_removeUnneededCode = true;
}
else if (paramPart == null)