-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
163 lines (133 loc) · 4.77 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#I @"packages/FsReveal/fsreveal/"
#I @"packages/FAKE/tools/"
#I @"packages/Suave/lib/net40"
#r "FakeLib.dll"
#r "Suave.dll"
#load "fsreveal.fsx"
// Git configuration (used for publishing documentation in gh-pages branch)
// The profile where the project is posted
let gitOwner = "clicksco"
let gitHome = "https://github.com/" + gitOwner
// The name of the project on GitHub
let gitProjectName = "freya-intro"
open FsReveal
open Fake
open Fake.Git
open System.IO
open System.Diagnostics
open Suave
open Suave.Web
open Suave.Http
open Suave.Operators
open Suave.Sockets
open Suave.Sockets.Control
open Suave.Sockets.AsyncSocket
open Suave.WebSocket
open Suave.Utils
open Suave.Files
let outDir = __SOURCE_DIRECTORY__ </> "output"
let slidesDir = __SOURCE_DIRECTORY__ </> "slides"
Target "Clean" (fun _ ->
CleanDirs [outDir]
)
let fsiEvaluator =
let evaluator = FSharp.Literate.FsiEvaluator()
evaluator.EvaluationFailed.Add(fun err ->
traceImportant <| sprintf "Evaluating F# snippet failed:\n%s\nThe snippet evaluated:\n%s" err.StdErr err.Text )
evaluator
let copyStylesheet() =
try
CopyFile (outDir </> "css" </> "custom.css") (slidesDir </> "custom.css")
with
| exn -> traceImportant <| sprintf "Could not copy stylesheet: %s" exn.Message
let copyPics() =
try
CopyDir (outDir </> "images") (slidesDir </> "images") (fun f -> true)
with
| exn -> traceImportant <| sprintf "Could not copy picture: %s" exn.Message
let generateFor (file:FileInfo) =
try
copyPics()
let rec tryGenerate trials =
try
FsReveal.GenerateFromFile(file.FullName, outDir, fsiEvaluator = fsiEvaluator)
with
| exn when trials > 0 -> tryGenerate (trials - 1)
| exn ->
traceImportant <| sprintf "Could not generate slides for: %s" file.FullName
traceImportant exn.Message
tryGenerate 3
copyStylesheet()
with
| :? FileNotFoundException as exn ->
traceImportant <| sprintf "Could not copy file: %s" exn.FileName
let refreshEvent = new Event<_>()
let handleWatcherEvents (events:FileChange seq) =
for e in events do
let fi = fileInfo e.FullPath
traceImportant <| sprintf "%s was changed." fi.Name
match fi.Attributes.HasFlag FileAttributes.Hidden || fi.Attributes.HasFlag FileAttributes.Directory with
| true -> ()
| _ -> generateFor fi
refreshEvent.Trigger()
let socketHandler (webSocket : WebSocket) =
fun cx -> socket {
while true do
let! refreshed =
Control.Async.AwaitEvent(refreshEvent.Publish)
|> Suave.Sockets.SocketOp.ofAsync
do! webSocket.send Text (ASCII.bytes "refreshed") true
}
let startWebServer () =
let rec findPort port =
let portIsTaken =
if isMono then false else
System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()
|> Seq.exists (fun x -> x.Port = port)
if portIsTaken then findPort (port + 1) else port
let port = findPort 8083
let serverConfig =
{ defaultConfig with
homeFolder = Some (FullName outDir)
bindings = [ HttpBinding.mkSimple HTTP "127.0.0.1" port ]
}
let app =
choose [
Filters.path "/websocket" >=> handShake socketHandler
Writers.setHeader "Cache-Control" "no-cache, no-store, must-revalidate"
>=> Writers.setHeader "Pragma" "no-cache"
>=> Writers.setHeader "Expires" "0"
>=> browseHome ]
startWebServerAsync serverConfig app |> snd |> Async.Start
Process.Start (sprintf "http://localhost:%d/index.html" port) |> ignore
Target "GenerateSlides" (fun _ ->
!! (slidesDir + "/**/*.md")
++ (slidesDir + "/**/*.fsx")
|> Seq.map fileInfo
|> Seq.iter generateFor
)
Target "KeepRunning" (fun _ ->
use watcher = !! (slidesDir + "/**/*.*") |> WatchChanges handleWatcherEvents
startWebServer ()
traceImportant "Waiting for slide edits. Press any key to stop."
System.Console.ReadKey() |> ignore
watcher.Dispose()
)
Target "ReleaseSlides" (fun _ ->
if gitOwner = "myGitUser" || gitProjectName = "MyProject" then
failwith "You need to specify the gitOwner and gitProjectName in build.fsx"
let tempDocsDir = __SOURCE_DIRECTORY__ </> "temp/gh-pages"
CleanDir tempDocsDir
Repository.cloneSingleBranch "" (gitHome + "/" + gitProjectName + ".git") "gh-pages" tempDocsDir
fullclean tempDocsDir
CopyRecursive outDir tempDocsDir true |> tracefn "%A"
StageAll tempDocsDir
Git.Commit.Commit tempDocsDir "Update generated slides"
Branches.push tempDocsDir
)
"Clean"
==> "GenerateSlides"
==> "KeepRunning"
"GenerateSlides"
==> "ReleaseSlides"
RunTargetOrDefault "KeepRunning"