Skip to content

Commit 02675fe

Browse files
committed
First pass attempt at ionide#118
1 parent e31c6da commit 02675fe

File tree

1 file changed

+41
-14
lines changed

1 file changed

+41
-14
lines changed

src/FSharp.Analyzers.Cli/Program.fs

+41-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Arguments =
1313
| Project of string list
1414
| Analyzers_Path of string
1515
| Fail_On_Warnings of string list
16+
| Fail_On_All_Warnings of except: string list
1617
| Ignore_Files of string list
1718
| Exclude_Analyzer of string list
1819
| Report of string
@@ -25,11 +26,17 @@ type Arguments =
2526
| Analyzers_Path _ -> "Path to a folder where your analyzers are located."
2627
| Fail_On_Warnings _ ->
2728
"List of analyzer codes that should trigger tool failures in the presence of warnings."
29+
| Fail_On_All_Warnings _ ->
30+
"All analyzer codes will trigger tool failure in the presence of warnings, except for the ones listed here."
2831
| Ignore_Files _ -> "Source files that shouldn't be processed."
2932
| Exclude_Analyzer _ -> "The names of analyzers that should not be executed."
3033
| Report _ -> "Write the result messages to a (sarif) report file."
3134
| Verbose -> "Verbose logging."
3235

36+
type WarningConfig =
37+
| FailOnWarnings of string list
38+
| FailOnAllWarnings of except: string list
39+
3340
let mutable verbose = false
3441

3542
let fcs = Utils.createFCS None
@@ -111,7 +118,7 @@ let runProject (client: Client<CliAnalyzerAttribute, CliContext>) toolsPath proj
111118
]
112119
}
113120

114-
let printMessages failOnWarnings (msgs: AnalyzerMessage list) =
121+
let printMessages warningConfig (msgs: AnalyzerMessage list) =
115122
if verbose then
116123
printfn ""
117124

@@ -123,12 +130,13 @@ let printMessages failOnWarnings (msgs: AnalyzerMessage list) =
123130
let m = analyzerMessage.Message
124131

125132
let color =
126-
match m.Severity with
127-
| Error -> ConsoleColor.Red
128-
| Warning when failOnWarnings |> List.contains m.Code -> ConsoleColor.Red
129-
| Warning -> ConsoleColor.DarkYellow
130-
| Info -> ConsoleColor.Blue
131-
| Hint -> ConsoleColor.Cyan
133+
match m.Severity, warningConfig with
134+
| Error, _ -> ConsoleColor.Red
135+
| Warning, FailOnWarnings inclusions when inclusions |> List.contains m.Code -> ConsoleColor.Red
136+
| Warning, FailOnAllWarnings exclusions when exclusions |> List.contains m.Code |> not -> ConsoleColor.Red
137+
| Warning, _ -> ConsoleColor.DarkYellow
138+
| Info, _ -> ConsoleColor.Blue
139+
| Hint, _ -> ConsoleColor.Cyan
132140

133141
Console.ForegroundColor <- color
134142

@@ -226,7 +234,7 @@ let writeReport (results: AnalyzerMessage list option) (report: string) =
226234
let details = if not verbose then "" else $" %s{ex.Message}"
227235
printfn $"Could not write sarif to %s{report}%s{details}"
228236

229-
let calculateExitCode failOnWarnings (msgs: AnalyzerMessage list option) : int =
237+
let calculateExitCode warningConfig (msgs: AnalyzerMessage list option) : int =
230238
match msgs with
231239
| None -> -1
232240
| Some msgs ->
@@ -235,8 +243,13 @@ let calculateExitCode failOnWarnings (msgs: AnalyzerMessage list option) : int =
235243
|> List.exists (fun analyzerMessage ->
236244
let message = analyzerMessage.Message
237245

238-
message.Severity = Error
239-
|| (message.Severity = Warning && failOnWarnings |> List.contains message.Code)
246+
match warningConfig with
247+
| FailOnWarnings inclusions ->
248+
message.Severity = Error
249+
|| (message.Severity = Warning && inclusions |> List.contains message.Code)
250+
| FailOnAllWarnings exclusions ->
251+
message.Severity = Error
252+
|| (message.Severity = Warning && not (exclusions |> List.contains message.Code))
240253
)
241254

242255
if check then -2 else 0
@@ -249,8 +262,22 @@ let main argv =
249262
verbose <- results.Contains <@ Verbose @>
250263
printInfo "Running in verbose mode"
251264

252-
let failOnWarnings = results.GetResult(<@ Fail_On_Warnings @>, [])
253-
printInfo "Fail On Warnings: [%s]" (failOnWarnings |> String.concat ", ")
265+
let warningConfig =
266+
match results.TryGetResult(<@ Fail_On_All_Warnings @>) with
267+
| Some exceptions ->
268+
match exceptions with
269+
| [] ->
270+
printInfo "Fail On All Warnings"
271+
FailOnAllWarnings []
272+
| _ ->
273+
printInfo "Fail On All Warnings Except: [%s]" (exceptions |> String.concat ", ")
274+
FailOnAllWarnings exceptions
275+
| None ->
276+
match results.TryGetResult(<@ Fail_On_Warnings @>) with
277+
| Some inclusions ->
278+
printInfo "Fail On Warnings: [%s]" (inclusions |> String.concat ", ")
279+
FailOnWarnings inclusions
280+
| None -> FailOnWarnings []
254281

255282
let ignoreFiles = results.GetResult(<@ Ignore_Files @>, [])
256283
printInfo "Ignore Files: [%s]" (ignoreFiles |> String.concat ", ")
@@ -309,7 +336,7 @@ let main argv =
309336
Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, proj))
310337

311338
let! results = runProject client toolsPath project ignoreFiles
312-
return results |> Option.map (printMessages failOnWarnings)
339+
return results |> Option.map (printMessages warningConfig)
313340
}
314341

315342
projects
@@ -322,4 +349,4 @@ let main argv =
322349

323350
report |> Option.iter (writeReport results)
324351

325-
calculateExitCode failOnWarnings results
352+
calculateExitCode warningConfig results

0 commit comments

Comments
 (0)