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

Remove root parameter override in watch mode #925

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
34 changes: 25 additions & 9 deletions src/fsdocs-tool/BuildCommand.fs
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,11 @@ module Serve =
let refreshEvent = FSharp.Control.Event<string>()

/// generate the script to inject into html to enable hot reload during development
let generateWatchScript (port: int) =
let tag =
"""
let generateWatchScript =
"""
<script type="text/javascript">
var wsUri = "ws://localhost:{{PORT}}/websocket";
var loc = window.location.valueOf();
var wsUri = `${loc.protocol.replace("http", "ws")}//${loc.host}/websocket`;
function init()
{
websocket = new WebSocket(wsUri);
Expand All @@ -823,8 +823,6 @@ module Serve =
</script>
"""

tag.Replace("{{PORT}}", string<int> port)

let connectedClients = ConcurrentDictionary<WebSocket, unit>()

let socketHandler (webSocket: WebSocket) (context: HttpContext) =
Expand Down Expand Up @@ -1406,10 +1404,13 @@ type CoreBuildOptions(watch) =
// Adjust the user substitutions for 'watch' mode root
let userRoot, userParameters =
if watch then
let userRoot = sprintf "http://localhost:%d/" this.port_option
let userRoot =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would

let userRoot =
    match userParametersDict.TryGetValue(ParamKeys.root) with
    | true, root -> root
    | false, _ -> sprintf "http://localhost:%d/" this.port_option

work for you here?
I believe it can be an easy way to make

fsdocs watch --parameters root "/"                                                                                                                                                                               

work and would be the least disruptive for now.

We don't feel good enough about the new setting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will test and let you know! Thanks for talking it through!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, thanks for your patience!

match this.static_content_host_option with
| Some s -> sprintf "%O" s
| _ -> sprintf "http://localhost:%d/" this.port_option

if userParametersDict.ContainsKey(ParamKeys.root) then
printfn "ignoring user-specified root since in watch mode, root = %s" userRoot
printfn "ignoring user-specified root since in watch mode, root = '%s'" userRoot

let userParameters =
[ ParamKeys.root, userRoot ]
Expand Down Expand Up @@ -1667,7 +1668,7 @@ type CoreBuildOptions(watch) =
let getLatestWatchScript () =
if watch then
// if running in watch mode, inject hot reload script
[ ParamKeys.``fsdocs-watch-script``, Serve.generateWatchScript this.port_option ]
[ ParamKeys.``fsdocs-watch-script``, Serve.generateWatchScript ]
else
// otherwise, inject empty replacement string
[ ParamKeys.``fsdocs-watch-script``, "" ]
Expand Down Expand Up @@ -2088,6 +2089,9 @@ type CoreBuildOptions(watch) =
abstract port_option: int
default x.port_option = 0

abstract static_content_host_option: Uri option
default x.static_content_host_option = None

[<Verb("build", HelpText = "build the documentation for a solution based on content and defaults")>]
type BuildCommand() =
inherit CoreBuildOptions(false)
Expand Down Expand Up @@ -2115,3 +2119,15 @@ type WatchCommand() =

[<Option("port", Required = false, Default = 8901, HelpText = "Port to serve content for http://localhost serving.")>]
member val port = 8901 with get, set

[<Option("contenturlroot",
Required = false,
HelpText =
"Optional URL root 'http[s]://<host>[:<port>]' to use in static content for browsers not running on localhost.")>]
member val contenturlroot = "" with get, set

override x.static_content_host_option =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code violates some analyzers:

image

Run dotnet fsi build.fsx -- -p Verify for more info.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip with -p Verify! That's helpful.

Though the output is quite verbose with results from the other projects. It also seems to create diffs in the tests/ directory for me. Not sure if that's intentional.

if not (String.IsNullOrEmpty x.contenturlroot) then
x.contenturlroot |> Uri |> Some
else
None
Loading