Skip to content

Commit

Permalink
Merge pull request #67 from mrboring/main
Browse files Browse the repository at this point in the history
Added WindowStyle
  • Loading branch information
CaptnCodr authored Apr 12, 2024
2 parents d0de1c3 + 6d5704e commit 363cd7f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 6 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ cli {
|> Command.execute
```

For Windows applications it's possible to set their visibility. There are four possible values: `Hidden`, `Maximized`, `Minimized` and `Normal`. The default is `Hidden`.
```fsharp
cli {
Exec @"C:\Windows\regedit.exe"
WindowStyle Normal
}
|> Command.execute
```

#### `Command.execute`
`Command.execute` returns record: `type Output = { Id: int; Text: string option; ExitCode: int; Error: string option }`
which has getter methods to get only one value:
Expand Down Expand Up @@ -236,6 +245,7 @@ cli {
| `Input` | `string` |
| `Output` | `Outputs` (see below) |
| `WorkingDirectory` | `string` |
| `WindowStyle` | `Fli.WindowStyle` |
| `EnvironmentVariable` | `string * string` |
| `EnvironmentVariables` | `(string * string) list` |
| `Encoding` | `System.Text.Encoding` |
Expand All @@ -252,6 +262,7 @@ cli {
| `Username` | `string` |
| `Credentials` | `string * string * string` |
| `WorkingDirectory` | `string` |
| `WindowStyle` | `Fli.WindowStyle` |
| `EnvironmentVariable` | `string * string` |
| `EnvironmentVariables` | `(string * string) list` |
| `Encoding` | `System.Text.Encoding` |
Expand All @@ -270,6 +281,12 @@ Provided `Fli.Outputs`:
- `StringBuilder of StringBuilder` a StringBuilder which will be filled with the output text.
- `Custom of Func<string, unit>` a custom function (`string -> unit`) that will be called with the output string (logging, printing etc.).

Provided `Fli.WindowStyle`:
- `Hidden` (default)
- `Maximized`
- `Minimized`
- `Normal`

### Do you miss something?
Open an [issue](https://github.com/CaptnCodr/Fli/issues) or start a [discussion](https://github.com/CaptnCodr/Fli/discussions).

Expand Down
14 changes: 13 additions & 1 deletion src/Fli.Tests/ExecContext/ExecCommandConfigureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@ let ``Check Arguments in ProcessStartInfo with Arguments`` () =
[<Test>]
let ``Check WorkingDirectory in ProcessStartInfo with WorkingDirectory`` () =
cli {
Exec "cnd.exe"
Exec "cmd.exe"
WorkingDirectory @"C:\Users"
}
|> Command.buildProcess
|> _.WorkingDirectory
|> should equal @"C:\Users"

[<Test>]
let ``Check WindowStyle in ProcessStartInfo with WorkingDirectory`` () =
cli {
Exec "cmd.exe"
WindowStyle Normal
}
|> Command.buildProcess
|> _.WindowStyle
|> should equal Diagnostics.ProcessWindowStyle.Normal

[<Test>]
let ``Check Verb in ProcessStartInfo with Verb`` () =
if OperatingSystem.IsWindows() then
Expand Down Expand Up @@ -123,6 +133,7 @@ let ``Check all possible values in ProcessStartInfo for windows`` () =
EnvironmentVariable("Fli", "test")
EnvironmentVariables [ ("Fli.Test", "test") ]
Encoding Encoding.UTF8
WindowStyle Normal
}
|> Command.buildProcess

Expand All @@ -137,6 +148,7 @@ let ``Check all possible values in ProcessStartInfo for windows`` () =
config.Environment.Contains(KeyValuePair("Fli.Test", "test")) |> should be True
config.StandardOutputEncoding |> should equal Encoding.UTF8
config.StandardErrorEncoding |> should equal Encoding.UTF8
config.WindowStyle |> should equal Diagnostics.ProcessWindowStyle.Normal
else
let config =
cli {
Expand Down
8 changes: 8 additions & 0 deletions src/Fli.Tests/ExecContext/ExecConfigTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ let ``Check working directory config for executing program`` () =
|> _.config.WorkingDirectory
|> should equal (Some @"C:\Users")

[<Test>]
let ``Check windowstyle config for executing program`` () =
cli {
Exec "cmd.exe"
WindowStyle Normal }
|> _.config.WindowStyle
|> should equal (Some Normal)

[<Test>]
let ``Check Verb config for executing program`` () =
cli {
Expand Down
12 changes: 12 additions & 0 deletions src/Fli/CE.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ module CE =
member _.WorkingDirectory(context: ICommandContext<ShellContext>, workingDirectory) =
Cli.workingDirectory workingDirectory context.Context

/// The `WindowStyle` for newly created windows.
/// Hint: Hidden, Maximized, Minimized or Normal.
[<CustomOperation("WindowStyle")>]
member _.WindowStyle(context: ICommandContext<ShellContext>, windowStyle) =
Cli.windowStyle windowStyle context.Context

/// One tupled `EnvironmentVariable`.
[<CustomOperation("EnvironmentVariable")>]
member _.EnvironmentVariable(context: ICommandContext<ShellContext>, environmentVariable) =
Expand Down Expand Up @@ -118,6 +124,12 @@ module CE =
member _.WorkingDirectory(context: ICommandContext<ExecContext>, workingDirectory) =
Program.workingDirectory workingDirectory context.Context

/// The `WindowStyle` for newly created windows.
/// Hint: Hidden, Maximized, Minimized or Normal.
[<CustomOperation("WindowStyle")>]
member _.WindowStyle(context: ICommandContext<ExecContext>, windowStyle) =
Program.windowStyle windowStyle context.Context

/// `Verb` keyword that can be used to start the executable.
[<CustomOperation("Verb")>]
member _.Verb(context: ICommandContext<ExecContext>, verb) = Program.verb verb context.Context
Expand Down
10 changes: 9 additions & 1 deletion src/Fli/Command.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ module Command =
ProcessStartInfo(
FileName = executable,
Arguments = argumentString,
WindowStyle = ProcessWindowStyle.Hidden,
CreateNoWindow = true,
UseShellExecute = openDefault,
RedirectStandardInput = not (openDefault),
Expand Down Expand Up @@ -205,6 +204,13 @@ module Command =
| Shells.BASH -> context.config.Command |> Option.defaultValue "" |> (fun s -> $"\"{s}\"")
| _ -> context.config.Command |> Option.defaultValue ""

let private getProcessWindowStyle (windowStyle: WindowStyle) =
match windowStyle with
| Hidden -> ProcessWindowStyle.Hidden
| Maximized -> ProcessWindowStyle.Maximized
| Minimized -> ProcessWindowStyle.Minimized
| Normal -> ProcessWindowStyle.Normal

type Command =
static member internal buildProcess(context: ShellContext) =
let (proc, flag) = (context.config.Shell, context.config.Input) ||> shellToProcess
Expand All @@ -214,6 +220,7 @@ module Command =
.With(WorkingDirectory = (context.config.WorkingDirectory |> Option.defaultValue ""))
.With(StandardOutputEncoding = (context.config.Encoding |> Option.defaultValue null))
.With(StandardErrorEncoding = (context.config.Encoding |> Option.defaultValue null))
.With(WindowStyle = getProcessWindowStyle (context.config.WindowStyle |> Option.defaultValue Hidden))
|> addEnvironmentVariables context.config.EnvironmentVariables

static member internal buildProcess(context: ExecContext) =
Expand All @@ -233,6 +240,7 @@ module Command =
.With(UserName = (context.config.UserName |> Option.defaultValue ""))
.With(StandardOutputEncoding = (context.config.Encoding |> Option.defaultValue null))
.With(StandardErrorEncoding = (context.config.Encoding |> Option.defaultValue null))
.With(WindowStyle = getProcessWindowStyle (context.config.WindowStyle |> Option.defaultValue Hidden))
|> addCredentials context.config.Credentials
|> addEnvironmentVariables context.config.EnvironmentVariables

Expand Down
18 changes: 14 additions & 4 deletions src/Fli/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module Domain =
WorkingDirectory: string option
EnvironmentVariables: (string * string) list option
Encoding: Encoding option
CancelAfter: int option }
CancelAfter: int option
WindowStyle: WindowStyle option }

and Shells =
| CMD
Expand All @@ -32,6 +33,12 @@ module Domain =
| StringBuilder of StringBuilder
| Custom of Func<string, unit>

and WindowStyle =
| Hidden
| Maximized
| Minimized
| Normal

type ExecConfig =
{ Program: string
Arguments: string option
Expand All @@ -43,7 +50,8 @@ module Domain =
Credentials: Credentials option
EnvironmentVariables: (string * string) list option
Encoding: Encoding option
CancelAfter: int option }
CancelAfter: int option
WindowStyle: WindowStyle option }

and Credentials = Credentials of Domain: string * UserName: string * Password: string

Expand Down Expand Up @@ -72,7 +80,8 @@ module Domain =
WorkingDirectory = None
EnvironmentVariables = None
Encoding = None
CancelAfter = None }
CancelAfter = None
WindowStyle = None }
ExecConfig =
{ Program = ""
Arguments = None
Expand All @@ -84,7 +93,8 @@ module Domain =
Credentials = None
EnvironmentVariables = None
Encoding = None
CancelAfter = None } }
CancelAfter = None
WindowStyle = None } }

type Output =
{ Id: int
Expand Down
8 changes: 8 additions & 0 deletions src/Fli/Dsl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ module Cli =
{ context with
config.WorkingDirectory = Some workingDirectory }

let windowStyle (windowStyle: WindowStyle) (context: ShellContext) =
{ context with
config.WindowStyle = Some windowStyle }

let environmentVariables (variables: (string * string) list) (context: ShellContext) =
let vars =
match context.config.EnvironmentVariables with
Expand Down Expand Up @@ -65,6 +69,10 @@ module Program =
{ context with
config.WorkingDirectory = Some workingDirectory }

let windowStyle (windowStyle: WindowStyle) (context: ExecContext) =
{ context with
config.WindowStyle = Some windowStyle }

let verb (verb: string) (context: ExecContext) =
{ context with config.Verb = Some verb }

Expand Down

0 comments on commit 363cd7f

Please sign in to comment.