Repository navigation
Expand file tree
/
Copy pathhelp.txt
More file actions
1001 lines (775 loc) · 47.1 KB
/
Copy pathhelp.txt
File metadata and controls
1001 lines (775 loc) · 47.1 KB
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
C# Script execution engine (.NET Core). Version 4.14.14.0.
Copyright (C) 2004-2026 Oleg Shilo.
Usage: cscs <switch 1> <switch 2> <file> [params] [//x]
file Specifies name of a script file to be run.
params Specifies optional parameters for a script file to be run.
//x Launch debugger just before starting the script.
---------
<switch 1>
--help|-help|-? [<command>|<scope[:md]>|<-out:<file>>]
Displays either generic or command specific help info.
<command> - one of the supported CLI commands
<scope>
cli - print documentation for all CLI commands
syntax - print the complete documentation for scripting syntax
md - print the documentation in GitHub markdown format
(e.g. -help cli:md)
<file> - output file for the help content
Reversed order of parameters for the command specific help is also acceptable. The all following argument combinations print the
same help topic for 'cache' command:
-help cache
-? cache
-cache help
-cache ?
-e
Compiles script into console application executable.
Note this switch will force the use of dotnet compiler as csc is not capable of building executable assemblies. Thus
//css_engine and -engine options will always be ignored.
-ew
Compiles script into Windows application executable (applicable only on Windows).
Note this switch will force the use of dotnet compiler as csc is not capable of building executable assemblies. Thus
//css_engine and -engine options will always be ignored.
-rx
Run script as an external process. This option is incompatible with VB scripts.
This mode allows execution of the scripts that are otherwise incompatible with the runtime of the script engine (e.g. x86 vs x64,
.NETCore vs .NETFramework).
Note, this execution mode comes with some limitations (depending on the compilation engine):
- csc
The compiled script is executed with dotnet.exe launcher (e.g. dotnet script.dll).
- dotnet
The compiled script is executed directly as exe(e.g. script.exe).
- roslyn
This mode is not supported as Roslyn scripts do not support static main.
-c[:<0|1>]
Executes compiled script cache (e.g. <cache dir>/script.cs.dll) if found.
This command improves performance by avoiding compiling the script if it was not changed since last execution.
-c:1|-c - enable caching
-c:0 - disable caching (which might be enabled globally)
-ca
Compiles script file into cache file (e.g. <cache dir>/script.cs.dll).
-cd
Compiles script file into assembly (.dll) in the script folder without execution.
-check
Checks script for errors without execution.
-proj
Shows script 'project info' - script and all its dependencies.
-proj:out - create a Visual Studio project file for building the exe file from the script
An internal version of this command -proj:csproj can be also useful for troubleshooting
Visual Studio integration (see -vs command).
-vs <script>|-vs:init [index_of_detected_VS_executable]
Generates .NET project file and opens it in Visual Studio.
The path to the Visual Studio executable (devenv.exe) needs to be defined in the environment variable CSSCRIPT_VSEXE.
You can let CS-Script to detect installed Visual Studio executable and interactively select the detected executable for
integrating it with CS-Script by using -vs:init option.
Alternatively, you can even ask to integrate the first detected executable with:
cscs -vs:init 0
-vs:init 0 is also an instruction of selecting the latest version of detected Visual Studio executable since the list is of
available versions is ordered by the latest version first.
-vscode
Generates .NET project file and opens it in Visual Studio Code.
The path to the Visual Studio Code executable (code.exe) needs to be defined in the environment variable CSSCRIPT_VSCODEEXE.
-cache[:<ls|trim|clear>] [script]
Performs script cache operations.
ls - lists all cache items.
trim - removes all abandoned cache items.
clear - removes all cache items.
script - prints location of the cache folder for a given script.
-co:<options>
Passes compiler options directly to the language compiler (e.g. csc.exe or dotnet.exe).
Note, some compiler options may not be compatible if they are passed to the wrong compiler executable (see compiler
documentation). Though the fundamental switches like /platform:* are converted by CS-Script into compatible version between
csc.exe and dotnet.exe.
(e.g. -co:/d:TRACE pass /d:TRACE option to C# compiler
or -co:/platform:x86 to produce Win32 executable)
-ng|-engine:<csc|dotnet|roslyn>
Forces compilation to be done by one of the supported .NET engines.
dotnet - dotnet.exe compiler
This is the most versatile compilation engine though it does have a startup overhead when running the script for the first time.
It requires .NET SDK to be installed on the target system.
csc - csc.exe compiler; the fastest compiler available.
Though it is not suitable for WPF scripts as csc.exe cannot compile XAML.
It also requires .NET SDK to be installed on the target system.
This value is just a shorter form of the full equivalent csc-inproc.
Legacy:
Value csc-outproc triggers the compilation in the separate child process build.exe which is somewhat equivalent to
VBCSCompiler.exe from .NET toolset. CS-Script communicates with build.exe build server via socket (default port 17001).
You can control port value via the environment variable 'CSS_BUILDSERVER_CSC_PORT'.
This functionality was implemented before VBCSCompiler.exe became fully functional in the .NET Core family
distributions. And it's only retained for the compatibility reasons. Starting from v4.10.0 the use of csc-outproc
offers no benefits.
roslyn - Microsoft.CodeAnalysis.CSharp.Scripting.dll compiler
This is the most portable compilation engine. It does not require .NET SDK being installed.
Though it does have limitations. The script must have static Main(), you cannot have nested namespaces etc. (see documentation).
The compilation is performed in the separate child process cscs (another instance of script engine) which is somewhat
equivalent of VBCSCompiler.exe (build server) from .NET toolset.
CS-Script communicates with cscs build server via socket (default port 17002). You can control port value via the
environment variable 'CSS_BUILDSERVER_ROSLYN_PORT'
Value roslyn-inproc will suppress spinning off an external process and Roslyn compiler will be hosted in the original
process of script engine instead. This option is convenient when socket communication is undesirable for whatever
reason. Though in this case performance will be effected on the first run of the script.
(e.g. cscs -engine:dotnet sample.cs
cscs -ng:csc sample.cs
cscs -ng:roslyn-inproc sample.cs
cscs -ng:roslyn sample.cs)
-s|-sample[:<C# version>]
-s:7 - prints C# 7+ sample. Otherwise, it prints the default canonical 'Hello World' sample.
(e.g. cscs -s:7 > sample.cs).
-new[:<type>] [<script name>]
Creates a new script.
Usage: -new[:<type>] [<otput file>]
type - script template based on available types.
output - location to place the generated script file(s).
Type Template
---------------------------------------------------
console Console script application (Default)
console-vb Console VB script application
winform Windows Forms (WinForms) script application
winform-vb Windows Forms (WinForms) VB script application
wpf WPF script application
wpf-cm Caliburn.Micro based WPF script application
toplevel|top Top-level class script application with no entry point
toplevel-x Top-level class script application with no entry point; an advanced CS-Script integration samples.
cmd Custom command script. See https://github.com/oleg-shilo/cs-script/wiki/Custom-Commands.
Legacy templates:
auto Auto-class (classless) script application; use 'toplevel' instead
freestyle Freestyle (no entry point) script application; use 'toplevel' instead
Examples:
css -new script
css -new:toplevel script.cs
css -new:console console.cs
css -new:winform myapp.cs
css -new:wpf hello
css -new:cmd -edit
-code[:show] <script code>
Executes script code directly without using a script file.
Sample:
cscs -code "Console.WriteLine(Environment.UserDomainName);#nConsole.WriteLine(#''%USERNAME%#'');"
cscs -code "using System.Linq;#nSystem.Diagnostics.Process.GetProcessesByName(''notepad'').ToList().ForEach(x => x.Kill());"
cscs -code "SetEnvironmentVariable(ntp,notepad.exe, EnvironmentVariableTarget.Machine)"
The -code argument must be the last argument in the command. The only argument that is allowed after the <script code> is //x
Escaping special characters sometimes can be problematic as many shells have their own techniques (e.g. PowerShell collapses two
single quote characters) that may conflict with other shells' escaping approaches.This is the reason why CS-Script offers multiple
escape techniques.
It can be beneficial during the troubleshooting to use -code:show command that outputs the received CLI arguments and the
interpreted C# code without the execution.
CS-Script character escaping summary:
Escaped Interpreted character
-------------------------------------
#n -> <\n>
#r -> <\r>
#'' -> "
'' -> "
# -> "
-> "
n -> <\n>
r -> <\r>
-wait[:prompt]
Waits for user input after the execution before exiting.
If specified the execution will proceed with exit only after any std input is received.
Applicable for console mode only.
prompt - if none specified 'Press any key to continue...' will be used
-ac|-autoclass[:<0|1|2|out>]
Legacy command: executes scripts without class definition. Use top-level statements (C# 9) scripts instead.
-ac - enables auto-class decoration (which might be disabled globally).
-ac:0 - disables auto-class decoration (which might be enabled globally).
-ac:1 - same as '-ac'
-ac:2 - same as '-ac:1' and '-ac'
-ac:out - prints auto-class decoration for a given script file. The argument must be followed by the path to script file.
Automatically generates 'static entry point' class if the script doesn't define any.
using System;
void Main()
{
Console.WriteLine("Hello World!");
}
Using an alternative 'instance entry point' is even more convenient (and reliable).
The acceptable 'instance entry point' signatures are:
void main()
void main(string[] args)
int main()
int main(string[] args)
Note, having any active code above entry point is acceptable though it complicates the troubleshooting if such a code contains
errors. (see https://github.com/oleg-shilo/cs-script/wiki/CLI---User-Guide#command-auto-class)
By default CS-Script decorates the script by adding a class declaration statement to the start of the script routine and a class
closing bracket to the end. This may have an unintended effect as any class declared in the script becomes a 'nested class'. While
it is acceptable for practically all use-cases it may be undesired for just a few scenarios. For example, any class containing
method extensions must be a top-level static class, which conflicts with the auto-class decoration algorithm.
The solution to this problem is to allow some user code to be protected from being included in the decorated code.
Users can achieve this by placing '//css_ac_end' statement into the code. Any user code below this statement will be excluded from
the decoration and stay unchanged.
---------
<switch 2>
-dbg|-d
Forces compiler to include debug information.
-l[:<0|1>]
'local' (makes the script directory a 'current directory'). '1' is a default value.
Note, updating current directory this way is disabled for all custom command scripts.
-v|-ver|--version [output file]
Prints CS-Script version information.
-deploy-csc[:<sdk|fx|full|pre>]
Downloads and deploys C# compiler packages from NuGet.
Usage:
-deploy-csc - Deploy full toolset with references to teh system NuGet location (default)
-deploy-csc [path] - Deploy full SDK compiler toolset to specified folder
-deploy-csc:pre - Include pre-release versions
-deploy-csc:fx [path] - Deploy Framework compiler toolset
Examples:
css -deploy-csc
css -deploy-csc C:\Compilers
The deployed compiler can be used when .NET SDK is not installed.
-dbgprint[:<0:1>]
Controls whether to enable Python-like print methods (e.g. dbg.print(DateTime.Now)).
This setting allows controlling dynamic referencing of script engine assembly containing the implementation of Python-like print
methods dbg.print and derived extension methods object.print() and object.dup(). While dbg.print is extremely useful it can
and lead to some referencing challenges when the script being executed is referencing assemblies compiled with dbg.print already
included. The simplest way to solve this problem is to disable the dbg.cs inclusion.
-dbgprint:1 enable dbg.cs inclusion; Same as -dbgprint;
-dbgprint:0 disable dbg.cs inclusion;
-verbose
Prints runtime information during the script execution.
'-verbose2' additionally echoes compiling engine (e.g. csc.dll) input and output.
(applicable for console clients only)
-profile
Prints script loading performance information during the script execution.
-speed
Prints script initialization/compilation time information of the .NET compiler.
You can use -ng option ()
It is a convenient way of testing performance of the .NET distribution.
-install/-uninstall
Sets/unsets CSSCRIPT_ROOT environment variable to the location of the script engine being executed.
This environment variable is required for integration of CS-Script with Notepad++,Sublime Text and some other editors, which
require CS-Script installed on the host OS.
This command is only supported on Windows
-server[:<start|stop|restart|add|remove|ping>]
Prints the information about build server.
Note, the server starts automatically on the script execution that is configured to use the 'csc' or 'roslyn' engine.
Build server is a background process, which implements top loading of C# compiler csc.exe. Somewhat similar to VBCSCompiler.exe.
These options are only relevant if the compiler engine is set to 'csc' or roslyn (see '-engine' command).
Note, the build server is deployed on the first execution of the script with csc or roslyn engine.
Though on Linux it has to be an execution with root privileges. Or you can just run sudo css -server:add.
-server:start - deploys and starts build server. Useful if you want to start the server on system startup.
-server:stop - stops build server
-server:restart - restarts build server
-server:reset - stops, re-deploys and starts build server
-server:add - deploys build server
-server:remove - removes build server files. Useful for troubleshooting.
-server:ping - Pins running instance (if any) of the build server
The following options are only relevant if the compiler engine is set to 'roslyn' (see '-engine' command).
Roslyn based build server variant is much simpler so it only exposes start and stop interface.
-server_r:start - deploys and starts Roslyn build server
-server_r:stop - stops Roslyn build server
And this is how you can start and stop both Roslyn and csc build servers with a single command:
-servers:start - deploys and starts both Roslyn and csc build server
-servers:stop - stops both Roslyn and csc build server
-kill - a complete equivalent of -servers:stop
-netfx
Compile and execute the script on the latest .NET Framework compiler (csc.exe) found on the system.
The script will be automatically executed as an external process thus the value of the -rx switchwill be ignored.
-tc
Trace compiler input produced by CS-Script code provider CSSRoslynProvider.dll.
It's useful when troubleshooting custom compilers (e.g. Roslyn on Linux).
-wpf[:<enable|disable|1|0>]
Enables/disables WPF support on Windows by updating the framework name in the *.runtimeconfig.json file
-wpf - prints current enabled status
-wpf:<enable|1> - enables WPF support
-wpf:<disable|0> - disables WPF support
-config[:<option>]
Performs various CS-Script config operations
-config:none - ignores config file (uses default settings)
-config:create - creates config file with default settings
-config:default - prints default config file content
-config:<raw> - prints current config file content
-config[:ls] - lists/prints current config values
-config:<name> ? - prints help for the configuration value specified by name
-config:get:<name> - prints current config value
-config::<name> - the same as -config:get:name
-config:set:<name>=<value> - sets current config value
-config:set:<name>=add:<value> - updates the current config value content by appending the specified value.
-config:set:<name>=del:<value> - updates the current config value content by removing all occurrences of the specified value.
-config:<file> - uses custom config file
Note: The property name in -config:set and -config:set is case insensitive and can also contain '_' as a token separator that is
ignored during property lookup.
(e.g. cscs -config:none sample.cs
cscs -config:default > css_VB.xml
cscs -config:set:DefaultCompilerEngine=csc
cscs -config:set:DefaultArguments=add:-ac
cscs -config:set:default_arguments=del:-ac
cscs -config:c:\cs-script\css_VB.xml sample.vb)
-out[:<file>]
Forces the script to be compiled into a specific location.
Used only for very fine hosting tuning.
(e.g. cscs -out:%temp%\%pid%\sample.dll sample.cs
-r:<assembly 1>,<assembly N>
Uses explicitly referenced assembly.
It is required only for rare cases when namespace cannot be resolved into assembly.
(e.g. cscs /r:myLib.dll myScript.cs).
-dir:<directory 1>,<directory N>
Adds path(s) to the assembly probing directory list.
Directory probing order:
0: script dir
1: current dir
2: script host/engine dir
3: dirs from cmd args
4: dirs from code
5: dirs from config
6: csscript special dirs, e.g. cache dir
The precise runtime order can be discovered by executing the script with the -verbose switch.
You can use the reserved word 'show' as a directory name to print the configured probing directories.
(e.g. cscs -dir:C:\MyLibraries myScript.cs; cscs -dir:show).
-precompiler[:<file 1>,<file N>]
Specifies custom precompiler. This can be either script or assembly file.
Alias - pc[:<file 1>,<file N>][|entry-point]
You can specify precompiler as a C# (*.cs) file or as an assembly.
If no file(s) specified prints the code template for the custom precompiler. The special value 'print' has the same effect (e.g.
cscs -pc:print).
There is a special reserved word 'nodefault' to be used as a file name. It instructs script engine to prevent loading any built-in
precompilers like the one for removing shebang before the execution.
If it is desired, you may pass a specific class name for be used from the precompiler file (e.g. -pc:app.exe|MyPrecompiler).Though
the | character may interfere with your terminal. If it does, you wil need to switch to teh inlne precompile directive
//css_pc...
(see https://www.cs-script.net/cs-script/help-legacy/precompilers.html)
-pvdr|-provider:<file>
Location of the alternative/custom code provider assembly.
Alias - pvdr:<file>
If set it forces script engine to use an alternative code compiler.
C#7 support is implemented via Roslyn based provider: '-pvdr:CSSRoslynProvider.dll'.If the switch is not specified
CSSRoslynProvider.dll file will be use as a code provider if it is found in the same folder where the script engine is. Automatic
CSSRoslynProvider.dll loading can be disabled with a special 'none' argument: -pvdr:none.
(see https://www.cs-script.net/cs-script/help-legacy/help/non_cs_compilers.html)
-nuget[:restore]
Installs new or updates existing NuGet packages. It is a very close equivalent of dotnet restore command
Note, using nuget directives in the script text offers more flexibility (e.g. version support. See cscs -syntax //css_nuget
-nuget - prints the list of all root packages in the repository
-nuget:restore - Downloads and installs all packages specified in the script without executing the script. Effectively it is an
equivalent of -check but with the forced nuget packages restore operation.
Using this option is an alternative to having '//css_nuget -force ...' directive in the script code as it may be
a more convenient way of updating packages manually instead of having them updated on every script
execution/recompilation.
You can set CSS_RESTORE_DONOT_CLEAN environment variable to disable removing temporary folders created during
restore operations. This can be useful for troubleshooting NuGet packages restoring. ${<==}You
can also control how long you want to wait for packages to download.
Set environment variable CSS_NUGET_RESTORE_TIMEOUT for that. The default value is 180000 milliseconds.
---------------------------------------------
Note: A the current NuGet support model is available since v4.7.0. The old options of this -nuget command that are only
available with the legacy NuGet support algorithm. This mode can be enabled by setting LegacyNugetSupport option to false: css
-config:set:LegacyNugetSupport=true. Read more: https://github.com/oleg-shilo/cs-script/wiki/NuGet-Support
Legacy CLI:
-nuget[:<package|restore>]
This command allows a lightweight management of the NuGet packages. The functionality is limited to installing, restoring and
updating the packages.
-nuget - prints the list of all root packages in the repository
-nuget:<package> - downloads and installs the latest version of the package(s).
Wild cards can be used to update multiple packages. For example '-nuget:ServiceStack*' will update all already
installed ServiceStack packages.
You can also use the index of the package instead of its full name.
(Not available with new NuGet support)
-nuget:restore - Downloads and installs all packages specified in the script without executing the script.
-syntax
Prints documentation for CS-Script specific C# syntax.
-commands|-cmd[:x]
Prints list of supported commands (arguments) as well as the custom commands defined by user.
-cmd:x - Prints detailed information of the custom commands defined by user.
-list|-ls [<kill|k> | <kill-all|ka|*>
Prints list of all currently running scripts.
If script execution tracking is undesirable you can disable it by setting DisableCSScriptProcessTracking environment variable to a
non empty value.
kill|k - Allow user to select and terminate any running script.
* - Terminate all running scripts when 'kill' option is used.
(e.g. cscs -list kill * ).
-kill>
Terminates all instances of running scripts and CS-Script build services.
This command is the easiest way of ensuring no CS-Script files are locked.
E.g. before updating CS-Script installation.
---------
**************************************
CS-Script specific syntax
**************************************
Engine directives:
- //css_include <file>;
- //css_import <file>[, preserve_main][, rename_namespace(<oldName>, <newName>)];
- //css_nuget [-force] [-ver:<version>] [-pre|--prerelease] package0[,package1]..[,packageN];
- //css_args arg0[,arg1]..[,argN];
- //css_reference <file>;
- //css_precompiler <file 1>,<file 2>[|entry-point];
- //css_searchdir <directory>;
- //css_winapp
- //css_webapp
- //css_autoclass [style]
- //css_resource <file>[, <out_file>];
- //css_co <options>;
- //css_engine <csc|dotnet|roslyn>;
- //css_ignore_namespace <namespace>;
- //css_ac_end
- //css_prescript file([arg0][,arg1]..[,argN])[ignore];
- //css_postscript file([arg0][,arg1]..[,argN])[ignore];
------------------------------------
Engine directives can be controlled (enabled/disabled) with compiler conditional symbols and environment variables via the inline
#if syntax:
//css_include #if DEBUG debug_utils.cs
//css_dir #if (DEBUG) .\bin\Debug
//css_reference #if PRODUCTION_PC d:\temp\build\certificates.dll
------------------------------------
The script engine also always defines special compiler conditional symbol CS_SCRIPT:
#if CS_SCRIPT
Console.WriteLine("Running as a script...");
#endif
The script engine also defines another conditional symbol NETCORE to allow userto distinguish between executions under .NET (full)
and .NET Core
------------------------------------
//css_include <file>;
Alias - //css_inc
file - name of a script file to be included at compile-time.
This directive is available for both CLI and hosted script execution.
This directive is used to import/include one script into another one. It is a logical equivalent of '#include' in C++.
If a relative file path is specified with a single-dot prefix it will be automatically converted into the absolute path with respect
to the location of the script file containing the //css_include directive. Otherwise it will be resolved with respect to the process
current directory.
If for whatever reason it is preferred to always resolve path expression with respect to the parent script location you can configure
the script engine to do it with the following command:
cscs -config:set:ResolveRelativeFromParentScriptLocation = true
Note, if you use a wildcard in the imported script name (e.g. ./*_build.cs) the directive will only import from the first probing
directory where the matching file(s) is found. Be careful with the wide wildcard as '*.cs' as they may lead to unpredictable behavior.
For example they may match everything from the very first probing directory, which is typically a current directory. Using more
specific wildcards is arguably more practical (e.g. 'utils/*.cs', '*Helper.cs', './*.cs')
------------------------------------
//css_import <file>[, preserve_main][, rename_namespace(<oldName>, <newName>)];
Alias - //css_imp
This is a more specialized version of the default script importing directive //css_include (//css_inc) with some extra renaming
functionality.
While //css_include simply includes a script file in the execution as is, //css_import analyzes the file being imported and renames
namespaces and static Main(...) to avoid naming collisions. Thus you should use it only if you have naming collision problems.
file - name of a script file to be imported at compile-time.
preserve_main - do not rename 'static Main'.
.NET allows only one entry point 'static Main' method per application.Thus it is a problem if the primary and the
imported scripts both contain 'static Main'.To avoid this the script engine searches the imported script for
'static Main' method and renames it in 'i_Main' and then uses a temporary copy of the processed imported script
during the execution. If you need to use the imported script as is, then you should use 'preserve_main' argument
with the '//css_import' directive.
rename_namespace - rename namespace clause, it can appear in the directive multiple times
oldName - name of a namespace to be renamed during importing
newName - new name of a namespace to be renamed during importing
------------------------------------
//css_nuget [-force] [-ver:<version>] [-pre|--prerelease] package0[,package1]..[,packageN];
Note: .NET 10 file-based execution directive '#:package' is also supported by internally converting it to the //css_nuget equivalent.
Downloads/Installs the NuGet package. It also automatically references the downloaded package assemblies.
By default, the package is not downloaded again if it was already downloaded.
CS-Script uses dotnet.exe to manage NuGet packages. This makes the developer experience fully consistent with traditional SW
development of compiled .NET applications.
-force - switch to force individual packages downloading even when they were already downloaded.
<-ver|-v>:<version> - switch to download/reference a specific package version.
-pre|--prerelease - allows prerelease packages to be used.
Examples: //css_nuget cs-script;
//css_nuget -force -pre NLog
//css_nuget -pre Microsoft.SqlServer.SqlManagementObjects, Microsoft.SqlServer.ConnectionInfo
--- Legacy NuGet support ---
Before v4.7.0 CS-SCript was relying on nuget.exe, the only option available in .NET at that time.
To allow backwards compatibility this mode is still available and it can be enabled by setting LegacyNugetSupport configuration
value option to false with css -config:set:LegacyNugetSupport=true
Read more: https://github.com/oleg-shilo/cs-script/wiki/NuGet-Support
Note: Legacy NuGet support is less reliable, predictable or flexible (e.g. does not support package native assets) thus it's highly
recommend that you use this mode only if you have to.
The legacy NuGet support CLI is somewhat different:
(Legacy) - //css_nuget [-noref] [-force[:delay]] [-ver:<version>] [-rt:<runtime>] [-ng:<nuget arguments>]
package0[,package1]..[,packageN];
If no version is specified then the highest downloaded version (if any) will be used.
Referencing the downloaded packages can only handle simple dependency scenarios when all downloaded assemblies are to be referenced.
You should use '-noref' switch and reference assemblies manually for all other cases. For example multiple assemblies with the same
file name that target different CLRs (e.g. v3.5 vs v4.0) in the same package.
Switches:
-noref - switch for individual packages if automatic referencing isn't desired.
You can use 'css_nuget' environment variable for further referencing package content (e.g. //css_dir
%css_nuget%\WixSharp\**)
(Not available with new NuGet support)
-force[:delay] - switch to force individual packages downloading even when they were already downloaded.
You can optionally specify a delay for the next forced downloading by the number of seconds since last download.
'-force:3600' will delay it for one hour. This option is useful for preventing frequent download interruptions
during active script development.
(Not available with new NuGet support)
-ver:<version> - switch to download/reference a specific package version.
-rt:<runtime> - switch to use specific runtime binaries (e.g. '-rt:netstandard1.3').
(Not available with new NuGet support)
-ng:<args> - switch to pass nuget.exe/dotnet restore arguments for every individual package.
(-restore: as an alias of this switch)
Example: //css_nuget cs-script;
//css_nuget -restore:"-v minimal" -ver:4.1.2 NLog
//css_nuget -ver:4.1.2 -restore:"-f --no-cache" NLog
//css_nuget -ver:"4.1.1-rc1" -rt:netstandard2.0 -ng:"-f --no-cache" NLog
This directive will install CS-Script NuGet package.
(see http://www.csscript.net/help/script_nugets.html)
------------------------------------
//css_args arg0[,arg1]..[,argN];
Embedded script arguments. Both script and engine arguments are allowed except "/noconfig" engine command switch.
Example: //css_args -dbg, -inmem;
This directive will always force the script engine to execute the script in debug mode.
Note: the arguments must be coma separated.
------------------------------------
//css_reference <file>;
Alias - //css_ref
file - name of the assembly file to be loaded at run-time.
Note: .NET 10 file-based execution directive '#r' is also supported the same way as //css_reference.
This directive is used to reference assemblies required at run time.
The assembly must be in one of the search directories (in config file), the same folder with the script file or in the 'Script
Library' folders (see 'CS-Script settings').
Note if you use wildcard in the referenced assembly name (e.g. socket.*.dll) the directive will only reference from the first probing
directory where the matching file(s) is found. Be careful with the wide wildcard as '*.dll' as they may lead to unpredictable
behavior. For example they may match everything from the very first probing directory, which is typically a current directory. Using
more specific wildcards is arguably more practical (e.g. 'utils/*.dll', '*Helper.dll', './*.dll')
------------------------------------
//css_precompiler <file 1>,<file 2>[|entry-point];
Alias - //css_pc
file - name of the script or assembly file implementing precompiler.
This directive is used to specify the CS-Script precompilers (.cs or .dll) to be loaded and exercised against script at run time just
before compiling it. Precompilers are typically used to alter the script coder before the execution. Thus CS-Script uses built-in
precompiler to decorate classless scripts executed with -autoclass switch.
If it is desired, you may pass a specific class name for be used from the precompiler file (e.g. //css_pc app.exe|MyPrecompiler).
(see http://www.csscript.net/help/precompilers.html)
------------------------------------
//css_searchdir <directory>;
Alias - //css_dir
directory - name of the directory to be used for script and assembly probing at run-time.
This directive is used to extend the set of search directories (script and assembly probing).
The directory name can be a wildcard-=based expression. In such a case all directories matching the pattern will be this case all
directories will be probed.
The special case when the path ends with '**' is reserved to indicate 'sub directories' case. Examples:
//css_dir packages\ServiceStack*.1.0.21\lib\net40
//css_dir packages\**
Directory probing order:
0: script dir
1: current dir
2: script host/engine dir
3: dirs from cmd args
4: dirs from code
5: dirs from config
6: csscript special dirs, e.g. cache dir
The precise runtime order can be discovered by executing the script with the -verbose switch.
------------------------------------
//css_winapp
Alias - //css_winapp
Adds search directories required for running WinForm and WPF scripts.
Note: you need to use csws.exe engine to run WPF scripts.
Alternatively you can set environment variable 'CSS_WINAPP' to non empty value and css.exe shim will redirect the execution to the
csws.exe executable.
------------------------------------
//css_webapp
Alias - //css_webapp
Indicates that the script app needs to be compiled against Microsoft.AspNetCore.App framework.
A typical example is a WebAPI script application.
------------------------------------
//css_autoclass [style]
Alias - //css_ac
OBSOLETE, use top-class native C# 9 feature instead
Automatically generates 'static entry point' class if the script doesn't define any.
//css_ac
using System;
void Main()
{
Console.WriteLine("Hello World!");
}
Using an alternative 'instance entry point' is even more convenient (and reliable).
The acceptable 'instance entry point' signatures are:
void main()
void main(string[] args)
int main()
int main(string[] args)
The convention for the classless (auto-class) code structure is as follows:
- set of 'using' statements
- classless 'main'
- user code
- optional //css_ac_end directive
- optional user code that is not a subject of auto-class decoration
(see https://github.com/oleg-shilo/cs-script/wiki/CLI-Script-Execution#command-auto-class)
A special case of auto-class use case is a free style C# code that has no entry point 'main' at all:
//css_autoclass freestyle
using System;
Console.WriteLine(Environment.Version);
Since it's problematic to reliable auto-detect free style auto-classes, they must be defined with the special parameter 'freestyle'
after the '//css_ac' directive
By default, CS-Script decorates the script by adding a class declaration statement to the start of the script routine and a
class-closing bracket to the end. This may have an unintended effect as any class declared in the script becomes a 'nested class'.
While it is acceptable for practically all use-cases it may be undesired for just a few scenarios. For example, any class containing
method extensions must be a top-level static class, which conflicts with the auto-class decoration algorithm.
An additional '//css_autoclass_end' ('//css_ac_end') directive can be used to solve this problem.
It's nothing else but a marker indicating the end of the code that needs to be decorated as (wrapped into) an auto-class.
This directive allows defining top level static classes in the class-less scripts, which is required for implementing extension
methods.
//css_ac
using System;
void main()
{
...
}
//css_ac_end
static class Extensions
{
static public string Convert(this string text)
{
...
}
}
------------------------------------
//css_resource <file>[, <out_file>];
Alias - //css_res
file - name of the compiled resource file (.resources) to be used with the script.
Alternatively, it can be the name of the XML resource file (.resx) that will be compiled on-fly.
out_file - Optional name of the compiled resource file (.resources) to be generated form the .resx input.If not supplied then the
compiled file will have the same name as the input file but the file extension '.resx' changed to '.resources'.
This directive is used to reference resource file for script.
Example: //css_res Scripting.Form1.resources;
//css_res Resources1.resx;
//css_res Form1.resx, Scripting.Form1.resources;
------------------------------------
//css_co <options>;
options - options string.
This directive is used to pass compiler options string directly to the language-specific CLR compiling engine.
Note:
- the options may not be compatible with the compiling engine of your choice (see //css_engine).Thus //css_co /define:CS_SCRIPT will
work for csc engine but will not for dotnet since it does not support /define.
- character ; in compiler options interferes with //css_... directives so try to avoid it. Thus use -d:DEBUG -d:NET4 instead of
-d:DEBUG;NET4
Example: //css_co /d:TRACE pass /d:TRACE option to C# compiler
//css_co /platform:x86 to produce Win32 executable
//css_co -nullable:enable -warnaserror:nullable to enable nullable reference types.
------------------------------------
//css_engine <csc|dotnet|roslyn>;
Alias - //css_ng
This directive is used to select compiler services for building a script into an assembly.
dotnet - use dotnet.exe and on-fly .NET projects.
This is a default compiler engine that handles well even complicated heterogeneous multi-file scripts like WPF scripts.
csc - use csc.exe.
This compiler shows much better performance. Though it is not suitable for WPF scripts.
Using this option can in order of magnitude improve compilation speed. However it's not suitable for compiling WPF scripts
because csc.exe cannot compile XAML.
For csc more detailed information see css -ng ? help documentation.
roslyn - use Microsoft.CodeAnalysis.CSharp.Scripting.dll (Roslyn).
This compiler shows good performance and does not require .NET SDK. Though, it is not suitable for WPF scripts.
For roslyn-inproc version of this parameter see css -ng ? help documentation.
See [this wiki](https://github.com/oleg-shilo/cs-script/wiki/Choosing-Compiler-Engine) for details.
Example: //css_engine csc
------------------------------------
//css_ignore_namespace <namespace>;
Alias - //css_ignore_ns
namespace - name of the namespace. Use '*' to completely disable namespace resolution
This directive is used to prevent CS-Script from resolving the referenced namespace into the assembly.
------------------------------------
//css_ac_end
This directive is only applicable for class-less scripts executed with '-autoclass' CLI argument. It's nothing else but a marker
indicating the end of the code that needs to be decorated as (wrapped into) an auto-class.
This directive allows achieving top-level static classes in the class-less scripts, which is required for implementing extension
methods.
//css_args -autoclass
using System;
void main()
{
...
}
//css_ac_end
static class Extensions
{
static public void Convert(this string text)
{
...
}
}
------------------------------------
//css_prescript file([arg0][,arg1]..[,argN])[ignore];
Alias - //css_pre
file - script file (extension is optional)
arg0..N - script string arguments
ignore - continue execution of the main script in case of error
These directives are used to execute secondary pre-execution scripts.
If $this (or $this.name) is specified as arg0..N it will be replaced at execution time with the main script full name (or file name
only).
You may find that in many cases precompilers (//css_pc and -pc) are a more powerful and flexible alternative to the pre-execution
script.
------------------------------------
//css_postscript file([arg0][,arg1]..[,argN])[ignore];
Alias - //css_post
file - script file (extension is optional)
arg0..N - script string arguments
ignore - continue execution of the main script in case of error
These directives are used to execute secondary post-execution scripts.
If $this (or $this.name) is specified as arg0..N it will be replaced at execution time with the main script full name (or file name
only).
-------------------------------------
Note the script engine always sets the following environment variables:
pid - host processId (e.g. Environment.GetEnvironmentVariable("pid")
CSScriptRuntime - script engine version
CSScriptRuntimeLocation - script engine location
cscs_exe_dir - script engine directory
EntryScript - location of the entry script
EntryScriptAssembly - location of the compiled script assembly
location:<asm_hash> - location of the compiled script assembly.
This variable is particularly useful as it allows finding the compiled assembly file from the inside of the script code. Even when the
script loaded in-memory (InMemoryAssembly setting) but not from the original file. (e.g. var location =
Environment.GetEnvironmentVariable("location:" + Assembly.GetExecutingAssembly().GetHashCode());
Note that the default setting of 'location:<asm_hash>' is disabled. You can enable it by setting 'CSS_SCRIPTLOCATIONREFLECTION'
environment variable to non empty string.
The following is the optional set of environment variables that the script engine uses to improve the user experience:
'CSS_NUGET'
location of the NuGet packages, which scripts can load/reference
'CSSCRIPT_ROOT'
script engine location. Used by the engine to locate dependencies (e.g. resgen.exe). Typically this variable is set during the
CS-Script installation.
'CSSCRIPT_CONSOLE_ENCODING_OVERWRITE'
script engine output encoding if the one from the css_confix.xml needs to be overwritten.
'CSSCRIPT_INC'
a system-wide include directory for the all frequently used user scripts.
'CSSCRIPT_CSC_CMD_LOG'
the location of the log file that will be created during the script execution with the 'csc' compiler engine.the file will
contain the command line that is used to start csc.exe to compile the script. This behavior is useful for the advanced
debugging scenarios. Of the environment variable is not set or the value is not a valid file path then no log file will be
created.
'css_cli_roslyn_new_algorithm'
a non empty envar will trigger the use of a new algorithm of the Roslyn compiler in CLI scenarios.
This algorithm allows using a normal C# syntax that allows namespaces.
'css_csc_file'
the location of the csc.dll from the SDK folder. Useful if the default probing algorithm of the script engine cannot find the
right compiler file.
Ie, 'C:\Program Files\dotnet\sdk\10.0.103\Roslyn\bincore\csc.dll'.
'css_csc_asm_refs_dir'
the location of the csc.dll from the SDK folder. Useful if the default probing algorithm of the script engine cannot find the
right compiler file.
Ie, 'C:\Users\user\.nuget\packages\microsoft.AspNetCore.app.ref\10.0.3\ref\net10.0'.
'css_prefer_sdk_toolset_nuget'
controls whether SDK tools are preferred over the SDK NuGet Toolset package. Used when locating csc.exe/dll compilers.
---------
'css_csc_file'
the location of the csc.dll from the SDK folder.
During the script execution CS-Script always injects a little object inspector class 'dbg'. This class contains static printing
methods that mimic Python's 'print()'. It is particularly useful for object inspection in the absence of a proper debugger.
Examples:
dbg.print("Now:", DateTime.Now) - prints concatenated objects.
dbg.print(DateTime.Now) - prints object and values of its properties.
dbg.printf("Now: {0}", DateTime.Now) - formats and prints object and values of its fields and properties.
---------
Any directive has to be written as a single line in order to have no impact on compiling by CLI compliant compiler.It also must be
placed before any namespace or class declaration.
---------
Example:
//css_include web_api_host.cs;
//css_reference media_server.dll;
//css_nuget Newtonsoft.Json;
using System;
using static dbg;
class MediaServer
{
static void Main(string[] args)
{
print(args);
WebApi.SimpleHost(args)
.StartAsConosle("http://localhost:8080");
}
}
Or shorter form:
//css_args -ac
//css_inc web_api_host.cs
//css_ref media_server.dll
//css_nuget Newtonsoft.Json
using System;
void main(string[] args)
{
print(args);
WebApi.SimpleHost(args)
.StartAsConosle("http://localhost:8080");
}
---------
Project Website: https://github.com/oleg-shilo/cs-script