-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbuild.fsx
177 lines (147 loc) · 5.7 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
#r "paket:
source https://api.nuget.org/v3/index.json
framework: net6.0
nuget Fake.Core.Target
nuget Fake.IO.FileSystem
nuget Fake.DotNet.Cli
nuget Fake.Tools.Git
nuget Fake.DotNet.MSBuild
nuget Fake.Core.ReleaseNotes
nuget Fake.DotNet.AssemblyInfoFile
nuget Fake.Api.GitHub //"
#load "./.fake/build.fsx/intellisense.fsx"
open Fake.Core
open Fake.Core.TargetOperators
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.DotNet
CoreTracing.ensureConsoleListener()
module GitHubActions =
let BuildNumber = Environment.environVarOrNone "BUILD_NUMBER"
let NugetVersion = Environment.environVarOrNone "NUGET_VERSION"
let NugetKey = Environment.environVarOrNone "NUGET_KEY"
let detect() = BuildNumber |> Option.isSome
module Xml =
open System.Xml.Linq
let load x = XDocument.Load(x:string)
let xns x = (x:XDocument).Root.Name.Namespace
let xn2 n xn = (xn:XNamespace).GetName(n)
let descendants n x = (x:XDocument).Descendants(n)
let value x = (x:XElement).Value
module Analysis =
let projectReferencing packageName project =
let projDoc = Xml.load project
let ns = projDoc |> Xml.xns
let packageRefName = ns |> Xml.xn2 "PackageReference"
let includeName = ns |> Xml.xn2 "Include"
let pRefs =
projDoc
|> Xml.descendants packageRefName
|> Seq.filter (fun x -> not (isNull (x.Attribute(includeName))))
|> Seq.map (fun x -> x.Attribute(includeName).Value)
if pRefs |> Seq.contains packageName then Some project
else None
module ReleaseNotes =
let TickSpec = ReleaseNotes.load (__SOURCE_DIRECTORY__ </> "RELEASE_NOTES.md")
module Build =
let rootDir = __SOURCE_DIRECTORY__
let nuget = rootDir </> "packed_nugets"
let private fileVersion =
GitHubActions.BuildNumber
|> Option.defaultValue "0"
|> sprintf "%s.%s" ReleaseNotes.TickSpec.AssemblyVersion
let private continuousBuild =
if GitHubActions.detect() then
"/p:ContinuousIntegrationBuild=true"
else
""
let props =
sprintf "/p:Version=%s /p:AssemblyVersion=%s %s" ReleaseNotes.TickSpec.AssemblyVersion fileVersion continuousBuild
let setParams (defaults: DotNet.BuildOptions) =
{ defaults with
Configuration = DotNet.BuildConfiguration.Release
MSBuildParams = { defaults.MSBuildParams with DisableInternalBinLog = true }
Common =
DotNet.Options.Create()
|> DotNet.Options.withCustomParams (Some props) }
let Sln = "./TickSpec.sln"
Target.create "Clean" (fun _ ->
Shell.cleanDirs [Build.nuget]
DotNet.exec id "clean" "" |> ignore
)
Target.create "Build" (fun _ ->
Sln |> DotNet.build Build.setParams
)
Target.create "Test" (fun _ ->
// Xunit seems to be failing under Linux with net452 runner, let's just skip it
// the .NET 4 tests all together there
let framework = if Environment.isWindows then None else Some "net6.0"
Sln
|> DotNet.test (fun o ->
{ o with
Configuration = DotNet.Release
NoBuild = true
MSBuildParams = { o.MSBuildParams with DisableInternalBinLog = true }
Framework = framework
}
)
)
Target.create "Nuget" (fun _ ->
if Environment.isWindows then
let props =
let notes =
String.concat System.Environment.NewLine ReleaseNotes.TickSpec.Notes
|> (fun x -> x.Replace(",", "%2c"))
sprintf
"%s /p:PackageReleaseNotes=\"%s\";PackageVersion=\"%s\""
Build.props
notes
ReleaseNotes.TickSpec.NugetVersion
DotNet.pack (fun p ->
{ p with
Configuration = DotNet.Release
OutputPath = Some Build.nuget
NoBuild = false // Not sure why but it seems to be necessary to rebuild it
IncludeSymbols = true
MSBuildParams = { p.MSBuildParams with DisableInternalBinLog = true }
Common =
DotNet.Options.Create()
|> DotNet.Options.withCustomParams (Some props)
|> DotNet.Options.withVerbosity (Some DotNet.Verbosity.Minimal)
} )
Sln
else
Trace.tracef "--- Skipping Nuget target as the build is not running on Windows ---"
)
Target.create "PublishNuget" (fun _ ->
if Environment.isWindows then
match GitHubActions.NugetKey with
| Some k -> TraceSecrets.register k "<NUGET_KEY>"
| None -> ()
let publishNugets () =
let key =
match GitHubActions.NugetKey with
| Some x -> x
| None -> failwith "To publish nuget, it is needed to set NUGET_KEY environment variable"
let publishNuget nuget =
DotNet.exec id "nuget" (sprintf "push %s -k %s -s https://api.nuget.org/v3/index.json" nuget key)
!! (Build.nuget </> "*.nupkg")
-- (Build.nuget </> "*.symbols.nupkg")
|> Seq.map publishNuget
match GitHubActions.NugetVersion with
| None -> ()
| Some t when t = ReleaseNotes.TickSpec.NugetVersion ->
publishNugets () |> Seq.iter (fun x -> if not x.OK then failwithf "Nuget publish failed with %A" x)
| Some t -> failwithf "Unexpected tag %s" t
else
Trace.tracef "--- Skipping PublishNuget target as the build is not running on Windows ---"
)
Target.create "All" ignore
"Clean"
==> "Build"
==> "Nuget"
==> "Test"
==> "PublishNuget"
==> "All"
Target.runOrDefault "All"