Skip to content

Commit

Permalink
scripts/styleChecker.fsx: check C# & XAML files
Browse files Browse the repository at this point in the history
  • Loading branch information
tehraninasab committed Aug 9, 2023
1 parent 6f8edca commit cde3c84
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,5 @@ jobs:
dotnet tool install fantomless-tool --version 4.7.997-prerelease
dotnet fantomless --recurse .
git diff --exit-code
- name: Check style of our F#, TypeScript and YML code
- name: Check style of our F#, C#, TypeScript, YML and XAML code
run: sudo dotnet fsi scripts/styleChecker.fsx
101 changes: 101 additions & 0 deletions scripts/styleChecker.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ open Helpers

let fantomlessToolVersion = "4.7.997-prerelease"
let prettierVersion = "2.8.3"
let pluginXmlVersion = "v2.2.0"

let InstallFantomlessTool(version: string) =
let isFantomlessInstalled =
Expand Down Expand Up @@ -149,6 +150,69 @@ let StyleFSharpFiles(rootDir: DirectoryInfo) =
.UnwrapDefault()
|> ignore

let StyleCSharpFiles(rootDir: DirectoryInfo) =
Process
.Execute(
{
Command = "dotnet"
Arguments = $"format whitespace {rootDir.FullName} --folder"
},
Echo.Off
)
.UnwrapDefault()
|> ignore

let InstallPrettierPluginXml(version: string) =
let isPrettierPluginXmlInstalled =
Process.Execute(
{
Command = "npm"
Arguments = $"list @prettier/plugin-xml@{version}"
},
Echo.Off
)
|> IsProcessSuccessful

if not(isPrettierPluginXmlInstalled) then
Process
.Execute(
{
Command = "npm"
Arguments = $"install @prettier/plugin-xml@{version}"
},
Echo.Off
)
.UnwrapDefault()
|> ignore

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

Process
.Execute(
{
Command = "npm"
Arguments =
$"install --save-dev prettier@{prettierVersion} @prettier/plugin-xml@{pluginXmlVersion}"
},
Echo.Off
)
.UnwrapDefault()
|> ignore

Process
.Execute(
{
Command = "./node_modules/.bin/prettier"
Arguments =
"--xml-whitespace-sensitivity ignore --tab-width 4 --prose-wrap preserve --write '**/*.xaml'"
},
Echo.Off
)
.UnwrapDefault()
|> ignore

let RunPrettier(arguments: string) =

// We need this step so we can change the files using `npx prettier --write` in the next step.
Expand Down Expand Up @@ -290,14 +354,51 @@ let CheckStyleOfYmlFiles(rootDir: DirectoryInfo) : bool =

success

let CheckStyleOfCSharpFiles(rootDir: DirectoryInfo) : bool =
let suggestion =
"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
IsProcessSuccessful processResult
else
true

success

let CheckStyleOfXamlFiles(rootDir: DirectoryInfo) : bool =
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`"

GitRestore()

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

success

let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo

let processSuccessStates =
[|
CheckStyleOfFSharpFiles rootDir
CheckStyleOfCSharpFiles rootDir
CheckStyleOfTypeScriptFiles rootDir
CheckStyleOfYmlFiles rootDir
CheckStyleOfXamlFiles rootDir
|]

if processSuccessStates |> Seq.contains false then
Expand Down

0 comments on commit cde3c84

Please sign in to comment.