|
| 1 | +# How do I add build automation to the project? |
| 2 | + |
| 3 | +## FAKE |
| 4 | +[Fake](https://fake.build/) is a DSL for build tasks that is modular, extensible and easy to start with. Fake allows you to easily build, bundle, deploy your app and more by executing a single command. |
| 5 | + |
| 6 | +> The standard template comes [with a FAKE project](../../../template-safe-commands) by default, so **this recipe only applies to the minimal template**. |
| 7 | +
|
| 8 | +--- |
| 9 | +#### 1. Create a build project |
| 10 | + |
| 11 | +Create a new console app called 'Build' at the root of your solution |
| 12 | + |
| 13 | +```fsharp |
| 14 | +dotnet new console -lang f# -n Build -o . |
| 15 | +``` |
| 16 | + |
| 17 | +> We are creating the project directly at the root of the solution in order to allow us to execute the build without needing to navigate into a subfolder. |
| 18 | +
|
| 19 | +#### 2. Create a build script |
| 20 | + |
| 21 | +Open the project you just created in your IDE and rename the module it contains from `Program.fs` to `Build.fs`. |
| 22 | + |
| 23 | +This renaming isn't explicitly necessary, however it keeps your solution consistent with other SAFE apps and is a better name for the file really. |
| 24 | + |
| 25 | +> If you just rename the file directly rather than in your IDE, then the Build project won't be able to find it unless you edit the Build.fsproj file as well |
| 26 | +
|
| 27 | +Open `Build.fs` and paste in the following code. |
| 28 | + |
| 29 | +```fsharp |
| 30 | +open Fake.Core |
| 31 | +open Fake.IO |
| 32 | +open System |
| 33 | +
|
| 34 | +let redirect createProcess = |
| 35 | + createProcess |
| 36 | + |> CreateProcess.redirectOutputIfNotRedirected |
| 37 | + |> CreateProcess.withOutputEvents Console.WriteLine Console.WriteLine |
| 38 | +
|
| 39 | +let createProcess exe arg dir = |
| 40 | + CreateProcess.fromRawCommandLine exe arg |
| 41 | + |> CreateProcess.withWorkingDirectory dir |
| 42 | + |> CreateProcess.ensureExitCode |
| 43 | +
|
| 44 | +let dotnet = createProcess "dotnet" |
| 45 | +
|
| 46 | +let npm = |
| 47 | + let npmPath = |
| 48 | + match ProcessUtils.tryFindFileOnPath "npm" with |
| 49 | + | Some path -> path |
| 50 | + | None -> failwith "npm was not found in path." |
| 51 | + createProcess npmPath |
| 52 | +
|
| 53 | +let run proc arg dir = |
| 54 | + proc arg dir |
| 55 | + |> Proc.run |
| 56 | + |> ignore |
| 57 | +
|
| 58 | +let execContext = Context.FakeExecutionContext.Create false "build.fsx" [ ] |
| 59 | +Context.setExecutionContext (Context.RuntimeContext.Fake execContext) |
| 60 | +
|
| 61 | +Target.create "Clean" (fun _ -> Shell.cleanDir (Path.getFullName "deploy")) |
| 62 | +
|
| 63 | +Target.create "InstallClient" (fun _ -> run npm "install" ".") |
| 64 | +
|
| 65 | +Target.create "Run" (fun _ -> |
| 66 | + run dotnet "build" (Path.getFullName "src/Shared") |
| 67 | + [ dotnet "watch run" (Path.getFullName "src/Server") |
| 68 | + dotnet "fable watch --run npx vite" (Path.getFullName "src/Client") ] |
| 69 | + |> Seq.toArray |
| 70 | + |> Array.map redirect |
| 71 | + |> Array.Parallel.map Proc.run |
| 72 | + |> ignore |
| 73 | +) |
| 74 | +
|
| 75 | +open Fake.Core.TargetOperators |
| 76 | +
|
| 77 | +let dependencies = [ |
| 78 | + "Clean" |
| 79 | + ==> "InstallClient" |
| 80 | + ==> "Run" |
| 81 | +] |
| 82 | +
|
| 83 | +[<EntryPoint>] |
| 84 | +let main args = |
| 85 | + try |
| 86 | + match args with |
| 87 | + | [| target |] -> Target.runOrDefault target |
| 88 | + | _ -> Target.runOrDefault "Run" |
| 89 | + 0 |
| 90 | + with e -> |
| 91 | + printfn "%A" e |
| 92 | + 1 |
| 93 | +``` |
| 94 | + |
| 95 | +#### 3. Add the project to the solution |
| 96 | + |
| 97 | +Run the following command |
| 98 | + |
| 99 | +```bash |
| 100 | +dotnet sln add Build.fsproj |
| 101 | +``` |
| 102 | +#### 4. Installing dependencies |
| 103 | + |
| 104 | +You will need to install the following dependencies: |
| 105 | + |
| 106 | +``` |
| 107 | +Fake.Core.Target |
| 108 | +Fake.IO.FileSystem |
| 109 | +``` |
| 110 | + |
| 111 | +We recommend migrating to [Paket](https://fsprojects.github.io/Paket/). |
| 112 | +It is possible to use FAKE without Paket, however this will not be covered in this recipe. |
| 113 | + |
| 114 | +#### 5. Run the app |
| 115 | + |
| 116 | +At the root of the solution, run `dotnet paket install` to install all your dependencies. |
| 117 | + |
| 118 | +If you now execute `dotnet run`, the default target will be run. This will build the app in development mode and launch it locally. |
| 119 | + |
| 120 | +To learn more about targets and FAKE in general, see [Getting Started with FAKE](https://fake.build/guide/getting-started.html#Minimal-Example). |
| 121 | + |
0 commit comments