Skip to content

Commit cebcd20

Browse files
authored
Merge pull request #316 from SAFE-Stack/v5-docs-add-build-automation
Add recipe: 'Add build automation'
2 parents b30a120 + dcd4943 commit cebcd20

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+

docs/v4-recipes/build/add-build-script.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dotnet new console -lang f# -n Build -o .
2020

2121
Open the project you just created in your IDE and rename the module it contains from `Program.fs` to `Build.fs`.
2222

23-
This renaming is't explicitly necessary, however it keeps your solution consistent with other SAFE apps and is a better name for the file really.
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.
2424

2525
> 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
2626

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ nav:
7070
- Upgrade from V4 to V5: "recipes/upgrading/v4-to-v5.md"
7171
- Create a new Recipe: "recipes/template.md"
7272
- Build:
73+
- Add build automation: "recipes/build/add-build-script.md"
7374
- Create a docker image: "recipes/build/docker-image.md"
7475
- Remove FAKE: "recipes/build/remove-fake.md"
7576
- Package my SAFE app for deployment: "recipes/build/bundle-app.md"

0 commit comments

Comments
 (0)