Skip to content

Commit

Permalink
Update ConnectorPackageValidator.ps1 (#3610)
Browse files Browse the repository at this point in the history
Added solution checker and incorporated the below logic
Solutions.xml and customizations.xml and [Content_Types].xml should be present. (For any zip file, else its invalid solution zip file)
Reading specific non-empty nodes in customizations.xml we can figure out which folders we need to validate for existence. Namely, 'Connector' we have to validate for all solution zip files.
For flow solution, you need to validate Connector and Workflow folders both are present or not.
For ai plugin we have to check the aipluginoperations and aiplugins apart from connector folder validation
  • Loading branch information
vanadhar authored Aug 7, 2024
1 parent 8714b04 commit 3b4d898
Showing 1 changed file with 166 additions and 0 deletions.
166 changes: 166 additions & 0 deletions scripts/ConnectorPackageValidator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ function DisplayReferDocumentation() {
[System.Console]::WriteLine("For a better understanding of how to create a connector package zip file, please refer to the public document placed at ${blue}${underline}$Url${reset}")
}

function CheckValidFile($expectedFiles, $actualFiles) {
$missingFiles = $null
foreach ($filePattern in $expectedFiles) {
$matchingFiles = $actualFiles | Where-Object { $_.Name -eq $filePattern }
if ($matchingFiles.Count -eq 0) {
if (-not $missingFiles) {
$missingFiles = @()
}
$missingFiles += $filePattern
}
}
return $missingFiles
}

function ValidateFolderAndFilesInPackage {
param (
[bool] $pluginEnabled,
Expand Down Expand Up @@ -295,6 +309,158 @@ try {

# Call the function
$resultOfValidation = ValidateFolderAndFilesInPackage -pluginEnabled $isPluginEnabled -levelOfHierarchy 3 -parentFolderPath $parentFolderPath -folderPath $folderPath -expectedFolderCount $expectedFolderCount -expectedFileCounts $expectedFileCounts

if (-not $resultOfValidation) {
DisplayReferDocumentation
Write-Host ""
Write-Host "Validation failed: Invalid package structure. Check previous messages for details." -ForegroundColor Red
Write-Host ""
RemoveTempFolder $tempFolder
RemoveTempFolder $tempFolder2
exit
}

# Check inside the solution zip
$secondLevelZipFiles = Get-ChildItem -Path "$tempFolder2/$pkgAssetFoldereName" -Filter "*.zip" | Select-Object -ExpandProperty Name
$originalParentFolderPath = $parentFolderPath
$nodesToCheck = @()
foreach ($secondLevelZipFile in $secondLevelZipFiles) {
$tempFolder3 = New-Item -ItemType Directory -Path (Join-Path $env:TEMP (New-Guid))
# Update the corret currentFolder to the parentFolderPath
$parentFolderPath = "$originalParentFolderPath/$secondLevelZipFile"

$secondLevelZipFilePath = "$tempFolder2/$pkgAssetFoldereName/$secondLevelZipFile"
Expand-Archive -Path $secondLevelZipFilePath -DestinationPath $tempFolder3 -Force

$actualFiles = Get-ChildItem -Path $tempFolder3 -File
$expectedFiles = @("[Content_Types].xml", "customizations.xml", "solution.xml")
$missingFiles = $null
$missingFiles = CheckValidFile $expectedFiles $actualFiles
# Call the validation function
# $resultOfValidation = ValidateFolderAndFilesInPackage -pluginEnabled $isPluginEnabled -levelOfHierarchy 4 -parentFolderPath $parentFolderPath -folderPath $folderPath -expectedFolderCount $expectedFolderCount -expectedFileCounts $expectedFileCounts
if ($missingFiles) {
Write-Host "The solution zip file '$secondLevelZipFile' in '$originalParentFolderPath' should contain three files namely '[Content_Types].xml', 'customizations.xml', 'solution.xml'."
Write-Host "Please add the missing '$($missingFiles -join ', ')' to the solution zip file."
Write-Host "NOTE: These files are by default present in an exported solution and should not be manually modified/removed. If done, please revert the changes."
DisplayReferDocumentation
Write-Host ""
Write-Host "Validation failed: Invalid package structure. Check previous messages for details." -ForegroundColor Red
Write-Host ""
RemoveTempFolder $tempFolder
RemoveTempFolder $tempFolder2
RemoveTempFolder $tempFolder3
exit
}
# Read the customizations.xml and look for any node "Connector" or "Workflows". If they exist, validate if the folders also exist
# Define the path to your XML file and the node name you want to check
$xmlFilePath = "$tempFolder3/customizations.xml"
$nodeNameToCheck = "Connector"

# Load the XML file into an XmlDocument object
[xml]$xmlContent = Get-Content -Path $xmlFilePath

# Use XPath to find the node
$nodeToCheck = $xmlContent.SelectSingleNode("//$nodeNameToCheck")

# Check if the node exists and output the result
if ($nodeToCheck -ne $null) {
if ($nodeToCheck.HasChildNodes -or $nodeToCheck.Attributes.Count -gt 0) {
# check if the folder name is present with the same name
$nodeNameFolder = Get-ChildItem -Path "$tempFolder3" -Directory | Where-Object {$_.Name -eq $nodeNameToCheck}
if (-not $nodeNameFolder) {
Write-Host "The solution zip file '$secondLevelZipFile' in '$originalParentFolderPath' should contain one folder namely '$nodeNameToCheck'."
Write-Host "The folders should match the customization.xml file. They are present by default in an exported solution and should not be modified/removed manually."
Write-Host "Please add the required folder to the solution zip or export the correctsolution again."
DisplayReferDocumentation
Write-Host ""
Write-Host "Validation failed: Invalid package structure. Check previous messages for details." -ForegroundColor Red
Write-Host ""
RemoveTempFolder $tempFolder
RemoveTempFolder $tempFolder2
RemoveTempFolder $tempFolder3
exit
}
}
}

$nodeNameToCheck = "Workflows"
# Use XPath to find the node
$nodeToCheck = $xmlContent.SelectSingleNode("//$nodeNameToCheck")

# Check if the node exists and output the result
if ($nodeToCheck -ne $null) {
if ($nodeToCheck.HasChildNodes -or $nodeToCheck.Attributes.Count -gt 0) {
# check if the folder name is present with the same name
$nodeNameFolder = Get-ChildItem -Path "$tempFolder3" -Directory | Where-Object {$_.Name -eq $nodeNameToCheck}
if (-not $nodeNameFolder) {
Write-Host "The solution zip file '$secondLevelZipFile' in '$originalParentFolderPath' should contain one folder namely '$nodeNameToCheck'."
Write-Host "The folders should match the customization.xml file. They are present by default in an exported solution and should not be modified/removed manually."
Write-Host "Please add the required folder to the solution zip or export the correct solution again."
DisplayReferDocumentation
Write-Host ""
Write-Host "Validation failed: Invalid package structure. Check previous messages for details." -ForegroundColor Red
Write-Host ""
RemoveTempFolder $tempFolder
RemoveTempFolder $tempFolder2
RemoveTempFolder $tempFolder3
exit
}
}
}
# aiplugin operation
$nodeNameToCheck = "aicopilot_aiplugin"
# Use XPath to find the node
$nodeToCheck = $xmlContent.SelectSingleNode("//$nodeNameToCheck")

# Check if the node exists and output the result
if ($nodeToCheck -ne $null) {
if ($nodeToCheck.HasChildNodes -or $nodeToCheck.Attributes.Count -gt 0) {
# check if the folder name is present with the same name
$folderNameToCheck1 = "aiplugins"
$nodeNameFolder1 = Get-ChildItem -Path "$tempFolder3" -Directory | Where-Object {$_.Name -eq $folderNameToCheck1}
$folderNameToCheck2 = "aipluginoperations"
$nodeNameFolder2 = Get-ChildItem -Path "$tempFolder3" -Directory | Where-Object {$_.Name -eq $folderNameToCheck2}
if ((-not $nodeNameFolder1) -and (-not $nodeNameFolder2)) {
Write-Host "The solution zip file '$secondLevelZipFile' in '$originalParentFolderPath' should contain folders namely '$folderNameToCheck1, $folderNameToCheck2'."
Write-Host "The folders should match the customization.xml file. They are present by default in an exported solution and should not be modified/removed manually."
Write-Host "Please add the required folders to the solution zip or export the correct solution again."
DisplayReferDocumentation
Write-Host ""
Write-Host "Validation failed: Invalid package structure. Check previous messages for details." -ForegroundColor Red
Write-Host ""
RemoveTempFolder $tempFolder
RemoveTempFolder $tempFolder2
RemoveTempFolder $tempFolder3
exit
} elseif (-not $nodeNameFolder1) {
Write-Host "The solution zip file '$secondLevelZipFile' in '$originalParentFolderPath' should contain a folder namely '$folderNameToCheck1'."
Write-Host "The folders should match the customization.xml file. They are present by default in an exported solution and should not be modified/removed manually."
Write-Host "Please add the required folder to the solution zip or export the correct solution again."
DisplayReferDocumentation
Write-Host ""
Write-Host "Validation failed: Invalid package structure. Check previous messages for details." -ForegroundColor Red
Write-Host ""
RemoveTempFolder $tempFolder
RemoveTempFolder $tempFolder2
RemoveTempFolder $tempFolder3
exit
} elseif (-not $nodeNameFolder2) {
Write-Host "The solution zip file '$secondLevelZipFile' in '$originalParentFolderPath' should contain a folder namely '$folderNameToCheck2'."
Write-Host "The folders should match the customization.xml file. They are present by default in an exported solution and should not be modified/removed manually."
Write-Host "Please add the required folder to the solution zip or export the correct solution again."
DisplayReferDocumentation
Write-Host ""
Write-Host "Validation failed: Invalid package structure. Check previous messages for details." -ForegroundColor Red
Write-Host ""
RemoveTempFolder $tempFolder
RemoveTempFolder $tempFolder2
RemoveTempFolder $tempFolder3
exit
}
}
}
}

if ($resultOfValidation) {
Write-Host "Validation successful: The package structure is correct." -ForegroundColor Green
Write-Host ""
Expand Down

0 comments on commit 3b4d898

Please sign in to comment.