Add support for 'files' configuration (#2746)
Co-authored-by: Daniel Jurek <djurek@microsoft.com>
This commit is contained in:
parent
4a224e9e8d
commit
8d0e4dfd1c
@ -24,4 +24,5 @@ steps:
|
||||
-TargetBranch "origin/$("${{ parameters.TargetBranch }}" -replace "refs/heads/")"
|
||||
-SourceBranch ${{ parameters.SourceBranch }}
|
||||
-CspellConfigPath ${{ parameters.CspellConfigPath }}
|
||||
-ExitWithError:(!$${{ parameters.ContinueOnError }})
|
||||
pwsh: true
|
||||
|
||||
@ -7,7 +7,10 @@ Param (
|
||||
[string] $SourceBranch,
|
||||
|
||||
[Parameter()]
|
||||
[string] $CspellConfigPath = "./.vscode/cspell.json"
|
||||
[string] $CspellConfigPath = "./.vscode/cspell.json",
|
||||
|
||||
[Parameter()]
|
||||
[switch] $ExitWithError
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Continue"
|
||||
@ -19,7 +22,7 @@ if ((Get-Command git | Measure-Object).Count -eq 0) {
|
||||
}
|
||||
|
||||
if ((Get-Command npx | Measure-Object).Count -eq 0) {
|
||||
LogError "Could not locate npx. Install NodeJS (includes npm and npx) https://nodejs.org/en/download/"
|
||||
LogError "Could not locate npx. Install NodeJS (includes npx and npx) https://nodejs.org/en/download/"
|
||||
exit 1
|
||||
}
|
||||
|
||||
@ -43,16 +46,60 @@ if ($changedFilesCount -eq 0) {
|
||||
exit 0
|
||||
}
|
||||
|
||||
$changedFilesString = $changedFiles -join ' '
|
||||
$changedFilePaths = @()
|
||||
foreach ($file in $changedFiles) {
|
||||
$changedFilePaths += $file.Path
|
||||
}
|
||||
|
||||
Write-Host "npx cspell --config $CspellConfigPath $changedFilesString"
|
||||
$spellingErrors = Invoke-Expression "npx cspell --config $CspellConfigPath $changedFilesString"
|
||||
# Using GetTempPath because it works on linux and windows. Setting .json
|
||||
# extension because cspell requires the file have a .json or .jsonc extension
|
||||
$cspellConfigTemporaryPath = Join-Path `
|
||||
([System.IO.Path]::GetTempPath()) `
|
||||
"$([System.IO.Path]::GetRandomFileName()).json"
|
||||
|
||||
$cspellConfigContent = Get-Content $CspellConfigPath -Raw
|
||||
$cspellConfig = ConvertFrom-Json $cspellConfigContent
|
||||
|
||||
# If the config has no "files" property this adds it. If the config has a
|
||||
# "files" property this sets the value, overwriting the existing value. In this
|
||||
# case, spell checking is only intended to check files from $changedFiles so
|
||||
# preexisting entries in "files" will be overwritten.
|
||||
Add-Member `
|
||||
-MemberType NoteProperty `
|
||||
-InputObject $cspellConfig `
|
||||
-Name "files" `
|
||||
-Value $changedFilePaths `
|
||||
-Force
|
||||
|
||||
# Set the temporary config file with the mutated configuration. The temporary
|
||||
# location is used to configure the command and the original file remains
|
||||
# unchanged.
|
||||
Write-Host "Setting config in: $cspellConfigTemporaryPath"
|
||||
Set-Content `
|
||||
-Path $cspellConfigTemporaryPath `
|
||||
-Value (ConvertTo-Json $cspellConfig -Depth 100)
|
||||
|
||||
# Use the mutated configuration file when calling cspell
|
||||
Write-Host "npx cspell lint --config $cspellConfigTemporaryPath"
|
||||
$spellingErrors = npx cspell lint --config $cspellConfigTemporaryPath
|
||||
|
||||
if ($spellingErrors) {
|
||||
foreach ($spellingError in $spellingErrors) {
|
||||
LogWarning $spellingError
|
||||
$errorLoggingFunction = Get-Item 'Function:LogWarning'
|
||||
if ($ExitWithError) {
|
||||
$errorLoggingFunction = Get-Item 'Function:LogError'
|
||||
}
|
||||
LogWarning "Spelling errors detected. To correct false positives or learn about spell checking see: https://aka.ms/azsdk/engsys/spellcheck"
|
||||
|
||||
foreach ($spellingError in $spellingErrors) {
|
||||
&$errorLoggingFunction $spellingError
|
||||
}
|
||||
&$errorLoggingFunction "Spelling errors detected. To correct false positives or learn about spell checking see: https://aka.ms/azsdk/engsys/spellcheck"
|
||||
|
||||
if ($ExitWithError) {
|
||||
exit 1
|
||||
}
|
||||
} else {
|
||||
Write-Host "No spelling errors detected. Removing temporary config file."
|
||||
Remove-Item -Path $cspellConfigTemporaryPath -Force
|
||||
}
|
||||
|
||||
exit 0
|
||||
@ -66,22 +113,26 @@ This script checks files that have changed relative to a base branch (default
|
||||
branch) for spelling errors. Dictionaries and spelling configurations reside
|
||||
in a configurable `cspell.json` location.
|
||||
|
||||
This script uses `npx` and assumes that NodeJS (and by extension `npm` and `npx`
|
||||
) are installed on the machine. If it does not detect `npx` it will warn the
|
||||
user and exit with an error.
|
||||
This script uses `npx` and assumes that NodeJS (and by extension `npm` and
|
||||
`npx`) are installed on the machine. If it does not detect `npx` it will warn
|
||||
the user and exit with an error.
|
||||
|
||||
The entire file is scanned, not just changed sections. Spelling errors in parts
|
||||
of the file not touched will still be shown.
|
||||
|
||||
Running this on the local machine will trigger tests
|
||||
This script copies the config file supplied in CspellConfigPath to a temporary
|
||||
location, mutates the config file to include only the files that have changed,
|
||||
and then uses the mutated config file to call cspell. In the case of success
|
||||
the temporary file is deleted. In the case of failure the temporary file, whose
|
||||
location was logged to the console, remains on disk.
|
||||
|
||||
.PARAMETER TargetBranch
|
||||
Git ref to compare changes. This is usually the "base" (GitHub) or "target"
|
||||
(DevOps) branch for which a pull request would be opened.
|
||||
|
||||
.PARAMETER SouceBranch
|
||||
.PARAMETER SourceBranch
|
||||
Git ref to use instead of changes in current repo state. Use `HEAD` here to
|
||||
check spelling of files that have been committed and exlcude any new files or
|
||||
check spelling of files that have been committed and exclude any new files or
|
||||
modified files that are not committed. This is most useful in CI scenarios where
|
||||
builds may have modified the state of the repo. Leaving this parameter blank
|
||||
includes files for whom changes have not been committed.
|
||||
@ -90,6 +141,9 @@ includes files for whom changes have not been committed.
|
||||
Optional location to use for cspell.json path. Default value is
|
||||
`./.vscode/cspell.json`
|
||||
|
||||
.PARAMETER ExitWithError
|
||||
Exit with error code 1 if spelling errors are detected.
|
||||
|
||||
.EXAMPLE
|
||||
./eng/common/scripts/check-spelling-in-changed-files.ps1 -TargetBranch 'target_branch_name'
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user