-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.fsx
138 lines (113 loc) · 4.01 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
#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 = "kimsk"
let gitHome = "https://github.com/" + gitOwner
// The name of the project on GitHub
let gitProjectName = "fsreveal-sample-md"
open FsReveal
open Fake
open Fake.Git
open System.IO
open System.Diagnostics
open Suave
open Suave.Web
open Suave.Http
open Suave.Http.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
!! (slidesDir @@ "images/*.*")
|> CopyFiles (outDir @@ "images")
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 handleWatcherEvents (e:FileSystemEventArgs) =
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
let startWebServer () =
let serverConfig =
{ defaultConfig with
homeFolder = Some (FullName outDir)
}
let app =
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 "http://localhost:8083/fsreveal.html" |> ignore
Target "GenerateSlides" (fun _ ->
!! (slidesDir @@ "*.md")
++ (slidesDir @@ "*.fsx")
|> Seq.map fileInfo
|> Seq.iter generateFor
)
Target "KeepRunning" (fun _ ->
use watcher = new FileSystemWatcher(FullName slidesDir,"*.*")
watcher.EnableRaisingEvents <- true
watcher.IncludeSubdirectories <- true
watcher.Changed.Add(handleWatcherEvents)
watcher.Created.Add(handleWatcherEvents)
watcher.Renamed.Add(handleWatcherEvents)
startWebServer ()
traceImportant "Waiting for slide edits. Press any key to stop."
System.Console.ReadKey() |> ignore
watcher.EnableRaisingEvents <- false
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"