Skip to content

Commit

Permalink
Remove ExecBuilder and move execution into CommandBuilder (#7)
Browse files Browse the repository at this point in the history
* Include renovate.json in solution

* Execution with a single builder
  • Loading branch information
UnstoppableMango authored Jan 14, 2024
1 parent cf96317 commit 16ba137
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 48 deletions.
3 changes: 2 additions & 1 deletion CliWrap.FSharp.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
Expand All @@ -25,6 +25,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "github", "github", "{FE346152-5716-4224-9B25-C6B3815C70CA}"
ProjectSection(SolutionItems) = preProject
.github\workflows\main.yml = .github\workflows\main.yml
.github\renovate.json = .github\renovate.json
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{BC50591F-A365-4A92-9FDC-4CD2D75EE6A4}"
Expand Down
60 changes: 55 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CliWrap.FSharp

Idiomatic F# support for CliWrap
Idiomatic F# support for CliWrap.

## Usage

Expand All @@ -17,11 +17,59 @@ let main args =
cmd.ExecuteAsync()
```

The computation expression also supports executing the command with `exec`.

```fsharp
let main args = task {
let! result = command "dotnet" {
args = [ "build" ]
workingDirectory = "~/src/CliWrap.FSharp"
exec
}
result.ExitCode
}
```

Cancellation is also supported.

```fsharp
let main args = task {
use cts = new CancellationTokenSource()
let! result = command "dotnet" {
args = [ "build" ]
workingDirectory = "~/src/CliWrap.FSharp"
exec cts.Token
}
result.ExitCode
}
```

CliWrap's buffered execution is supported with `buffered`.

```fsharp
let main args = task {
use cts = new CancellationTokenSource()
let! result = command "dotnet" {
args = [ "build" ]
workingDirectory = "~/src/CliWrap.FSharp"
buffered Encoding.UTF8 cts.Token
}
result.ExitCode
}
```

Asynchrony with F#'s `Async<'T>` is supported with `async`.

```fsharp
let main args = async {
let! result = exec "dotnet" {
use cts = new CancellationTokenSource()
let! result = command "dotnet" {
args = [ "build" ]
workingDirectory = "~/src/CliWrap.FSharp"
async cts.Token
}
result.ExitCode
Expand All @@ -38,10 +86,12 @@ let main args =
cmd.ExecuteAsync()
```

## Idiomatic? This looks nothing like normal F# code!
## Q/A

### Idiomatic? This looks nothing like the F# I write!

I've only recently been diving further into the F# ecosystem, if something looks off please open an issue!
If something looks off please open an issue! I've only recently been diving further into the F# ecosystem.

## Why not paket?
### Why not paket?

If renovate ever [supports it](https://github.com/renovatebot/renovate/issues/11211)!
1 change: 0 additions & 1 deletion src/CliWrap.FSharp/CliWrap.FSharp.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<Compile Include="Pipes.fs" />
<Compile Include="PipeBuilder.fs" />
<Compile Include="CommandBuilder.fs" />
<Compile Include="ExecBuilder.fs" />
<None Include="packages.lock.json" />
</ItemGroup>

Expand Down
35 changes: 35 additions & 0 deletions src/CliWrap.FSharp/CommandBuilder.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
module UnMango.CliWrap.FSharp.CommandBuilder

open System.ComponentModel
open System.Text
open CliWrap
open CliWrap.Buffered

type CommandBuilder(target: string) =
[<EditorBrowsable(EditorBrowsableState.Never)>]
Expand All @@ -11,6 +13,12 @@ type CommandBuilder(target: string) =
[<EditorBrowsable(EditorBrowsableState.Never)>]
member _.Run(command: Command) = command

[<EditorBrowsable(EditorBrowsableState.Never)>]
member _.Run<'T>(command: CommandTask<'T>) = command

[<EditorBrowsable(EditorBrowsableState.Never)>]
member _.Run<'T>(task: Async<'T>) = task

[<CustomOperation("env")>]
member _.Env(command: Command, env) = Cli.env env command

Expand Down Expand Up @@ -47,4 +55,31 @@ type CommandBuilder(target: string) =
[<CustomOperation("validation")>]
member _.Validation(command: Command, validation) = Cli.validation validation command

[<CustomOperation("exec")>]
member _.Exec(command: Command) = command.ExecuteAsync()

[<CustomOperation("exec")>]
member _.Exec(command: Command, cancellationToken) = command.ExecuteAsync(cancellationToken)

[<CustomOperation("buffered")>]
member _.Buffered(command: Command) = command.ExecuteBufferedAsync()

[<CustomOperation("buffered")>]
member _.Buffered(command: Command, encoding: Encoding) = command.ExecuteBufferedAsync(encoding)

[<CustomOperation("buffered")>]
member _.Buffered(command: Command, encoding, cancellationToken) =
command.ExecuteBufferedAsync(encoding, cancellationToken)

[<CustomOperation("async")>]
member _.Async<'T>(task: CommandTask<'T>) =
task |> CommandTask.op_Implicit |> Async.AwaitTask

[<CustomOperation("async")>]
member this.Async(command: Command) = this.Async(command.ExecuteAsync())

[<CustomOperation("async")>]
member this.Async(command: Command, cancellationToken) =
this.Async(command.ExecuteAsync(cancellationToken))

let command target = CommandBuilder(target)
41 changes: 0 additions & 41 deletions src/CliWrap.FSharp/ExecBuilder.fs

This file was deleted.

0 comments on commit 16ba137

Please sign in to comment.