-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.fsx
283 lines (236 loc) · 9.47 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
278
279
280
281
282
283
#load ".fake/build.fsx/intellisense.fsx"
open System
open Fake.Core
open Fake.DotNet
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.Core.TargetOperators
open Fake.Tools.Git
type ToolDir =
/// Global tool dir must be in PATH - ${PATH}:/root/.dotnet/tools
| Global
/// Just a dir name, the location will be used as: ./{LocalDirName}
| Local of string
// ========================================================================================================
// === F# / Console Application fake build ======================================================== 1.5.0 =
// --------------------------------------------------------------------------------------------------------
// Options:
// - no-clean - disables clean of dirs in the first step (required on CI)
// - no-lint - lint will be executed, but the result is not validated
// --------------------------------------------------------------------------------------------------------
// Table of contents:
// 1. Information about project, configuration
// 2. Utilities, DotnetCore functions
// 3. FAKE targets
// 4. FAKE targets hierarchy
// ========================================================================================================
// --------------------------------------------------------------------------------------------------------
// 1. Information about the project to be used at NuGet and in AssemblyInfo files and other FAKE configuration
// --------------------------------------------------------------------------------------------------------
let project = "TUC Language Server"
let summary = "Language server for a Tuc vs-code extension."
let release = ReleaseNotes.parse (System.IO.File.ReadAllLines "CHANGELOG.md" |> Seq.filter ((<>) "## Unreleased"))
let gitCommit = Information.getCurrentSHA1(".")
let gitBranch = Information.getBranchName(".")
let toolsDir = Global
/// Runtime IDs: https://docs.microsoft.com/en-us/dotnet/core/rid-catalog#macos-rids
let runtimeIds =
[
"osx-x64"
"win-x64"
"linux-x64"
]
// --------------------------------------------------------------------------------------------------------
// 2. Utilities, DotnetCore functions, etc.
// --------------------------------------------------------------------------------------------------------
[<AutoOpen>]
module private Utils =
let tee f a =
f a
a
let skipOn option action p =
if p.Context.Arguments |> Seq.contains option
then Trace.tracefn "Skipped ..."
else action p
module private DotnetCore =
let run cmd workingDir =
let options =
DotNet.Options.withWorkingDirectory workingDir
>> DotNet.Options.withRedirectOutput true
DotNet.exec options cmd ""
let runOrFail cmd workingDir =
run cmd workingDir
|> tee (fun result ->
if result.ExitCode <> 0 then failwithf "'dotnet %s' failed in %s" cmd workingDir
)
|> ignore
let runInRoot cmd = run cmd "."
let runInRootOrFail cmd = runOrFail cmd "."
let installOrUpdateTool toolDir tool =
let toolCommand action =
match toolDir with
| Global -> sprintf "tool %s --global %s" action tool
| Local dir -> sprintf "tool %s --tool-path ./%s %s" action dir tool
match runInRoot (toolCommand "install") with
| { ExitCode = code } when code <> 0 ->
match runInRoot (toolCommand "update") with
| { ExitCode = code } when code <> 0 -> Trace.tracefn "Warning: Install and update of %A has failed." tool
| _ -> ()
| _ -> ()
let execute command args (dir: string) =
let cmd =
sprintf "%s/%s"
(dir.TrimEnd('/'))
command
let processInfo = System.Diagnostics.ProcessStartInfo(cmd)
processInfo.RedirectStandardOutput <- true
processInfo.RedirectStandardError <- true
processInfo.UseShellExecute <- false
processInfo.CreateNoWindow <- true
processInfo.Arguments <- args |> String.concat " "
use proc =
new System.Diagnostics.Process(
StartInfo = processInfo
)
if proc.Start() |> not then failwith "Process was not started."
proc.WaitForExit()
if proc.ExitCode <> 0 then failwithf "Command '%s' failed in %s." command dir
(proc.StandardOutput.ReadToEnd(), proc.StandardError.ReadToEnd())
let stringToOption = function
| null | "" -> None
| string -> Some string
[<RequireQualifiedAccess>]
module ProjectSources =
let release =
!! "./*.fsproj"
let tests =
!! "tests/*.fsproj"
let all =
!! "./*.fsproj"
++ "src/**/*.fsproj"
++ "tests/*.fsproj"
// --------------------------------------------------------------------------------------------------------
// 3. Targets for FAKE
// --------------------------------------------------------------------------------------------------------
Target.create "Clean" <| skipOn "no-clean" (fun _ ->
!! "./**/bin/Release"
++ "./**/bin/Debug"
++ "./**/obj"
++ "./**/.ionide"
|> Shell.cleanDirs
)
Target.create "AssemblyInfo" (fun _ ->
let getAssemblyInfoAttributes projectName =
let now = DateTime.Now
let gitValue initialValue =
initialValue
|> stringToOption
|> Option.defaultValue "unknown"
[
AssemblyInfo.Title projectName
AssemblyInfo.Product project
AssemblyInfo.Description summary
AssemblyInfo.Version release.AssemblyVersion
AssemblyInfo.FileVersion release.AssemblyVersion
AssemblyInfo.InternalsVisibleTo "tests"
AssemblyInfo.Metadata("gitbranch", gitBranch |> gitValue)
AssemblyInfo.Metadata("gitcommit", gitCommit |> gitValue)
AssemblyInfo.Metadata("createdAt", now.ToString("yyyy-MM-dd HH:mm:ss"))
]
let getProjectDetails (projectPath: string) =
let projectName = IO.Path.GetFileNameWithoutExtension(projectPath)
(
projectPath,
projectName,
IO.Path.GetDirectoryName(projectPath),
(getAssemblyInfoAttributes projectName)
)
ProjectSources.all
|> Seq.map getProjectDetails
|> Seq.iter (fun (_, _, folderName, attributes) ->
AssemblyInfoFile.createFSharp (folderName </> "AssemblyInfo.fs") attributes
)
)
Target.create "Build" (fun _ ->
ProjectSources.release
|> Seq.iter (DotNet.build id)
)
Target.create "Lint" <| skipOn "no-lint" (fun _ ->
DotnetCore.installOrUpdateTool toolsDir "dotnet-fsharplint"
let checkResult (messages: string list) =
let rec check: string list -> unit = function
| [] -> failwithf "Lint does not yield a summary."
| head :: rest ->
if head.Contains "Summary" then
match head.Replace("= ", "").Replace(" =", "").Replace("=", "").Replace("Summary: ", "") with
| "0 warnings" -> Trace.tracefn "Lint: OK"
| warnings -> failwithf "Lint ends up with %s." warnings
else check rest
messages
|> List.rev
|> check
ProjectSources.all
|> Seq.map (fun fsproj ->
match toolsDir with
| Global ->
DotnetCore.runInRoot (sprintf "fsharplint lint %s" fsproj)
|> fun (result: ProcessResult) -> result.Messages
| Local dir ->
DotnetCore.execute "dotnet-fsharplint" ["lint"; fsproj] dir
|> fst
|> tee (Trace.tracefn "%s")
|> String.split '\n'
|> Seq.toList
)
|> Seq.iter checkResult
)
Target.create "Tests" (fun _ ->
if ProjectSources.tests |> Seq.isEmpty
then Trace.tracefn "There are no tests yet."
else DotnetCore.runOrFail "run" "tests"
)
let zipRelease releaseDir =
if releaseDir </> "zipCompiled" |> File.exists
then
Trace.tracefn "\nZipping released files in %s ..." releaseDir
releaseDir
|> DotnetCore.execute "zipCompiled" []
|> Trace.tracefn "Zip result:\n%A\n"
else
Trace.tracefn "\nZip compiled files"
runtimeIds
|> List.iter (fun runtimeId ->
Trace.tracefn " -> zipping %s ..." runtimeId
let zipFile = sprintf "%s.zip" runtimeId
IO.File.Delete zipFile
Zip.zip releaseDir (releaseDir </> zipFile) !!(releaseDir </> runtimeId </> "*")
)
Target.create "Release" (fun _ ->
let releaseDir = Path.getFullName "./dist"
ProjectSources.release
|> Seq.collect (fun project -> runtimeIds |> List.collect (fun runtimeId -> [project, runtimeId]))
|> Seq.iter (fun (project, runtimeId) ->
sprintf "publish -c Release /p:PublishSingleFile=true -o %s/%s --self-contained -r %s %s" releaseDir runtimeId runtimeId project
|> DotnetCore.runInRootOrFail
)
zipRelease releaseDir
)
Target.create "Watch" (fun _ ->
DotnetCore.runInRootOrFail "watch run"
)
Target.create "Run" (fun _ ->
DotnetCore.runInRootOrFail "run"
)
// --------------------------------------------------------------------------------------------------------
// 4. FAKE targets hierarchy
// --------------------------------------------------------------------------------------------------------
"Clean"
==> "AssemblyInfo"
==> "Build"
==> "Lint"
==> "Tests"
==> "Release"
"Build"
==> "Watch" <=> "Run"
Target.runOrDefaultWithArguments "Build"