Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added WindowStyle #67

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 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
12 changes: 12 additions & 0 deletions src/Fli.Tests/ExecContext/ExecCommandConfigureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ let ``Check WorkingDirectory in ProcessStartInfo with WorkingDirectory`` () =
|> _.WorkingDirectory
|> should equal @"C:\Users"

[<Test>]
let ``Check Window Style in ProcessStartInfo with WorkingDirectory`` () =
mrboring marked this conversation as resolved.
Show resolved Hide resolved
cli {
Exec "cnd.exe"
mrboring marked this conversation as resolved.
Show resolved Hide resolved
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 window style config for executing program`` () =
mrboring marked this conversation as resolved.
Show resolved Hide resolved
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 `windowsstyle` for newly created windows.
mrboring marked this conversation as resolved.
Show resolved Hide resolved
/// 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 `windowsstyle` for newly created windows.
mrboring marked this conversation as resolved.
Show resolved Hide resolved
/// 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