-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.fsx
More file actions
178 lines (146 loc) · 5.56 KB
/
build.fsx
File metadata and controls
178 lines (146 loc) · 5.56 KB
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
#r @"packages/build/FAKE/tools/FakeLib.dll"
open Fake
open Fake.Git
open Fake.ReleaseNotesHelper
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"
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 "DotnetRestore" (fun _ ->
!! srcGlob
|> 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
})
))
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 "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"
==> "InstallDotNetCore"
==> "DotnetRestore"
==> "DotnetBuild"
==> "DotnetPack"
==> "Publish"
==> "Release"
RunTargetOrDefault "DotnetPack"