Skip to content

dotnet format

dotnet format #2233

Workflow file for this run

name: dotnet format
on:
workflow_dispatch:
push:
branches: [ main ]
paths-ignore:
- "**.md"
pull_request:
branches: [ main ]
schedule:
- cron: '0 0 * * 0' # Once a week: "At 00:00 on Sunday."
defaults:
run:
shell: pwsh
jobs:
main:
name: dotnet format
permissions:
contents: read
pull-requests: read
runs-on: ubuntu-latest
env:
WORKFLOW_INFO_ARTIFACT_FILEPATH: ${{github.workspace}}/dotnet-format-workflow-info.json
WORKFLOW_RUN_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
DOTNET_FORMAT_REPORT_FILEPATH: ${{github.workspace}}/dotnet-format-report.json
steps:
- name: Dump github context for debug purposes
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: $env:GITHUB_CONTEXT
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: |
6.0.x
7.0.x
global-json-file: global.json
- name: Cache/Restore NuGets
uses: actions/cache@v4
with:
path:
~/.nuget/packages
key: ${{ runner.os }}-nuget-
restore-keys: |
${{ runner.os }}-nuget-
- name: dotnet format
run: |
Write-Output "::group::Running dotnet format"
dotnet format ${{ github.workspace }}/DotNet.Sdk.Extensions.sln `
--severity info `
--verbosity diagnostic `
--report ${{ env.DOTNET_FORMAT_REPORT_FILEPATH }}
Write-Output "::endgroup::"
- name: Parse dotnet format
id: dotnet-format-parse
run: |
$dotnetFormatReport = Get-Content ${{ env.DOTNET_FORMAT_REPORT_FILEPATH }} | ConvertFrom-Json
$filePaths = $dotnetFormatReport.FilePath | Get-Unique
$dotnetFormatHasChanges = $filePaths.Count -ne 0
$dotnetFormatHasChangesAsString = $dotnetFormatHasChanges.ToString()
Write-Output "has-changes=$dotnetFormatHasChangesAsString" >> $env:GITHUB_OUTPUT
- name: Delete all files without dotnet format changes
if: steps.dotnet-format-parse.outputs.has-changes == 'true'
run: |
$files = Get-ChildItem -Path "${{ github.workspace }}" -File -Recurse -Force
$dotnetFormatReport = Get-Content ${{ env.DOTNET_FORMAT_REPORT_FILEPATH }} | ConvertFrom-Json
$filePaths = $dotnetFormatReport.FilePath | Get-Unique
# delete all files except for the ones with dotnet format changes
foreach($file in $files) {
if($filePaths -Contains $file.FullName) {
# don't do anything on files that have changes from dotnet format
}
else {
rm $file.FullName
}
}
# delete all empty folders
Get-ChildItem $tdc -Recurse -Force -Directory |
Sort-Object -Property FullName -Descending |
Where-Object { $($_ | Get-ChildItem -Force | Select-Object -First 1).Count -eq 0 } |
Remove-Item -Force
- name: Upload files changed by dotnet format
if: steps.dotnet-format-parse.outputs.has-changes == 'true'
uses: actions/upload-artifact@v4
with:
name: dotnet-format-files
path: ${{ github.workspace }}
- name: Save workflow info
run: |
$hasChanges = [System.Convert]::ToBoolean("${{ steps.dotnet-format-parse.outputs.has-changes }}")
$isPullRequest = "${{ github.event_name }}" -eq "pull_request"
if($isPullRequest) {
$body = @{
hasChanges = $hasChanges
workflow = "${{ github.workflow }}"
workflowUrl = "${{ env.WORKFLOW_RUN_URL }}"
prNumber = "${{ github.event.pull_request.number }}"
prBranchName = "${{ github.event.pull_request.head.ref }}"
}
}
else {
$body = @{
hasChanges = $hasChanges
workflow = "${{ github.workflow }}"
workflowUrl = "${{ env.WORKFLOW_RUN_URL }}"
}
}
$bodyAsJson = $body | ConvertTo-Json
$bodyAsJson > ${{ env.WORKFLOW_INFO_ARTIFACT_FILEPATH }}
cat ${{ env.WORKFLOW_INFO_ARTIFACT_FILEPATH }}
- name: Upload workflow info
uses: actions/upload-artifact@v4
with:
name: dotnet-format-workflow-info
path: ${{ env.WORKFLOW_INFO_ARTIFACT_FILEPATH }}
- name: Log workflow
run: |
$hasChanges = [System.Convert]::ToBoolean("${{ steps.dotnet-format-parse.outputs.has-changes }}")
if($hasChanges) {
Write-Output "::warning title=dotnet format::dotnet format detected code guidelines violations. The files resulting from dotnet format have been published as a workflow artifact with the name: dotnet-format-files."
}
else {
Write-Output "::notice title=dotnet format::dotnet format did NOT detect code guidelines violations."
}