Repository F# setup
open System
open System.IO
open System.Threading
open System.Threading.Tasks
open Axial
open Axial.Console
open Axial.FileSystem
open Axial.ProcessOpen an editor, file manager or URL
A desktop application often hands work to another program: open a file in an editor, show a folder in the file
manager, or open a URL in the browser. These launches differ from the rest of Axial.Process: the caller does not
wait for the child, does not capture its output, and does not own its lifetime. Closing the application leaves the
editor open.
Process.launchcommandstarts one command and returns once it has started.Process.shellOpentargetopens a path or URL with the operating system's default handler:ShellExecuteon Windows,openon macOS andxdg-openelsewhere.
Both return Flow<'env, ProcessError, unit> and go through the environment's IProcess service, so a failure to start
is the typed ProcessError.StartFailed and a test can assert the launch without starting anything. Building either
flow never throws; an invalid target is a typed failure when the flow runs.
Show page setup
open Axial
open Axial.Process
let shouldEqual expected actual =
if actual <> expected then failwithf "Expected %A but got %A" expected actuallet openInEditor (path: string) =
Process.commandArgs "code" [ "--goto"; path ] |> Process.launch
let revealFolder (folder: string) = Process.shellOpen folder
openInEditor: string -> Flow<'a,ProcessError,unit>path: stringstringAn abbreviation for the CLI type . Basic Types
Axial.Process.ProcesscommandArgs: string -> string list -> ProcessCommandCreates a one-command process specification from an executable and already-separated arguments. Example: Process.commandArgs "git" [ "status"; "--short" ] |> Process.toFlow
(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
launch: ProcessCommand -> Flow<'env,ProcessError,unit>Starts one command without waiting for it, capturing its output, or owning its lifetime. Use it to hand off to an editor or another desktop program. The flow succeeds once the child has started; the child inherits the caller's standard handles and keeps running after the flow, and its exit code is never observed. Interrupting the flow does not stop the child. A console program gets its own window on Windows. The launch goes through the environment's IProcess.Launch, so Process.test () records it in Launches instead of starting anything. Executable, arguments, working directory and environment variables apply; output routing, capture, timeouts and success codes do not. Arguments are passed to the executable as a list and never through a shell. An editor installed as a script, such as code.cmd on Windows, is not an executable: resolve the executable it runs instead of wrapping it in cmd.exe /c, which reinterprets & | ^ % in a path. The command to start, for example Process.commandArgs "code" [ path ]. A flow that fails with ProcessError.StartFailed when the command cannot be started.
revealFolder: string -> Flow<'a,ProcessError,unit>folder: stringshellOpen: string -> Flow<'env,ProcessError,unit>Opens a file, folder or URL with the operating system's default handler. The live service uses ShellExecute on Windows, open on macOS and xdg-open elsewhere, and does not wait for the handler. The flow succeeds once the opener has started, so a target that no handler accepts is not reported. To reveal a file in the file manager, open its folder. The launch goes through IProcess.Launch, so Process.test () records it in Launches. Opening a target can run a program. ShellExecute runs an .exe, .bat or .cmd file, open runs an .app bundle or an executable script, and xdg-open hands the target to whatever the desktop associates with it. Do not pass a path or URL from an untrusted source without checking it, for example that it is an https: URL or a folder the application owns. An empty target, or one starting with - (which an opener reads as an option), fails with ProcessError.ShellOpenRejected and starts nothing; prefix a relative path with ./. A path or URL. A flow that fails with ProcessError.ShellOpenRejected for a refused target and ProcessError.StartFailed when the opener cannot be started.
Requires & types
Requires: open Axial.Process (packages: Axial.Process)val openInEditor: string -> Flow<'a,ProcessError,unit>val revealFolder: string -> Flow<'a,ProcessError,unit>Process.launch applies the command's executable, arguments, working directory and environment variables. The child
inherits the caller's standard handles; output routing, capture, timeouts and success codes do not apply, because
nothing waits for the child. Interrupting the flow after the launch does not stop the child. A console program gets
its own window on Windows. Arguments go to the executable as a list, never through a shell, so a path with spaces or
& in it is one argument.
Process.shellOpen succeeds once the opener has started. On Linux and macOS the opener exits after handing the target
on, and a target with no registered handler is not reported. To reveal a file, open its folder.
Opening untrusted targets
Opening a target can run a program. ShellExecute runs an .exe, .bat or .cmd file, open runs an .app
bundle or an executable script, and xdg-open hands the target to whatever the desktop associates with it. Treat a
path or URL that comes from a file, a network response or another user as untrusted: check that it is what the
application means to open, for example an https: URL or a folder the application owns, before passing it to
Process.shellOpen.
An opener reads a leading - as an option, so Process.shellOpen refuses an empty target or one that starts with
- with ProcessError.ShellOpenRejected, and starts nothing. Prefix a relative path with ./.
let openDocsPage (url: string) =
match Uri.TryCreate(url, UriKind.Absolute) with
| true, uri when uri.Scheme = "https" -> Process.shellOpen uri.AbsoluteUri
| _ -> Flow.fail (ProcessError.ShellOpenRejected { Target = url; Reason = "not an https URL" })
openDocsPage: string -> Flow<'a,ProcessError,unit>url: stringstringAn abbreviation for the CLI type . Basic Types
System.UriProvides an object representation of a uniform resource identifier (URI) and easy access to the parts of the URI.
TryCreate: string * UriKind * byref<Uri> -> boolCreates a new using the specified instance and a . The representing the . The type of the Uri. When this method returns, contains the constructed . A value that is if the was successfully created; otherwise, .
System.UriKindDefines the different kinds of URIs.
Absolute: UriKindThe URI is absolute.
uri: UriScheme: stringGets the scheme name for this URI. A that contains the scheme for this URI, converted to lowercase. This instance represents a relative URI, and this property is valid only for absolute URIs.
(=): 'T -> 'T -> boolStructural equality The first parameter. The second parameter. The result of the comparison. 5 = 5 // Evaluates to true 5 = 6 // Evaluates to false [1; 2] = [1; 2] // Evaluates to true (1, 5) = (1, 6) // Evaluates to false
Axial.Process.ProcessshellOpen: string -> Flow<'env,ProcessError,unit>Opens a file, folder or URL with the operating system's default handler. The live service uses ShellExecute on Windows, open on macOS and xdg-open elsewhere, and does not wait for the handler. The flow succeeds once the opener has started, so a target that no handler accepts is not reported. To reveal a file in the file manager, open its folder. The launch goes through IProcess.Launch, so Process.test () records it in Launches. Opening a target can run a program. ShellExecute runs an .exe, .bat or .cmd file, open runs an .app bundle or an executable script, and xdg-open hands the target to whatever the desktop associates with it. Do not pass a path or URL from an untrusted source without checking it, for example that it is an https: URL or a folder the application owns. An empty target, or one starting with - (which an opener reads as an option), fails with ProcessError.ShellOpenRejected and starts nothing; prefix a relative path with ./. A path or URL. A flow that fails with ProcessError.ShellOpenRejected for a refused target and ProcessError.StartFailed when the opener cannot be started.
AbsoluteUri: stringGets the absolute URI. A containing the entire URI. This instance represents a relative URI, and this property is valid only for absolute URIs.
Axial.Flowfail: 'error -> Flow<'env,'error,'value>Same as error. The error value to wrap in a failing flow. A flow that always fails with the provided error. let result = Flow.fail "error" |> Flow.run () // result = Failure (Cause.Fail "error")
Axial.Process.ProcessErrorExpected process errors. Runtime cancellation remains Cause.Interrupt.
ShellOpenRejectedProcess.shellOpen - open xdg-open
Target: stringReason: stringRequires & types
Requires: open Axial; open Axial.Process; open System (packages: Axial, Axial.Process)val openDocsPage: string -> Flow<'a,ProcessError,unit>Editors installed as scripts on Windows
Some editors install a script rather than an executable, for example code.cmd on Windows. Starting the script
directly fails with StartFailed. Do not wrap it in cmd.exe /c: cmd.exe parses the whole command line again and
treats &, |, ^ and % in a file name as shell syntax, so an untrusted path can run other commands. Start the
executable the script runs instead, which receives the path as one argument. code.cmd in
Microsoft VS Code\bin runs Code.exe in the folder above it:
let editorCommand (codeExecutable: string option) (path: string) =
match codeExecutable with
| Some executable -> Process.commandArgs executable [ path ]
| None -> Process.commandArgs "code" [ path ]
editorCommand: string option -> string -> ProcessCommandcodeExecutable: string optionstringAn abbreviation for the CLI type . Basic Types
optionThe type of optional values. When used from other CLI languages the empty option is the null value. Use the constructors Some and None to create values of this type. Use the values in the Option module to manipulate values of this type, or pattern match against the values directly. 'None' values will appear as the value null to other CLI languages. Instance methods on this type will appear as static methods to other CLI languages due to the use of null as a value representation. Options
path: stringSomeThe representation of "Value of type 'T" The input value. An option representing the value.
executable: stringAxial.Process.ProcesscommandArgs: string -> string list -> ProcessCommandCreates a one-command process specification from an executable and already-separated arguments. Example: Process.commandArgs "git" [ "status"; "--short" ] |> Process.toFlow
NoneThe representation of "No value"
Requires & types
Requires: open Axial.Process (packages: Axial.Process)val editorCommand: string option -> string -> ProcessCommandTest a launch
Process.test () records every launch in Launches and succeeds by default. Set LaunchHandler to simulate a
failure:
let host = ProcessHostEnvironment.test ()
openInEditor "notes.txt"
|> Flow.run host.Environment
|> shouldEqual (Exit.Success ())
match List.ofSeq host.Process.Launches with
| [ ProcessLaunch.Command command ] ->
Process.arguments command |> shouldEqual [ "--goto"; "notes.txt" ]
| _ -> failwith "expected one launch"
let notInstalled = ProcessError.StartFailed { Command = "xdg-open"; Message = "not installed" }
host.Process.LaunchHandler <- Some(fun _ -> Flow.fail notInstalled)
match revealFolder "/var/log" |> Flow.run host.Environment with
| Exit.Failure(Cause.Fail(ProcessError.StartFailed failure)) ->
failure.Message |> shouldEqual "not installed"
| _ -> failwith "expected StartFailed"
match Process.shellOpen "--version" |> Flow.run host.Environment with
| Exit.Failure(Cause.Fail(ProcessError.ShellOpenRejected rejection)) ->
rejection.Target |> shouldEqual "--version"
| _ -> failwith "expected ShellOpenRejected"
host: TestProcessHostEnvironmentAxial.Process.ProcessHostEnvironmentModuletest: unit -> TestProcessHostEnvironmentopenInEditor: string -> Flow<'a,ProcessError,unit>(|>): 'T1 -> ('T1 -> 'U) -> 'UApply a function to a value, the value being on the left, the function on the right The argument. The function. The function result. let doubleIt x = x * 2 3 |> doubleIt // Evaluates to 6
Axial.Flowrun: 'env -> Flow<'env,'error,'value> -> Exit<'value,'error>Runs the workflow and blocks until the final exit is available. The environment used by the workflow. The workflow to run. The final workflow exit. let exit = workflow |> Flow.run environment
Environment: ProcessHostEnvironmentshouldEqual: 'a -> 'a -> unitAxial.Exit`2Represents the final outcome of a workflow execution. The type of the success value. The type of the domain-specific failure value.
SuccessThe workflow completed successfully.
Microsoft.FSharp.Collections.ListModuleContains operations for working with values of type . Operations for collections such as lists, arrays, sets, maps and sequences. See also F# Collection Types in the F# Language Guide.
ofSeq: 'T seq -> 'T listBuilds a new list from the given enumerable object. The input sequence. The list of elements from the sequence. let inputs = seq { 1; 2; 5 } inputs |> List.ofSeq Evaluates to [ 1; 2; 5 ].
Launches: Collections.Generic.IReadOnlyList<ProcessLaunch>Every detached launch requested, in order. Launches never call <c>RunHandler</c>.
Process: TestProcessAxial.Process.ProcessLaunchA start-and-forget launch: the caller does not wait for the child, capture its output, or own its lifetime.
CommandStarts one command; the child inherits the caller's standard handles.
command: ProcessCommandAxial.Process.Processarguments: ProcessCommand -> string listfailwith: string -> 'TThrow a exception. The exception message. Never returns. let failingFunction() = failwith "Oh no" // Throws an exception true // Never reaches this failingFunction() // Throws a System.Exception
notInstalled: ProcessErrorAxial.Process.ProcessErrorExpected process errors. Runtime cancellation remains Cause.Interrupt.
StartFailedCommand: stringMessage: stringLaunchHandler: (ProcessLaunch -> Flow<unit,ProcessError,unit>) optionReplaces the default launch result (success) — for example, to return <c>ProcessError.StartFailed</c>.
SomeThe representation of "Value of type 'T" The input value. An option representing the value.
fail: 'error -> Flow<'env,'error,'value>Same as error. The error value to wrap in a failing flow. A flow that always fails with the provided error. let result = Flow.fail "error" |> Flow.run () // result = Failure (Cause.Fail "error")
revealFolder: string -> Flow<'a,ProcessError,unit>FailureThe workflow failed due to a specific cause.
Axial.Cause`1Represents the cause of a failed workflow. The type of the domain-specific failure value.
FailAn expected domain-specific failure.
failure: ProcessStartFailureshellOpen: string -> Flow<'env,ProcessError,unit>Opens a file, folder or URL with the operating system's default handler. The live service uses ShellExecute on Windows, open on macOS and xdg-open elsewhere, and does not wait for the handler. The flow succeeds once the opener has started, so a target that no handler accepts is not reported. To reveal a file in the file manager, open its folder. The launch goes through IProcess.Launch, so Process.test () records it in Launches. Opening a target can run a program. ShellExecute runs an .exe, .bat or .cmd file, open runs an .app bundle or an executable script, and xdg-open hands the target to whatever the desktop associates with it. Do not pass a path or URL from an untrusted source without checking it, for example that it is an https: URL or a folder the application owns. An empty target, or one starting with - (which an opener reads as an option), fails with ProcessError.ShellOpenRejected and starts nothing; prefix a relative path with ./. A path or URL. A flow that fails with ProcessError.ShellOpenRejected for a refused target and ProcessError.StartFailed when the opener cannot be started.
ShellOpenRejectedProcess.shellOpen - open xdg-open
rejection: ProcessShellOpenRejectionTarget: stringRequires & types
Requires: open Axial; open Axial.Process (packages: Axial, Axial.Process)val host: TestProcessHostEnvironmentval notInstalled: ProcessErrorA custom IProcess implements Launch alongside Execute. A host that cannot start desktop programs, such as a
browser worker, returns ProcessError.StartFailed from Launch.

