-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.fsx
183 lines (150 loc) · 5.05 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
#r "paket:
nuget Argu
nuget Fake.DotNet.Cli
nuget Fake.IO.FileSystem
nuget Fake.JavaScript.Yarn
nuget Fake.Core.Target //"
#load ".fake/build.fsx/intellisense.fsx"
open Argu
open Fake.Core
open Fake.Core.TargetOperators
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.DotNet
open Fake.JavaScript
open System
open System.Collections.Generic
open System.Threading
open System.Text
open System.Text.RegularExpressions
let webProject = "src/Konsens.Web"
let pagesDir = webProject </> "Pages"
let outDir = webProject </> "wwwroot"
let indexHtml = "index.html"
module UserInput =
open System.Threading.Tasks
let waitForAnyKey (cancellationToken: CancellationToken) =
let x = Task.Run(fun () -> System.Console.ReadKey())
let tcs = TaskCompletionSource()
use __ = cancellationToken.Register(fun () -> tcs.SetCanceled())
Task.WhenAny(x, tcs.Task).Wait()
module Tasks =
open System.IO
let waitFor file =
TaskRunner.waitFor (fun () ->
try
System.IO.File.Open(file, FileMode.Open, FileAccess.Read).Dispose()
true
with
_ -> false) (TimeSpan.FromSeconds(1.)) 100 id |> ignore
let buildCss () =
let result =
[pagesDir </> "Styles.styl"; "-o"; outDir]
|> CreateProcess.fromRawCommand "node_modules/.bin/stylus.cmd"
|> CreateProcess.redirectOutput
|> CreateProcess.disableTraceCommand
|> Proc.run
if result.ExitCode <> 0 then
Trace.traceError result.Result.Error
failwith "Error while compiling CSS"
Trace.tracef "CSS compiled to %s.\n" outDir
let buildHtml () =
let evaluator = MatchEvaluator(fun m ->
let indent s = m.Groups.[1].Value + s
let src = m.Groups.[2].Value
seq {
yield "<!-- " + src + " -->"
yield! File.read (pagesDir </> src)
yield "" }
|> Seq.map indent
|> String.toLines)
let replaceTemplates s =
Regex.Replace(s, """^(\s*)<page-templates src="([^"]+)" \/>""", evaluator)
let hash = HashSet<string>()
let validateTemplates s =
Regex.Matches(s, @"ws-(?:children-)?template=\""([^""]+)\""")
|> Seq.cast<Match>
|> Seq.map (fun m -> m.Groups.[1].Value)
|> Seq.tryFind (hash.Add >> not)
|> Option.map (failwithf "Template '%s' exists multiple times")
|> ignore
s
pagesDir </> indexHtml
|> File.read
|> Seq.map (replaceTemplates >> validateTemplates)
|> File.writeWithEncoding Encoding.UTF8 false (outDir </> indexHtml)
Trace.tracef "HTML compiled to %s.\n" outDir
let restoreYarn () = Yarn.install id
let restoreLibs () =
let options o =
o
|> DotNet.Options.withWorkingDirectory webProject
|> DotNet.Options.withRedirectOutput true
let result = DotNet.exec options "libman" "restore"
if not (List.isEmpty result.Errors) then failwith "Error on libman restore"
let buildApp config =
let setOptions (o: DotNet.BuildOptions) = { o with Configuration = config }
DotNet.build setOptions webProject
type BuildArgs =
| [<AltCommandLine "-r">] Release
interface IArgParserTemplate with
member s.Usage =
match s with
| Release -> "build with Release configuration."
Target.initEnvironment ()
Target.create "Clean" (fun _ ->
!! "src/**/bin"
++ "src/**/obj"
|> Shell.cleanDirs
)
Target.create "Restore" (fun _ ->
Tasks.restoreYarn ()
Tasks.restoreLibs ()
)
Target.create "BuildApp" (fun { Context = { Arguments = args } } ->
let cli = ArgumentParser.Create<BuildArgs>().ParseCommandLine(args |> List.toArray)
let config =
if cli.Contains(<@ Release @>) then
DotNet.BuildConfiguration.Release
else
DotNet.BuildConfiguration.Debug
Tasks.buildApp config
)
Target.create "Watch" (fun { Context = { CancellationToken = ct } } ->
let watch glob f = !! glob |> ChangeWatcher.run (fun changes ->
let change = Seq.head changes
Trace.logf "Change in '%s'\n" change.Name
Tasks.waitFor change.FullPath
f()
)
let watchers = [
watch (pagesDir </> "**/*.styl") Tasks.buildCss
watch (pagesDir </> "**/*.html") Tasks.buildHtml
]
Trace.log "Waiting for changes..."
UserInput.waitForAnyKey ct
watchers |> Seq.iter (fun d -> d.Dispose())
)
Target.create "BuildCSS" (fun _ -> Tasks.buildCss())
Target.create "BuildHTML" (fun _ -> Tasks.buildHtml())
Target.create "Build" ignore
Target.create "RestoreAndBuild" ignore
"Restore"
?=> "BuildHTML"
?=> "BuildCSS"
?=> "BuildApp"
"RestoreAndBuild" <== [
"Restore"
"Build"
]
"Build" <== [
"BuildApp"
"BuildHTML"
"BuildCSS"
]
"Watch" <== [
"BuildHTML"
"BuildCSS"
]
Target.runOrDefaultWithArguments "RestoreAndBuild"