-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
380 lines (279 loc) · 9.37 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#if FAKE
#r "paket:
source https://api.nuget.org/v3/index.json
storage: none
nuget FSharp.Core >= 6.0
nuget Fake.Core.Target
nuget Fake.IO.FileSystem
nuget Fake.DotNet.Cli
nuget Fake.Net.Http
nuget FSharp.Json //"
#endif
#load ".fake/build.fsx/intellisense.fsx"
#r "netstandard"
open Fake.Core
open Fake.Core.TargetOperators
open Fake.IO
open Fake.IO.Globbing.Operators
module private Params =
// ターゲットのプロジェクト名を参照する
let ProjectName = "Altseed2Template"
// ターゲットのアセンブリ名を参照する
let AssemblyName = ProjectName
// ターゲットのプロジェクトを参照する
let PublishTarget = $"src/%s{ProjectName}/%s{ProjectName}.csproj"
module Resources =
// 指定したファイルの内容をリソースパッケージのパスワードとして利用する
let PasswordPath = $"src/%s{ProjectName}/ResourcesPassword.txt"
// リソースパッケージのパス
let PackagePath = "Resources.pack"
module Dist =
let WindowsZipName = $"%s{ProjectName}.win-x64.zip"
let MacOSDmgName = $"%s{ProjectName}.osx-x64.dmg"
/// オートフォーマッターを使って自動整形する場合の対象を指定する
module FormatTargets =
let csharp =
!! "src/**/*.csproj"
++ "tests/**/*.csproj"
let fsharp =
!! "src/**/*.fs" ++ "build.fsx"
-- "src/*/obj/**/*.fs"
-- "src/*/bin/**/*.fs"
open Fake.DotNet
open Fake.Net
open FSharp.Json
[<AutoOpen>]
module private Utils =
let runtimeToPubDir (runtime: string) = sprintf "publish/%s" runtime
let getConfiguration (input: string option) =
input
|> Option.map (fun s -> s.ToLower())
|> function
| Some "debug" -> DotNet.BuildConfiguration.Debug
| Some "release" -> DotNet.BuildConfiguration.Release
| Some c -> failwithf "Invalid configuration '%s'" c
| None -> DotNet.BuildConfiguration.Debug
let dotnet cmd =
Printf.kprintf (fun arg ->
let res = DotNet.exec id cmd arg
if not res.OK then
let msg = res.Messages |> String.concat "\n"
failwithf "Failed to run 'dotnet %s %s' due to: %A" cmd arg msg
)
let shell (dir: string option) cmd =
Printf.kprintf (fun arg ->
let dir = dir |> Option.defaultValue "."
let res = Shell.Exec(cmd, arg, dir)
if res <> 0 then
failwithf "Failed to run '%s %s' at '%A'" cmd arg dir
)
module Zip =
open System.IO.Compression
open System.Text
let zipDirectory src dest =
ZipFile.CreateFromDirectory(src, dest, CompressionLevel.Optimal, true)
Target.initEnvironment ()
let args = Target.getArguments ()
(* クリーンする *)
Target.create
"Clean"
(fun _ ->
!! "src/**/bin"
++ "src/**/obj"
++ "tests/**/bin"
++ "tests/**/obj"
++ "lib/**/bin"
++ "lib/**/obj"
++ "publish"
|> Shell.cleanDirs
)
(* ビルドする *)
Target.create
"Build"
(fun _ ->
let conf =
args
|> Option.bind Array.tryHead
|> getConfiguration
!! "src/**/*.*proj"
++ "tests/**/*.*proj"
|> Seq.iter (DotNet.build (fun p -> { p with Configuration = conf }))
)
(* runtime 向けの単一実行ファイルにpublishビルドする *)
let publishForRuntime runtime =
Params.PublishTarget
|> DotNet.publish (fun p ->
{ p with
Runtime = Some runtime
Configuration = DotNet.BuildConfiguration.Release
SelfContained = Some true
OutputPath = Some(runtimeToPubDir runtime)
MSBuildParams =
{ p.MSBuildParams with
DisableInternalBinLog = true
Properties =
List.append
[ "OutputType", "WinExe"
"PublishSingleFile", "true"
"PublishTrimmed", "true"
"DebugSymbols", "false"
"DebugType", "None" ]
p.MSBuildParams.Properties } }
)
Target.create "Publish.win" (fun _ -> publishForRuntime "win-x64")
Target.create "Publish.osx" (fun _ -> publishForRuntime "osx-x64")
Target.create "Publish" ignore
"Publish.win" ==> "Publish"
"Publish.osx" ==> "Publish"
(* 配布用にファイルをまとめる *)
Target.create
"LICENSES"
(fun _ ->
Directory.ensure "publish/licenses"
dotnet "dotnet-project-licenses" "--input %s --output-directory publish/licenses -e" Params.PublishTarget
)
Target.create
"Dist.win"
(fun _ ->
File.checkExists Params.Resources.PackagePath
if not (System.IO.Directory.Exists "publish/licenses") then
Target.run 1 "LICENSES" []
let tempDirToZip = $"publish/temp/win-x64/%s{Params.ProjectName}"
let targetZipName = $"publish/output/%s{Params.Dist.WindowsZipName}"
Trace.tracefn "Cleaning"
do
File.delete targetZipName
Directory.delete tempDirToZip
Trace.tracefn "Ensuring"
do
Directory.ensure tempDirToZip
Directory.ensure "publish/output"
Trace.tracefn "Copying files"
do
Shell.cp_r $"dist/contents" $"%s{tempDirToZip}/"
Shell.cp_r "publish/licenses" $"%s{tempDirToZip}/licenses"
Shell.cp_r (runtimeToPubDir "win-x64") tempDirToZip
Trace.tracefn "Creating zip"
do Zip.zipDirectory tempDirToZip targetZipName
)
Target.create
"Dist.osx"
(fun _ ->
File.checkExists Params.Resources.PackagePath
if not (System.IO.Directory.Exists "publish/licenses") then
Target.run 1 "LICENSES" []
let tempDirToDmg = $"publish/temp/osx-x64/%s{Params.ProjectName}"
let tempDirToApp = $"%s{tempDirToDmg}/%s{Params.ProjectName}.app"
let scriptDir = $"%s{tempDirToApp}/Contents/MacOS"
let resourceDir = $"%s{tempDirToApp}/Contents/Resources"
let scriptPath = $"%s{scriptDir}/script.sh"
let targetDmgName = $"publish/output/%s{Params.Dist.MacOSDmgName}"
Trace.tracefn "Cleaning"
do
Directory.delete tempDirToApp
Directory.delete tempDirToDmg
File.delete targetDmgName
Trace.tracefn "Ensuring"
do
Directory.ensure tempDirToApp
Directory.ensure "publish/output"
Trace.tracefn "Copying files"
do
Shell.cp_r $"dist/contents" $"%s{tempDirToDmg}/"
Shell.cp_r "publish/licenses" $"%s{resourceDir}/licenses"
Shell.cp_r "dist/App" tempDirToApp
Trace.tracefn "Creating script.sh"
do
if not <| File.exists scriptPath then
File.create scriptPath
$"""#!/bin/bash
cd `dirname $0`
./%s{Params.AssemblyName}
"""
|> File.writeString false scriptPath
Shell.cp_r (runtimeToPubDir "osx-x64") scriptDir
Trace.tracefn "Creating dmg"
do shell None "hdiutil" "create %s -volname \"%s\" -srcfolder \"%s\"" targetDmgName Params.ProjectName tempDirToDmg
)
(* オートフォーマッターを利用してソースコードを整形する *)
Target.create
"Format.CSharp"
(fun _ ->
Params.FormatTargets.csharp
|> Seq.iter (fun proj -> dotnet "format" "%s -v diag" proj)
)
Target.create
"Format.Check.CSharp"
(fun _ ->
Params.FormatTargets.csharp
|> Seq.iter (fun proj -> dotnet "format" "%s -v diag --verify-no-changes" proj)
)
Target.create
"Format.FSharp"
(fun _ ->
Params.FormatTargets.fsharp
|> String.concat " "
|> dotnet "fantomas" "%s"
)
Target.create
"Format.Check.FSharp"
(fun _ ->
Params.FormatTargets.fsharp
|> String.concat " "
|> dotnet "fantomas" "--check %s"
)
Target.create "Format" ignore
Target.create "Format.Check" ignore
(*
dotnet-format を使用してC#コードをフォーマットする場合、
`dotnet tool install dotnet-format` を実行した上で、
下記のコメントアウトを外す
"Format.CSharp" ==> "Format"
"Format.Check.CSharp" ==> "Format.Check"
*)
(*
fantomas を使用してF#コードをフォーマットする場合、
`dotnet tool install fantomas-tool` を実行した上で、
下記のコメントアウトを外す
"Format.FSharp" ==> "Format"
"Format.Check.FSharp" ==> "Format.Check"
*)
(* localにインストールされている.NET CLI Toolのバージョンをまとめて更新する *)
Target.create
"Tool.Update"
(fun _ ->
let content = File.readAsString ".config/dotnet-tools.json"
let manifest: {| tools: Map<string, {| version: string |}> |} =
Json.deserialize content
for x in manifest.tools do
dotnet "tool" "update %s" x.Key
)
(* Altseed2用のパッケージファイルを扱う *)
// Altseed2 .NETツールを利用してリソースフォルダをパッケージ化する
Target.create
"Resources.Pack"
(fun _ ->
let password = File.readAsString Params.Resources.PasswordPath
// Altseed2.Tools (.NETツール) http://altseed.github.io/Manual/CLITool.html
dotnet "altseed2" "file -s ./Resources -o %s -p %s" Params.Resources.PackagePath password
)
// パッケージファイルをダウンロードする
Target.create
"Resources.Download"
(fun _ ->
let url = Environment.environVar "RESOURCES_DOWNLOAD_URL"
Http.downloadFile Params.Resources.PackagePath url
|> ignore
)
Target.create "Resources.CI" ignore
(* CIで直接 `Resources.pack` を作る場合 *)
"Resources.Pack" ==> "Resources.CI"
(*
CIでリソースパッケージをダウンロードする場合、
GitHubリポジトリのSettingsから、secretsに`RESOURCES_DOWNLOAD_URL`を追加して、
以下のコメントを外す
"Resources.Download" ==> "Resources.CI"
*)
// 何もしないターゲット
Target.create "Nothing" ignore
Target.runOrDefaultWithArguments "Build"