-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
250 lines (211 loc) · 7.97 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#r @"packages/build/FAKE/tools/FakeLib.dll"
open Fake
open Fake.Git
open Fake.AssemblyInfoFile
open Fake.ReleaseNotesHelper
open Fake.UserInputHelper
open Fake.YarnHelper
open System
open System.IO
open System.Text.RegularExpressions
#if MONO
// prevent incorrect output encoding (e.g. https://github.com/fsharp/FAKE/issues/1196)
System.Console.OutputEncoding <- System.Text.Encoding.UTF8
#endif
let dotnetcliVersion = "2.0.0"
let mutable dotnetExePath = "dotnet"
let release = LoadReleaseNotes "RELEASE_NOTES.md"
let srcGlob = "src/**/*.fsproj"
let testsGlob = "tests/**/*.fsproj"
module Util =
let visitFile (visitor: string->string) (fileName : string) =
File.ReadAllLines(fileName)
|> Array.map (visitor)
|> fun lines -> File.WriteAllLines(fileName, lines)
let replaceLines (replacer: string->Match->string option) (reg: Regex) (fileName: string) =
fileName |> visitFile (fun line ->
let m = reg.Match(line)
if not m.Success
then line
else
match replacer line m with
| None -> line
| Some newLine -> newLine)
// Module to print colored message in the console
module Logger =
let consoleColor (fc : ConsoleColor) =
let current = Console.ForegroundColor
Console.ForegroundColor <- fc
{ new IDisposable with
member x.Dispose() = Console.ForegroundColor <- current }
let warn str = Printf.kprintf (fun s -> use c = consoleColor ConsoleColor.DarkYellow in printf "%s" s) str
let warnfn str = Printf.kprintf (fun s -> use c = consoleColor ConsoleColor.DarkYellow in printfn "%s" s) str
let error str = Printf.kprintf (fun s -> use c = consoleColor ConsoleColor.Red in printf "%s" s) str
let errorfn str = Printf.kprintf (fun s -> use c = consoleColor ConsoleColor.Red in printfn "%s" s) str
Target "Clean" (fun _ ->
["bin"]
|> CleanDirs
!! srcGlob
|> Seq.collect(fun p ->
["bin";"obj"]
|> Seq.map(fun sp ->
Path.GetDirectoryName p @@ sp)
)
|> CleanDirs
)
Target "InstallDotNetCore" (fun _ ->
dotnetExePath <- DotNetCli.InstallDotNetSDK dotnetcliVersion
)
Target "YarnInstall"(fun _ ->
Yarn (fun p ->
{ p with
Command = Install Standard
})
)
Target "DotnetRestore" (fun _ ->
!! srcGlob
++ testsGlob
|> Seq.iter (fun proj ->
DotNetCli.Restore (fun c ->
{ c with
Project = proj
ToolPath = dotnetExePath
//This makes sure that Proj2 references the correct version of Proj1
AdditionalArgs = [sprintf "/p:PackageVersion=%s" release.NugetVersion]
})
))
Target "DotnetBuild" (fun _ ->
!! srcGlob
|> Seq.iter (fun proj ->
DotNetCli.Build (fun c ->
{ c with
Project = proj
ToolPath = dotnetExePath
})
))
let fableSplitter workingDir =
DotNetCli.RunCommand(fun c ->
{ c with WorkingDir = workingDir
ToolPath = dotnetExePath }
) "fable npm-run build:test"
let mocha args =
Yarn(fun yarnParams ->
{ yarnParams with Command = args |> sprintf "run mocha -- %s" |> YarnCommand.Custom }
)
Target "QuickTest" (fun _ ->
!! testsGlob
|> Seq.iter(fun proj ->
let projDir = proj |> DirectoryName
printfn "projDir: %A" projDir
//Compile to JS
fableSplitter projDir
//Run mocha tests
let projDirOutput = projDir </> "bin" </> "lib" </> "**/*.test.js"
mocha projDirOutput
)
)
Target "MochaTest" ignore
Target "Meta" (fun _ ->
[ "<Project xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">"
"<PropertyGroup>"
"<Description>A Validation library for Fable</Description>"
"<PackageProjectUrl>https://github.com/zaaack/fable-validation</PackageProjectUrl>"
"<PackageLicenseUrl>https://raw.githubusercontent.com/zaaack/fable-validation/master/LICENSE.md</PackageLicenseUrl>"
// "<PackageIconUrl>https://raw.githubusercontent.com/zaaack/fable-validation/master/docs/files/img/logo.png</PackageIconUrl>"
"<RepositoryUrl>https://github.com/zaaack/fable-validation.git</RepositoryUrl>"
"<PackageTags>fable;elm;elmish;validation;fsharp</PackageTags>"
"<Authors>Zack Young</Authors>"
sprintf "<Version>%s</Version>" (string release.SemVer)
"</PropertyGroup>"
"</Project>"]
|> WriteToFile false "Meta.props"
)
Target "DotnetPack" (fun _ ->
!! srcGlob
|> Seq.iter (fun proj ->
DotNetCli.Pack (fun c ->
{ c with
Project = proj
Configuration = "Release"
ToolPath = dotnetExePath
AdditionalArgs =
[
sprintf "/p:PackageVersion=%s" release.NugetVersion
sprintf "/p:PackageReleaseNotes=\"%s\"" (String.Join("\n",release.Notes))
]
})
)
)
let needsPublishing (versionRegex: Regex) (releaseNotes: ReleaseNotes) projFile =
printfn "Project: %s" projFile
if releaseNotes.NugetVersion.ToUpper().EndsWith("NEXT")
then
Logger.warnfn "Version in Release Notes ends with NEXT, don't publish yet."
false
else
File.ReadLines(projFile)
|> Seq.tryPick (fun line ->
let m = versionRegex.Match(line)
if m.Success then Some m else None)
|> function
| None -> failwith "Couldn't find version in project file"
| Some m ->
let sameVersion = m.Groups.[1].Value = releaseNotes.NugetVersion
if sameVersion then
Logger.warnfn "Already version %s, no need to publish." releaseNotes.NugetVersion
not sameVersion
Target "GenDocs" <| fun _ ->
// FSIHelper would cause errors, don't know why
let out = ExecProcessAndReturnMessages (fun p ->
p.FileName <- if EnvironmentHelper.isWindows then "fsi" else "fsharpi"
p.Arguments <- "./tools/docgen.fsx"
p.WorkingDirectory <- Environment.CurrentDirectory
p.UseShellExecute <- false) TimeSpan.MaxValue
for err in out.Errors do printfn "%s" err
for msg in out.Messages do printfn "%s" msg
if not out.OK then
printfn "Warn: Generate docs has errors"
Target "Publish" (fun _ ->
let versionRegex = Regex("<Version>(.*?)</Version>", RegexOptions.IgnoreCase)
!! srcGlob
|> Seq.filter(needsPublishing versionRegex release)
|> Seq.iter(fun projFile ->
let projDir = Path.GetDirectoryName(projFile)
let nugetKey =
match environVarOrNone "NUGET_KEY" with
| Some nugetKey -> nugetKey
| None -> failwith "The Nuget API key must be set in a NUGET_KEY environmental variable"
Directory.GetFiles(projDir </> "bin" </> "Release", "*.nupkg")
|> Array.find (fun nupkg -> nupkg.Contains(release.NugetVersion))
|> (fun nupkg ->
(Path.GetFullPath nupkg, nugetKey)
||> sprintf "nuget push %s -s nuget.org -k %s"
|> DotNetCli.RunCommand (fun c ->
{ c with ToolPath = dotnetExePath }))
// After successful publishing, update the project file
(versionRegex, projFile) ||> Util.replaceLines (fun line _ ->
versionRegex.Replace(line, "<Version>" + release.NugetVersion + "</Version>") |> Some)
)
)
Target "Release" (fun _ ->
if Git.Information.getBranchName "" <> "master" then failwith "Not on master"
StageAll ""
Git.Commit.Commit "" (sprintf "Bump version to %s" release.NugetVersion)
Branches.push ""
Branches.tag "" release.NugetVersion
Branches.pushTag "" "origin" release.NugetVersion
)
"Clean"
==> "Meta"
==> "InstallDotNetCore"
==> "YarnInstall"
==> "DotnetRestore"
==> "DotnetBuild"
==> "MochaTest"
==> "DotnetPack"
==> "GenDocs"
==> "Publish"
==> "Release"
"QuickTest"
==> "MochaTest"
RunTargetOrDefault "DotnetPack"