Skip to content

Commit

Permalink
scripts/styleChecker.fsx: add
Browse files Browse the repository at this point in the history
Add styleChecker.fsx script to check style of our F#, TS
and YML codes.

The `git respore package.json` command in the
styleChecker.fsx script was failing with the following
error, even when I was running the
`git config --global --add safe.directory '*'` command in
both the styleChecker.fsx script and in the CI. In the
[1] issue, it's suggested by someone to use --system
instead of --global in the mentioned command, when the
git command is running in a container, which solved the
problem.

```
fatal: detected dubious ownership in repository at '/__w/conventions/conventions'
To add an exception for this directory, call:

	git config --global --add safe.directory /__w/conventions/conventions

Error when running 'git restore package.json'
Fsdk.Process+ProcessFailed: Exception of type 'Fsdk.Process+ProcessFailed' was thrown.
   at Fsdk.Process.ProcessResult.Unwrap(String errMsg)
   at Fsdk.Process.ProcessResult.UnwrapDefault()
   at FSI_0002.RunPrettier(String arguments)
   at <StartupCode$FSI_0002>.$FSI_0002.main@()
Stopped due to error
Error: Process completed with exit code 1.
```

[1] actions/checkout#1048
  • Loading branch information
tehraninasab committed Apr 4, 2023
1 parent 282d3f1 commit 07c3a8f
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 22 deletions.
26 changes: 4 additions & 22 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ jobs:
fetch-depth: 0
# workaround for https://github.com/actions/runner/issues/2033
- name: ownership workaround
run: git config --global --add safe.directory '*'
run: git config --system --add safe.directory '*'
- name: Print versions
run: |
git --version
Expand Down Expand Up @@ -146,24 +146,6 @@ jobs:
run: dotnet fsi scripts/detectNotUsingGitPush1by1.fsx
- name: Install prettier
run: npm install [email protected]
- name: Change file permissions
# We need this step so we can change the files using `npx prettier --write` in the next step.
# Otherwise we get permission denied error in the CI.
run: sudo chmod 777 -R .
- name: Run "prettier" to check the style of our TypeScript and YML code
run: |
sudo npx prettier --quote-props=consistent --write './**/*.ts'
sudo npx prettier --quote-props=consistent --write './**/*.yml'
# Since we changed file modes in the previous step we need the following command to
# make git ignore mode changes in files and doesn't include them in the git diff command.
git config core.fileMode false
# Since after installing commitlint dependencies package.json file changes, we need to
# run the following command to ignore package.json file
git restore package.json
git diff --exit-code
- name: fantomless
run: |
dotnet new tool-manifest
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
run: sudo dotnet fsi scripts/styleChecker.fsx

149 changes: 149 additions & 0 deletions scripts/styleChecker.fsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/usr/bin/env -S dotnet fsi

#r "nuget: Fsdk, Version=0.6.0--date20230326-0544.git-5c4f55b"

open System

open Fsdk
open Fsdk.Process

let StyleFSharpFiles() =
Process
.Execute(
{
Command = "dotnet"
Arguments = "new tool-manifest --force"
},
Process.Echo.Off
)
.UnwrapDefault()
|> ignore

Process
.Execute(
{
Command = "dotnet"
Arguments =
"tool install fantomless-tool --version 4.7.997-prerelease"
},
Process.Echo.Off
)
.UnwrapDefault()
|> ignore

Process
.Execute(
{
Command = "dotnet"
Arguments = "fantomless --recurse ."
},
Process.Echo.Off
)
.UnwrapDefault()
|> ignore

let RunPrettier(arguments: string) =
let processResult =
Process.Execute(
{
Command = "npx"
Arguments = $"prettier {arguments}"
},
Process.Echo.All
)

let errMsg =
sprintf
"Error when running '%s %s'"
processResult.Details.Command
processResult.Details.Args

match processResult.Result with
| Success output -> output
| Error(_, output) ->
if processResult.Details.Echo = Echo.Off then
output.PrintToConsole()
Console.WriteLine()
Console.Out.Flush()

Console.Error.WriteLine errMsg
raise <| ProcessFailed errMsg
| WarningsOrAmbiguous output ->
if processResult.Details.Echo = Echo.Off then
output.PrintToConsole()
Console.WriteLine()
Console.Out.Flush()

let fullErrMsg = sprintf "%s (with warnings?)" errMsg
fullErrMsg
|> printfn "%A"


// Since after installing commitlint dependencies package.json file changes, we need to
// run the following command to ignore package.json file
Process
.Execute(
{
Command = "git"
Arguments = "restore package.json"
},
Process.Echo.Off
)
.UnwrapDefault()
|> ignore

let StyleTypeScriptFiles() =
RunPrettier "--quote-props=consistent --write ./**/*.ts"

let StyleYmlFiles() =
RunPrettier "--quote-props=consistent --write ./**/*.yml"

StyleFSharpFiles()
StyleTypeScriptFiles()
StyleYmlFiles()

let processResult =
Process.Execute(
{
Command = "git"
Arguments = "diff --exit-code"
},
Process.Echo.Off
)

let errMsg =
sprintf
"Error when running '%s %s'"
processResult.Details.Command
processResult.Details.Args

let suggestion =
"Please use the following commands to style your code:"
+ System.Environment.NewLine
+ "Style your F# code using: `dotnet fantomless --recurse .`"
+ System.Environment.NewLine
+ "Style your TypeScript code using: `npx prettier --quote-props=consistent --write ./**/*.ts`"
+ System.Environment.NewLine
+ "Style your YML code using: `npx prettier --quote-props=consistent --write ./**/*.yml`"
+ System.Environment.NewLine

match processResult.Result with
| Success output -> output
| Error(_, output) ->
if processResult.Details.Echo = Echo.Off then
output.PrintToConsole()
Console.WriteLine()
Console.Out.Flush()

let fullErrMsg = suggestion + System.Environment.NewLine + errMsg
Console.Error.WriteLine fullErrMsg
raise <| ProcessFailed fullErrMsg
| WarningsOrAmbiguous output ->
if processResult.Details.Echo = Echo.Off then
output.PrintToConsole()
Console.WriteLine()
Console.Out.Flush()

let fullErrMsg = sprintf "%s (with warnings?)" errMsg
fullErrMsg
|> printfn "%A"

0 comments on commit 07c3a8f

Please sign in to comment.