firmware_tools/differences/json.groovy
2021-07-01 00:07:24 +01:00

55 lines
1.6 KiB
Groovy

def comparefiles(importedjsonfile, currentjsonfile) {
echo "Comparing file: ${importedjsonfile} with: ${currentjsonfile}"
if (isUnix()) {
sh """
which jq
if [ \$? -ne 0 ]; then exit 1; fi
# jq -S sorts json output
diff -c <(jq -S . "${importedjsonfile}") <(jq -S . "${currentjsonfile}")
"""
}
else {
// Jq and diffutils can be installed on windows with chocolatey
powershell """
# echo Checking utilities exist
# set
# echo %PATH%
try {
if (Get-Command jq) {}
}
catch {
echo "jq is unavailable"
exit \$false
}
try {
if (Get-Command diff) {}
}
catch {
echo "diff is unavailable"
exit \$false
}
# echo Sorting json in files to compare
# jq -S sorts json output
jq -S . "${importedjsonfile}" > temp1.txt
if (\$lastexitcode -ge 1) { exit 1 }
jq -S . "${importedjsonfile}" > temp2.txt
if (\$lastexitcode -ge 1) { exit 1 }
# echo Diffing sorted json files
$str1 = Get-Content "temp1.txt"
$str2 = Get-Content "temp2.txt"
$diff = Compare-Object $str1 $str2
if ($diff -ne '') {
echo "Difference between JSON files:"
echo $diff
exit 1
}
Remove-Item temp1.txt
Remove-Item temp2.txt
"""
}
}
return this;