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 Jul 13, 2023
1 parent de0855a commit 2ecf57b
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 @@ -71,8 +71,8 @@ let InstallFantomlessTool(version: string) =
|> ignore

let UnwrapProcessResult
(suggestion: string)
(raiseError: bool)
(maybeSuggestion: Option<string>)
(ignoreErrorExitCode: bool)
(processResult: ProcessResult)
: string =
let errMsg =
Expand All @@ -91,13 +91,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 @@ -132,11 +137,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 @@ -185,8 +190,8 @@ let InstallPrettierPluginXml(version: string) =
|> ignore

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

Process
.Execute(
Expand All @@ -200,12 +205,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 @@ -234,7 +248,7 @@ let RunPrettier(arguments: string) =
},
Echo.Off
)
|> UnwrapProcessResult "" true
|> UnwrapProcessResult None false
|> ignore


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

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

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

let StyleYmlFiles() =
RunPrettier "--quote-props=consistent --write ./**/*.yml"
let pattern =
$"{Directory.GetCurrentDirectory()}{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 @@ -300,15 +320,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 @@ -317,8 +337,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 @@ -327,7 +351,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 @@ -336,8 +360,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 @@ -346,7 +374,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 @@ -355,34 +383,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 2ecf57b

Please sign in to comment.