Skip to content

Commit 7908288

Browse files
Update README.md
1 parent 0749be3 commit 7908288

File tree

3 files changed

+243
-15
lines changed

3 files changed

+243
-15
lines changed

README.md

Lines changed: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,129 @@
44

55
![](docs/csharp_cat.png)
66

7-
This is a repository of dotnet-csi which is an interactive tool for running C# scripts. It can installed as a command-line tool on Windows, Linux, or macOS.
7+
Example of usage:
88

9-
## Prerequisites
9+
```c#
10+
// Output API
11+
WriteLine("Hello");
12+
WriteLine("Hello !!!", Color.Highlighted);
13+
Error("Error details", "ErrorId");
14+
Warning("Warning");
15+
Info("Some info");
16+
Trace("Trace message");
17+
18+
// API for arguments and parameters
19+
Info("First argument: " + (Args.Count > 0 ? Args[0] : "empty"));
20+
Info("Version: " + Props.Get("version", "1.0.0"));
21+
Props["version"] = "1.0.1";
22+
23+
var configuration = Props.Get("configuration", "Release");
24+
Info($"Configuration: {configuration}");
25+
26+
// Command line API
27+
var cmd = new CommandLine("whoami");
28+
29+
cmd.Run().EnsureSuccess();
30+
31+
// Asynchronous way
32+
await cmd.RunAsync().EnsureSuccess();
33+
34+
// API for Docker CLI
35+
await new DockerRun("ubuntu")
36+
.WithCommandLine(cmd)
37+
.WithPull(DockerPullType.Always)
38+
.WithAutoRemove(true)
39+
.RunAsync()
40+
.EnsureSuccess();
41+
42+
// Microsoft DI API to resolve dependencies
43+
var nuget = GetService<INuGet>();
44+
45+
// Creating a custom service provider
46+
var serviceCollection = GetService<IServiceCollection>();
47+
serviceCollection.AddSingleton<MyTool>();
48+
49+
var myServiceProvider = serviceCollection.BuildServiceProvider();
50+
var tool = myServiceProvider.GetRequiredService<MyTool>();
51+
52+
// API for NuGet
53+
var settings = new NuGetRestoreSettings("MySampleLib")
54+
.WithVersionRange(VersionRange.Parse("[1.0.14, 1.1)"))
55+
.WithTargetFrameworkMoniker("net6.0")
56+
.WithPackagesPath(".packages");
57+
58+
var packages = nuget.Restore(settings);
59+
foreach (var package in packages)
60+
{
61+
Info(package.Path);
62+
}
63+
64+
// API for .NET CLI
65+
var buildResult = new DotNetBuild().WithConfiguration(configuration).WithNoLogo(true)
66+
.Build().EnsureSuccess();
1067

11-
The tool requires [.NET 6+ runtime](https://dotnet.microsoft.com/en-us/download).
68+
var warnings = buildResult.Warnings
69+
.Where(warn => Path.GetFileName(warn.File) == "Calculator.cs")
70+
.Select(warn => $"{warn.Code}({warn.LineNumber}:{warn.ColumnNumber})")
71+
.Distinct();
72+
73+
foreach (var warning in warnings)
74+
{
75+
await new HttpClient().GetAsync(
76+
"https://api.telegram.org/bot7102686717:AAEHw7HZinme_5kfIRV7TwXK4Xql9WPPpM3/" +
77+
"sendMessage?chat_id=878745093&text="
78+
+ HttpUtility.UrlEncode(warning));
79+
}
80+
81+
// Asynchronous way
82+
var cts = new CancellationTokenSource();
83+
await new DotNetTest()
84+
.WithConfiguration(configuration)
85+
.WithNoLogo(true)
86+
.WithNoBuild(true)
87+
.BuildAsync(CancellationOnFirstFailedTest, cts.Token)
88+
.EnsureSuccess();
89+
90+
void CancellationOnFirstFailedTest(BuildMessage message)
91+
{
92+
if (message.TestResult is { State: TestState.Failed }) cts.Cancel();
93+
}
94+
95+
// Parallel tests
96+
var tempDir = Directory.CreateTempSubdirectory();
97+
try
98+
{
99+
new DotNetPublish()
100+
.WithConfiguration(configuration)
101+
.WithNoLogo(true)
102+
.WithNoBuild(true)
103+
.WithFramework("net8.0")
104+
.AddProps(("PublishDir", tempDir.FullName))
105+
.Build().EnsureSuccess();
106+
107+
var test = new VSTest().WithTestFileNames("*.Tests.dll");
108+
109+
var tasks = from tagSuffix in new[] {"bookworm-slim", "alpine", "noble"}
110+
let image = $"mcr.microsoft.com/dotnet/sdk:8.0-{tagSuffix}"
111+
let dockerRun = new DockerRun(image)
112+
.WithCommandLine(test)
113+
.WithAutoRemove(true)
114+
.WithVolumes((tempDir.FullName, "/app"))
115+
.WithContainerWorkingDirectory("/app")
116+
select dockerRun.BuildAsync(CancellationOnFirstFailedTest, cts.Token);
117+
118+
await Task.WhenAll(tasks).EnsureSuccess();
119+
}
120+
finally { tempDir.Delete(); }
121+
122+
class MyTool(INuGet nuGet);
123+
```
12124

