forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigFile.fs
69 lines (58 loc) · 2.31 KB
/
ConfigFile.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
module internal Fantomas.ConfigFile
open System
open System.IO
open Fantomas.FormatConfig
open Fantomas.Version
let jsonConfigFileName = "fantomas-config.json"
let editorConfigFileName = ".editor-config"
let private allowedFileNames = [jsonConfigFileName; editorConfigFileName]
let rec private getParentFolders acc current =
let parent = Directory.GetParent(current) |> Option.ofObj
match parent with
| Some p -> getParentFolders (current::acc) p.FullName
| None -> current::acc
/// Returns all the found configuration files for the given path
/// fileOrFolder can be a concrete json file or a directory path
let rec findConfigurationFiles fileOrFolder : string list =
let findConfigInFolder folderPath =
allowedFileNames
|> List.map (fun fn -> Path.Combine(folderPath, fn))
|> List.filter (File.Exists)
if Directory.Exists fileOrFolder then
getParentFolders [] fileOrFolder
|> List.collect findConfigInFolder
elif File.Exists(fileOrFolder) then
let parentFolder =
if String.IsNullOrWhiteSpace(Path.GetDirectoryName(fileOrFolder)) then
Directory.GetCurrentDirectory()
|> DirectoryInfo
|> Some
else
Directory.GetParent(Path.GetDirectoryName(fileOrFolder))
|> Option.ofObj
match parentFolder with
| Some pf -> findConfigurationFiles pf.FullName @ [fileOrFolder]
| None -> [fileOrFolder]
else
[]
let makeWarningLocationAware configPath warning =
sprintf "%s, in %s" warning configPath
let private fantomasFields = Reflection.getRecordFields FormatConfig.Default |> Array.map fst
let private (|FantomasSetting|_|) (s:string) =
let s = s.Trim('\"')
Array.tryFind ((=) s) fantomasFields
let private (|Number|_|) d =
match System.Int32.TryParse(d) with
| true, d -> Some (box d)
| _ -> None
let private (|Boolean|_|) b =
if b = "true" then Some (box true)
elif b = "false" then Some (box false)
else None
let processSetting originalLine key value =
match (key, value) with
| (FantomasSetting(fs), Number(v))
| (FantomasSetting(fs), Boolean(v)) -> Ok (fs, v)
| _ ->
let warning = sprintf "%s is no valid setting for Fantomas v%s" originalLine (fantomasVersion.Value)
Error warning