-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.fsx
89 lines (76 loc) · 3.49 KB
/
build.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// --------------------------------------------------------------------------------------
// Generates the playground in `output` folder and hosts it with background watcher
// --------------------------------------------------------------------------------------
#load "packages/FSharp.Formatting/FSharp.Formatting.fsx"
#r "packages/FAKE/tools/FakeLib.dll"
#r "packages/Suave/lib/net40/Suave.dll"
open Fake
open Suave
open System
open System.IO
open System.Text
open Suave
open Suave.Web
open Suave.Http
// --------------------------------------------------------------------------------------
// Generating the web site
// --------------------------------------------------------------------------------------
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
/// Generates the javascript file by invoking `fsi translate.fsx`
let generateScript () =
trace "Translating to JavaScript"
let _, res = FSIHelper.executeFSI __SOURCE_DIRECTORY__ "translate.fsx" []
for line in res do
if line.IsError then traceError line.Message
else printfn "%s" line.Message
/// Generates the `index.html` file (by translating Markdown blocks)
let processFile (name) =
trace "Generating index file"
let fi = File.ReadAllText("content/" + name + ".html")
let reg = RegularExpressions.Regex(">>>>(.*?)<<<<", RegularExpressions.RegexOptions.Singleline)
let counter = ref 0
let output = reg.Replace(fi,RegularExpressions.MatchEvaluator(fun m ->
let md = m.Groups.[1].Value.Split [|'\n'|]
let drop = md |> Seq.filter (System.String.IsNullOrWhiteSpace >> not) |> Seq.map (Seq.takeWhile ((=) ' ') >> Seq.length) |> Seq.min
let md = md |> Seq.map (fun s -> if System.String.IsNullOrWhiteSpace s then s else s.Substring(drop)) |> String.concat "\n"
let doc = FSharp.Literate.Literate.ParseMarkdownString(md)
incr counter
FSharp.Literate.Literate.WriteHtml(doc, prefix=sprintf "s%d" counter.Value, lineNumbers=false) ))
File.WriteAllText("output/" + name + ".html", output)
/// Copies static CSS and JS files to output
let copyFiles () =
trace "Copying static files"
!!"web/*" |> CopyFiles "output"
for d in Directory.GetDirectories("web") do
CopyDir ("output" </> Path.GetFileName d) d (fun _ -> true)
// --------------------------------------------------------------------------------------
// FAKE targets
// --------------------------------------------------------------------------------------
Target "generate" (fun _ ->
CleanDir "output"
copyFiles()
generateScript()
processFile "index"
processFile "slides"
)
Target "run" (fun _ ->
// Start Suave & open web browser with the site
let app = Files.browse (Path.Combine(__SOURCE_DIRECTORY__, "output"))
let _, server = startWebServerAsync defaultConfig app
Async.Start(server)
System.Diagnostics.Process.Start("http://localhost:8083/index.html") |> ignore
// Watch for changes & reload when app.fsx changes
use watcher1 =
{ BaseDirectory = __SOURCE_DIRECTORY__; Includes = [ "**/*.fs*" ]; Excludes = [] }
|> WatchChanges (fun _ -> generateScript())
use watcher2 =
{ BaseDirectory = __SOURCE_DIRECTORY__; Includes = [ "**/web/*" ]; Excludes = [] }
|> WatchChanges (fun _ -> copyFiles ())
use watcher3 =
{ BaseDirectory = __SOURCE_DIRECTORY__; Includes = [ "**/content/*" ]; Excludes = [] }
|> WatchChanges (fun _ -> processFile "index"; processFile "slides")
traceImportant "Waiting for app.fsx edits. Press any key to stop."
System.Console.ReadLine() |> ignore
)
"generate" ==> "run"
RunTargetOrDefault "run"