Skip to content

Commit

Permalink
scripts/styleChecker.fsx: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tehraninasab committed Aug 9, 2023
1 parent cde3c84 commit f57f128
Showing 1 changed file with 59 additions and 25 deletions.
84 changes: 59 additions & 25 deletions scripts/styleChecker.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ let InstallFantomlessTool(version: string) =
|> ignore

let UnwrapProcessResult
(suggestion: string)
(raiseError: bool)
(maybeSuggestion: Option<string>)
(ignoreErrorExitCode: bool)
(processResult: ProcessResult)
: string =
let errMsg =
Expand All @@ -92,13 +92,18 @@ let UnwrapProcessResult
Console.WriteLine()
Console.Out.Flush()

let fullErrMsg = errMsg + Environment.NewLine + suggestion
let fullErrMsg =
match maybeSuggestion with
| Some suggestion -> errMsg + Environment.NewLine + suggestion
| None -> errMsg

Console.Error.WriteLine fullErrMsg

if raiseError then
raise <| ProcessFailed errMsg
else
if ignoreErrorExitCode then
fullErrMsg
else
raise <| ProcessFailed errMsg

| WarningsOrAmbiguous output ->
if processResult.Details.Echo = Echo.Off then
output.PrintToConsole()
Expand Down Expand Up @@ -133,11 +138,11 @@ let InstallPrettier(version: string) =
},
Echo.Off
)
|> UnwrapProcessResult "" true
|> UnwrapProcessResult None false
|> ignore

let StyleFSharpFiles(rootDir: DirectoryInfo) =
InstallFantomlessTool(fantomlessToolVersion)
InstallFantomlessTool fantomlessToolVersion

Process
.Execute(
Expand Down Expand Up @@ -186,8 +191,8 @@ let InstallPrettierPluginXml(version: string) =
|> ignore

let StyleXamlFiles() =
InstallPrettier(prettierVersion)
InstallPrettierPluginXml(pluginXmlVersion)
InstallPrettier prettierVersion
InstallPrettierPluginXml pluginXmlVersion

Process
.Execute(
Expand All @@ -201,12 +206,21 @@ let StyleXamlFiles() =
.UnwrapDefault()
|> ignore

let pattern = $"**{Path.DirectorySeparatorChar}*.xaml"

Process
.Execute(
{
Command = "./node_modules/.bin/prettier"
Command =
Path.Combine(
Directory.GetCurrentDirectory(),
"node_modules",
".bin",
"prettier"
)

Arguments =
"--xml-whitespace-sensitivity ignore --tab-width 4 --prose-wrap preserve --write '**/*.xaml'"
$"--xml-whitespace-sensitivity ignore --tab-width 4 --prose-wrap preserve --write {pattern}"
},
Echo.Off
)
Expand Down Expand Up @@ -235,7 +249,7 @@ let RunPrettier(arguments: string) =
},
Echo.Off
)
|> UnwrapProcessResult "" true
|> UnwrapProcessResult None false
|> ignore


Expand All @@ -253,10 +267,16 @@ let RunPrettier(arguments: string) =
|> ignore

let StyleTypeScriptFiles() =
RunPrettier "--quote-props=consistent --write ./**/*.ts"
let pattern =
$".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.ts"

RunPrettier $"--quote-props=consistent --write {pattern}"

let StyleYmlFiles() =
RunPrettier "--quote-props=consistent --write ./**/*.yml"
let pattern =
$".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.yml"

RunPrettier $"--quote-props=consistent --write {pattern}"

let ContainsFiles (rootDir: DirectoryInfo) (searchPattern: string) =
Helpers.GetFiles rootDir searchPattern |> Seq.length > 0
Expand Down Expand Up @@ -301,15 +321,15 @@ let GitRestore() =

let CheckStyleOfFSharpFiles(rootDir: DirectoryInfo) : bool =
let suggestion =
"Please style your F# code using: `dotnet fantomless --recurse .`"
Some "Please style your F# code using: `dotnet fantomless --recurse .`"

