Skip to content

Commit

Permalink
Merge pull request #68 from xgqt/2024-xgqt-feature-shells
Browse files Browse the repository at this point in the history
Add sh and zsh, add Linux's pwsh, always quote default shell
  • Loading branch information
CaptnCodr authored Apr 16, 2024
2 parents 307b5ee + 019ddaa commit 4bc9985
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ Currently provided `Fli.Shells`:
- `PS` runs `powershell.exe -Command ...`
- `PWSH` runs `pwsh.exe -Command ...`
- `WSL` runs `wsl.exe -- ...`
- `SH` runs `sh -c ...`
- `BASH` runs `bash -c ...`
- `ZSH` runs `zsh -c ...`
- `CUSTOM (shell: string * flag: string)` runs the specified `shell` with the specified starting argument (`flag`)

Provided `Fli.Outputs`:
Expand All @@ -291,4 +293,4 @@ Provided `Fli.WindowStyle`:
Open an [issue](https://github.com/CaptnCodr/Fli/issues) or start a [discussion](https://github.com/CaptnCodr/Fli/discussions).

### Inspiration
Use CE's for CLI commands came in mind while using [FsHttp](https://github.com/fsprojects/FsHttp).
Use CE's for CLI commands came in mind while using [FsHttp](https://github.com/fsprojects/FsHttp).
28 changes: 27 additions & 1 deletion src/Fli.Tests/ShellContext/ShellCommandExecuteTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ let ``Hello World with CUSTOM shell`` () =
else
cli {
Shell(CUSTOM("bash", "-c"))
Command "\"echo Hello World!\""
Command "echo Hello World!"
}
|> Command.execute
|> Output.toText
Expand Down Expand Up @@ -187,6 +187,19 @@ let ``Hello World with PWSH`` () =
else
Assert.Pass()

[<Test>]
let ``Hello World with SH`` () =
if OperatingSystem.IsWindows() |> not then
cli {
Shell SH
Command "echo Hello World!"
}
|> Command.execute
|> Output.toText
|> should equal "Hello World!"
else
Assert.Pass()

[<Test>]
let ``Hello World with BASH`` () =
if OperatingSystem.IsWindows() |> not then
Expand All @@ -200,6 +213,19 @@ let ``Hello World with BASH`` () =
else
Assert.Pass()

[<Test>]
let ``Hello World with ZSH`` () =
if OperatingSystem.IsMacOS() then
cli {
Shell ZSH
Command "echo Hello World!"
}
|> Command.execute
|> Output.toText
|> should equal "Hello World!"
else
Assert.Pass()

[<Test>]
let ``Input text in BASH`` () =
if OperatingSystem.IsWindows() |> not then
Expand Down
21 changes: 15 additions & 6 deletions src/Fli.Tests/ShellContext/ShellCommandToStringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
open NUnit.Framework
open FsUnit
open Fli
open System


[<Test>]
Expand Down Expand Up @@ -35,12 +36,20 @@ let ``PS command toString returns full line`` () =

[<Test>]
let ``PWSH command toString returns full line`` () =
cli {
Shell PWSH
Command "Write-Host Hello World!"
}
|> Command.toString
|> should equal "pwsh.exe -Command Write-Host Hello World!"
if OperatingSystem.IsWindows() then
cli {
Shell PWSH
Command "Write-Host Hello World!"
}
|> Command.toString
|> should equal "pwsh.exe -Command Write-Host Hello World!"
else
cli {
Shell PWSH
Command "Write-Host Hello World!"
}
|> Command.toString
|> should equal "pwsh -Command Write-Host Hello World!"

[<Test>]
let ``WSL command toString returns full line`` () =
Expand Down
23 changes: 19 additions & 4 deletions src/Fli/Command.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ module Command =
open System.Runtime.InteropServices
open System.Threading

let private getAvailablePwshExe () =
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then
"pwsh.exe"
else
"pwsh"

let private shellToProcess (shell: Shells) (input: string option) =
match shell with
| CMD -> "cmd.exe", (if input.IsNone then "/c" else "/k")
| PS -> "powershell.exe", "-Command"
| PWSH -> "pwsh.exe", "-Command"
| PWSH -> getAvailablePwshExe (), "-Command"
| WSL -> "wsl.exe", "--"
| SH -> "sh", "-c"
| BASH -> "bash", "-c"
| ZSH -> "zsh", "-c"
| CUSTOM(shell, flag) -> shell, flag

let private toOption =
Expand Down Expand Up @@ -200,9 +208,16 @@ module Command =
cts.Token

let private quoteBashCommand (context: ShellContext) =
match context.config.Shell with
| Shells.BASH -> context.config.Command |> Option.defaultValue "" |> (fun s -> $"\"{s}\"")
| _ -> context.config.Command |> Option.defaultValue ""
let noQuoteNeeded = [|
Shells.CMD
Shells.PWSH
Shells.PS
Shells.WSL
|]

match Array.contains context.config.Shell noQuoteNeeded with
| true -> context.config.Command |> Option.defaultValue ""
| false -> context.config.Command |> Option.defaultValue "" |> (fun s -> $"\"{s}\"")

let private getProcessWindowStyle (windowStyle: WindowStyle) =
match windowStyle with
Expand Down
2 changes: 2 additions & 0 deletions src/Fli/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ module Domain =
| PS
| PWSH
| WSL
| SH
| BASH
| ZSH
| CUSTOM of shell: string * flag: string

and Outputs =
Expand Down

0 comments on commit 4bc9985

Please sign in to comment.