I'm writing a Powershell script, and it reads in files for each device version, and some of the files are different format, so I have to post-process the files to get the correct format, or handle it differently due to different data. So I have about 2 groups of 12 if statements (for 12 devices) to handle the differences. If it was object oriented, I would have classes to handle these, but I couldn't get the object oriented PowerShell working. Does anyone have any ideas how to handle this sort of thing? My list of devices has grown from 3 to about 20 (I have about 8 left), and I hadn't foreseen this at the start that more would be given to Parse, causing spaghetti code.
My code looks like this:
if($currentVersion -Match '^(68)')
{
#special code for 68
#prepare Map for ErrorCodeList and Error Descriptions before loop through each error
$errorCodeListFile = Join-Path -Path $pathViewBase -ChildPath
$errorCodeListBase68
#call method to return basePathFull cppFile method contents
$matchFound = Get-MethodContents -codePath $File[0] -methodNameToReturn "Device68::GetFromCode" -followingMethodName "Device4=68::SkipIt"
#call method to get case info
$resultsCase = (Get-CaseContents68 -parsedCaseMethod $matchFound )
}
if($currentVersion -Match '^(30)')
{
$HopResultMap = [ordered]@{}
$HopResultMap2 = [ordered]@{}
#prepare Map for ErrorCodeList and Error Descriptions before loop through each error
$errorCodeListFile = Join-Path -Path $pathViewBase -ChildPath $errorCodeListBase30
#call method to return basePathFull cppFile method contents
$matchFound = Get-MethodContents -codePath $File[0] -methodNameToReturn "30Device::GetFromCode" -followingMethodName "30Device::GetFirst" #
#call method to get case info
$HopResultMap = (Get-Contents30 -fileContent $matchFound)
$FileHandler = Join-Path -Path $basePathFull -ChildPath "30Device\30Device.h"
$matchFound2 = Get-MethodContents -codePath $FileHandler -methodNameToReturn "NoError" -followingMethodName "PageID;"
$HopResultMap2 = Get-Contents30_b -fileContent $matchFound2
#combine the data structures so can reuse 70 process flow/variables
$resultsCase = foreach ($kvp in $HopResultMap.GetEnumerator()){ #foreach key in $HopResultMap[1]
Write-Host $kvp.sdkDesc
Write-Host $kvp.sdkOutErr
Write-Host $HopResultMap2[$kvp.sdkErrKey]
[PSCustomObject][ordered]@{
sdkErr = $HopResultMap2[$kvp.sdkErrKey]
sdkDesc = $kvp.sdkDesc
sdkOutErr = $kvp.sdkOutErr
}
}#resultsCase
}
#etc
#plus another section like this later for special processing needed later for different data pieces
It's not all different code for the different device versions. There is about 1000+ lines shared, plus shared functions, and also separate functions for each version to handle reading different files/layouts.
I am using PowerShell 5.1 and VSCode.