1
+ //css_engine csc
2
+ //css_include global-usings
3
+ using static System . Console ;
4
+ using System . Diagnostics ;
5
+ using static System . Environment ;
6
+ using static System . Reflection . Assembly ;
7
+ using System . Text . Json ;
8
+ using System . Text . Json . Nodes ;
9
+
10
+ var arg1 = args . FirstOrDefault ( ) ;
11
+
12
+ if ( arg1 == "?" || arg1 == "/?" || arg1 == "-?" || arg1 == "-help" )
13
+ {
14
+ string version = Path . GetFileNameWithoutExtension (
15
+ Directory . GetFiles ( Path . GetDirectoryName ( GetEnvironmentVariable ( "EntryScript" ) ) , "*.version" )
16
+ . FirstOrDefault ( ) ?? "0.0.0.0.version" ) ;
17
+
18
+ WriteLine ( $@ "v{ version } ({ Environment . GetEnvironmentVariable ( "EntryScript" ) } )") ;
19
+ WriteLine ( $ "Sets the script engine target runtime to one of the available .NET ideployments.") ;
20
+ WriteLine ( $ " css -self-rt [-help] ") ;
21
+ return ;
22
+ }
23
+
24
+ // ===============================================
25
+
26
+ Console . WriteLine ( "Available .NET SDKs:" ) ;
27
+
28
+ var sdk_list = "dotnet" . run ( "--list-runtimes" ) // Microsoft.NETCore.App 9.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
29
+ . Split ( NewLine )
30
+ . Where ( line => ! string . IsNullOrWhiteSpace ( line ) )
31
+ . Select ( line => line . Split ( ' ' ) . Skip ( 1 ) . FirstOrDefault ( ) ? . Trim ( ) )
32
+ . DistinctBy ( line => line )
33
+ . OrderBy ( line => new Version ( line . Split ( '-' ) . FirstOrDefault ( ) ) ) // to split by `-` handle pre-releases (e.g. 10.0.0-preview.7.25380.108)
34
+ . Select ( ( line , i ) => new { Index = i + 1 , Version = line } )
35
+ . ToList ( ) ;
36
+
37
+ foreach ( var line in sdk_list )
38
+ {
39
+ WriteLine ( $ "{ line . Index } : { line . Version } ") ;
40
+ }
41
+
42
+ WriteLine ( ) ;
43
+ Write ( "Enter the index of the desired target SDK: " ) ;
44
+ var input = ReadLine ( ) ;
45
+
46
+ if ( int . TryParse ( input , out int index ) )
47
+ {
48
+ var i = index - 1 ;
49
+ if ( i < 0 || i >= sdk_list . Count )
50
+ {
51
+ WriteLine ( $ "Invalid index: { index } . Please enter a number between 1 and { sdk_list . Count } .") ;
52
+ return ;
53
+ }
54
+
55
+ WriteLine ( $ "{ NewLine } " +
56
+ $ "Setting target runtime{ NewLine } " +
57
+ $ " CLR: { sdk_list [ i ] . Version } { NewLine } " +
58
+ $ " Assembly: { GetEntryAssembly ( ) ? . Location } { NewLine } ") ;
59
+
60
+ var runtimeconfig = Path . ChangeExtension ( GetEntryAssembly ( ) . Location , ".runtimeconfig.json" ) ;
61
+
62
+ //update the .runtimeconfig.json file
63
+ if ( File . Exists ( runtimeconfig ) )
64
+ {
65
+ var changedConfig = runtimeconfig . UpdateFrameworkVersionTo ( sdk_list [ i ] . Version ) ;
66
+
67
+ WriteLine ( $ "The assembly's { Path . GetFileName ( runtimeconfig ) } has been updated:") ;
68
+ try
69
+ {
70
+ Console . ForegroundColor = ConsoleColor . Green ;
71
+ WriteLine ( changedConfig ) ;
72
+ }
73
+ finally
74
+ {
75
+ Console . ResetColor ( ) ;
76
+ }
77
+ }
78
+ else
79
+ {
80
+ WriteLine ( $ "Runtime configuration file not found: { runtimeconfig } ") ;
81
+ }
82
+ }
83
+ else
84
+ WriteLine ( $ "Invalid input: '{ input } '. Please enter a valid number.") ;
85
+
86
+ //===============================================
87
+ static class Extensions
88
+ {
89
+ public static string UpdateFrameworkVersionTo ( this string file , string version )
90
+ {
91
+ var ver = version . Split ( '.' ) ; //10.0.100-preview.7.25380.108 or 9.0.304
92
+ var tfm = $ "net{ ver [ 0 ] } .{ ver [ 1 ] } ";
93
+ var json = JsonSerializer . Deserialize < JsonObject > ( File . ReadAllText ( file ) ) ;
94
+ json [ "runtimeOptions" ] [ "tfm" ] = tfm ;
95
+ json [ "runtimeOptions" ] [ "framework" ] [ "version" ] = version ;
96
+ File . WriteAllText ( file , json . ToJsonString ( new JsonSerializerOptions { WriteIndented = true } ) ) ;
97
+
98
+ return json [ "runtimeOptions" ] . ToJsonString ( new JsonSerializerOptions { WriteIndented = true } ) ;
99
+ }
100
+
101
+ public static string run ( this string exe , string args , string dir = null )
102
+ {
103
+ var proc = new Process ( ) ;
104
+ proc . StartInfo . FileName = exe ;
105
+ proc . StartInfo . Arguments = args ;
106
+ proc . StartInfo . WorkingDirectory = dir ;
107
+ proc . StartInfo . RedirectStandardOutput = true ;
108
+ proc . Start ( ) ;
109
+ return proc . StandardOutput . ReadToEnd ( ) ? . Trim ( ) ;
110
+ }
111
+ }
0 commit comments