GitRestore()

let success =
if ContainsFiles rootDir "*.fs" || ContainsFiles rootDir ".fsx" then
StyleFSharpFiles rootDir
let processResult = GitDiff()
UnwrapProcessResult suggestion false processResult |> ignore
UnwrapProcessResult suggestion true processResult |> ignore
IsProcessSuccessful processResult

else
Expand All @@ -318,8 +338,12 @@ let CheckStyleOfFSharpFiles(rootDir: DirectoryInfo) : bool =
success

let CheckStyleOfTypeScriptFiles(rootDir: DirectoryInfo) : bool =
let pattern =
$".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.ts"

let suggestion =
"Please style your TypeScript code using: `npx prettier --quote-props=consistent --write ./**/*.ts`"
Some
$"Please style your TypeScript code using: `npx prettier --quote-props=consistent --write {pattern}`"

GitRestore()

Expand All @@ -328,7 +352,7 @@ let CheckStyleOfTypeScriptFiles(rootDir: DirectoryInfo) : bool =
InstallPrettier prettierVersion
StyleTypeScriptFiles()
let processResult = GitDiff()
UnwrapProcessResult suggestion false processResult |> ignore
UnwrapProcessResult suggestion true processResult |> ignore
IsProcessSuccessful processResult

else
Expand All @@ -337,8 +361,12 @@ let CheckStyleOfTypeScriptFiles(rootDir: DirectoryInfo) : bool =
success

let CheckStyleOfYmlFiles(rootDir: DirectoryInfo) : bool =
let pattern =
$".{Path.DirectorySeparatorChar}**{Path.DirectorySeparatorChar}*.yml"

let suggestion =
"Please style your YML code using: `npx prettier --quote-props=consistent --write ./**/*.yml`"
Some
$"Please style your YML code using: `npx prettier --quote-props=consistent --write {pattern}`"

GitRestore()

Expand All @@ -347,7 +375,7 @@ let CheckStyleOfYmlFiles(rootDir: DirectoryInfo) : bool =
InstallPrettier prettierVersion
StyleYmlFiles()
let processResult = GitDiff()
UnwrapProcessResult suggestion false processResult |> ignore
UnwrapProcessResult suggestion true processResult |> ignore
IsProcessSuccessful processResult
else
true
Expand All @@ -356,34 +384,40 @@ let CheckStyleOfYmlFiles(rootDir: DirectoryInfo) : bool =

let CheckStyleOfCSharpFiles(rootDir: DirectoryInfo) : bool =
let suggestion =
"Please style your C# code using: `dotnet format whitespace . --folder"
Some
"Please style your C# code using: `dotnet format whitespace . --folder"

GitRestore()

let success =
if ContainsFiles rootDir "*.cs" then
StyleCSharpFiles rootDir
let processResult = GitDiff()
UnwrapProcessResult suggestion false processResult |> ignore
UnwrapProcessResult suggestion true processResult |> ignore
IsProcessSuccessful processResult
else
true

success

let CheckStyleOfXamlFiles(rootDir: DirectoryInfo) : bool =
let prettierPath = Path.Combine(".", "node_modules", ".bin", "prettier")

let pattern = $"**{Path.DirectorySeparatorChar}*.xaml"

let suggestion =
"Please style your XAML code using:"
+ Environment.NewLine
+ "`./node_modules/.bin/prettier --xml-whitespace-sensitivity ignore --tab-width 4 --prose-wrap preserve --write '**/*.xaml`"
+ $"`{prettierPath} --xml-whitespace-sensitivity ignore --tab-width 4 --prose-wrap preserve --write {pattern}`"
|> Some

GitRestore()

let success =
if ContainsFiles rootDir "*.xaml" then
StyleXamlFiles()
let processResult = GitDiff()
UnwrapProcessResult suggestion false processResult |> ignore
UnwrapProcessResult suggestion true processResult |> ignore
IsProcessSuccessful processResult
else
true
Expand Down

0 comments on commit f57f128

Please sign in to comment.