Skip to content

Commit 8770d90

Browse files
Update README.md
1 parent 0749be3 commit 8770d90

File tree

3 files changed

+249
-15
lines changed

3 files changed

+249
-15
lines changed

README.md

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,132 @@
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+
```c#
8+
using System.Web;
9+
using HostApi;
10+
using Microsoft.Extensions.DependencyInjection;
11+
using NuGet.Versioning;
12+
13+
// Output API
14+
WriteLine("Hello");
15+
WriteLine("Hello !!!", Color.Highlighted);
16+
Error("Error details", "ErrorId");
17+
Warning("Warning");
18+
Info("Some info");
19+
Trace("Trace message");
20+
21+
// API for arguments and parameters
22+
Info("First argument: " + (Args.Count > 0 ? Args[0] : "empty"));
23+
Info("Version: " + Props.Get("version", "1.0.0"));
24+
Props["version"] = "1.0.1";
25+
26+
var configuration = Props.Get("configuration", "Release");
27+
Info($"Configuration: {configuration}");
28+
29+
// Command line API
30+
var cmd = new CommandLine("whoami");
31+
32+
cmd.Run().EnsureSuccess();
33+
34+
// Asynchronous way
35+
await cmd.RunAsync().EnsureSuccess();
36+
37+
// API for Docker CLI
38+
await new DockerRun("ubuntu")
39+
.WithCommandLine(cmd)
40+
.WithPull(DockerPullType.Always)
41+
.WithAutoRemove(true)
42+
.RunAsync()
43+
.EnsureSuccess();
844

9-
## Prerequisites
45+
// Microsoft DI API to resolve dependencies
46+
var nuget = GetService<INuGet>();
1047

11-
The tool requires [.NET 6+ runtime](https://dotnet.microsoft.com/en-us/download).
48+
// Creating a custom service provider
49+
var serviceCollection = GetService<IServiceCollection>();
50+
serviceCollection.AddSingleton<MyTool>();
51+
52+
var myServiceProvider = serviceCollection.BuildServiceProvider();
53+
var tool = myServiceProvider.GetRequiredService<MyTool>();
54+
55+
// API for NuGet
56+
var settings = new NuGetRestoreSettings("MySampleLib")
57+
.WithVersionRange(VersionRange.Parse("[1.0.14, 1.1)"))
58+
.WithTargetFrameworkMoniker("net6.0")
59+
.WithPackagesPath(".packages");
60+
61+
var packages = nuget.Restore(settings);
62+
foreach (var package in packages)
63+
{
64+
Info(package.Path);
65+
}
66+
67+
// API for .NET CLI
68+
var buildResult = new DotNetBuild().WithConfiguration(configuration).WithNoLogo(true)
69+
.Build().EnsureSuccess();
70+
71+
var warnings = buildResult.Warnings
72+
.Where(warn => Path.GetFileName(warn.File) == "Calculator.cs")
73+
.Select(warn => $"{warn.Code}({warn.LineNumber}:{warn.ColumnNumber})")
74+
.Distinct();
75+
76+
foreach (var warning in warnings)
77+
{
78+
await new HttpClient().GetAsync(
79+
"https://api.telegram.org/bot7102686717:AAEHw7HZinme_5kfIRV7TwXK4Xql9WPPpM3/" +
80+
"sendMessage?chat_id=878745093&text="
81+
+ HttpUtility.UrlEncode(warning));
82+
}
83+
84+
// Asynchronous way
85+
var cts = new CancellationTokenSource();
86+
await new DotNetTest()
87+
.WithConfiguration(configuration)
88+
.WithNoLogo(true)
89+
.WithNoBuild(true)
90+
.BuildAsync(CancellationOnFirstFailedTest, cts.Token)
91+
.EnsureSuccess();
92+
93+
void CancellationOnFirstFailedTest(BuildMessage message)
94+
{
95+
if (message.TestResult is { State: TestState.Failed }) cts.Cancel();
96+
}
97+
98+
// Parallel tests
99+
var tempDir = Directory.CreateTempSubdirectory();
100+
try
101+
{
102+
new DotNetPublish()
103+
.WithConfiguration(configuration)
104+
.WithNoLogo(true)
105+
.WithNoBuild(true)
106+
.WithFramework("net8.0")
107+
.AddProps(("PublishDir", tempDir.FullName))
108+
.Build().EnsureSuccess();
109+
110+
var test = new VSTest().WithTestFileNames("*.Tests.dll");
111+
112+
var tasks = from tagSuffix in new[] {"bookworm-slim", "alpine", "noble"}
113+
let image = $"mcr.microsoft.com/dotnet/sdk:8.0-{tagSuffix}"
114+
let dockerRun = new DockerRun(image)
115+
.WithCommandLine(test)
116+
.WithAutoRemove(true)
117+
.WithVolumes((tempDir.FullName, "/app"))
118+
.WithContainerWorkingDirectory("/app")
119+
select dockerRun.BuildAsync(CancellationOnFirstFailedTest, cts.Token);
120+
121+
await Task.WhenAll(tasks).EnsureSuccess();
122+
}
123+
finally { tempDir.Delete(); }
124+
125+
class MyTool(INuGet nuGet);
126+
```
12127

13128
## Usage
14129

15-
### Script runner
130+
### Script runner tool
131+
132+
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).
16133

17134
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/).
18135

@@ -76,12 +193,12 @@ Supported arguments:
76193

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

79-
### .NET application
196+
### .NET project
80197

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

83200
```shell
84-
dotnet new -i CSharpInteractive.Templates
201+
dotnet new install CSharpInteractive.Templates
85202
```
86203

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

README_BODY.md

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

13128
## Usage
14129

15-
### Script runner
130+
### Script runner tool
131+
132+
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).
16133

17134
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/).
18135

@@ -76,12 +193,12 @@ Supported arguments:
76193

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

79-
### .NET application
196+
### .NET project
80197

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

83200
```shell
84-
dotnet new -i CSharpInteractive.Templates
201+
dotnet new install CSharpInteractive.Templates
85202
```
86203

87204
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)