|
| 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. Here’s 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 | + ``` |
0 commit comments