This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
tools.fsx
233 lines (181 loc) · 5.98 KB
/
tools.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
#r "nuget: FsMake, 0.6.1"
open FsMake
open System
open System.IO
open System.IO.Compression
let runtimes = [|
"linux-x64"
"linux-arm64"
"osx-x64"
"osx-arm64"
"win10-x64"
"win10-arm64"
|]
let projects = [ "Perla" ]
let libraries = [ "Perla.PackageManager"; "Perla.Plugins"; "Perla.Logger" ]
let NugetApiKey = EnvVar.getOrFail "NUGET_DEPLOY_KEY"
[<Literal>]
let PackageVersion = "1.0.0-beta-024"
let fsSources =
Glob.create "*.fsx"
|> Glob.toPaths
|> Seq.append (
Glob.createWithRootDir "src" "**/*.fs"
|> Glob.add "**/*.fsi"
|> Glob.exclude "**/fable_modules/*.fs"
|> Glob.exclude "**/fable_modules/**/*.fs"
|> Glob.exclude "**/obj/*.fs"
|> Glob.exclude "**/obj/**/*.fs"
|> Glob.toPaths
)
let outDir = Path.GetFullPath("./dist")
module Operations =
type FantomasError =
| PendingFormat
| FailedToFormat
let cleanDist = make {
let! ctx = Make.context
try
Directory.Delete(outDir, true)
with ex ->
ctx.Console.WriteLine(
Console.warn
$"We tried to delete '{outDir}' but '{ex.Message}' happened"
)
}
let fantomas (command: string) = make {
let! result =
Cmd.createWithArgs "dotnet" [ "fantomas"; command; yield! fsSources ]
|> Cmd.checkExitCode Cmd.ExitCodeCheckOption.CheckCodeNone
|> Cmd.redirectOutput Cmd.RedirectToBoth
|> Cmd.result
match result.ExitCode with
| 0 -> return ()
| 99 -> return! Make.fail (nameof FantomasError.PendingFormat)
| _ -> return! Make.fail (nameof FantomasError.FailedToFormat)
}
let dotnet (args: string) =
Cmd.createWithArgs "dotnet" (args.Split(' ') |> List.ofArray) |> Cmd.run
let nugetPush (nupkg: string, apiKey) =
Cmd.createWithArgs "dotnet" [
"nuget"
"push"
nupkg
"--skip-duplicate"
"-s"
"https://api.nuget.org/v3/index.json"
"-k"
]
|> Cmd.argSecret apiKey
|> Cmd.run
let buildBinaries (project: string) (runtime: string) =
let cmd =
let framework = "net7.0"
let outdir = $"{outDir}/{runtime}"
$"publish {project} -c Release -f {framework} -r {runtime} --self-contained -p:Version={PackageVersion} -o {outdir}"
dotnet cmd
module Steps =
let installTools = Step.create "Install Tools" {
do! Operations.dotnet "tool restore"
}
let restore = Step.create "Restore" { do! Operations.dotnet "restore" }
let clean = Step.create "Clean" { do! Operations.cleanDist }
let packNugets = Step.create "Pack Nugets" {
let! ctx = Step.context
Console.info "Generating NuGet Package" |> ctx.Console.WriteLine
for packable in projects @ libraries do
do!
Operations.dotnet
$"pack src/{packable}/{packable}.fsproj -p:Version={PackageVersion} -o {outDir}"
}
let zip = Step.create "Zip binaries" {
let! ctx = Step.context
Console.info "Zipping Binaries" |> ctx.Console.WriteLine
runtimes
|> Array.Parallel.iter (fun runtime ->
let sources = $"{outDir}/{runtime}"
ZipFile.CreateFromDirectory(sources, $"{outDir}/{runtime}.zip")
Directory.Delete(sources, true))
Console.info $"Binaries Zipped at '{outDir}'" |> ctx.Console.WriteLine
}
let buildBin = Step.create "Build Binaries" {
let! ctx = Step.context
Console.info "Building Binaries" |> ctx.Console.WriteLine
for runtime in runtimes do
Console.info $"Starting [{runtime}]" |> ctx.Console.WriteLine
for project in projects do
do! Operations.buildBinaries $"src/{project}/{project}.fsproj" runtime
}
let buildForRuntime = Step.create "Building binary for runtime" {
let! ctx = Step.context
match ctx.ExtraArgs |> List.tryHead with
| Some runtime ->
Console.info $"Starting [{runtime}]" |> ctx.Console.WriteLine
for project in projects do
do! Operations.buildBinaries $"src/{project}/{project}.fsproj" runtime
| None ->
[
Console.error "No runtime found in the extra arguments."
Console.error "example: dotnet fsi build.fsx build:runtime -- linux-x64"
]
|> ctx.Console.WriteLines
return! Step.fail "No runtime found in the extra arguments."
}
let build = Step.create "build" {
do! Operations.dotnet "build src/Perla/Perla.fsproj --no-restore"
}
let format = Step.create "format" { do! Operations.fantomas "format" }
let test = Step.create "tests" {
do! Operations.dotnet "test tests/Perla.Tests --no-restore"
}
let pushNugets = Step.create "nuget" {
let! apiKey = NugetApiKey
for library in projects @ libraries do
let nupkName = $"./dist/{library}.{PackageVersion}.nupkg"
do! Operations.nugetPush (nupkName, apiKey)
}
module Pipelines =
let cleanDist = Pipeline.create "clean:dist" { run Steps.clean }
let restore = Pipeline.create "restore" {
run Steps.installTools
run Steps.restore
}
let format = Pipeline.createFrom restore "format" { run Steps.format }
let packNuget = Pipeline.createFrom restore "build:nuget" {
run Steps.clean
run Steps.packNugets
}
let pushNugets = Pipeline.createFrom packNuget "push:nuget" {
run Steps.pushNugets
}
let pushExistingNugets = Pipeline.create "push:existing:nuget" {
run Steps.pushNugets
}
let buildRelease = Pipeline.create "build:release" {
run Steps.clean
run Steps.installTools
run Steps.restore
run Steps.packNugets
run Steps.pushNugets
// run Steps.buildBin
// run Steps.zip
}
let buildRuntime = Pipeline.createFrom packNuget "build:runtime" {
run Steps.buildForRuntime
}
let build = Pipeline.createFrom restore "build" { run Steps.build }
let test = Pipeline.createFrom build "test" { run Steps.test }
Pipelines.create {
add Pipelines.cleanDist
add Pipelines.restore
add Pipelines.format
add Pipelines.packNuget
add Pipelines.pushNugets
add Pipelines.buildRelease
add Pipelines.buildRuntime
add Pipelines.build
add Pipelines.test
add Pipelines.pushExistingNugets
default_pipeline Pipelines.build
}
|> Pipelines.runWithArgsAndExit fsi.CommandLineArgs