-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathWorkspacePeek.fs
171 lines (142 loc) · 5.7 KB
/
WorkspacePeek.fs
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
namespace Ionide.ProjInfo.ProjectSystem
open System
open System.IO
open Ionide.ProjInfo
open Ionide.ProjInfo.Logging
module WorkspacePeek =
let rec logger = LogProvider.getLoggerByQuotation <@ logger @>
[<RequireQualifiedAccess>]
type Interesting =
| Solution of string * InspectSln.SolutionData
| Directory of string * string list
open System.IO
type private UsefulFile =
| FsProj
| Sln
| Slnf
| Slnx
| Fsx
[<return:Struct>]
let inline (|HasExt|_|) (ext: string) (file: FileInfo) =
if file.Extension = ext then ValueSome() else ValueNone
let private partitionByChoice3 =
let foldBy (a, b, c) t =
match t with
| Choice1Of3 x -> (x :: a, b, c)
| Choice2Of3 x -> (a, x :: b, c)
| Choice3Of3 x -> (a, b, x :: c)
Array.fold foldBy ([], [], [])
let peek (rootDir: string) deep (excludedDirs: string list) =
if isNull rootDir then
[]
else
let dirInfo = DirectoryInfo(rootDir)
//TODO accept glob list to ignore
let ignored =
let normalizedDirs =
excludedDirs
|> List.map (fun s -> s.ToUpperInvariant())
|> Array.ofList
(fun (s: string) ->
normalizedDirs
|> Array.contains (s.ToUpperInvariant())
)
let scanDir (dirInfo: DirectoryInfo) =
let hasExt (ext: string) (s: FileInfo) = s.FullName.EndsWith(ext)
let topLevelFiles =
try
dirInfo.EnumerateFiles("*.*", SearchOption.TopDirectoryOnly)
with
| :? UnauthorizedAccessException as ex ->
logger.error (
Log.setMessage "Unauthorized access error while reading files of {dir}"
>> Log.addContextDestructured "dir" dirInfo.Name
>> Log.addExn ex
)
Seq.empty
| ex ->
logger.error (
Log.setMessage "Failed to read files of {dir}"
>> Log.addContextDestructured "dir" dirInfo.Name
>> Log.addExn ex
)
Seq.empty
topLevelFiles
|> Seq.choose (fun s ->
match s with
| HasExt ".sln" -> Some(UsefulFile.Sln, s)
| HasExt ".slnf" -> Some(UsefulFile.Slnf, s)
| HasExt ".fsx" -> Some(UsefulFile.Fsx, s)
| HasExt ".fsproj" -> Some(UsefulFile.FsProj, s)
| HasExt ".slnx" -> Some(UsefulFile.Slnx, s)
| _ -> None
)
|> Seq.toArray
let dirs =
let getDirectories (dirInfo: DirectoryInfo) =
try
dirInfo.GetDirectories()
with
| :? UnauthorizedAccessException as ex ->
logger.error (
Log.setMessage "Unauthorized access error while reading sub directories of {dir}"
>> Log.addContextDestructured "dir" dirInfo.Name
>> Log.addExn ex
)
Array.empty
| ex ->
logger.error (
Log.setMessage "Failed to read sub directories of {dir}"
>> Log.addContextDestructured "dir" dirInfo.Name
>> Log.addExn ex
)
Array.empty
let rec scanDirs (dirInfo: DirectoryInfo) lvl =
seq {
if
lvl
<= deep
then
yield dirInfo
for s in getDirectories dirInfo do
if not (ignored s.Name) then
yield! scanDirs s (lvl + 1)
}
scanDirs dirInfo 0
|> Array.ofSeq
let getInfo (t, (f: FileInfo)) =
match t with
| UsefulFile.Sln
| UsefulFile.Slnf
| UsefulFile.Slnx ->
match InspectSln.tryParseSln f.FullName with
| Ok(d) -> Some(Choice1Of3(f.FullName, d))
| Error e ->
logger.warn (
Log.setMessage "Failed to load file: {filePath} : {data}"
>> Log.addContext "filePath" f.FullName
>> Log.addExn e
)
None
| UsefulFile.Fsx -> Some(Choice2Of3(f.FullName))
| UsefulFile.FsProj -> Some(Choice3Of3(f.FullName))
let found =
dirs
|> Array.Parallel.collect scanDir
|> Array.Parallel.choose getInfo
let slns, _fsxs, fsprojs =
found
|> partitionByChoice3
//TODO weight order of fsprojs from sln
let dir =
rootDir,
(fsprojs
|> List.sort)
[
yield!
slns
|> List.map Interesting.Solution
yield
dir
|> Interesting.Directory
]