13125
## Usage
14126

15-
### Script runner
127+
### Script runner tool
128+
129+
It can be installed as a command-line tool on Windows, Linux, or macOS. The dotnet tool requires [.NET 6+ runtime](https://dotnet.microsoft.com/en-us/download).
16130

17131
After installing tool you can use this tool to run C# scripts from the command line. dotnet-csi is available as a [NuGet package](https://www.nuget.org/packages/dotnet-csi/).
18132

@@ -76,12 +190,12 @@ Supported arguments:
76190

77191
```using HostApi;``` directive in a script allows you to use host API types without specifying the fully qualified namespace of these types.
78192

79-
### .NET application
193+
### .NET project
80194

81195
Install the C# script template [CSharpInteractive.Templates](https://www.nuget.org/packages/CSharpInteractive.Templates)
82196

83197
```shell
84-
dotnet new -i CSharpInteractive.Templates
198+
dotnet new install CSharpInteractive.Templates
85199
```
86200

87201
Create a console project "Build" containing a script from the template *__build__*

README_BODY.md

Lines changed: 122 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,129 @@
44

55
![](docs/csharp_cat.png)
66

7-
This is a repository of dotnet-csi which is an interactive tool for running C# scripts. It can installed as a command-line tool on Windows, Linux, or macOS.
8-
9-
## Prerequisites
10-
11-
The tool requires [.NET 6+ runtime](https://dotnet.microsoft.com/en-us/download).
7+
Example of usage:
8+
9+
```c#
10+
// Output API
11+
WriteLine("Hello");
12+
WriteLine("Hello !!!", Color.Highlighted);
13+
Error("Error details", "ErrorId");
14+
Warning("Warning");
15+
Info("Some info");
16+
Trace("Trace message");
17+
18+
// API for arguments and parameters
19+
Info("First argument: " + (Args.Count > 0 ? Args[0] : "empty"));
20+
Info("Version: " + Props.Get("version", "1.0.0"));
21+
Props["version"] = "1.0.1";
22+
23+
var configuration = Props.Get("configuration", "Release");
24+
Info($"Configuration: {configuration}");
25+
26+
// Command line API
27+
var cmd = new CommandLine("whoami");
28+
29+
cmd.Run().EnsureSuccess();
30+
31+
// Asynchronous way
32+
await cmd.RunAsync().EnsureSuccess();
33+
34+
// API for Docker CLI
35+
await new DockerRun("ubuntu")
36+
.WithCommandLine(cmd)
37+
.WithPull(DockerPullType.Always)
38+
.WithAutoRemove(true)
39+
.RunAsync()
40+
.EnsureSuccess();
41+
42+
// Microsoft DI API to resolve dependencies
43+
var nuget = GetService<INuGet>();
44+
45+
// Creating a custom service provider
46+
var serviceCollection = GetService<IServiceCollection>();
47+
serviceCollection.AddSingleton<MyTool>();
48+
49+
var myServiceProvider = serviceCollection.BuildServiceProvider();
50+
var tool = myServiceProvider.GetRequiredService<MyTool>();
51+
52+
// API for NuGet
53+
var settings = new NuGetRestoreSettings("MySampleLib")
54+
.WithVersionRange(VersionRange.Parse("[1.0.14, 1.1)"))
55+
.WithTargetFrameworkMoniker("net6.0")
56+
.WithPackagesPath(".packages");
57+
58+
var packages = nuget.Restore(settings);
59+
foreach (var package in packages)
60+
{
61+
Info(package.Path);
62+
}
63+
64+
// API for .NET CLI
65+
var buildResult = new DotNetBuild().WithConfiguration(configuration).WithNoLogo(true)
66+
.Build().EnsureSuccess();
67+
68+
var warnings = buildResult.Warnings
69+
.Where(warn => Path.GetFileName(warn.File) == "Calculator.cs")
70+
.Select(warn => $"{warn.Code}({warn.LineNumber}:{warn.ColumnNumber})")
71+
.Distinct();
72+
73+
foreach (var warning in warnings)
74+
{
75+
await new HttpClient().GetAsync(
76+
"https://api.telegram.org/bot7102686717:AAEHw7HZinme_5kfIRV7TwXK4Xql9WPPpM3/" +
77+
"sendMessage?chat_id=878745093&text="
78+
+ HttpUtility.UrlEncode(warning));
79+
}
80+
81+
// Asynchronous way
82+
var cts = new CancellationTokenSource();
83+
await new DotNetTest()
84+
.WithConfiguration(configuration)
85+
.WithNoLogo(true)
86+
.WithNoBuild(true)
87+
.BuildAsync(CancellationOnFirstFailedTest, cts.Token)
88+
.EnsureSuccess();
89+
90+
void CancellationOnFirstFailedTest(BuildMessage message)
91+
{
92+
if (message.TestResult is { State: TestState.Failed }) cts.Cancel();
93+
}
94+
95+
// Parallel tests
96+
var tempDir = Directory.CreateTempSubdirectory();
97+
try
98+
{
99+
new DotNetPublish()
100+
.WithConfiguration(configuration)
101+
.WithNoLogo(true)
102+
.WithNoBuild(true)
103+
.WithFramework("net8.0")
104+
.AddProps(("PublishDir", tempDir.FullName))
105+
.Build().EnsureSuccess();
106+
107+
var test = new VSTest().WithTestFileNames("*.Tests.dll");
108+
109+
var tasks = from tagSuffix in new[] {"bookworm-slim", "alpine", "noble"}
110+
let image = $"mcr.microsoft.com/dotnet/sdk:8.0-{tagSuffix}"
111+
let dockerRun = new DockerRun(image)
112+
.WithCommandLine(test)
113+
.WithAutoRemove(true)
114+
.WithVolumes((tempDir.FullName, "/app"))
115+
.WithContainerWorkingDirectory("/app")
116+
select dockerRun.BuildAsync(CancellationOnFirstFailedTest, cts.Token);
117+
118+
await Task.WhenAll(tasks).EnsureSuccess();
119+
}
120+
finally { tempDir.Delete(); }
121+
122+
class MyTool(INuGet nuGet);
123+
```
12124

13125
## Usage
14126

15-
### Script runner
127+
### Script runner tool
128+
129+
It can be installed as a command-line tool on Windows, Linux, or macOS. The dotnet tool requires [.NET 6+ runtime](https://dotnet.microsoft.com/en-us/download).
16130

17131
After installing tool you can use this tool to run C# scripts from the command line. dotnet-csi is available as a [NuGet package](https://www.nuget.org/packages/dotnet-csi/).
18132

@@ -76,12 +190,12 @@ Supported arguments:
76190

77191
```using HostApi;``` directive in a script allows you to use host API types without specifying the fully qualified namespace of these types.
78192

79-
### .NET application
193+
### .NET project
80194

81195
Install the C# script template [CSharpInteractive.Templates](https://www.nuget.org/packages/CSharpInteractive.Templates)
82196

83197
```shell
84-
dotnet new -i CSharpInteractive.Templates
198+
dotnet new install CSharpInteractive.Templates
85199
```
86200

87201
Create a console project "Build" containing a script from the template *__build__*

Samples/MySampleLib/Build/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
+ HttpUtility.UrlEncode(warning));
7575
}
7676

77-
// Asynchronous wayS
77+
// Asynchronous way
7878
var cts = new CancellationTokenSource();
7979
/*await new DotNetTest()
8080
.WithConfiguration(configuration)

0 commit comments

Comments
 (0)