Skip to content

Commit

Permalink
script changes
Browse files Browse the repository at this point in the history
  • Loading branch information
grobyns committed Sep 20, 2024
1 parent f2f1782 commit 8fe9f6b
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 133 deletions.
150 changes: 17 additions & 133 deletions .github/actions/VerifyAppChanges/TestPreprocessorSymbols.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,144 +2,28 @@

Import-Module "$PSScriptRoot\..\..\..\build\scripts\EnlistmentHelperFunctions.psm1" -DisableNameChecking
Import-Module "$PSScriptRoot\..\..\..\build\scripts\GuardingV2ExtensionsHelper.psm1" -DisableNameChecking
Import-Module "$PSScriptRoot\..\..\..\build\scripts\TestPreprocessorSymbols.psm1" -Force

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace

<#
.SYNOPSIS
Verifies preprocessor symbols in a given file.
# Get the major build version from the main branch

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$mainVersion = Get-MaxAllowedObsoleteVersion

.DESCRIPTION
This function checks for the following in the specified file:
- Ensures preprocessor symbols are lowercased.
- Ensures preprocessor symbols match the specified patterns.
- Ensures preprocessor symbols have uppercase stems.
- Ensures there is no space after the '#' character.
# Get the major build version from the current branch

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$currentVersion = (Get-ConfigValue -Key "repoVersion" -ConfigType AL-Go) -split '\.' | Select-Object -First 1

