-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFscHost.fs
More file actions
449 lines (369 loc) · 16.5 KB
/
FscHost.fs
File metadata and controls
449 lines (369 loc) · 16.5 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
namespace Queil.FSharp.FscHost
open Queil.FSharp.Hashing
open System.Runtime.Loader
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Diagnostics
open FSharp.Compiler.Text
open System
open System.IO
open System.Reflection
[<RequireQualifiedAccess>]
module private Const =
[<Literal>]
let FschDir = ".fsch"
[<Literal>]
let FschDeps = "fsch.deps"
[<Literal>]
let InlineFsx = "inline.fsx"
type Script =
| File of path: string
| Inline of body: string
type Member<'a> = Path of string
type CompilerOptions =
{ Args: string -> string list -> CompilerOptions -> string list
IncludeHostEntryAssembly: bool
LangVersion: string option
Target: string
TargetProfile: string
WarningLevel: int
Symbols: string list
Standalone: bool }
static member Default =
{ Args =
fun scriptPath refs opts ->
[ "-a"
scriptPath
$"--targetprofile:%s{opts.TargetProfile}"
$"--target:%s{opts.Target}"
$"--warn:%i{opts.WarningLevel}"
yield! refs
match opts.IncludeHostEntryAssembly with
| true -> $"-r:%s{Assembly.GetEntryAssembly().GetName().Name}"
| _ -> ()
match opts.LangVersion with
| Some ver -> $"--langversion:%s{ver}"
| _ -> ()
for s in opts.Symbols do
$"--define:%s{s}"
match opts.Standalone with
| true -> "--standalone"
| _ -> () ]
IncludeHostEntryAssembly = true
LangVersion = None
Target = "library"
TargetProfile = "netcore"
WarningLevel = 3
Symbols = []
Standalone = false }
type CompileOutput =
{ AssemblyFilePath: string
Assembly: Lazy<Assembly> }
type ScriptCache =
{ References: string list
SourceFiles: string list
FilePath: string }
static member Default =
{ References = []
SourceFiles = []
FilePath = "" }
static member Load(path: string) =
path
|> File.ReadAllLines
|> Seq.fold
(fun x s ->
match s.Split "#" |> Seq.toList with
| [ "n"; v ] ->
{ x with
References = v :: x.References }
| [ "s"; v ] ->
{ x with
SourceFiles = v :: x.SourceFiles }
| _ -> failwith $"Could not parse line: %s{s} in file: %s{path}")
ScriptCache.Default
|> fun x -> { x with FilePath = path }
member cache.Save() =
[ yield! cache.SourceFiles |> Seq.map (fun v -> $"s#{v}")
yield! cache.References |> Seq.map (fun v -> $"n#{v}") ]
|> Seq.sort
|> fun lines -> File.WriteAllLines(cache.FilePath, lines)
type ScriptContext =
{ FilePath: string
Dir: string
OutputRootDir: string
OutputVersionDir: string
LockFilePath: string }
type Options =
{ Compiler: CompilerOptions
UseCache: bool
OutputDir: string
Verbose: bool
Logger: (string -> unit) option
LogListTypes: bool
AutoLoadNugetReferences: bool }
static member Default =
{ Compiler = CompilerOptions.Default
UseCache = false
OutputDir = Path.Combine(Path.GetTempPath(), Const.FschDir)
Verbose = false
Logger = None
LogListTypes = false
AutoLoadNugetReferences = true }
[<RequireQualifiedAccess>]
module CompilerHost =
open Errors
module private Internals =
let checker = FSharpChecker.Create(parallelReferenceResolution = true)
let ensureScriptFile (outputRootDir: string) (script: Script) =
let getScriptFilePath =
function
| File path ->
let hashes = (path, None) ||> Hash.fileHash
let path =
if Path.IsPathRooted path then
path
else
Path.GetFullPath path
let scriptDir = Path.GetDirectoryName path
{ FilePath = path
Dir = scriptDir
OutputRootDir = hashes.HashedScriptDir outputRootDir
OutputVersionDir = hashes.HashedScriptVersionDir outputRootDir
LockFilePath =
Path.Combine(Path.GetTempPath(), Const.FschDir, "lock", Hash.shortHash scriptDir + ".lock") }
| Inline body ->
let shallowHash = body |> Hash.sha256 |> Hash.short
let scriptDir = Path.Combine(outputRootDir, "__inline", shallowHash)
let filePath = Path.Combine(scriptDir, Const.InlineFsx)
let hashes = (filePath, Some shallowHash) ||> Hash.fileHash
{ FilePath = filePath
Dir = scriptDir
OutputRootDir = hashes.HashedScriptDir outputRootDir
OutputVersionDir = hashes.HashedScriptVersionDir outputRootDir
LockFilePath =
Path.Combine(Path.GetTempPath(), Const.FschDir, "lock", Hash.shortHash scriptDir + ".lock") }
let createInlineScriptFile (filePath: string) =
function
| Inline body ->
filePath |> Path.GetDirectoryName |> Directory.CreateDirectory |> ignore
File.WriteAllText(filePath, body)
| _ -> ()
let ctx = script |> getScriptFilePath
script |> createInlineScriptFile ctx.FilePath
ctx
let compileScript (rootFilePath: string) (metadata: ScriptCache) (options: Options) : Async<CompileOutput> =
let log = options.Logger |> Option.defaultValue ignore
let asmLoadContext = AssemblyLoadContext("script", true)
let getCompileOutput dllPath =
{ AssemblyFilePath = dllPath
Assembly =
Lazy<Assembly>(fun () ->
if options.AutoLoadNugetReferences then
metadata.References
|> Seq.iter (fun path ->
log $"Loading assembly: %s{path}"
path |> asmLoadContext.LoadFromAssemblyPath |> ignore)
dllPath |> Path.GetFullPath |> asmLoadContext.LoadFromAssemblyPath) }
async {
let outputDllName =
if options.UseCache then
let hash =
Hash.deepSourceHash
(rootFilePath |> File.ReadAllText |> Hash.sha256 |> Hash.short)
metadata.SourceFiles
Path.Combine(options.OutputDir.TrimEnd('\\', '/'), $"{hash}.dll")
else
$"{Path.GetTempFileName()}.dll"
match outputDllName with
| path when File.Exists path ->
log $"Found cached assembly: %s{path}"
log $"Cached deps file: %s{metadata.FilePath}"
return getCompileOutput path
| path ->
let refs = metadata.References |> Seq.map (sprintf "-r:%s") |> Seq.toList
let compilerArgs =
[ yield! options.Compiler.Args rootFilePath refs options.Compiler
$"--out:{path}" ]
log (sprintf "Compiling with args: %s" (compilerArgs |> String.concat " "))
let! errors, _ = checker.Compile(compilerArgs |> List.toArray, "fsch-getAssembly")
match errors with
| xs when xs |> Array.exists (fun x -> x.Severity = FSharpDiagnosticSeverity.Error) ->
raise (ScriptCompileError(errors |> Seq.map string))
| xs -> xs |> Seq.iter (string >> log)
let compileOutput = getCompileOutput outputDllName
if options.LogListTypes then
compileOutput.Assembly.Value.GetTypes() |> Seq.iter (fun t -> log t.FullName)
return compileOutput
}
open Internals
open Queil.FSharp.DependencyManager.Paket
open System.Text.Json
open System.Text.Json.Serialization
let acquireLock
(lockFilePath: string)
(timeout: TimeSpan)
(log: string -> unit)
(config: Configuration)
: Async<IDisposable> =
let stopwatch = Diagnostics.Stopwatch.StartNew()
let jsonOptions = JsonSerializerOptions()
jsonOptions.WriteIndented <- true
jsonOptions.DefaultIgnoreCondition <- JsonIgnoreCondition.WhenWritingDefault
let mutable stream = None
async {
while stream.IsNone && stopwatch.Elapsed < timeout do
try
FileInfo lockFilePath |> _.DirectoryName |> Directory.CreateDirectory |> ignore
let fs =
new FileStream(lockFilePath, FileMode.Create, FileAccess.Write, FileShare.Read)
do! JsonSerializer.SerializeAsync(fs, config, jsonOptions) |> Async.AwaitTask
do! fs.FlushAsync() |> Async.AwaitTask
stream <- Some fs
with :? IOException as exn ->
log exn.Message
log $"Waiting to acquire lock on {lockFilePath}"
do! Async.Sleep 1000
match stream with
| Some fs ->
return
{ new IDisposable with
member _.Dispose() =
fs.Dispose()
File.Delete lockFilePath }
| None ->
raise (TimeoutException $"Could not acquire lock on {lockFilePath} within {timeout}")
return
{ new IDisposable with
member _.Dispose() = () }
}
let getAssembly (options: Options) (script: Script) : Async<CompileOutput> =
let log = options.Logger |> Option.defaultValue ignore
async {
let ctx = script |> ensureScriptFile options.OutputDir
log $"Root file path: %s{ctx.FilePath}"
log $"Script dir: %s{ctx.Dir}"
log $"Cache dir: %s{ctx.OutputVersionDir}"
log $"Lock file: %s{ctx.LockFilePath}"
match script with
| Inline _ -> Directory.CreateDirectory ctx.Dir |> ignore
| _ -> ()
Directory.CreateDirectory ctx.OutputVersionDir |> ignore
let cacheDepsFilePath = Path.Combine(ctx.OutputVersionDir, Const.FschDeps)
let lockContent =
{ Configuration.Default with
RootScriptFilePath = Some ctx.FilePath
OutputRootDir = options.OutputDir
Verbose = options.Verbose
IsDefault = false
ScriptOutputRootDir = Some ctx.OutputRootDir
ScriptOutputVersionDir = Some ctx.OutputVersionDir }
use! _lock = acquireLock ctx.LockFilePath (TimeSpan.FromMinutes 1.0) log lockContent
let! metadataResult =
async {
let buildMetadata () =
async {
let source = File.ReadAllText ctx.FilePath |> SourceText.ofString
let! projOptions, errors =
checker.GetProjectOptionsFromScript(
ctx.FilePath,
source,
previewEnabled = true,
otherFlags =
[| for s in options.Compiler.Symbols do
$"--define:%s{s}" |]
)
match errors with
| [] ->
let metadata =
{ ScriptCache.Default with
FilePath = cacheDepsFilePath
SourceFiles =
projOptions.SourceFiles |> Seq.except [ ctx.FilePath ] |> Seq.toList }
log "Source files:"
for sf in projOptions.SourceFiles do
log $" %s{sf}"
if options.Compiler.Standalone then
return Ok metadata
else
return
Ok
{ metadata with
References =
projOptions.SourceFiles
|> Seq.collect File.ReadAllLines
|> Seq.choose (function
| Utils.ParseRegex """^#r @?"(.*\.dll)"\s?$""" [ dllPath ] ->
Some dllPath
| _ -> None)
|> Seq.map (function
| p when Path.IsPathRooted p -> p
| p -> Path.GetFullPath(Path.Combine(ctx.Dir, p)))
|> Seq.distinct
|> Seq.toList }
| errors -> return Error errors
}
if File.Exists cacheDepsFilePath then
return Ok(ScriptCache.Load cacheDepsFilePath)
else
return! buildMetadata ()
}
let originalDir = Directory.GetCurrentDirectory()
try
match metadataResult with
| Ok metadata ->
Directory.SetCurrentDirectory ctx.Dir
metadata.Save()
return!
compileScript
ctx.FilePath
metadata
{ options with
OutputDir = ctx.OutputVersionDir }
| Error errors -> return raise (ScriptParseError(errors |> Seq.map string))
finally
Directory.SetCurrentDirectory originalDir
}
let getMember<'a> (options: Options) (Path pathA: Member<'a>) (script: Script) : Async<'a> =
async {
let! output = script |> getAssembly options
return output.Assembly.Value |> Member.get pathA
}
let getMember2<'a, 'b>
(options: Options)
(Path pathA: Member<'a>)
(Path pathB: Member<'b>)
(script: Script)
: Async<'a * 'b> =
async {
let! output = script |> getAssembly options
return output.Assembly.Value |> Member.get<'a> pathA, output.Assembly.Value |> Member.get<'b> pathB
}
let getMember3<'a, 'b, 'c>
(options: Options)
(Path pathA: Member<'a>)
(Path pathB: Member<'b>)
(Path pathC: Member<'c>)
(script: Script)
: Async<'a * 'b * 'c> =
async {
let! output = script |> getAssembly options
return
output.Assembly.Value |> Member.get<'a> pathA,
output.Assembly.Value |> Member.get<'b> pathB,
output.Assembly.Value |> Member.get<'c> pathC
}
let getMember4<'a, 'b, 'c, 'd>
(options: Options)
(Path pathA: Member<'a>)
(Path pathB: Member<'b>)
(Path pathC: Member<'c>)
(Path pathD: Member<'d>)
(script: Script)
: Async<'a * 'b * 'c * 'd> =
async {
let! output = script |> getAssembly options
return
output.Assembly.Value |> Member.get<'a> pathA,
output.Assembly.Value |> Member.get<'b> pathB,
output.Assembly.Value |> Member.get<'c> pathC,
output.Assembly.Value |> Member.get<'d> pathD
}