Skip to content

Commit 5cd3420

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

File tree

5 files changed

+128
-25
lines changed

5 files changed

+128
-25
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();")

src/cscs/cscs.csproj

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<AssemblyName>cscs</AssemblyName>
77
<RootNamespace>CSScripting</RootNamespace>
88
<StartupObject />
9-
<Version>4.11.0.0</Version>
9+
<Version>4.11.1.0</Version>
1010
<Authors>Oleg Shilo</Authors>
1111
<Product>CS-Script</Product>
1212
<Copyright>(C) 2004-2023 Oleg Shilo</Copyright>
@@ -16,23 +16,17 @@
1616
<RepositoryUrl>https://github.com/oleg-shilo/cs-script</RepositoryUrl>
1717
<RepositoryType>git</RepositoryType>
1818
<PackageTags>C# script dynamic</PackageTags>
19-
<AssemblyVersion>4.11.0.0</AssemblyVersion>
20-
<FileVersion>4.11.0.0</FileVersion>
19+
<AssemblyVersion>4.11.1.0</AssemblyVersion>
20+
<FileVersion>4.11.1.0</FileVersion>
2121
<PackageReleaseNotes>---
2222

2323
## Changes
2424

2525
### CLI
26-
- fixed problem with `css -ls &lt;kill-all|ka&gt;`
27-
- Added support for pre-release packages `//css_nuget -pre &lt;package&gt;`
28-
- WDBG:
29-
- Improved tracking of declaration scope
30-
- UX improvements
31-
- Inject dbg metadata is ported to pure Roslyn.
32-
- Detect debug metadata out of sync and report it (2)
26+
- Added `-self-rt` custom command for setting the script engine runtime version
3327

3428
### CSScriptLib
35-
- Added NativeAOT sample</PackageReleaseNotes>
29+
- no changes</PackageReleaseNotes>
3630
<PackageLicenseExpression>MIT</PackageLicenseExpression>
3731
<PackageIcon>css_logo.png</PackageIcon>
3832
<ApplicationIcon>css_logo.ico</ApplicationIcon>
@@ -120,6 +114,8 @@
120114
<Content Include="..\out\static_content\-pkill\**\*" Link="ToolPackage/-pkill" Pack="true" PackagePath="tools/net8.0/any/-pkill" />
121115
<Content Include="..\out\static_content\-runas\**\*" Link="ToolPackage/-runas" Pack="true" PackagePath="tools/net9.0/any/-runas" />
122116
<Content Include="..\out\static_content\-runas\**\*" Link="ToolPackage/-runas" Pack="true" PackagePath="tools/net8.0/any/-runas" />
117+
<Content Include="..\out\static_content\-scramble\**\*" Link="ToolPackage/-scramble" Pack="true" PackagePath="tools/net9.0/any/-scramble" />
118+
<Content Include="..\out\static_content\-scramble\**\*" Link="ToolPackage/-scramble" Pack="true" PackagePath="tools/net8.0/any/-scramble" />
123119
<Content Include="..\out\static_content\-self\**\*" Link="ToolPackage/-self" Pack="true" PackagePath="tools/net9.0/any/-self" />
124120
<Content Include="..\out\static_content\-self\**\*" Link="ToolPackage/-self" Pack="true" PackagePath="tools/net8.0/any/-self" />
125121
<Content Include="..\out\static_content\-set\**\*" Link="ToolPackage/-set" Pack="true" PackagePath="tools/net9.0/any/-set" />
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 runtimes:");
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 runtime version: ");
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.

src/release_notes.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
# Release v4.11.0.0
1+
# Release v4.11.1.0
22

33
---
44

55
## Changes
66

77
### CLI
8-
- fixed problem with `css -ls <kill-all|ka>`
9-
- Added support for pre-release packages `//css_nuget -pre <package>`
10-
- WDBG:
11-
- Improved tracking of declaration scope
12-
- UX improvements
13-
- Inject dbg metadata is ported to pure Roslyn.
14-
- Detect debug metadata out of sync and report it (2)
8+
- Added `-self-rt` custom command for setting the script engine runtime version
159

1610
### CSScriptLib
17-
- Added NativeAOT sample
11+
- no changes

0 commit comments

Comments
 (0)