Skip to content

Commit 28b4eb2

Browse files
committed
CLI:
- Added `-self-rt` command for setting the script engine runtime version
1 parent 4053035 commit 28b4eb2

File tree

3 files changed

+118
-5
lines changed

3 files changed

+118
-5
lines changed

src/cscs/HelpProvider.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,7 @@ static SampleInfo[] CSharp_auto_Sample(string context)
20022002
.AppendLine("{")
20032003
.AppendLine(" (string message, int version) setup_say_hello()")
20042004
.AppendLine(" {")
2005-
.AppendLine(" return (\"Hello from C#\", 9);")
2005+
.AppendLine(" return (\"Hello from C#\", 10);")
20062006
.AppendLine(" }")
20072007
.AppendLine("")
20082008
.AppendLine(" var info = setup_say_hello();")
@@ -2038,12 +2038,13 @@ static SampleInfo[] CSharp10_Sample(string context)
20382038
builder
20392039
.AppendLine()
20402040
.AppendLine("\"------------------------------------\".print();")
2041-
.AppendLine("Console.WriteLine($\"Date: {DateTime.Now}\");")
2041+
.AppendLine("Console.WriteLine($\"CLR: v{Environment.Version}\");")
2042+
.AppendLine()
20422043
.AppendLine("(string message, int version) setup_say_hello()")
20432044
.AppendLine("{")
2044-
.AppendLine(" return (\"Hello from C#\", 9);")
2045+
.AppendLine(" return (\"Hello from C#\", 10);")
20452046
.AppendLine("}")
2046-
.AppendLine("")
2047+
.AppendLine()
20472048
.AppendLine("var info = setup_say_hello();")
20482049
.AppendLine()
20492050
.AppendLine("print(info.message, info.version);")
@@ -2083,7 +2084,7 @@ static SampleInfo[] CSharp_console_Sample(string context)
20832084
.AppendLine(" {")
20842085
.AppendLine(" (string message, int version) setup_say_hello()")
20852086
.AppendLine(" {")
2086-
.AppendLine(" return (\"Hello from C#\", 9);")
2087+
.AppendLine(" return (\"Hello from C#\", 10);")
20872088
.AppendLine(" }")
20882089
.AppendLine("")
20892090
.AppendLine(" var info = setup_say_hello();")
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Sets the specified assembly(s) target runtime to the currently active .NET configuration.

0 commit comments

Comments
 (0)