Skip to content

Commit 3339696

Browse files
committed
scripts/styleChecker.fsx: add
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
1 parent 73ada7e commit 3339696

2 files changed

Lines changed: 148 additions & 21 deletions

File tree

.github/workflows/CI.yml

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ jobs:
112112
fetch-depth: 0
113113
# workaround for https://github.com/actions/runner/issues/2033
114114
- name: ownership workaround
115-
run: git config --global --add safe.directory '*'
115+
run: git config --system --add safe.directory '*'
116116
- name: Print versions
117117
run: |
118118
git --version
@@ -146,24 +146,6 @@ jobs:
146146
run: dotnet fsi scripts/detectNotUsingGitPush1by1.fsx
147147
- name: Install prettier
148148
run: npm install prettier@2.8.3
149-
- name: Change file permissions
150-
# We need this step so we can change the files using `npx prettier --write` in the next step.
151-
# Otherwise we get permission denied error in the CI.
152-
run: sudo chmod 777 -R .
153-
- name: Run "prettier" to check the style of our TypeScript code
154-
run: |
155-
sudo npx prettier --quote-props=consistent --write './**/*.ts'
156-
# Since we changed file modes in the previous step we need the following command to
157-
# make git ignore mode changes in files and doesn't include them in the git diff command.
158-
git config core.fileMode false
159-
# Since after installing commitlint dependencies package.json file changes, we need to
160-
# run the following command to ignore package.json file
161-
git restore package.json
162-
git diff --exit-code
163-
- name: fantomless
164-
run: |
165-
dotnet new tool-manifest
166-
dotnet tool install fantomless-tool --version 4.7.997-prerelease
167-
dotnet fantomless --recurse .
168-
git diff --exit-code
149+
- name: Check style of our F#, TypeScript and YML code
150+
run: sudo dotnet fsi scripts/styleChecker.fsx
169151

scripts/styleChecker.fsx

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/env -S dotnet fsi
2+
3+
#r "nuget: Fsdk, Version=0.6.0--date20230326-0544.git-5c4f55b"
4+
5+
open System
6+
7+
open Fsdk
8+
open Fsdk.Process
9+
10+
let StyleFSharpFiles() =
11+
Process
12+
.Execute(
13+
{
14+
Command = "dotnet"
15+
Arguments = "new tool-manifest --force"
16+
},
17+
Process.Echo.Off
18+
)
19+
.UnwrapDefault()
20+
|> ignore
21+
22+
Process
23+
.Execute(
24+
{
25+
Command = "dotnet"
26+
Arguments =
27+
"tool install fantomless-tool --version 4.7.997-prerelease"
28+
},
29+
Process.Echo.Off
30+
)
31+
.UnwrapDefault()
32+
|> ignore
33+
34+
Process
35+
.Execute(
36+
{
37+
Command = "dotnet"
38+
Arguments = "fantomless --recurse ."
39+
},
40+
Process.Echo.Off
41+
)
42+
.UnwrapDefault()
43+
|> ignore
44+
45+
let RunPrettier(arguments: string) =
46+
let processResult =
47+
Process.Execute(
48+
{
49+
Command = "npx"
50+
Arguments = $"prettier {arguments}"
51+
},
52+
Process.Echo.All
53+
)
54+
55+
let errMsg =
56+
sprintf
57+
"Error when running '%s %s'"
58+
processResult.Details.Command
59+
processResult.Details.Args
60+
61+
match processResult.Result with
62+
| Success output -> output
63+
| Error(_, output) ->
64+
if processResult.Details.Echo = Echo.Off then
65+
output.PrintToConsole()
66+
Console.WriteLine()
67+
Console.Out.Flush()
68+
69+
Console.Error.WriteLine errMsg
70+
raise <| ProcessFailed errMsg
71+
| WarningsOrAmbiguous output ->
72+
if processResult.Details.Echo = Echo.Off then
73+
output.PrintToConsole()
74+
Console.WriteLine()
75+
Console.Out.Flush()
76+
77+
let fullErrMsg = sprintf "%s (with warnings?)" errMsg
78+
fullErrMsg
79+
|> printfn "%A"
80+
81+
82+
// Since after installing commitlint dependencies package.json file changes, we need to
83+
// run the following command to ignore package.json file
84+
Process
85+
.Execute(
86+
{
87+
Command = "git"
88+
Arguments = "restore package.json"
89+
},
90+
Process.Echo.Off
91+
)
92+
.UnwrapDefault()
93+
|> ignore
94+
95+
let StyleTypeScriptFiles() =
96+
RunPrettier "--quote-props=consistent --write ./**/*.ts"
97+
98+
let StyleYmlFiles() =
99+
RunPrettier "--quote-props=consistent --write ./**/*.yml"
100+
101+
StyleFSharpFiles()
102+
StyleTypeScriptFiles()
103+
StyleYmlFiles()
104+
105+
let processResult =
106+
Process.Execute(
107+
{
108+
Command = "git"
109+
Arguments = "diff --exit-code"
110+
},
111+
Process.Echo.Off
112+
)
113+
114+
let errMsg =
115+
sprintf
116+
"Error when running '%s %s'"
117+
processResult.Details.Command
118+
processResult.Details.Args
119+
120+
let suggestion =
121+
"Please use the following commands to style your code:"
122+
+ "Style your F# code using: `dotnet fantomless --recurse .`"
123+
+ "Style your TypeScript code using: `npx prettier --quote-props=consistent --write ./**/*.ts`"
124+
+ "Style your YML code using: `npx prettier --quote-props=consistent --write ./**/*.yml`"
125+
126+
match processResult.Result with
127+
| Success output -> output
128+
| Error(_, output) ->
129+
if processResult.Details.Echo = Echo.Off then
130+
output.PrintToConsole()
131+
Console.WriteLine()
132+
Console.Out.Flush()
133+
134+
let fullErrMsg = suggestion + System.Environment.NewLine + errMsg
135+
Console.Error.WriteLine fullErrMsg
136+
raise <| ProcessFailed fullErrMsg
137+
| WarningsOrAmbiguous output ->
138+
if processResult.Details.Echo = Echo.Off then
139+
output.PrintToConsole()
140+
Console.WriteLine()
141+
Console.Out.Flush()
142+
143+
let fullErrMsg = sprintf "%s (with warnings?)" errMsg
144+
fullErrMsg
145+
|> printfn "%A"

0 commit comments

Comments
 (0)