.PARAMETER filePath
The path to the file to be checked.
.PARAMETER symbolStems
An array of symbol stems to be checked.
.PARAMETER lowerBound
The lower bound for the CLEANxx pattern.
.PARAMETER upperBound
The upper bound for the CLEANxx pattern.
.EXAMPLE
$alfiles = Get-ChildItem -Recurse -Filter *.al -Path .\App\
foreach ($alfile in $alfiles) {
Test-PreprocessorSymbols -filePath $alfile.FullName -symbolStems @("CLEAN", "CLEANSCHEMA") -lowerBound 22 -upperBound 26
}
.NOTES
Author: Gert Robyns
Date: 2024-09-03
#>
function Test-PreprocessorSymbols {
param (
[Parameter(Mandatory=$true)]
[string]$filePath,
[Parameter(Mandatory=$true)]
[string[]]$symbolStems,
[Parameter(Mandatory=$true)]
[int]$lowerBound,
[Parameter(Mandatory=$true)]
[int]$upperBound
)

# check if extension is .al, else return $null
if ('.al' -ne [system.io.path]::GetExtension($filePath)) {
return $null
}

# Generate the regex pattern for the CLEANxx range
$rangePattern = "$($lowerBound..$upperBound -join '|')"

# Define the regex patterns for the preprocessor symbols and the configurable stems
# Define the regex pattern for disallowing a space after #
$noSpaceAfterHashPattern = "^#\s"
$lowercasePattern = "^#(if|elseif|else\b|endif)"
$lowercaseNotPattern = "^#if not "
$symbolPattern = @()

foreach ($stem in $symbolStems) {
$upperStem = $stem.ToUpper()
$symbolPattern += "^#if\s${upperStem}($rangePattern)"
$symbolPattern += "^#if\snot\s${upperStem}($rangePattern)"
$symbolPattern += "^#elseif\s${upperStem}($rangePattern)"
}

# Add #endif to the symbol pattern but not to the strict pattern
$symbolPattern += "#else\b"
$symbolPattern += "#endif"

# Read the content of the file
$content = Get-Content -Path $filePath

# Initialize lists to store any invalid preprocessor symbols with line numbers
$invalidLowercaseSymbols = @()
$invalidPatternSymbols = @()
$invalidStemSymbols = @()

# Iterate through each line in the file content with line numbers
for ($i = 0; $i -lt $content.Count; $i++) {
$line = $content[$i]
$lineNumber = $i + 1

# Check for space after #
if ($line -cmatch $noSpaceAfterHashPattern) {
$invalidPatternSymbols += "${filePath}:${lineNumber}: $line"
}

# Check for lowercase
if (($line -match $lowercasePattern) -and ($line -cnotmatch $lowercasePattern)) {
$invalidLowercaseSymbols += "${filePath}:${lineNumber}: $line"
}

# Check for lowercase not
if (($line -match $lowercaseNotPattern) -and ($line -cnotmatch $lowercaseNotPattern)) {
$invalidLowercaseSymbols += "${filePath}:${lineNumber}: $line"
}

# Check for strict pattern match
$isValidPattern = $false
foreach ($pattern in $symbolPattern) {
if ($line -match $pattern) {
$isValidPattern = $true
break
}
}
if ($line -match $lowercasePattern -and -not $isValidPattern -and $line -notmatch "^#endif") {
$invalidPatternSymbols += "${filePath}:${lineNumber}: $line"
}

# Check for uppercase stem
foreach ($stem in $symbolStems) {
$upperStem = $stem.ToUpper()
if ($line -match "#(if|if not|elseif)\s+${stem}($rangePattern)" -and $line -cnotmatch "#((?i)(if|if not|elseif))\s+${upperStem}($rangePattern)") {
$invalidStemSymbols += "${filePath}:${lineNumber}: $line"
}
}
}

if (($invalidLowercaseSymbols.Count -gt 0) -or ($invalidPatternSymbols -gt 0) -or ($invalidStemSymbols -gt 0)) {
return @{ "invalidLowercaseSymbols" = $invalidLowercaseSymbols; "invalidPatternSymbols" = $invalidPatternSymbols; "invalidStemSymbols" = $invalidStemSymbols }
} else {
return $null
}
# CLEANSCHEMA is on a 5y cycle starting from 26
if ($CurrentVersion -le 26) {
$schemaLowerBound = 15
} else {
$schemaLowerBound = ([math]::Floor(($CurrentVersion - 2) / 5) * 5) - 2
}

# Define the preprocessor symbols to check for
$symbolConfigs = @(
@{stem = "CLEAN"; lowerBound = ($CurrentVersion - 4); upperBound = $mainVersion},
@{stem = "CLEANSCHEMA"; lowerBound = $schemaLowerBound; upperBound = $mainVersion + 3} # next lowerbound, after cleanup should be 25, then
)

$symbolStems = @("CLEAN")
# Get the current major version
$currentMajorVerion = (Get-ConfigValue -Key "repoVersion" -ConfigType AL-Go) -split '\.' | Select-Object -First 1

$upperBound = Get-MaxAllowedObsoleteVersion
# Set the lower bound to the current version minus 4
$lowerBound = $currentMajorVerion - 4

Write-Host "Checking preprocessor symbols $symbolStems with a lower bound of $lowerBound and an upper bound of $upperBound"
Write-Host "Checking preprocessor symbols with $symbolConfigs"

#initialize arrays to store any invalid preprocessor symbols with line numbers
$invalidLowercaseSymbols = @()
Expand All @@ -149,7 +33,7 @@ $invalidStemSymbols = @()
$alfiles = (Get-ChildItem -Filter '*.al' -Recurse) | Select-Object -ExpandProperty FullName
foreach ($file in $alfiles) {
# Call the Test-PreprocessorSymbols function with the file path and calculated version bounds
$result = Test-PreprocessorSymbols -filePath $file -symbolStems $symbolStems -lowerBound $lowerBound -upperBound $upperBound
$result = Test-PreprocessorSymbols -filePath $file -symbolConfigs $symbolConfigs
if ($null -ne $result) {
$invalidLowercaseSymbols += $result.invalidLowercaseSymbols
$invalidPatternSymbols += $result.invalidPatternSymbols
Expand Down
122 changes: 122 additions & 0 deletions build/scripts/TestPreprocessorSymbols.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<#
- Ensures preprocessor symbols have uppercase stems.
- Ensures there is no space after the '#' character.
.PARAMETER filePath
The path to the file to be checked.
.PARAMETER symbolConfigs
An array of hash tables where each entry has a stem, an upper, and a lower bound.
.EXAMPLE
$alfiles = Get-ChildItem -Recurse -Filter *.al -Path .\App\
foreach ($alfile in $alfiles) {
$symbolConfigs = @(
@{stem="CLEAN"; lowerBound=22; upperBound=26},
@{stem="CLEANSCHEMA"; lowerBound=22; upperBound=26}
)
Test-PreprocessorSymbols -filePath $alfile.FullName -symbolConfigs $symbolConfigs
}
.NOTES
Author: Gert Robyns
Date: 2024-09-03
Updated by: Gert Robyns
Date: 2024-09-20
#>
function Test-PreprocessorSymbols {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Test-PreprocessorSymbols' does not have a help comment. Note

The cmdlet 'Test-PreprocessorSymbols' does not have a help comment.
param (
[Parameter(Mandatory=$true)]
[string]$filePath,
[Parameter(Mandatory=$true)]
[hashtable[]]$symbolConfigs
)

# check if extension is .al, else return $null
if ('.al' -ne [system.io.path]::GetExtension($filePath)) {
return $null
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace

# Define the regex pattern for disallowing a space after #
$noSpaceAfterHashPattern = "^#\s"
$lowercasePattern = "^#(if|elseif|else\b|endif)"
$lowercaseNotPattern = "^#if not "
$symbolPattern = @()

foreach ($config in $symbolConfigs) {
$stem = $config.stem
$lowerBound = $config.lowerBound
$upperBound = $config.upperBound

# Generate the regex pattern for the SymbolStem range
$rangePattern = "$($lowerBound..$upperBound -join '|')"

$upperStem = $stem.ToUpper()
$symbolPattern += "^#if\s${upperStem}($rangePattern)"
$symbolPattern += "^#if\snot\s${upperStem}($rangePattern)"
$symbolPattern += "^#elseif\s${upperStem}($rangePattern)"
}

# Add #endif to the symbol pattern but not to the strict pattern
$symbolPattern += "#else\b"
$symbolPattern += "#endif"

# Read the content of the file
$content = Get-Content -Path $filePath

# Initialize lists to store any invalid preprocessor symbols with line numbers
$invalidLowercaseSymbols = @()
$invalidPatternSymbols = @()
$invalidStemSymbols = @()

# Iterate through each line in the file content with line numbers
for ($i = 0; $i -lt $content.Count; $i++) {
$line = $content[$i]
$lineNumber = $i + 1

# Check for space after #
if ($line -cmatch $noSpaceAfterHashPattern) {
$invalidPatternSymbols += "${filePath}:${lineNumber}: $line"
}

# Check for lowercase
if (($line -match $lowercasePattern) -and ($line -cnotmatch $lowercasePattern)) {
$invalidLowercaseSymbols += "${filePath}:${lineNumber}: $line"
}

# Check for lowercase not
if (($line -match $lowercaseNotPattern) -and ($line -cnotmatch $lowercaseNotPattern)) {
$invalidLowercaseSymbols += "${filePath}:${lineNumber}: $line"
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Check for strict pattern match
$isValidPattern = $false
foreach ($pattern in $symbolPattern) {
if ($line -match $pattern) {
$isValidPattern = $true
break
}
}
if ($line -match $lowercasePattern -and -not $isValidPattern -and $line -notmatch "^#endif") {
$invalidPatternSymbols += "${filePath}:${lineNumber}: $line"
}

# Check for uppercase stem
foreach ($config in $symbolConfigs) {
$stem = $config.stem
$upperStem = $stem.ToUpper()
if ($line -match "#(if|if not|elseif)\s+${stem}($rangePattern)" -and $line -cnotmatch "#((?i)(if|if not|elseif))\s+${upperStem}($rangePattern)") {
$invalidStemSymbols += "${filePath}:${lineNumber}: $line"
}
}
}

if (($invalidLowercaseSymbols.Count -gt 0) -or ($invalidPatternSymbols -gt 0) -or ($invalidStemSymbols -gt 0)) {
return @{ "invalidLowercaseSymbols" = $invalidLowercaseSymbols; "invalidPatternSymbols" = $invalidPatternSymbols; "invalidStemSymbols" = $invalidStemSymbols }
} else {
return $null
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
}

Export-ModuleMember -Function Test-PreprocessorSymbols

0 comments on commit 8fe9f6b

Please sign in to comment.