Skip to content

Commit

Permalink
add CLEANSCHEMA symbols (#2065)
Browse files Browse the repository at this point in the history
<!-- Thank you for submitting a Pull Request. If you're new to
contributing to BCApps please read our pull request guideline below
* https://github.com/microsoft/BCApps/Contributing.md
-->
#### Summary <!-- Provide a general summary of your changes -->
draft of CLEANSCHEMA symbols 

#### Work Item(s) <!-- Add the issue number here after the #. The issue
needs to be open and approved. Submitting PRs with no linked issues or
unapproved issues is highly discouraged. -->
Fixes
[AB#549296](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/549296)

---------

Co-authored-by: mazhelez <[email protected]>
Co-authored-by: Maria Zhelezova <[email protected]>
  • Loading branch information
3 people authored Sep 24, 2024
1 parent 7c9f076 commit 7429883
Show file tree
Hide file tree
Showing 28 changed files with 599 additions and 18 deletions.
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 {
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

0 comments on commit 7429883

Please sign in to comment.