forked from fsprojects/FsReveal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
214 lines (176 loc) · 6.62 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#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 = "ImaginaryDevelopment"
let gitHome = "https://github.com/" + gitOwner
// The name of the project on GitHub
let gitProjectName = "FsReveal"
// The name of the GitHub repo subdirectory to publish slides to
let gitSubDir = ""
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 "Release" (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"
// )
// Target "ReleaseSlides" (fun _ ->
// let tempDocsDir = __SOURCE_DIRECTORY__ </> "temp/gh-pages"
// StageAll tempDocsDir
// Git.Commit.Commit tempDocsDir "Update generated slides"
// Branches.push tempDocsDir
// )
let tempDocsRoot = __SOURCE_DIRECTORY__ </> "temp/gh-pages"
let tempDocsDir = tempDocsRoot </> gitSubDir
// would be lovely if we check for uncommitted/unpushed changes in the local branch before allowing new releases to be generated and pushed up
// Git.FileStatus.getChangedFiles should be able to do this check
Target "ReleaseSlides" (fun _ ->
if gitOwner = "myGitUser" || gitProjectName = "MyProject" then
failwith "You need to specify the gitOwner and gitProjectName in build.fsx"
CleanDir tempDocsRoot
printfn "cloning into %s" tempDocsDir
Repository.cloneSingleBranch "" (gitHome + "/" + gitProjectName + ".git") "gh-pages" tempDocsDir
fullclean tempDocsDir
CopyRecursive outDir tempDocsDir true |> tracefn "%A"
printfn "Staging"
StageAll tempDocsRoot
Git.Commit.Commit tempDocsRoot "Update generated slides"
printfn "Pushing"
Branches.push tempDocsRoot
)
Target "Push" (fun _ ->
if not <| Directory.Exists tempDocsDir then
failwithf "Slides not prepared for release, could not locate %s" tempDocsDir
Branches.push tempDocsRoot
)
// my own version of their Release just produce the output, don't do any git fetch/stage/commit/push
Target "Release" (fun _ ->
if gitProjectName = "MyProject" then
failwith "You need to specify the gitOwner and gitProjectName in build.fsx"
let tempDocsRoot = __SOURCE_DIRECTORY__ </> "temp/gh-pages"
let tempDocsDir = tempDocsRoot </> gitSubDir
CleanDir tempDocsRoot
fullclean tempDocsDir
CopyRecursive outDir tempDocsDir true |> tracefn "%A"
)
"Clean"
==> "GenerateSlides"
==> "KeepRunning"
"GenerateSlides"
==> "Release"
For "Release" ["GenerateSlides"]
For "ReleaseSlides" ["GenerateSlides"]
"GenerateSlides"
==> "Release"
==> "ReleaseSlides"
RunTargetOrDefault "KeepRunning"