If I go through the "How to start" guide for Saturn:
https://saturnframework.org/tutorials/how-to-start.html
when I run the last step:
dotnet fake build -t run
the webserver does indeed appear to start. I can go to http://localhost:8085/books and see the resulting application.
However, if I ctrl-c at the console to stop the webserver, I notice the following:
If I look at build.fsx
, it contains the following line:
Process.start (fun i -> { i with FileName = "http://localhost:8085" }) |> ignore
within the Run
target:
Target.create "Run" (fun _ ->
let server = async {
DotNet.exec (fun p -> { p with WorkingDirectory = appPath } ) "watch" "run" |> ignore
}
let browser = async {
Thread.Sleep 5000
Process.start (fun i -> { i with FileName = "http://localhost:8085" }) |> ignore
}
[ server; browser]
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
)
So it looks like the intention of the Run
target is to start a browser for us.
The warning in the output above indicates:
Warning FS0044: This construct is deprecated. use the CreateProcess APIs instead.
The Just start a process page in the Fake documentation has some examples. Going off those, I used the changed build.fsx
to use the following line:
CreateProcess.fromRawCommand "cmd.exe" [ "/C"; "start http://localhost:8085" ] |> Proc.run |> ignore
And that does indeed seem to work.
Couple of questions:
- Is that the recommended way to achieve something like this?
- The above can only be expected to work on Windows. Is there a cross-platform way to open a URL with a browser using Fake?
Note
If you decide to go through the Saturn "How to start" guide, I recommend that you remove the file global.json
after step 3. The default one currently looks for a particular version of .NET SDK. There is a pull-request open to resolve this.
Thanks!
You can see the Plotly.NET source for a way to start a browser: https://github.com/plotly/Plotly.NET/blob/62f297649320783ea0e64725ff4703bb225268d0/src/Plotly.NET/ChartExtensions.fs#L16