Skip to content

Commit 24da714

Browse files
authored
Created Single File C# Example in docs (#1144)
* Create single-file-csharp.md * Added additional language to docussaurus.config.ts
1 parent e39a15c commit 24da714

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: Single File C# Example
3+
---
4+
5+
Starting with dotnet 10 (preview 4), you can use the new file-based C# application feature to write scripts in C# without needing to create a full project structure. This is particularly useful for quick scripts or small utilities.
6+
7+
## Using File-Based C# Application with ModularPipelines
8+
9+
To use ModularPipelines in a single file C# application, you can follow these steps:
10+
11+
1. **Create a C# file**: Create a new file named `example.cs` (or any name with `.cs` extension).
12+
2. **Add ModularPipelines to your project**: You can add TUnit as a package reference in your file. At the top of your `example.cs`, add the following line:
13+
14+
```csharp
15+
#:package ModularPipelines@2.*
16+
```
17+
18+
Alternatively, you can specify a specific version:
19+
20+
```csharp
21+
22+
```
23+
24+
3. **Write your C# code**: Below the package reference, you can write your C# code using ModularPipelines. Heres a simple example that uses ModularPipelines to check the dotnet version:
25+
26+
```csharp
27+
#!/usr/bin/dotnet run
28+
#:package ModularPipelines.DotNet@2.44.*
29+
using ModularPipelines.Attributes;
30+
using ModularPipelines.Context;
31+
using ModularPipelines.DotNet.Extensions;
32+
using ModularPipelines.Host;
33+
using ModularPipelines.Models;
34+
using ModularPipelines.Modules;
35+
36+
await PipelineHostBuilder.Create()
37+
.AddModule<UpdateDotnetWorkloads>()
38+
.AddModule<CheckDotnetSdkModule>()
39+
.ExecutePipelineAsync();
40+
41+
public class UpdateDotnetWorkloads : Module<CommandResult>
42+
{
43+
protected override async Task<CommandResult?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
44+
{
45+
return await context.DotNet().DotNetWorkload.Update(token: cancellationToken);
46+
}
47+
}
48+
49+
[DependsOn<UpdateDotnetWorkloads>]
50+
public class CheckDotnetSdkModule : Module<CommandResult>
51+
{
52+
53+
protected override async Task<CommandResult?> ExecuteAsync(IPipelineContext context, CancellationToken cancellationToken)
54+
{
55+
return await context.DotNet().Sdk.Check(token: cancellationToken);
56+
}
57+
}
58+
```
59+
60+
4. **Run your script**: You can run your script directly using the `dotnet run` command. Make sure you have the .NET SDK installed and available in your PATH.
61+
62+
```powershell
63+
dotnet run example.cs
64+
```
65+
66+
If you need to convert the file based application to a regular C# project, you can run the following command:
67+
68+
```powershell
69+
dotnet project convert example.cs
70+
```

docs/docusaurus.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {themes as prismThemes} from 'prism-react-renderer';
2-
import type {Config} from '@docusaurus/types';
1+
import { themes as prismThemes } from 'prism-react-renderer';
2+
import type { Config } from '@docusaurus/types';
33
import type * as Preset from '@docusaurus/preset-classic';
44

55
const config: Config = {
@@ -100,6 +100,7 @@ const config: Config = {
100100
copyright: `Copyright © ${new Date().getFullYear()} Tom Longhurst. Built with Docusaurus.`,
101101
},
102102
prism: {
103+
additionalLanguages: ['csharp', 'powershell'],
103104
theme: prismThemes.github,
104105
darkTheme: prismThemes.dracula,
105106
},

0 commit comments

Comments
 (0)