Skip to content

Commit 206ca38

Browse files
authored
Merge pull request #1173 from json-api-dotnet/cleanupcode-diff
Enable running code cleanup locally only on files changed since the specified branch
2 parents 3069341 + 770b848 commit 206ca38

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
]
1010
},
1111
"regitlint": {
12-
"version": "6.0.8",
12+
"version": "6.1.1",
1313
"commands": [
1414
"regitlint"
1515
]

.github/CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ Please follow these steps to have your contribution considered by the maintainer
5858

5959
We use [CSharpGuidelines](https://csharpcodingguidelines.com/) as our coding standard (with a few minor exceptions). Coding style is validated during PR build, where we inject an extra settings layer that promotes various suggestions to warning level. This ensures a high-quality codebase without interfering too much when editing code.
6060
You can run the following [PowerShell scripts](https://github.com/PowerShell/PowerShell/releases) locally:
61-
- `pwsh inspectcode.ps1`: Scans the code for style violations and opens the result in your web browser.
62-
- `pwsh cleanupcode.ps1`: Reformats the entire codebase to match with our configured style.
61+
- `pwsh ./inspectcode.ps1`: Scans the code for style violations and opens the result in your web browser.
62+
- `pwsh ./cleanupcode.ps1 [branch-name-or-commit-hash]`: Reformats the codebase to match with our configured style, optionally only changed files since the specified branch (usually master).
6363

6464
Code inspection violations can be addressed in several ways, depending on the situation:
6565
- Types that are reported to be never instantiated (because the IoC container creates them dynamically) should be decorated with `[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]`.

Build.ps1

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,21 @@ function RunCleanupCode {
4040
# When running in cibuild for a pull request, this reformats only the files changed in the PR and fails if the reformat produces changes.
4141

4242
if ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) {
43-
Write-Output "Running code cleanup on changed files in pull request"
44-
4543
# In the past, we used $env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT for the merge commit hash. That is the pinned hash at the time the build is enqueued.
4644
# When a force-push happens after that, while the build hasn't yet started, this hash becomes invalid during the build, resulting in a lookup error.
47-
# To prevent failing the build for unobvious reasons we use HEAD, which is always the latest version.
48-
$mergeCommitHash = git rev-parse "HEAD"
49-
$targetCommitHash = git rev-parse "$env:APPVEYOR_REPO_BRANCH"
45+
# To prevent failing the build for unobvious reasons we use HEAD, which is always a detached head (the PR merge result).
46+
47+
$headCommitHash = git rev-parse HEAD
48+
CheckLastExitCode
5049

51-
dotnet regitlint -s JsonApiDotNetCore.sln --print-command --disable-jb-path-hack --jb --profile='\"JADNC Full Cleanup\"' --jb --properties:Configuration=Release --jb --verbosity=WARN -f commits -a $mergeCommitHash -b $targetCommitHash --fail-on-diff --print-diff
50+
$baseCommitHash = git rev-parse "$env:APPVEYOR_REPO_BRANCH"
5251
CheckLastExitCode
52+
53+
if ($baseCommitHash -ne $headCommitHash) {
54+
Write-Output "Running code cleanup on commit range $baseCommitHash..$headCommitHash in pull request."
55+
dotnet regitlint -s JsonApiDotNetCore.sln --print-command --skip-tool-check --jb-profile="JADNC Full Cleanup" --jb --properties:Configuration=Release --jb --verbosity=WARN -f commits -a $headCommitHash -b $baseCommitHash --fail-on-diff --print-diff
56+
CheckLastExitCode
57+
}
5358
}
5459
}
5560

cleanupcode.ps1

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,44 @@
11
#Requires -Version 7.0
22

3-
# This script reformats the entire codebase to make it compliant with our coding guidelines.
3+
# This script reformats (part of) the codebase to make it compliant with our coding guidelines.
44

5-
dotnet tool restore
5+
param(
6+
# Git branch name or base commit hash to reformat only the subset of changed files. Omit for all files.
7+
[string] $revision
8+
)
69

7-
if ($LASTEXITCODE -ne 0) {
8-
throw "Tool restore failed with exit code $LASTEXITCODE"
10+
function VerifySuccessExitCode {
11+
if ($LastExitCode -ne 0) {
12+
throw "Command failed with exit code $LastExitCode."
13+
}
914
}
1015

16+
dotnet tool restore
17+
VerifySuccessExitCode
18+
1119
dotnet restore
20+
VerifySuccessExitCode
1221

13-
if ($LASTEXITCODE -ne 0) {
14-
throw "Package restore failed with exit code $LASTEXITCODE"
15-
}
22+
if ($revision) {
23+
$headCommitHash = git rev-parse HEAD
24+
VerifySuccessExitCode
1625

17-
dotnet regitlint -s JsonApiDotNetCore.sln --print-command --disable-jb-path-hack --jb --profile='\"JADNC Full Cleanup\"' --jb --properties:Configuration=Release --jb --verbosity=WARN
26+
$baseCommitHash = git rev-parse $revision
27+
VerifySuccessExitCode
28+
29+
if ($baseCommitHash -eq $headCommitHash) {
30+
Write-Output "Running code cleanup on staged/unstaged files."
31+
dotnet regitlint -s JsonApiDotNetCore.sln --print-command --skip-tool-check --jb-profile="JADNC Full Cleanup" --jb --properties:Configuration=Release --jb --verbosity=WARN -f staged,modified
32+
VerifySuccessExitCode
33+
}
34+
else {
35+
Write-Output "Running code cleanup on commit range $baseCommitHash..$headCommitHash, including staged/unstaged files."
36+
dotnet regitlint -s JsonApiDotNetCore.sln --print-command --skip-tool-check --jb-profile="JADNC Full Cleanup" --jb --properties:Configuration=Release --jb --verbosity=WARN -f staged,modified,commits -a $headCommitHash -b $baseCommitHash
37+
VerifySuccessExitCode
38+
}
39+
}
40+
else {
41+
Write-Output "Running code cleanup on all files."
42+
dotnet regitlint -s JsonApiDotNetCore.sln --print-command --skip-tool-check --jb-profile="JADNC Full Cleanup" --jb --properties:Configuration=Release --jb --verbosity=WARN
43+
VerifySuccessExitCode
44+
}

0 commit comments

Comments
 (0)