-
Notifications
You must be signed in to change notification settings - Fork 809
/
Copy pathfilesystem.fsx
244 lines (188 loc) · 8.03 KB
/
filesystem.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
(**
---
title: IFileSystem
category: FSharp.Compiler.Service
categoryindex: 300
index: 1200
---
*)
(*** hide ***)
#I "../../artifacts/bin/FSharp.Compiler.Service/Debug/netstandard2.0"
(**
Compiler Services: Virtualized File System
==========================================
The `FSharp.Compiler.Service` component has a global variable
representing the file system. By setting this variable you can host the compiler in situations where a file system
is not available.
> **NOTE:** The FSharp.Compiler.Service API is subject to change when later versions of the nuget package are published.
Setting the FileSystem
----------------------
In the example below, we set the file system to an implementation which reads from disk
*)
#r "FSharp.Compiler.Service.dll"
open System.IO
open System.Text
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.IO
let defaultFileSystem = FileSystem
let fileName1 = @"c:\mycode\test1.fs" // note, the path doesn't exist
let fileName2 = @"c:\mycode\test2.fs" // note, the path doesn't exist
type MyFileSystem() =
let file1 =
"""
module File1
let A = 1"""
let file2 =
"""
module File2
let B = File1.A + File1.A"""
let files =
dict [ (fileName1, file1)
(fileName2, file2) ]
interface IFileSystem with
// Implement the service to open files for reading and writing
member _.OpenFileForReadShim(fileName, ?useMemoryMappedFile: bool, ?shouldShadowCopy: bool) =
match files.TryGetValue fileName with
| true, text -> new MemoryStream(Encoding.UTF8.GetBytes(text)) :> Stream
| _ ->
defaultFileSystem.OpenFileForReadShim(
fileName,
?useMemoryMappedFile = useMemoryMappedFile,
?shouldShadowCopy = shouldShadowCopy
)
member _.OpenFileForWriteShim(fileName, ?fileMode: FileMode, ?fileAccess: FileAccess, ?fileShare: FileShare) =
defaultFileSystem.OpenFileForWriteShim(
fileName,
?fileMode = fileMode,
?fileAccess = fileAccess,
?fileShare = fileShare
)
// Implement the service related to file existence and deletion
member _.FileExistsShim(fileName) =
files.ContainsKey(fileName)
|| defaultFileSystem.FileExistsShim(fileName)
// Implement the service related to temporary paths and file time stamps
member _.GetTempPathShim() = defaultFileSystem.GetTempPathShim()
member _.GetLastWriteTimeShim(fileName) =
defaultFileSystem.GetLastWriteTimeShim(fileName)
member _.GetFullPathShim(fileName) =
defaultFileSystem.GetFullPathShim(fileName)
member _.IsInvalidPathShim(fileName) =
defaultFileSystem.IsInvalidPathShim(fileName)
member _.IsPathRootedShim(fileName) =
defaultFileSystem.IsPathRootedShim(fileName)
member _.FileDeleteShim(fileName) =
defaultFileSystem.FileDeleteShim(fileName)
member _.AssemblyLoader = defaultFileSystem.AssemblyLoader
member _.GetFullFilePathInDirectoryShim dir fileName =
defaultFileSystem.GetFullFilePathInDirectoryShim dir fileName
member _.NormalizePathShim(path) =
defaultFileSystem.NormalizePathShim(path)
member _.GetDirectoryNameShim(path) =
defaultFileSystem.GetDirectoryNameShim(path)
member _.GetCreationTimeShim(path) =
defaultFileSystem.GetCreationTimeShim(path)
member _.CopyShim(src, dest, overwrite) =
defaultFileSystem.CopyShim(src, dest, overwrite)
member _.DirectoryCreateShim(path) =
defaultFileSystem.DirectoryCreateShim(path)
member _.DirectoryExistsShim(path) =
defaultFileSystem.DirectoryExistsShim(path)
member _.DirectoryDeleteShim(path) =
defaultFileSystem.DirectoryDeleteShim(path)
member _.EnumerateFilesShim(path, pattern) =
defaultFileSystem.EnumerateFilesShim(path, pattern)
member _.EnumerateDirectoriesShim(path) =
defaultFileSystem.EnumerateDirectoriesShim(path)
member _.IsStableFileHeuristic(path) =
defaultFileSystem.IsStableFileHeuristic(path)
member this.ChangeExtensionShim(path, extension) =
defaultFileSystem.ChangeExtensionShim(path, extension)
let myFileSystem = MyFileSystem()
FileSystem <- MyFileSystem()
(**
Doing a compilation with the FileSystem
---------------------------------------
*)
let checker = FSharpChecker.Create()
let projectOptions =
let sysLib nm =
if System.Environment.OSVersion.Platform = System.PlatformID.Win32NT then // file references only valid on Windows
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
+ @"\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\"
+ nm
+ ".dll"
else
let sysDir =
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
let (++) a b = System.IO.Path.Combine(a, b)
sysDir ++ nm + ".dll"
let fsCore4300 () =
if System.Environment.OSVersion.Platform = System.PlatformID.Win32NT then // file references only valid on Windows
System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
+ @"\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\FSharp.Core.dll"
else
sysLib "FSharp.Core"
let allFlags =
[| "--simpleresolution"
"--noframework"
"--debug:full"
"--define:DEBUG"
"--optimize-"
"--doc:test.xml"
"--warn:3"
"--fullpaths"
"--flaterrors"
"--target:library"
let references =
[ sysLib "mscorlib"
sysLib "System"
sysLib "System.Core"
fsCore4300 () ]
for r in references do
"-r:" + r |]
{ ProjectFileName = @"c:\mycode\compilation.fsproj" // Make a name that is unique in this directory.
ProjectId = None
SourceFiles = [| fileName1; fileName2 |]
OriginalLoadReferences = []
Stamp = None
OtherOptions = allFlags
ReferencedProjects = [||]
IsIncompleteTypeCheckEnvironment = false
UseScriptResolutionRules = true
LoadTime = System.DateTime.Now // Note using 'Now' forces reloading
UnresolvedReferences = None }
let results =
checker.ParseAndCheckProject(projectOptions)
|> Async.RunSynchronously
results.Diagnostics
results.AssemblySignature.Entities.Count //2
results.AssemblySignature.Entities.[0]
.MembersFunctionsAndValues
.Count
results.AssemblySignature.Entities.[0]
.MembersFunctionsAndValues.[0]
.DisplayName
(**
Summary
-------
In this tutorial, we've seen how to globally customize the view of the file system used by the FSharp.Compiler.Service
component.
At the time of writing, the following System.IO operations are not considered part of the virtualized file system API.
Future iterations on the compiler service implementation may add these to the API.
- Path.Combine
- Path.DirectorySeparatorChar
- Path.GetDirectoryName
- Path.GetFileName
- Path.GetFileNameWithoutExtension
- Path.HasExtension
- Path.GetRandomFileName (used only in generation compiled win32 resources in assemblies)
**NOTE:** Several operations in the `SourceCodeServices` API accept the contents of a file to parse
or check as a parameter, in addition to a file name. In these cases, the file name is only used for
error reporting.
**NOTE:** Type provider components do not use the virtualized file system.
**NOTE:** The compiler service may use MSBuild for assembly resolutions unless `--simpleresolution` is
provided. When using the `FileSystem` API you will normally want to specify `--simpleresolution` as one
of your compiler flags. Also specify `--noframework`. You will need to supply explicit resolutions of all
referenced .NET assemblies.
*)