forked from thoth-org/Thoth.Json.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
277 lines (230 loc) · 8.57 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#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.JavaScript
open Fake.Api
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__
module Source =
let dir = root </> "src"
let projectFile = dir </> "Thoth.Json.Net.fsproj"
module Tests =
let dir = root </> "tests"
let projectFile = dir </> "Tests.fsproj"
let gitOwner = "thoth-org"
let repoName = "Thoth.Json.Net"
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"
++ "temp/"
|> Shell.cleanDirs
)
Target.create "YarnInstall"(fun _ ->
Yarn.install id
)
Target.create "DotnetRestore" (fun _ ->
DotNet.restore (dtntWorkDir Source.dir) ""
DotNet.restore (dtntWorkDir Tests.dir) ""
)
let mono workingDir args =
Command.RawCommand("mono", Arguments.OfArgs args)
|> CreateProcess.fromCommand
|> CreateProcess.withWorkingDirectory workingDir
|> Proc.run
|> ignore
let build project framework =
DotNet.build (fun p ->
{ p with Framework = Some framework } ) project
let testNetFrameworkDir = root </> "tests" </> "bin" </> "Release" </> "net461"
let testNetCoreDir = root </> "tests" </> "bin" </> "Release" </> "netcoreapp3.0"
Target.create "AdaptTest" (fun _ ->
[ "Types.fs"
"Decoders.fs"
"Encoders.fs"
"ExtraCoders.fs" ]
|> List.map (fun fileName ->
root </> "paket-files" </> "tests" </> "thoth-org" </> "Thoth.Json" </> "tests" </> fileName
)
|> List.iter (fun path ->
File.ReadLines path
|> Seq.toList
|> List.map (fun originalLine ->
match originalLine.Trim() with
| "open Thoth.Json" -> "open Thoth.Json.Net"
// | "open Fable.Core"
| "open Fable.Core.JsInterop" -> ""
| _ -> originalLine
)
// This is important to manually concat the lines using `\n` otherwise we end up with `CRLF`
// and the tests will fail on Windows
|> String.concat "\n"
|> File.writeString false path
)
)
Target.create "Test" (fun _ ->
build Tests.projectFile "netcoreapp3.0"
build Tests.projectFile "net461"
if Environment.isUnix then
mono testNetFrameworkDir [ "Tests.exe" ]
else
run (testNetFrameworkDir </> "Tests.exe") root []
let result = DotNet.exec (dtntWorkDir testNetCoreDir) "" "Tests.dll"
if not result.OK then failwithf "Expecto for netcore tests failed."
)
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 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 ->
DotNet.Options.lift dotnetSdk.Value p
)
projFile
let projDir = Path.GetDirectoryName(projFile)
Directory.GetFiles(projDir </> "bin" </> "Release", "*.nupkg")
|> Array.find (fun nupkg -> nupkg.Contains(newVersion))
|> (fun nupkg ->
DotNet.nugetPush (fun p ->
{ p with
PushParams =
{ p.PushParams with
ApiKey = Some nugetKey
Source = Some "nuget.org"
}
}
) nupkg
)
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
(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
)
Target.create "Publish" (fun _ ->
let version = getLastVersion()
pushNuget version Source.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 (System.String.IsNullOrWhiteSpace s) -> s
| _ -> failwith "The Github token must be set in a GITHUB_TOKEN environmental variable"
GitHub.createClientWithToken token
|> GitHub.draftNewRelease gitOwner repoName version (isPreRelease version) (getNotes version)
|> GitHub.publishDraft
|> Async.RunSynchronously
)
"Clean"
==> "YarnInstall"
==> "DotnetRestore"
==> "AdaptTest"
==> "Test"
==> "Publish"
==> "Release"
Target.runOrList ()