Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CLEANSCHEMA symbols #2065

Merged
merged 25 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a2418a2
add CLEANSCHEMA symbols
grobyns Sep 19, 2024
2199b34
Merge branch 'main' of https://github.com/microsoft/BCApps into featu…
grobyns Sep 19, 2024
eac961b
Port the gate for testing app clean symbols
mazhelez Sep 19, 2024
4eda538
Fix typo
mazhelez Sep 19, 2024
b7d4605
Merge branch 'main' of https://github.com/microsoft/BCApps into mazhe…
mazhelez Sep 20, 2024
ce43d16
Adapt script to BCApps (#2072)
mazhelez Sep 20, 2024
1deb205
Merge branch 'main' of https://github.com/microsoft/BCApps into featu…
grobyns Sep 20, 2024
f2f1782
Merge branch 'mazhelez/add-verifyappchanges-check' of https://github.…
grobyns Sep 20, 2024
8fe9f6b
script changes
grobyns Sep 20, 2024
18003a3
review comment
grobyns Sep 20, 2024
bd00fbe
fix whitespace
grobyns Sep 20, 2024
0913524
comments
grobyns Sep 20, 2024
0b1c1b9
cuz maria said so
grobyns Sep 20, 2024
9e3000a
review comment
grobyns Sep 20, 2024
e97ff84
move pester tests
grobyns Sep 20, 2024
8095f7b
Add job to run PS tests
mazhelez Sep 20, 2024
c9fb219
Merge branch 'features/549296' of https://github.com/microsoft/BCApps…
mazhelez Sep 20, 2024
8fa24f1
Add runTests.ps1
mazhelez Sep 20, 2024
c22aad0
Fix failing tests
mazhelez Sep 20, 2024
3e3f7f0
review comments
grobyns Sep 23, 2024
1550886
let's see if this works
grobyns Sep 23, 2024
cedb78a
Rename action folder
mazhelez Sep 23, 2024
07fac42
Use SHA for action ref (to be changed to main afterwards)
mazhelez Sep 23, 2024
a35a643
Rename workflow
mazhelez Sep 23, 2024
9119ba3
Merge branch 'main' of https://github.com/microsoft/BCApps into featu…
mazhelez Sep 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/actions/TestPreprocessorSymbols/action.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Current path is .github/actions/VerifyAppChanges

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

# Get the major build version from the main branch
$mainVersion = Get-MaxAllowedObsoleteVersion

# Get the major build version from the current branch
$currentVersion = (Get-ConfigValue -Key "repoVersion" -ConfigType AL-Go) -split '\.' | Select-Object -First 1

# CLEANSCHEMA is on a 5y cycle starting from 26
if ($CurrentVersion -le 26) {
$schemaLowerBound = 15
} else {
$schemaLowerBound = ([math]::Floor(($CurrentVersion - 2) / 5) * 5) - 2 # makes a series: 23, 28, 33, 38, 43 etc. This is the version is which we clean up (26, 31, 36, etc.) - 3.
}

# 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
)

Write-Host "Checking preprocessor symbols with $symbolConfigs"

#initialize arrays to store any invalid preprocessor symbols with line numbers
$invalidLowercaseSymbols = @()
$invalidPatternSymbols = @()
$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 -symbolConfigs $symbolConfigs
if ($null -ne $result) {
$invalidLowercaseSymbols += $result.invalidLowercaseSymbols
$invalidPatternSymbols += $result.invalidPatternSymbols
$invalidStemSymbols += $result.invalidStemSymbols
}
}

$symbolErrors = $invalidLowercaseSymbols + $invalidPatternSymbols + $invalidStemSymbols
if ($symbolErrors.Count -gt 0) {
throw "Errors found in preprocessor symbols:`n $symbolErrors"
}
13 changes: 13 additions & 0 deletions .github/actions/TestPreprocessorSymbols/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Test Preprocessor Symbols
author: Microsoft Corporation
description: Verifies the preprocessor symbols on .al files
runs:
using: composite
steps:
- name: Test Preprocessor Symbols
shell: pwsh
run: |
${{ github.action_path }}/action.ps1
branding:
icon: terminal
color: blue
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# https://github.com/microsoft/action-psscriptanalyzer
# For more information on PSScriptAnalyzer in general, see
# https://github.com/PowerShell/PSScriptAnalyzer

name: PSScriptAnalyzer
name: 'PowerShell'

on:
push:
Expand All @@ -16,8 +12,11 @@ permissions:
contents: read

jobs:
build:
name: PSScriptAnalyzer
PSScriptAnalyzer:
# https://github.com/microsoft/psscriptanalyzer-action
# For more information on PSScriptAnalyzer in general, see
# https://github.com/PowerShell/PSScriptAnalyzer
name: Run PSScriptAnalyzer
runs-on: ubuntu-latest
permissions:
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
Expand All @@ -37,3 +36,13 @@ jobs:
uses: github/codeql-action/upload-sarif@294a9d92911152fe08befb9ec03e240add280cb3 # v3.26.8
with:
sarif_file: results.sarif

RunTests:
name: Run PS Tests
runs-on: windows-latest
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

- name: Run PS Tests
run: |
. (Join-Path "." "build/scripts/tests/runTests.ps1")
21 changes: 21 additions & 0 deletions .github/workflows/VerifyAppChanges.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: 'Verify App Changes'

on:
pull_request:
branches: [ 'main', 'releases/*']

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
VerifyAppChanges:
runs-on: windows-latest
name: Verify App Changes
steps:
- name: checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
ref: ${{ github.sha }}

- uses: microsoft/BCApps/.github/actions/TestPreprocessorSymbols@cedb78a7972da09fcdaf9ccaa86aef0ded2b5da7
4 changes: 2 additions & 2 deletions build/scripts/GuardingV2ExtensionsHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function Update-AppSourceCopVersion

# All major versions greater than current but less or equal to main should be allowed
$currentBuildVersion = [int] $buildVersion.Split('.')[0]
$maxAllowedObsoleteVersion = [int] (GetMaxAllowedObsoleteVersion)
$maxAllowedObsoleteVersion = [int] (Get-MaxAllowedObsoleteVersion)
$obsoleteTagAllowedVersions = @()

# Add 3 versions for tasks built with CLEANpreProcessorSymbols
Expand Down Expand Up @@ -257,7 +257,7 @@ function Test-IsStrictModeEnabled
return $false
}

function GetMaxAllowedObsoleteVersion() {
function Get-MaxAllowedObsoleteVersion() {
git fetch origin main
$alGoSettings = $(git show origin/main:.github/AL-Go-Settings.json) | ConvertFrom-Json
if (-not $alGoSettings.repoVersion) {
Expand Down
130 changes: 130 additions & 0 deletions build/scripts/TestPreprocessorSymbols.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<#
.SYNOPSIS
This script checks the preprocessor symbols in an AL file.

.DESCRIPTION
This script checks the preprocessor symbols in an AL file for the following:
- Ensures there is no space after the '#' character.
- Ensures preprocessor symbols have uppercase stems.
- Ensures preprocessor symbols are within a specified range.
- Ensures preprocessor symbols are in the correct format.
- Ensures preprocessor symbols are not in lowercase.

.PARAMETER filePath
The path to the file to be checked.

.PARAMETER symbolConfigs
An array of objects 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 {
Fixed Show fixed Hide fixed
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
}

# 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 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
}
}

Export-ModuleMember -Function Test-PreprocessorSymbols
Loading
Loading