Skip to content

Commit 482347c

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 issue [1], 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 282d3f1 commit 482347c

2 files changed

Lines changed: 266 additions & 24 deletions

File tree

.github/workflows/CI.yml

Lines changed: 3 additions & 24 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
@@ -144,26 +144,5 @@ jobs:
144144
- name: Check if gitPush1by1 was used
145145
if: github.event_name == 'pull_request'
146146
run: dotnet fsi scripts/detectNotUsingGitPush1by1.fsx
147-
- name: Install prettier
148-
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 and YML code
154-
run: |
155-
sudo npx prettier --quote-props=consistent --write './**/*.ts'
156-
sudo npx prettier --quote-props=consistent --write './**/*.yml'
157-
# Since we changed file modes in the previous step we need the following command to
158-
# make git ignore mode changes in files and doesn't include them in the git diff command.
159-
git config core.fileMode false
160-
# Since after installing commitlint dependencies package.json file changes, we need to
161-
# run the following command to ignore package.json file
162-
git restore package.json
163-
git diff --exit-code
164-
- name: fantomless
165-
run: |
166-
dotnet new tool-manifest
167-
dotnet tool install fantomless-tool --version 4.7.997-prerelease
168-
dotnet fantomless --recurse .
169-
git diff --exit-code
147+
- name: Check style of our F#, TypeScript and YML code
148+
run: sudo dotnet fsi scripts/styleChecker.fsx

