|
| 1 | +namespace Fake.DotNet |
| 2 | + |
| 3 | +open System.IO |
| 4 | +open Fake.Core |
| 5 | +open Fake.IO |
| 6 | +open Fake.IO.FileSystemOperators |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Contains tasks to interact with <a href="https://github.com/ionide/Fornax">Fornax</a> static site generator |
| 10 | +/// for F# documentation generation. |
| 11 | +/// </summary> |
| 12 | +[<RequireQualifiedAccess>] |
| 13 | +module Fornax = |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Fornax build command parameters and options |
| 17 | + /// </summary> |
| 18 | + type BuildParams = { |
| 19 | + /// Working directory to run Fornax from (default: docs directory) |
| 20 | + WorkingDirectory : string option |
| 21 | + |
| 22 | + /// Timeout for the build process |
| 23 | + Timeout : System.TimeSpan option |
| 24 | + |
| 25 | + /// Whether to fail if Fornax returns non-zero exit code |
| 26 | + FailOnError : bool |
| 27 | + } with |
| 28 | + /// Parameter default values. |
| 29 | + static member Default = { |
| 30 | + WorkingDirectory = None |
| 31 | + Timeout = None |
| 32 | + FailOnError = true |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Fornax watch command parameters and options |
| 37 | + /// </summary> |
| 38 | + type WatchParams = { |
| 39 | + /// Working directory to run Fornax from (default: docs directory) |
| 40 | + WorkingDirectory : string option |
| 41 | + |
| 42 | + /// Port to serve content on (default: 8080) |
| 43 | + Port : int option |
| 44 | + |
| 45 | + /// Whether to fail if Fornax returns non-zero exit code |
| 46 | + FailOnError : bool |
| 47 | + } with |
| 48 | + /// Parameter default values. |
| 49 | + static member Default = { |
| 50 | + WorkingDirectory = None |
| 51 | + Port = None |
| 52 | + FailOnError = true |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Build documentation using Fornax |
| 57 | + /// </summary> |
| 58 | + /// |
| 59 | + /// <param name="setBuildParams">Function used to overwrite the build command default parameters.</param> |
| 60 | + /// |
| 61 | + /// <example> |
| 62 | + /// <code lang="fsharp"> |
| 63 | + /// Fornax.build (fun p -> { p with WorkingDirectory = Some "./docs" }) |
| 64 | + /// </code> |
| 65 | + /// </example> |
| 66 | + let build setBuildParams = |
| 67 | + let buildParams = setBuildParams BuildParams.Default |
| 68 | + |
| 69 | + let processArgs = |
| 70 | + CreateProcess.fromRawCommandLine "dotnet" "fornax build" |
| 71 | + |> CreateProcess.withTimeout (buildParams.Timeout |> Option.defaultValue (System.TimeSpan.FromMinutes 10.0)) |
| 72 | + |> (fun args -> |
| 73 | + match buildParams.WorkingDirectory with |
| 74 | + | Some dir -> CreateProcess.withWorkingDirectory dir args |
| 75 | + | None -> args) |
| 76 | + |> (fun args -> |
| 77 | + if buildParams.FailOnError then |
| 78 | + CreateProcess.ensureExitCode args |
| 79 | + else |
| 80 | + args) |
| 81 | + |
| 82 | + let result = processArgs |> Proc.run |
| 83 | + |
| 84 | + if buildParams.FailOnError && result.ExitCode <> 0 then |
| 85 | + failwithf "Fornax build failed with exit code %d" result.ExitCode |
| 86 | + else |
| 87 | + result |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Watch documentation using Fornax with hot reload |
| 91 | + /// </summary> |
| 92 | + /// |
| 93 | + /// <param name="setWatchParams">Function used to overwrite the watch command default parameters.</param> |
| 94 | + /// |
| 95 | + /// <example> |
| 96 | + /// <code lang="fsharp"> |
| 97 | + /// Fornax.watch (fun p -> { p with Port = Some 3000; WorkingDirectory = Some "./docs" }) |
| 98 | + /// </code> |
| 99 | + /// </example> |
| 100 | + let watch setWatchParams = |
| 101 | + let watchParams = setWatchParams WatchParams.Default |
| 102 | + |
| 103 | + let args = |
| 104 | + match watchParams.Port with |
| 105 | + | Some port -> $"fornax watch --port {port}" |
| 106 | + | None -> "fornax watch" |
| 107 | + |
| 108 | + let processArgs = |
| 109 | + CreateProcess.fromRawCommandLine "dotnet" args |
| 110 | + |> (fun args -> |
| 111 | + match watchParams.WorkingDirectory with |
| 112 | + | Some dir -> CreateProcess.withWorkingDirectory dir args |
| 113 | + | None -> args) |
| 114 | + |> (fun args -> |
| 115 | + if watchParams.FailOnError then |
| 116 | + CreateProcess.ensureExitCode args |
| 117 | + else |
| 118 | + args) |
| 119 | + |
| 120 | + let result = processArgs |> Proc.run |
| 121 | + |
| 122 | + if watchParams.FailOnError && result.ExitCode <> 0 then |
| 123 | + failwithf "Fornax watch failed with exit code %d" result.ExitCode |
| 124 | + else |
| 125 | + result |
| 126 | + |
| 127 | + /// <summary> |
| 128 | + /// Clean Fornax cache and generated files |
| 129 | + /// </summary> |
| 130 | + /// |
| 131 | + /// <param name="workingDirectory">Working directory where Fornax cache should be cleaned</param> |
| 132 | + let cleanCache workingDirectory = |
| 133 | + let cacheDir = workingDirectory </> "_public" |
| 134 | + let tempDir = workingDirectory </> "_temp" |
| 135 | + |
| 136 | + if Directory.Exists cacheDir then |
| 137 | + Shell.cleanDir cacheDir |
| 138 | + if Directory.Exists tempDir then |
| 139 | + Shell.cleanDir tempDir |
0 commit comments