-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild.fsx
195 lines (159 loc) · 6.13 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
#r "nuget: Fun.Build, 0.3.7"
#r "nuget: Fake.IO.FileSystem, 6.0.0"
#r "nuget: BlackFox.CommandLine, 1.0.0"
#r "nuget: FsToolkit.ErrorHandling, 4.3.0"
#r "nuget: Fake.Tools.Git, 6.0.0"
#r "nuget: Fake.Api.GitHub, 6.0.0"
open Fun.Build
open System.IO
open System.Text.RegularExpressions
open Fake.IO
open FsToolkit.ErrorHandling
open BlackFox.CommandLine
open Fake.Tools
open Fake.Api
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)
let needsPublishing (versionRegex: Regex) (searchedVersion: string) projFile =
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 = searchedVersion
if sameVersion then
printfn "Already version %s, no need to publish." searchedVersion
not sameVersion
module Changelog =
let versionRegex = Regex("^## ?\\[?v?([\\w\\d.-]+\\.[\\w\\d.-]+[a-zA-Z0-9])\\]?", RegexOptions.IgnoreCase)
let getLastVersion (changelodPath : string) =
File.ReadLines changelodPath
|> 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 getNotesForVersion (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
)
let root = __SOURCE_DIRECTORY__
let gitOwner = "thoth-org"
let repoName = "Thoth.Json.Giraffe"
module Stages =
let clean =
stage "Clean" {
run (fun _ ->
[
"src/bin"
"src/obj"
"tests/bin"
"tests/obj"
]
|> Shell.cleanDirs
)
}
let test =
stage "Tests" {
workingDir "tests"
run "dotnet test"
}
pipeline "Test" {
Stages.clean
noPrefixForStep
Stages.test
runIfOnlySpecified
}
pipeline "Publish" {
whenEnvVar "NUGET_KEY"
Stages.clean
Stages.test
stage "Publish packages to NuGet" {
workingDir "src"
run (fun ctx ->
let nugetKey = ctx.GetEnvVar "NUGET_KEY"
asyncResult {
let version = Changelog.getLastVersion "CHANGELOG.md"
let versionRegex = Regex("<Version>(.*?)</Version>", RegexOptions.IgnoreCase)
let projectFile = "src/Thoth.Json.Giraffe.fsproj"
if Util.needsPublishing versionRegex version projectFile then
(versionRegex, projectFile)
||> Util.replaceLines (fun line _ ->
versionRegex.Replace(line, "<Version>" + version + "</Version>")
|> Some
)
let! dotnetPackOutput =
CmdLine.empty
|> CmdLine.appendRaw "dotnet"
|> CmdLine.appendRaw "pack"
|> CmdLine.appendPrefix "-c" "Release"
// |> CmdLine.appendRaw $"""/p:PackageReleaseNotes="{toPackageReleaseNotes releaseNotes.Notes}" """
|> CmdLine.toString
|> ctx.RunCommandCaptureOutput
let m = Regex.Match(dotnetPackOutput, ".*'(?<nupkg_path>.*\.(?<version>.*\..*\..*)\.nupkg)'")
if not m.Success then
failwithf "Couldn't find NuGet package in output: %s" dotnetPackOutput
let nupkgPath = m.Groups.["nupkg_path"].Value
do! CmdLine.empty
|> CmdLine.appendRaw "dotnet"
|> CmdLine.appendRaw "nuget"
|> CmdLine.appendRaw "push"
|> CmdLine.appendRaw nupkgPath
|> CmdLine.appendPrefix "--api-key" nugetKey
|> CmdLine.appendPrefix "--source" "nuget.org"
|> CmdLine.toString
|> ctx.RunCommand
else
return! Error "Already published version"
}
)
}
stage "Release on Github" {
run (fun ctx ->
let githubToken = ctx.GetEnvVar "GITHUB_TOKEN"
let version = Changelog.getLastVersion "CHANGELOG.md"
let isPreRelease = Changelog.isPreRelease version
let notes = Changelog.getNotesForVersion version
Git.Staging.stageAll root
let commitMsg = $"Release version {version}"
Git.Commit.exec root commitMsg
Git.Branches.push root
GitHub.createClientWithToken githubToken
|> GitHub.draftNewRelease gitOwner repoName version isPreRelease notes
|> GitHub.publishDraft
)
}
runIfOnlySpecified
}
tryPrintPipelineCommandHelp ()