$codeFormatterDownloadUri = "https://github.com/dotnet/codeformatter/releases/download/v1.0.0-alpha6/CodeFormatter.zip"
$codeFormatterDownloadLocation = "$env:TEMP\code-formatter.zip"
$codeFormatterExtractLocation = "$env:TEMP\code-formatter\"

# Check that there are no outstanding changes as we will use git status to test if CodeFormatter applied any changes
Write-Host "Checking for changes..."
$gitOutput = $(git status)
$gitHasChanges = ($gitOutput | ?{ $_.Contains("nothing to commit") } | measure).Count -eq 0 # counts lines with "nothing to commit" message
if($gitHasChanges)
{
    Write-Error "Outstanding changes to workspace (before code formatting) - aborting!"
    git status
    exit 10
}
Write-Host "No changes, downloading CodeFormatter..."

# Download CodeFormatter
(New-Object Net.WebClient).DownloadFile($codeFormatterDownloadUri, $codeFormatterDownloadLocation)
Expand-Archive -Path $codeFormatterDownloadLocation -DestinationPath $codeFormatterExtractLocation -Force

# Run CodeFormatter
Write-Host "Running CodeFormatter..."
&"$codeFormatterExtractLocation\CodeFormatter\CodeFormatter.exe" code-formatter.csproj /copyright:code-formatter.license.txt

# Check for changes, i.e. did CodeFormatter make changes?
Write-Host "Checking for changes..."
git add -A | out-null # add all files as this avoids changes due to CRLF/LF issues (providing we're set up to normalize to LF)
$gitOutput = $(git status)
$gitHasChanges = ($gitOutput | ?{ $_.Contains("nothing to commit") } | measure).Count -eq 0 # counts lines with "nothing to commit" message
if($gitHasChanges)
{
    Write-Error "Outstanding changes to workspace - aborting!"
    Write-Error "Run CodeFormatter and resubmit the pull request"
    git status
    git diff
    exit 20
}
Add script to run code formatter
Add script to run code formatter
