-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
build.fsx
252 lines (207 loc) · 7.58 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
251
252
#r "paket: groupref netcorebuild //"
#load ".fake/build.fsx/intellisense.fsx"
#if !FAKE
#r "Facades/netstandard"
#r "netstandard"
#endif
#nowarn "52"
open System
open System.IO
open System.Text.RegularExpressions
open Fake.Core
open Fake.Core.TargetOperators
open Fake.DotNet
open Fake.IO
open Fake.IO.Globbing.Operators
open Fake.IO.FileSystemOperators
open Fake.Tools
open Fake.Api
open Fake.JavaScript
let versionFromGlobalJson : DotNet.CliInstallOptions -> DotNet.CliInstallOptions = (fun o ->
{ o with Version = DotNet.Version (DotNet.getSDKVersionFromGlobalJson()) }
)
let dotnetSdk = lazy DotNet.install versionFromGlobalJson
let inline dtntWorkDir wd =
DotNet.Options.lift dotnetSdk.Value
>> DotNet.Options.withWorkingDirectory wd
let inline yarnWorkDir (ws : string) (yarnParams : Yarn.YarnParams) =
{ yarnParams with WorkingDirectory = ws }
let root = __SOURCE_DIRECTORY__
let projectFile = "./src/Thoth.Fetch.fsproj"
let testsFile = "./tests/Tests.fsproj"
let gitOwner = "thoth-org"
let repoName = "Thoth.Fetch"
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
let run (cmd:string) dir args =
Command.RawCommand(cmd, Arguments.OfArgs args)
|> CreateProcess.fromCommand
|> CreateProcess.withWorkingDirectory dir
|> Proc.run
|> ignore
Target.create "Clean" (fun _ ->
!! "src/**/bin"
++ "src/**/obj"
++ "tests/**/bin"
++ "tests/**/obj"
|> Shell.cleanDirs
)
Target.create "YarnInstall"(fun _ ->
Yarn.install id
)
Target.create "DotnetRestore" (fun _ ->
DotNet.restore id projectFile
DotNet.restore id testsFile
)
Target.create "MochaTest" (fun _ ->
let projDir = testsFile |> Path.getDirectory
let configFile = projDir </> "splitter.config.js"
//Compile to JS
Yarn.exec ("fable-splitter -c " + configFile) id
//Run mocha tests
let projDirOutput = projDir </> "bin"
Yarn.exec ("run mocha " + projDirOutput) id
)
let versionRegex = Regex("^## ?\\[?v?([\\w\\d.-]+\\.[\\w\\d.-]+[a-zA-Z0-9])\\]?", RegexOptions.IgnoreCase)
let getLastVersion () =
File.ReadLines("CHANGELOG.md")
|> 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 changelog file"
| Some m ->
m.Groups.[1].Value
let isPreRelease (version : string) =
let regex = Regex(".*(alpha|beta|rc).*", RegexOptions.IgnoreCase)
regex.IsMatch(version)
let getNotes (version : string) =
File.ReadLines("CHANGELOG.md")
|> Seq.skipWhile(fun line ->
let m = versionRegex.Match(line)
if m.Success then
not (m.Groups.[1].Value = version)
else
true
)
// Remove the version line
|> Seq.skip 1
// Take all until the next version line
|> Seq.takeWhile (fun line ->
let m = versionRegex.Match(line)
not m.Success
)
let needsPublishing (versionRegex: Regex) (newVersion: string) projFile =
printfn "Project: %s" projFile
if newVersion.ToUpper().EndsWith("NEXT")
|| newVersion.ToUpper().EndsWith("UNRELEASED")
then
Logger.warnfn "Version marked as unreleased version in Changelog, 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 = newVersion
if sameVersion then
Logger.warnfn "Already version %s, no need to publish." newVersion
not sameVersion
let pushNuget (newVersion: string) (projFile: string) =
let versionRegex = Regex("<Version>(.*?)</Version>", RegexOptions.IgnoreCase)
if needsPublishing versionRegex newVersion projFile then
let projDir = Path.GetDirectoryName(projFile)
let nugetKey =
match Environment.environVarOrNone "NUGET_KEY" with
| Some nugetKey -> nugetKey
| None -> failwith "The Nuget API key must be set in a NUGET_KEY environmental variable"
(versionRegex, projFile) ||> Util.replaceLines (fun line _ ->
versionRegex.Replace(line, "<Version>" + newVersion + "</Version>") |> Some)
DotNet.pack (fun p ->
{ p with
Configuration = DotNet.Release
Common = { p.Common with DotNetCliPath = "dotnet" } } )
projFile
let file =
Directory.GetFiles(projDir </> "bin" </> "Release", "*.nupkg")
|> Array.find (fun nupkg -> nupkg.Contains(newVersion))
run
"dotnet"
root
[
"paket"
"push"
"--url"
"https://www.nuget.org/api/v2/package"
"--api-key"
nugetKey
file
]
Target.create "Publish" (fun _ ->
let version = getLastVersion()
pushNuget version projectFile
)
Target.create "Release" (fun _ ->
let version = getLastVersion()
Git.Staging.stageAll root
let commitMsg = sprintf "Release version %s" version
Git.Commit.exec root commitMsg
Git.Branches.push root
let token =
match Environment.environVarOrDefault "GITHUB_TOKEN" "" with
| s when not (String.IsNullOrWhiteSpace s) -> s
| _ -> failwith "The Github token must be set in a GITHUB_TOKEN environmental variable"
// let nupkg =
// let projDir = Path.GetDirectoryName(projectFile)
// Directory.GetFiles(projDir </> "bin" </> "Release", "*.nupkg")
// |> Array.find (fun nupkg -> nupkg.Contains(version))
GitHub.createClientWithToken token
|> GitHub.draftNewRelease gitOwner repoName version (isPreRelease version) (getNotes version)
// |> GitHub.uploadFile nupkg
|> GitHub.publishDraft
|> Async.RunSynchronously
)
Target.create "BuildDocs" (fun _ ->
Yarn.exec "nacara" id
)
Target.create "WatchDocs" (fun _ ->
Yarn.exec "nacara --watch" id
)
Target.create "PublishDocs" (fun _ ->
Yarn.exec "gh-pages -d docs_deploy" id
)
"Clean"
==> "YarnInstall"
==> "DotnetRestore"
==> "MochaTest"
==> "Publish"
==> "Release"
"BuildDocs"
==> "PublishDocs"
Target.runOrDefault "MochaTest"