scripts/styleChecker.fsx

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
#!/usr/bin/env -S dotnet fsi
2+
3+
#r "nuget: Fsdk, Version=0.6.0--date20230326-0544.git-5c4f55b"
4+
#load "../src/FileConventions/Helpers.fs"
5+
6+
open System
7+
open System.IO
8+
9+
open Fsdk
10+
open Fsdk.Process
11+
12+
open Helpers
13+
14+
let fantomlessToolVersion = "4.7.997-prerelease"
15+
let prettierVersion = "2.8.3"
16+
17+
let StyleFSharpFiles() =
18+
Process
19+
.Execute(
20+
{
21+
Command = "dotnet"
22+
Arguments = "new tool-manifest --force"
23+
},
24+
Echo.Off
25+
)
26+
.UnwrapDefault()
27+
|> ignore
28+
29+
Process
30+
.Execute(
31+
{
32+
Command = "dotnet"
33+
Arguments =
34+
$"tool install fantomless-tool --version {fantomlessToolVersion}"
35+
},
36+
Echo.Off
37+
)
38+
.UnwrapDefault()
39+
|> ignore
40+
41+
Process
42+
.Execute(
43+
{
44+
Command = "dotnet"
45+
Arguments = "fantomless --recurse ."
46+
},
47+
Echo.Off
48+
)
49+
.UnwrapDefault()
50+
|> ignore
51+
52+
let RunPrettier(arguments: string) =
53+
54+
// We need this step so we can change the files using `npx prettier --write` in the next step.
55+
// Otherwise we get permission denied error in the CI.
56+
Process.Execute(
57+
{
58+
Command = "chmod"
59+
Arguments = "777 -R ."
60+
},
61+
Echo.Off
62+
)
63+
|> ignore
64+
65+
let processResult =
66+
Process.Execute(
67+
{
68+
Command = "npx"
69+
Arguments = $"prettier {arguments}"
70+
},
71+
Echo.Off
72+
)
73+
74+
let errMsg =
75+
sprintf
76+
"Error when running '%s %s'"
77+
processResult.Details.Command
78+
processResult.Details.Args
79+
80+
match processResult.Result with
81+
| Success output -> Console.WriteLine output
82+
| Error(_, output) ->
83+
if processResult.Details.Echo = Echo.Off then
84+
output.PrintToConsole()
85+
Console.WriteLine()
86+
Console.Out.Flush()
87+
88+
Console.Error.WriteLine errMsg
89+
raise <| ProcessFailed errMsg
90+
| WarningsOrAmbiguous output ->
91+
if processResult.Details.Echo = Echo.Off then
92+
output.PrintToConsole()
93+
Console.WriteLine()
94+
Console.Out.Flush()
95+
96+
let fullErrMsg = sprintf "%s (with warnings?)" errMsg
97+
Console.Error.WriteLine fullErrMsg
98+
99+
// Since after installing commitlint dependencies package.json file changes, we need to
100+
// run the following command to ignore package.json file
101+
Process
102+
.Execute(
103+
{
104+
Command = "git"
105+
Arguments = "restore package.json"
106+
},
107+
Echo.Off
108+
)
109+
.UnwrapDefault()
110+
|> ignore
111+
112+
let StyleTypeScriptFiles() =
113+
RunPrettier "--quote-props=consistent --write ./**/*.ts"
114+
115+
let StyleYmlFiles() =
116+
RunPrettier "--quote-props=consistent --write ./**/*.yml"
117+
118+
let ContainsFiles (rootDir: DirectoryInfo) (searchPattern: string) =
119+
Helpers.GetFiles rootDir searchPattern |> Seq.length > 0
120+
121+
let GitDiff() : ProcessResult =
122+
123+
// Since we changed file modes in the prettier step we need the following command to
124+
// make git ignore mode changes in files and doesn't include them in the git diff command.
125+
Process.Execute(
126+
{
127+
Command = "git"
128+
Arguments = "config core.fileMode false"
129+
},
130+
Echo.Off
131+
)
132+
|> ignore
133+
134+
let processResult =
135+
Process.Execute(
136+
{
137+
Command = "git"
138+
Arguments = "diff --exit-code"
139+
},
140+
Echo.Off
141+
)
142+
143+
processResult
144+
145+
let GitRestore() =
146+
Process.Execute(
147+
{
148+
Command = "git"
149+
Arguments = "restore ."
150+
},
151+
Echo.Off
152+
)
153+
|> ignore
154+
155+
let InstallPrettier(version: string) =
156+
Process.Execute(
157+
{
158+
Command = "npm"
159+
Arguments = $"install prettier@{version}"
160+
},
161+
Echo.Off
162+
)
163+
|> ignore
164+
165+
let PrintProcessResult (processResult: ProcessResult) (suggestion: string) =
166+
let errMsg =
167+
sprintf
168+
"Error when running '%s %s'"
169+
processResult.Details.Command
170+
processResult.Details.Args
171+
172+
match processResult.Result with
173+
| Success output -> Console.WriteLine output
174+
| Error(_, output) ->
175+
if processResult.Details.Echo = Echo.Off then
176+
output.PrintToConsole()
177+
Console.WriteLine()
178+
Console.Out.Flush()
179+
180+
let fullErrMsg = errMsg + Environment.NewLine + suggestion
181+
Console.Error.WriteLine fullErrMsg
182+
183+
| WarningsOrAmbiguous output ->
184+
if processResult.Details.Echo = Echo.Off then
185+
output.PrintToConsole()
186+
Console.WriteLine()
187+
Console.Out.Flush()
188+
189+
let fullErrMsg = sprintf "%s (with warnings?)" errMsg
190+
Console.Error.WriteLine fullErrMsg
191+
192+
let GetProcessExitCode(processResult: ProcessResult) : int =
193+
match processResult.Result with
194+
| Success output -> 0
195+
| _ -> 1
196+
197+
let CheckStyleOfFSharpFiles(rootDir: DirectoryInfo) : bool =
198+
let suggestion =
199+
"Please style your F# code using: `dotnet fantomless --recurse .`"
200+
201+
GitRestore()
202+
203+
let success =
204+
if ContainsFiles rootDir "*.fs" || ContainsFiles rootDir ".fsx" then
205+
StyleFSharpFiles()
206+
let processResult = GitDiff()
207+
PrintProcessResult processResult suggestion
208+
GetProcessExitCode processResult = 0
209+
210+
else
211+
true
212+
213+
success
214+
215+
let CheckStyleOfTypeScriptFiles(rootDir: DirectoryInfo) : bool =
216+
let suggestion =
217+
"Please style your TypeScript code using: `npx prettier --quote-props=consistent --write ./**/*.ts`"
218+
219+
GitRestore()
220+
221+
let success =
222+
if ContainsFiles rootDir "*.ts" then
223+
InstallPrettier prettierVersion
224+
StyleTypeScriptFiles()
225+
let processResult = GitDiff()
226+
PrintProcessResult processResult suggestion
227+
GetProcessExitCode processResult = 0
228+
229+
else
230+
true
231+
232+
success
233+
234+
let CheckStyleOfYmlFiles(rootDir: DirectoryInfo) : bool =
235+
let suggestion =
236+
"Please style your YML code using: `npx prettier --quote-props=consistent --write ./**/*.yml`"
237+
238+
GitRestore()
239+
240+
let success =
241+
if ContainsFiles rootDir "*.yml" then
242+
InstallPrettier prettierVersion
243+
StyleYmlFiles()
244+
let processResult = GitDiff()
245+
PrintProcessResult processResult suggestion
246+
GetProcessExitCode processResult = 0
247+
else
248+
true
249+
250+
success
251+
252+
253+
let rootDir = Path.Combine(__SOURCE_DIRECTORY__, "..") |> DirectoryInfo
254+
255+
let processSuccessStates =
256+
[|
257+
CheckStyleOfFSharpFiles rootDir
258+
CheckStyleOfTypeScriptFiles rootDir
259+
CheckStyleOfYmlFiles rootDir
260+
|]
261+
262+
if processSuccessStates |> Seq.contains false then
263+
Environment.Exit 1

0 commit comments

Comments
 (0)