Skip to content

Commit c8c87f9

Browse files
authored
Merge pull request #2615 from licon4812/docs/fsi
docs: Added FSharp Example to Docs
2 parents f408790 + 30743d1 commit c8c87f9

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
title: F# Interactive Example
3+
---
4+
5+
F# Interactive (FSI) is a powerful tool for executing F# code snippets and scripts in an interactive environment. It allows you to quickly test and iterate on your F# code without needing to create a full project structure.
6+
7+
## Using F# Interactive with ModularPipelines
8+
9+
1. **Create a new F# script file:** Create a new file named `example.fsx` (or any name with `.fsx` extension).
10+
2. **Add ModularPipelines to your script:** You can add ModularPipelines as a package reference in your F# script. At the top of your `example.fsx`, add the following line:
11+
12+
```fsharp
13+
#r "nuget: ModularPipelines, 3.*"
14+
```
15+
16+
Alternatively, you can specify a specific version:
17+
18+
```fsharp
19+
#r "nuget: ModularPipelines, 3.2.8"
20+
```
21+
3. **Write your F# code:** Below the package reference, you can write your F# code using ModularPipelines. Here’s a simple example that uses ModularPipelines to check the dotnet version:
22+
23+
```fsharp
24+
#r "nuget: ModularPipelines.DotNet, 3.*"
25+
open ModularPipelines.DotNet
26+
open ModularPipelines.Attributes
27+
open ModularPipelines.Context
28+
open ModularPipelines.DotNet.Extensions
29+
open ModularPipelines.Extensions
30+
open ModularPipelines.Models
31+
open ModularPipelines.Modules
32+
open ModularPipelines
33+
open System.Threading
34+
35+
type UpdateDotnetWorkloads() =
36+
inherit Module<CommandResult>()
37+
override this.ExecuteAsync (context: IModuleContext, cancellationToken: CancellationToken): Tasks.Task<CommandResult> =
38+
context.DotNet().Workload.Update(cancellationToken = cancellationToken)
39+
40+
/// Generic attributes are not supported in fsharp, so have to use the old way of declaring dependencies
41+
[<DependsOn(typeof<UpdateDotnetWorkloads>)>]
42+
type CheckDotnetSdkModule () =
43+
inherit Module<CommandResult>()
44+
override this.ExecuteAsync (context: IModuleContext, cancellationToken: CancellationToken): Tasks.Task<CommandResult> =
45+
context.DotNet().Sdk.Check(cancellationToken = cancellationToken);
46+
47+
let args = System.Environment.GetCommandLineArgs()
48+
let builder = Pipeline.CreateBuilder(args)
49+
50+
builder.Services
51+
.RegisterDotNetContext()
52+
.AddModule<UpdateDotnetWorkloads>()
53+
.AddModule<CheckDotnetSdkModule>()
54+
55+
builder.Build().RunAsync()
56+
|> Async.AwaitTask
57+
|> Async.RunSynchronously
58+
```
59+
4. **Run your F# script:** You can run your F# script using the F# Interactive environment. If you are using Visual Studio, you can simply open the `example.fsx` file and execute it. Alternatively, you can run it from the command line using:
60+
61+
```powershell
62+
dotnet fsi example.fsx
63+
```
64+
## Additional Notes
65+
66+
- Fsharp Interactive does allow you to have modules and types across different files, for simplicity we have only 1 file in this example.
67+
- The fsharp compiler does not support generic attributes
68+
- The fsharp compiler requires code and files to be declared in order, due to the fsharp compiler being sequential. For example, if you have a module that depends on another module, the module it depends on must be declared first.

docs/docusaurus.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const config: Config = {
112112
copyright: `Copyright © ${new Date().getFullYear()} Tom Longhurst. Built with Docusaurus.`,
113113
},
114114
prism: {
115-
additionalLanguages: ['csharp', 'powershell'],
115+
additionalLanguages: ['csharp', 'powershell', 'fsharp'],
116116
theme: prismThemes.github,
117117
darkTheme: prismThemes.dracula,
118118
},

0 commit comments

Comments
 (0)